Berth Control for OXP authors

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

Nyarlatothep
Average
Average
Posts: 12
Joined: Tue Jan 21, 2014 2:58 pm

Re: Berth Control for OXP authors

Post by Nyarlatothep »

Smivs wrote:
I couldn't really call it anything else, could I? :D
Couldn't agree more! :)

Anyway, I did a couple of copy and paste snafus, but I've got a very nice script & plist combo thing going now, combining both fixed berths & min berths!
I'll do some renaming / cleaning up, & some testing to make sure I didn't break anything, then I'll post that whole thing later on!


OK, here goes:

Berth Control v1.2

Needs shipdata.plist, shipyard.plist, equipment.plist plus 2 scripts which I've called berthControl.js & berthControl-conditions.js


Here's what I did to get a type of ship that would be for sale with between 2 and 5 berths (max cargo > 25t to begin with).
The option to remove berths only shows up when we've got more than 2 berths.


shipdata.plist:

Code: Select all

		script = "berthControl.js";
		script_info =
					{
						fixedBerths = "No";
						minBerths = 2;
					};	
shipyard.plist

Code: Select all

		"optional_equipment" =
		(
			"EQ_ECM",
			"EQ_FUEL_SCOOPS",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
					...

		)

					...

			extras =(

					...

					"EQ_PASSENGER_BERTH",
					"EQ_PASSENGER_BERTH"
				   );

					...
equipment.plist

Code: Select all

(
	( /* Passenger Compartment, like missiles this can be bought multiple times */
		5, 8250, "Passenger Berth - takes up 5t of cargo space",
		"EQ_PASSENGER_BERTH",
		"Provides life support, entertainment and comfort for a single passenger.",
		{
			available_to_all = true;
			available_to_NPCs = false;
			condition_script = "berthControl-conditions.js";
			requires_cargo_space = 5;
			damage_probability = 0;
		}
	),
	( /* Passenger Compartment Removal */
		1, 1000, "Remove Passenger Berth - reclaims 5t of cargo space",
		"EQ_PASSENGER_BERTH_REMOVAL",
		"Removes a passenger berth.",
		{
			available_to_all = true;
			available_to_NPCs = false;
			condition_script = "berthControl-conditions.js";
			requires_free_passenger_berth = true;
		}
	)

)
berthControl.js => initialises the missionVariables

Code: Select all

/*jslint white: true, undef: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */

"use strict";

// Standard attributes
this.name           = "BerthControl";
this.author         = "Smivs, Nyarlatothep";
this.copyright      = "This script is hereby placed in the public domain.";
this.version        = "1.2";
this.description    = "Passenger berths management script.";


this.playerBoughtNewShip = function(ship)
// set removable berth count when ship is bought
{
	missionVariables.berthControl_removableBerths = null;
	missionVariables.berthControl_marketMessage = null;
	
	var msg = "";
	var berths = ship.passengerCapacity;  // !
	var fixedBerths = ship.scriptInfo.fixedBerths;
	// get bool value from string
	fixedBerths = (fixedBerths === "Yes" || fixedBerths === "Y");
	
	if (fixedBerths)
	{
		msg = "This ship cannot be equipped with standard Passenger Berths.";
	}
	
	var minBerths = parseInt(ship.scriptInfo.minBerths);
	// ensure minBerth is a valid integer & it's > 0
	if (fixedBerths || (!isNaN(minBerths) && minBerths > 0))
	{
		// no berths to start with? no need for minBerths!
		if (berths > 0)
		{
			//  calculate the actual number of permanent berths
			if (fixedBerths || minBerths > berths) minBerths = berths;
			//  compose a nice formatted message...
			msg = (minBerths ===  berths ? "The " + minBerths : minBerths + " of the" ) + " Passenger Berth" + (berths === 1 ? "" : "s");
			msg += " that came with the ship cannot be " + (fixedBerths? "modified" : "removed") + " at this shipyard.";
			// save the number of removable berths for later
			missionVariables.berthControl_removableBerths =  berths - minBerths;
		}
	}
	
	if (msg !== "")
	{
		player.consoleMessage(msg, 5);
		//  save a message for later
		if (berths > 0 && minBerths > 0)
		{
			msg = minBerths + " Passenger Berths cannot be modified at this shipyard."
		}
		missionVariables.berthControl_marketMessage = msg;
	}
}

