Scripts/yasenturret-buysell.js |
"use strict";
this.name = "yasenturret-buysell";
this.author = "phkb, adapted for yasen-n Amah";
this.copyright = "2015 phkb";
this.description = "Allows user buy/sell a turret for the yasen-N";
this.licence = "CC BY-NC-SA 4.0";
this._yasenshiptype = "yasen-n-adv"; // the shiptype to up/dowgrade
this._yasenshipname = "Yasen-N 'Advanced'"; // the shiptype to up/dowgrade
this._data = []; // storage array of ship data keys
this._selectedItem = ""; // currently selected item from the main list
this._selectedIndex = 0; // index of the currently selected item
this._personality = 0; // current entityPersonality setting
this._displayType = 0; // respray gui display mode 0 = main list, 1 = selected item
this._lastchoice = null; // last item chosen from the list
this._curpage = 0; // current page being shown
this._pagesize = 16; // max number of entries on a page
this._lastsubchoice = ""; // last item chosen from the selected item display
this._showDetails = false; // when on, displays the datakey on the list and on the selected item page
this._baseTime = 48; // base time, using cobra mk3 as a reference
this._bigGuiHUD = ["XenonHUD.plist"]; // until there is a property we can check, I'll be listing HUD's that have the allow_big_gui property set here
//-------------------------------------------------------------------------------------------------------------
this.playerBoughtEquipment = function(equipment) {
var buy = false;
var sell = false;
if (equipment == "EQ_YASEN-TURRET") {
player.ship.removeEquipment("EQ_YASEN-TURRET");
// for 1.82 and later, refund the credits at this point - if the player buys the respray it will be deducted later
//player.credits += 1; //refund
this._yasenshiptype = "yasen-n-adv";
this._yasenshipname = "Yasen-N 'Advanced'";
buy = true;
}
if (equipment == "EQ_YASEN-TURRET_REMOVE") {
player.ship.removeEquipment("EQ_YASEN-TURRET_REMOVE");
// for 1.82 and later, refund the credits at this point - if the player buys the respray it will be deducted later
//player.credits += 1; //refund
this._yasenshiptype = "yasen-n";
this._yasenshipname = "Yasen-N";
sell = true;
}
if (buy == true || sell == true) {
this.$compileData();
// pick a random personality to start with
this._personality = this.$rand(32768) - 1;
// reset the menu before displaying
this._displayType = 0;
this._curpage = 0;
this._selectedItem = "";
this._selectedIndex = 0;
this.$showPage();
}
}
//-------------------------------------------------------------------------------------------------------------
// get a list of all datakeys avaiable for the players current ship class type (ie. "Cobra Mark III")
this.$compileData = function() {
this._data = [];
// get a list of all ship keys that have a role of "player" (ie. these are playable ships, not NPC ships)
var shipKeys = Ship.keysForRole(this._yasenshiptype);
var freq = "";
// loop through the list
for (var i = 0; i < shipKeys.length; i++) {
// get the shipdata entry for this key
var shipdata = Ship.shipDataForKey(shipKeys[i]);
// add it to the array if it's not there already
if (this.$itemIsInArray(shipKeys[i], this._data) == false) {
this._data.push(shipKeys[i]);
}
}
// sort the array (basic alpha sort)
this._data.sort();
}
//-------------------------------------------------------------------------------------------------------------
this.playerBoughtNewShip = function(ship, price) {
// recompile the list whenever the player buys a new ship
this.$compileData();
}
//=============================================================================================================
// screen interfaces
//-------------------------------------------------------------------------------------------------------------
this.$showPage = function() {
var curChoices = {};
if (this.$isBigGuiActive() == true) {
this._pagesize = 23;
} else {
this._pagesize = 16;
}
if (this._displayType == 0) {
var min = (this._curpage * this._pagesize);
var max = this._curpage * this._pagesize + this._pagesize;
if (max >= this._data.length) max = this._data.length;
for (var i = min; i < max; i++) {
if (this._yasenshiptype == "yasen-n-adv") {
curChoices["01_ITEM_" + (i < 10 ? "0" : "") + i + "~" + this._data[i]] = {text:player.ship.shipClassName + " Advanced turret setup "+ (i + 1).toString() +
(this._showDetails == true ? " (" + this._data[i] + ")" : ""), color:"orangeColor", alignment:"LEFT"};
} else {
curChoices["01_ITEM_" + (i < 10 ? "0" : "") + i + "~" + this._data[i]] = {text:player.ship.shipClassName + " - Basic model" +
(this._showDetails == true ? " (" + this._data[i] + ")" : ""), color:"orangeColor", alignment:"LEFT"};
}
}
if (max - min < this._pagesize) {
for (var i = (this._pagesize - (max - min)); i > 0; i--) {
curChoices["02_SPACER_" + (i < 10 ? "0" : "") + i] = "";
}
}
curChoices["99_EXIT"] = {text:"Exit", color:"yellowColor"};
var def = "99_EXIT";
if (this._yasenshiptype == "yasen-n-adv") {
var opts = {
screenID: "Turret Outfitter",
title: "Arkadi's " + player.ship.shipClassName + " Turrets LTD.",
allowInterrupt: true,
//overlay: {name:"paint.png", height:546},
exitScreen: "GUI_SCREEN_EQUIP_SHIP",
choices: curChoices,
initialChoicesKey: this._lastchoice?this._lastchoice:def,
message: "Select a turret setup"
};
} else {
var opts = {
screenID: "Turret Outfitter",
title: "Arkadi's " + player.ship.shipClassName + " Turrets LTD.",
allowInterrupt: true,
//overlay: {name:"paint.png", height:546},
exitScreen: "GUI_SCREEN_EQUIP_SHIP",
choices: curChoices,
initialChoicesKey: this._lastchoice?this._lastchoice:def,
message: "Remove turrets..."
};
}
}
if (this._displayType == 1) {
var calc = Math.floor((player.ship.mass / 214737.6875) * this._baseTime);
if (calc < 24) calc = 24;
var def = "98_CLOSE";
if (this._yasenshiptype == "yasen-n-adv") {
if (player.credits > 99999) { // does the player have enough cash?
curChoices["02_SELECT"] = {text:"Purchase this turret (100000 Cr, " + calc + " hours)", color:"yellowColor"};
} else {
curChoices["02_SELECT"] = {text:"Not enough money - You need 100000 Cr to purchase this turret", color:"darkGrayColor", unselectable:true};
};
} else {
curChoices["02_SELECT"] = {text:"Sell the Turret ( 31337 Cr refund, " + calc + " hours)", color:"yellowColor"};
}
curChoices["98_CLOSE"] = {text:"Close", color:"yellowColor"};
var opts = {
screenID: "Turret outfitter",
title: "Arkadi's " + player.ship.shipClassName + " Turrets LTD.",
model:"[" + this._selectedItem + "]",
modelPersonality: this._personality,
allowInterrupt: true,
choices: curChoices,
initialChoicesKey: this._lastsubchoice?this._lastsubchoice:def,
message: player.ship.shipClassName + " turret setup " + (this._selectedIndex + 1).toString() + (this._showDetails == true ? " (" + this._selectedItem + ")" : "")
};
}
mission.runScreen(opts, this.$selectScreenHandler, this);
}
//-------------------------------------------------------------------------------------------------------------
this.$selectScreenHandler = function(choice) {
if (choice.indexOf("~") >= 0) {
this._displayType = 1;
this._selectedItem = choice.substring(choice.indexOf("~") + 1);
this._selectedIndex = parseInt(choice.substring(8, 10));
this._lastchoice = choice;
this._lastsubchoice = null;
} else if (choice == "02_SELECT") {
// need code to recreate player ship, transferring all cargo and equipment
this.$addturretShip(this._selectedItem, this._personality);
choice = "99_EXIT";
} else if (choice == "96_PREV") {
this._curpage -= 1;
if (this._curpage > 0) {
this._lastchoice = choice;
} else {
this._lastchoice = "97_NEXT";
}
} else if (choice == "98_CLOSE") {
this._displayType = 0;
}
if (choice != "99_EXIT") {
this.$showPage();
}
}
//-------------------------------------------------------------------------------------------------------------
// do the change, using ShipStorageHelper
this.$addturretShip = function(dataKey, personality) {
var p = player.ship;
var playershipname = p.shipUniqueName;
var hud = p.hud;
//Do we have a Largecargobay
var largecargobay = p.canAwardEquipment("EQ_CARGO_BAY");
// first, store all equipment and cargo the player might have
var shipstr = worldScripts["Ship_Storage_Helper.js"].storeCurrentShip();
// change the datakey to the new one
var data = JSON.parse(shipstr);
data[1] = dataKey; // apply the data key to the ship
data[13] = personality; // apply the selected personality to the ship
shipstr = JSON.stringify(data);
// restore the players ship
worldScripts["Ship_Storage_Helper.js"].restoreStoredShip(shipstr);
// restore some things that the storage helper doesn't replace
p.shipUniqueName = playershipname;
p.shipClassName = this._yasenshipname;
p.hud = hud;
// email system is installed ?
var w = worldScripts.EmailSystem;
if (this._yasenshiptype == "yasen-n-adv") {
// remove large cargo bay capacity if present
if (!largecargobay){
p.cargoSpaceCapacity = p.cargoSpaceCapacity-15;
}
// charge the player
player.credits -= 100000;
// send a buy email if the email system is installed
if (w) {
var ga = worldScripts.GalCopAdminServices;
w.$createEmail({sender:expandDescription("[purchase-maintenance-sender]"),
subject:"Your " + p.shipClassName + " Turret- setup",
date:global.clock.seconds,
message:expandDescription("[turretbuy_email]", {shipclass:p.shipClassName}),
expiryDays:ga._defaultExpiryDays}
);
}
} else {
//refund player
player.credits += 31337; //a refund for 1337 captains
// send a sell email if the email system is installed
if (w) {
var ga = worldScripts.GalCopAdminServices;
w.$createEmail({sender:expandDescription("[purchase-maintenance-sender]"),
subject:"Your " + p.shipClassName + " Turret removal",
date:global.clock.seconds,
message:expandDescription("[turretsell_email]", {shipclass:p.shipClassName}),
expiryDays:ga._defaultExpiryDays}
);
}
}
// work out how much install time is required
// using cobra mass as the benchmark
var install = Math.floor((p.mass / 214737.6875) * this._baseTime);
if (install < 24) install = 24;
install = install * 60 * 60;
global.clock.addSeconds(install);
// if the station dock control is installed, tell it to check for launched ships
if (worldScripts.StationDockControl) {
var w = worldScripts.StationDockControl;
if (w._disable == false) {
w.$checkForLaunchedShips();
}
}
this._displayType = 0;
this._lastchoice = null;
}
//=============================================================================================================
// helper functions
//-------------------------------------------------------------------------------------------------------------
// return a random number between 1 and max
this.$rand = function(max) {
return Math.floor((Math.random() * max) + 1)
}
//-------------------------------------------------------------------------------------------------------------
// checks if a given element is in the array
this.$itemIsInArray = function(element, array) {
var found = false;
if (array != null && array.length > 0) {
for (var i = 0; i < array.length; i++) {
if (array[i] == element) found = true;
}
}
return found;
}
//-------------------------------------------------------------------------------------------------------------
// returns true if a HUD with allowBigGUI is enabled, otherwise false
this.$isBigGuiActive = function() {
if (oolite.compareVersion("1.83") < 0) {
return player.ship.hudAllowsBigGui;
} else {
if (this._bigGuiHUD.indexOf(player.ship.hud) >= 0) {
return true;
} else {
return false;
}
}
}
|