Re: Berth Control for OXP authors
Posted: Tue Jan 21, 2014 11:48 pm
Couldn't agree more!Smivs wrote:I couldn't really call it anything else, could I?
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;
};
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"
);
...
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;
}
)
)
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
}
}
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!
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?