this.playerBoughtEquipment = function(equipment)
{
  if (missionVariables.berthControl_removableBerths === null)
	{
		// no berthControl mission variable? do nothing
		return;
	}
 
  if (equipment == "EQ_PASSENGER_BERTH")
    {
		missionVariables.berthControl_removableBerths++;  // update counter
    }

  if (equipment == "EQ_PASSENGER_BERTH_REMOVAL")
    {
		missionVariables.berthControl_removableBerths--;  // update counter
    }
}
berthControl-conditions.js => here's where the magic happens! :)

Code: Select all

/*jslint white: true, undef: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
/*global missionVariables, player*/


"use strict";


this.name			= "berthControl-conditions";
this.author			= "Nyarlatothep";
this.copyright		= "This script is hereby placed in the public domain.";
this.version		= "1.2";


/* contexts: npc, purchase, scripted, newShip, (loading), (damage), (portable) */
this.allowAwardEquipment = function(equipment, ship, context)
{

	// OXP hook to allow stations to forbid specific equipment
	if (context == "purchase" && player.ship.dockedStation && player.ship.dockedStation.scriptInfo["oolite-barred-equipment"])
	{
		if (player.ship.dockedStation.scriptInfo["oolite-barred-equipment"].indexOf(equipment) != -1)
		{
			return false;
		}
	}

	// fixed berthts?
	var fixedBerths = ship.scriptInfo.fixedBerths;
	if (context == "purchase" && (fixedBerths === "Yes" || fixedBerths === "Y"))
	{
		player.consoleMessage(missionVariables.berthControl_marketMessage, 1.5);
		return false;
	}
	
	if (context == "purchase" && equipment == "EQ_PASSENGER_BERTH_REMOVAL")
	{
		//var removableBerths = parseInt(missionVariables.berthControl_removableBerths);
		if (missionVariables.berthControl_removableBerths <= 0)
		{
			player.consoleMessage(missionVariables.berthControl_marketMessage, 1.5);
			return false;
		}
	}


	// otherwise allowed
	return true;
}


At the moment it's quite chatty: it shows a message when you buy a new ship, plus you get a reminder whenever you go to the shipyard.
As I was testing it, I kept getting confused as to why it wouldn't let me buy / remove berths, so for me this level of chattiness was basically a necessity.

That fixedBerths = "No"; inside the script_info above is optional, a blank line would work just as well. However, the second the two .js files spot a fixedBerths = "Yes"; inside script_info, they'll remove the standard berth equipment options from the shipyard, just as cim's original solution did!


I tested this in Oolite-trunk (1.79.0.5558-140120-c272d89), so your mileage may vary. Once again, thanks for all the info: in the end I used up a lot more neurons than I was expecting to! :D


PS I couldn't find one of my test ships for sale anywhere, so I did spend a lot of time jumping from system to system, wasting a lot of time in the process. Is there a way to get a ship to appear for sale in the station you're docked at?
Paradox
---- E L I T E ----
---- E L I T E ----
Posts: 607
Joined: Wed Feb 20, 2013 1:24 am
Location: Aboard the D.T.T Snake Charmer: My Xanadu
Contact:

Re: Berth Control for OXP authors

Post by Paradox »

Nyarlatothep wrote:
PS I couldn't find one of my test ships for sale anywhere, so I did spend a lot of time jumping from system to system, wasting a lot of time in the process. Is there a way to get a ship to appear for sale in the station you're docked at?
I will give this a test on 1.77.1 tomorrow, As for finding your ship, in your shipyard.plist, set your "chance" = 1.0; and "techlevel" = 1; Might also want to set the price to 1 as well... then cross your fingers! }:]
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Re: Berth Control for OXP authors

Post by another_commander »

Nyarlatothep wrote:
PS I couldn't find one of my test ships for sale anywhere, so I did spend a lot of time jumping from system to system, wasting a lot of time in the process. Is there a way to get a ship to appear for sale in the station you're docked at?
For this and many other situations where a specific test is required, get your hands on the Oolite Debug Console application and install Basic-debug.oxp to be able to use it. It gives you god-like powers in the game (more info here). The download contains a pre-built Windows binary plus all the source Python files that will enable you to run it on other OSs if needed.
Nyarlatothep
Average
Average
Posts: 12
Joined: Tue Jan 21, 2014 2:58 pm

Re: Berth Control for OXP authors

Post by Nyarlatothep »

Thanks for the info, another_commander: unlimited power, kind of tempting, though it looks like I'll have to learn yet more stuff! :)

Among those god-like powers, what's the command that adds a specific ship to the market?
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Berth Control for OXP authors

Post by cim »

Nyarlatothep wrote:
Among those god-like powers, what's the command that adds a specific ship to the market?
There isn't one that does that, but you can use player.replaceShip("cobra3-player"); - only while docked - to give the player a new ship of the specified type with all standard (and no optional) equipment fitted.

In the debug console specifically, "P" is an alias for "player", so you can do P.replaceShip.

Essentially if it's in the [wiki]Oolite JavaScript Reference[/wiki] you can call it from the console.
Nyarlatothep
Average
Average
Posts: 12
Joined: Tue Jan 21, 2014 2:58 pm

Re: Berth Control for OXP authors

Post by Nyarlatothep »

Thanks, appreciated! :)
User avatar
JazHaz
---- E L I T E ----
---- E L I T E ----
Posts: 2991
Joined: Tue Sep 22, 2009 11:07 am
Location: Enfield, Middlesex
Contact:

Re: Berth Control for OXP authors

Post by JazHaz »

I only just got the joke! Birth control ooer! :lol:
Paradox
---- E L I T E ----
---- E L I T E ----
Posts: 607
Joined: Wed Feb 20, 2013 1:24 am
Location: Aboard the D.T.T Snake Charmer: My Xanadu
Contact:

Re: Berth Control for OXP authors

Post by Paradox »

It doesn't seem to work with the current 1.77.1 version. I buy the ship, it automatically switches to the F5 screen, and gives a message that the 4 berths that came with this ship cannot be modified at this shipyard (this tells me the script is being activated. Then I went back to the F3 screen and proceeded to sell all 4 berths. }:[ Tried changing the line fixedBerths = "Yes"; instead of No, but that made no difference at all...

Wait a sec.. Might have been my stupidity (as usual) Let me test again, think I caught a spelling mistake..

I used a _ instead of a - when I saved berthControl-conditions.js, however once corrected, it still doesn't seem to work...
Nyarlatothep
Average
Average
Posts: 12
Joined: Tue Jan 21, 2014 2:58 pm

Re: Berth Control for OXP authors

Post by Nyarlatothep »

Hmm, it seems to work OK at this end, both with 1.77.1 & oolite-trunk.

Hang on, I'll PM you what I've got.
Paradox
---- E L I T E ----
---- E L I T E ----
Posts: 607
Joined: Wed Feb 20, 2013 1:24 am
Location: Aboard the D.T.T Snake Charmer: My Xanadu
Contact:

Re: Berth Control for OXP authors

Post by Paradox »

Nyarlatothep wrote:
Hmm, it seems to work OK at this end, both with 1.77.1 & oolite-trunk.

Hang on, I'll PM you what I've got.
For one thing, the berthControl-conditions.js that you posted above, is not the same as the one you e-mailed me... Line 39 above showes if (missionVariables.berthControl_removableBerths <= 0) where as the one you e-mailed me has if (missionVariables.berthControl_removableBerths === 0). I don't know if that would keep this from working entirely or not, but you might want to change that in your code above. The only other thing I see different (and I must admit it confuses me };] ) , is that you list EQ_PASSNEGER_BERTH repeatedly in the optional equipment part of the shipyard.plist:

Code: Select all

		"optional_equipment" =
		(
			"EQ_ECM",
			"EQ_FUEL_SCOOPS",
			"EQ_PASSENGER_BERTH",
			"EQ_ESCAPE_POD",
			"EQ_ENERGY_BOMB",
			"EQ_ENERGY_UNIT",
			"EQ_NAVAL_ENERGY_UNIT",
			"EQ_DOCK_COMP",
			"EQ_WEAPON_PULSE_LASER",
			"EQ_WEAPON_BEAM_LASER",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_WEAPON_MINING_LASER",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_WEAPON_MILITARY_LASER",
			"EQ_FUEL_INJECTION",
			"EQ_PASSENGER_BERTH",
			"EQ_PASSENGER_BERTH",
			"EQ_SCANNER_SHOW_MISSILE_TARGET",
			"EQ_MULTI_TARGET",
			"EQ_GAL_DRIVE",
			"EQ_ADVANCED_COMPASS",
			"EQ_SHIELD_BOOSTER",
			"EQ_NAVAL_SHIELD_BOOSTER",
			"EQ_HEAT_SHIELD"
		);


I did not do that on mine, but what exactly is the purpose of that? Would that prevent this from working? I guess I can go try it again on my ships to see... I will let you know what happens. };]
Paradox
---- E L I T E ----
---- E L I T E ----
Posts: 607
Joined: Wed Feb 20, 2013 1:24 am
Location: Aboard the D.T.T Snake Charmer: My Xanadu
Contact:

Re: Berth Control for OXP authors

Post by Paradox »

ok, the scripts you sent me work!
(beating head against wall repeatedly...!)
Cim wasn't kidding when he warned that equipment.plist changes can conflict with each other. I was experimenting with 3 different ships, each using a different method of "berth control" ( :lol: that still cracks me up!), one from Cim, one from Smivs ,an then Nyarlatothep's. Once I updated them all to the same equipment.plists and scripts, they worked.

Bottom line: Nyarlatothep modified and built upon Smivs and Cims scripts to give us the option to either hard code a set number of passenger berths that cannot be changed, or a set number of passenger berths that can be added to or removed, without changing the original number of hard coded berths. (for example, you hard code 4 berths into the ship, when someone buys it, they can add another berth for a total of 5, then later on, then can remove the 5th berth, but never the original 4.)

Thank you all for all the work to get this working for us!
Nyarlatothep
Average
Average
Posts: 12
Joined: Tue Jan 21, 2014 2:58 pm

Re: Berth Control for OXP authors

Post by Nyarlatothep »

Paradox wrote:
For one thing, the berthControl-conditions.js that you posted above, is not the same as the one you e-mailed me...

....

I did not do that on mine, but what exactly is the purpose of that? Would that prevent this from working? I guess I can go try it again on my ships to see... I will let you know what happens. };]
Well, I'm still figuring stuff out and, most of all, still tweaking that if statement. I'm not sure that bit is 100% reliable as yet.

About those multiple "EQ_PASSENGER_BERTH"s inside optional_equipment, maybe Smivs or cim could confirm this, but it seems to me that the more duplicate berths in optional equipment there are, the more likely the ship will have more than 1 extra removable berth when buying it.

At one point I did put more "EQ_PASSENGER_BERTH"s that I knew I had space for, to see if it would break anything, and it doesn't seem to!

Talking about conflicting equipment.plists, I just thought of somethin: I'll try using a replacement oolite-conditions.js file, without using equipment.plist at all, and see what happens.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Berth Control for OXP authors

Post by cim »

Nyarlatothep wrote:
About those multiple "EQ_PASSENGER_BERTH"s inside optional_equipment, maybe Smivs or cim could confirm this, but it seems to me that the more duplicate berths in optional equipment there are, the more likely the ship will have more than 1 extra removable berth when buying it.
Yes, that would happen. For optional equipment (if any is to be installed), it picks a random item from the list, tries to install it (which might fail - e.g. if the item TL is too much higher than the shipyard TL). The chance of further equipment being installed (even if none was installed this time) is then multiplied by the chance number and it goes around again.

(Which is why ships with chance = 1 tend to end up mostly full of passenger cabins)
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Berth Control for OXP authors

Post by Smivs »

Nyarlatothep wrote:
About those multiple "EQ_PASSENGER_BERTH"s inside optional_equipment...
Just a quick reminder for anybody using my original script, DO NOT include berths in the optional_equipment list. They will still appear in the outfitters for purchase, but any already installed in the ship when it is bought would be treated as permanent berths and will not be removeable.
Commander Smivs, the friendliest Gourd this side of Riedquat.
Post Reply