| Scripts/shipversion.js | "use strict";
this.name        = "shipversion";
this.author      = "Norby";
this.copyright   = "2013 Norbert Nagy";
this.licence     = "CC BY-NC-SA 3.0";
this.description = "Give rechage and other bonuses to all ships based on the random personality value.";
//customizable properties
this.$ShipVersion_AwardMax = 10; //Additional equipments awarded to NPCs in shipSpawned function, level 0-10
this.$ShipVersion_BasicRulesOnly = false; //if true then default rules get versions only (trader, pirate...)
this.$ShipVersion_Colors = true; //flag color adjustments on the IFF scanner
this.$ShipVersion_Debug = false; //show extra messages to help developing
//internal properties, should not touch
this.$ShipVersion_PlayerCredits = null; //store the current money
this.$ShipVersion_PlayerShip = null; //store the ship
this.$ShipVersion_PlayerShipDataKey = null; //store the ship type
this.$ShipVersion_PlayerShipEqs = []; //store equipments
this.$ShipVersion_PlayerShipEqDmg = []; //store equipment damage status
this.$ShipVersion_PlayerShipFuel = 0; //store fuel
this.$ShipVersion_PlayerShipMan = null; //store manifest (cargo)
this.$ShipVersion_PlayerShipServiceLevel = 100; //store serviceLevel
this.$ShipVersion_PlayerShipPass = []; //store passengers
this.$ShipVersion_PlayerShipPCap = 0; //store passenger capacity
this.$ShipVersion_PlayerShipPersonality = 16384; //store personality which is the source of the ship version
this.$ShipVersion_PlayerShipWeapons = []; //store weapons
this.$ShipVersion_ReplaceShip = false; //to avoid buy loop
this.$ShipVersion_ServiceLevelCounter = 0; //count seconds to repair 1% in every 5 minutes
this.$ShipVersion_ServiceLevelPrev = 0; //to detect changes
this.$ShipVersion_Ships = [ player.ship ]; //this array will contain all version-equipped ships, the 0. is the player
this.$ShipVersion_Simple = true; //show main NPC versions only, 1 - 10 instead of 1.0 - 10.0
this.$ShipVersion_SLCr = 0; //cost of increasing sercive level on current player ship
this.$ShipVersion_storedShipString = null;//store the current ship into this string
this.$ShipVersion_Timer = null;
/*** begin dybal ***/
this.$hyperspaceHangarRestoredShip = false; // for HyperspaceHangar to set when restoring a ship, so playerBoughtNewShip can be ignored
/*** end dybal ***/
this.startUp = function() {
    this.$ShipVersion_ServiceLevelCounter = 0;
    this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel;
    player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
    if( this.$ShipVersion_Debug )
        log("ShipVersion", "Service level in startUp: "+player.ship.serviceLevel+"%"); //debug
        this.$ShipVersion_SLCost();//save cost of increasing sercive level on current player ship
    this.$ShipVersion_Timer = new Timer(this, this.$ShipVersion_Timed, 1, 1);//run in every second
}
this.equipmentDestroyed = function(equipment) {//run only for player, not for NPCs
    var shipVersion = this.$ShipVersion_Int(player.ship);
    if(equipment === "EQ_SHIP_VERSION_"+shipVersion) {//do not destroy because it is not a buyable equipment
        if(player.ship.equipmentStatus(equipment) == "EQUIPMENT_UNAVAILABLE" )
            player.ship.awardEquipment(equipment); 
        player.ship.setEquipmentStatus(equipment, "EQUIPMENT_DAMAGED");//but set to damaged
    }
    if(equipment.indexOf("EQ_SERVICE_LEVEL_") > -1 ) {//do not destroy because it is not a buyable equipment
        if(player.ship.equipmentStatus(equipment) == "EQUIPMENT_UNAVAILABLE" )
            player.ship.awardEquipment(equipment); 
        player.ship.setEquipmentStatus(equipment, "EQUIPMENT_DAMAGED");//but set to damaged
    }
}
this.guiScreenWillChange = function(to, from) {
    if(to === "GUI_SCREEN_STATUS") {//fix a bug when the player got new ship without shipSpawned
//      if( this.$ShipVersion_Debug ) player.consoleMessage(this.$ShipVersion_PlayerShipName());
        var shipVersion = this.$ShipVersion_Int(player.ship);
//      player.consoleMessage(shipVersion+player.ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion)+Math.random());//debug
        if(player.ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion) == "EQUIPMENT_UNAVAILABLE" )
//&& shipVersion > 10 ) //it was a bugfix for the doubled eq if v=1.0 - founded: was doubled in equipment.plist
            player.ship.awardEquipment("EQ_SHIP_VERSION_"+shipVersion); 
    }
    if(to === "GUI_SCREEN_SHIPYARD") {//store the current ship
//player.ship.setEquipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel, "EQUIPMENT_DAMAGED");//debug
        this.$ShipVersion_PlayerCredits = player.credits;
        if(worldScripts["Ship_Storage_Helper.js"]) { //another method
            this.$ShipVersion_storedShipString = worldScripts["Ship_Storage_Helper.js"].storeCurrentShip();
            worldScripts["Ship_Storage_Helper.js"].disableVortexPBNSFunc("delete");
            worldScripts["Ship_Storage_Helper.js"].disableMaelstromPBNSFunc("delete");
            worldScripts["Ship_Storage_Helper.js"].disableTCATPBNSFunc();
//            if( this.$ShipVersion_Debug ) player.consoleMessage("Your "+this.$ShipVersion_PlayerShipName()+" stored.", 10);
        } else { //store the current ship - original method
            this.$ShipVersion_PlayerShip = player.ship; 
            this.$ShipVersion_PlayerShipDataKey = player.ship.dataKey;
            delete this.$ShipVersion_PlayerShipEqs;
            this.$ShipVersion_PlayerShipEqs = [];
            this.$ShipVersion_PlayerShipFuel = player.ship.fuel;
            this.$ShipVersion_PlayerShipMan = player.ship.manifest.list;
            delete this.$ShipVersion_PlayerShipPass;
            this.$ShipVersion_PlayerShipPass = [];
            var i=-1;
            var pa = player.ship.passengers; //local variable, faster
            while(pa[++i]) {
                this.$ShipVersion_PlayerShipPass[i] = [];
                this.$ShipVersion_PlayerShipPass[i][1] = pa[i].name;
                this.$ShipVersion_PlayerShipPass[i][2] = pa[i].start;
                this.$ShipVersion_PlayerShipPass[i][3] = pa[i].destination;
                this.$ShipVersion_PlayerShipPass[i][4] = pa[i].eta;
                this.$ShipVersion_PlayerShipPass[i][5] = pa[i].fee;
                this.$ShipVersion_PlayerShipPass[i][6] = pa[i].premium;
            }
            this.$ShipVersion_PlayerShipPCap = player.ship.passengerCapacity;
            var i=-1;//index in the old eq array
            var p=-1;//index in the new eq array
            var eq = player.ship.equipment; //local variable, faster
            while(eq[++i]) {
                if( eq[i].name.length > 0 ) {
                    if( this.$ShipVersion_Debug )
                        log("ShipVersion", "i:"+i+" name:"+eq[i].name);
                    this.$ShipVersion_PlayerShipEqs[++p] = eq[i];
                    if(player.ship.equipmentStatus(eq[i].equipmentKey) ==="EQUIPMENT_DAMAGED")
                        this.$ShipVersion_PlayerShipEqDmg[p] = true;
                    else this.$ShipVersion_PlayerShipEqDmg[p] = false;
                } else {//skip noname eqs awarded by NumericHUD to avoid timeLimit error
                    for( var j = i + 10; j < eq.length; j+=10 ) {
                        if( eq[j].name.length != 0 ) {
                            i = j - 10; //end of skip
                            j = eq.length;
                        }
                    }
                    while( i < eq.length && eq[i].name.length == 0 ) i++;
                    i--;
                }
            }
            this.$ShipVersion_PlayerShipServiceLevel = player.ship.serviceLevel;
            this.$ShipVersion_PlayerShipPersonality = player.ship.entityPersonality;
            this.$ShipVersion_PlayerShipWeapons[0] = player.ship.forwardWeapon;
            this.$ShipVersion_PlayerShipWeapons[1] = player.ship.aftWeapon;
            this.$ShipVersion_PlayerShipWeapons[2] = player.ship.portWeapon;
            this.$ShipVersion_PlayerShipWeapons[3] = player.ship.starboardWeapon;
            if( this.$ShipVersion_Debug )
                log("ShipVersion", "Your "+this.$ShipVersion_PlayerShipName()+" has been stored.");
        }
    }
}
this.playerBoughtEquipment = function(equipmentKey) {
    if(equipmentKey === ("EQ_RENOVATION")) {
        if( this.$ShipVersion_ServiceLevelPrev != player.ship.serviceLevel ) {
            var st = player.ship.equipmentStatus("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
            player.ship.removeEquipment("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
            player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
            if( st == "EQUIPMENT_DAMAGED" ) //set to damaged
                player.ship.setEquipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel, st);
            if( this.$ShipVersion_Debug )
                log("ShipVersion", "Service level renovated to "+player.ship.serviceLevel+"%"); //debug
            this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel; //save
            player.consoleMessage("Service level changed to "+
                this.$ShipVersion_SL(player.ship.serviceLevel),10);
        }
    }
    if(equipmentKey === "EQ_SERVICE_LEVEL_SMALL_FIX_1" //1% fix
        || equipmentKey === "EQ_SERVICE_LEVEL_SMALL_FIX_2"
        || equipmentKey === "EQ_SERVICE_LEVEL_SMALL_FIX_3"
        || equipmentKey === "EQ_SERVICE_LEVEL_SMALL_FIX_4") {
        player.ship.removeEquipment(equipmentKey);
        var cost = this.$ShipVersion_SLCost();
                if( player.ship.serviceLevel < 80 ) cost = 0; //cost of bad levels is already deducted by equipment.plist
        if( player.credits < cost )
            player.consoleMessage("Not enough money: need "+cost+" credits.");
        else {
            player.credits -= cost;
                        if( player.ship.serviceLevel < 80 ) cost = 100; //display cost of bad levels below
                        //installation_time is set in equipment.plist from Oolite 1.82 instead of the next lines
                        if( 0 < oolite.compareVersion("1.82") ) {
                            if( player.ship.serviceLevel < 80 )
                                ; //clock.addSeconds ("300"); //5 minutes as the repair bot, but core already add 10min
                            else clock.addSeconds ("432000"); //5 days, like about an original 1% maintenance overhaul
                        }
            player.ship.serviceLevel++;
            var st = player.ship.equipmentStatus("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
            player.ship.removeEquipment("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
            player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
            if( st == "EQUIPMENT_DAMAGED" ) //set to damaged
                player.ship.setEquipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel, st);
            if( this.$ShipVersion_Debug )
                log("ShipVersion", "Service level fixed for "+cost+" credits to "+
                    player.ship.serviceLevel+"%"); //debug
            this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel; //save
            player.consoleMessage("Payed "+cost+" credits for fix, service level now "+
                this.$ShipVersion_SL(player.ship.serviceLevel),10);
        }
    }
    if(equipmentKey === ("EQ_SERVICE_LEVEL_REPAIR")) {
        var shipVersion = this.$ShipVersion_Int(player.ship);
        if(player.ship.equipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel) === "EQUIPMENT_UNAVAILABLE")
            player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);//for sure
        else player.ship.setEquipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel, "EQUIPMENT_OK");
        player.ship.removeEquipment("EQ_SERVICE_LEVEL_REPAIR");
        clock.addSeconds ("3600");
//          player.credits += 0;
    }
    if(equipmentKey === ("EQ_SHIP_VERSION_REPAIR")) {
        var shipVersion = this.$ShipVersion_Int(player.ship);
        if(player.ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion) === "EQUIPMENT_UNAVAILABLE")
            player.ship.awardEquipment("EQ_SHIP_VERSION_"+shipVersion);//for sure
        else player.ship.setEquipmentStatus("EQ_SHIP_VERSION_"+shipVersion, "EQUIPMENT_OK");
        player.ship.removeEquipment("EQ_SHIP_VERSION_REPAIR");
        clock.addSeconds ("3600");
//          player.credits += 0;
    }
    var mass = 30000;//below:Adder, Viper, Rocket Fighter and Gunship but not the Rocket Corvette
    if( !this.$ShipVersion_EQLimiter("EQ_WEAPON_MILITARY_LASER", equipmentKey, mass ) )
        player.ship.forwardWeapon = null;
    if( !this.$ShipVersion_EQLimiter("EQ_WEAPON_MINING_LASER", equipmentKey, mass ) )
        player.ship.forwardWeapon = null;
    this.$ShipVersion_EQLimiter("EQ_GAL_DRIVE", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_IRONHIDE", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_RAILGUN_2", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_FORWARD_SHIELD_CAPACITOR", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_AFT_SHIELD_CAPACITOR", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_SHIELD_BOOSTER", equipmentKey, mass );//for sure only
    
    mass = 130000;//Cobra3, Rocket Miner and above
    this.$ShipVersion_EQLimiter("EQ_IRONHIDE_MIL", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_NAVAL_ENERGY_UNIT", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_NAVAL_SHIELD_BOOSTER", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_RAILGUN_3", equipmentKey, mass );
    this.$ShipVersion_EQLimiter("EQ_RAILGUN_4", equipmentKey, mass );
    if(equipmentKey === "EQ_TURRET_RECOVER") {
        player.ship.removeEquipment("EQ_TURRET_RECOVER");
        var sub = 0;
        if(player.ship.subEntities && player.ship.subEntities.length > 0)
            for(var i=0; i < player.ship.subEntities.length; i++)
                if(player.ship.subEntities[i] && player.ship.subEntities[i].energy > 0)
                    sub++; //count remaining turrets (assuming all subEntities are turrets)
        var missingturrets = 0;
        if((!player.ship.subEntities || sub === 0) && player.ship.subEntityCapacity > 0)
            missingturrets = player.ship.subEntityCapacity;
        if(player.ship.subEntities && player.ship.subEntityCapacity > sub)
            missingturrets = player.ship.subEntityCapacity - sub;
        if(missingturrets < 1 )    player.consoleMessage("No missing turrets.");
        else if(player.credits < 1000 * missingturrets) {
            var ts = "";
            if(missingturrets > 1) ts = "s";
            var nopart = "";
            if(player.credits >= 1000) nopart = " (and no partial recovery)";
            player.consoleMessage("Not enough money: "+missingturrets+" missing turret"+ts+" need "+
                (1000*missingturrets)+" credits to recover"+nopart+".");
        } else {
            clock.addSeconds ( 3600 * missingturrets );
            player.credits -= 1000 * missingturrets;
            player.ship.restoreSubEntities();
            var ts = "";
            if(missingturrets > 1) ts = "s";
            player.consoleMessage(missingturrets+" turret"+ts+" recovered for "+(1000*missingturrets)+" credits.");
        }
    }
}
/*** begin dybal ***/
//this.playerBoughtNewShip = function(ship) {
this.playerBoughtNewShip = function(ship, price) {
    /*** this could have a (Oolite_version < X.XX) test whenever playerReplaceShip stops firing this event handler ***/
    if (this.$hyperspaceHangarRestoredShip) {
        // HyperSpaceHangar indicated it's restoring a ship, so this event isn't consequence of a ship purchase
        this.$hyperspaceHangarRestoredShip = false;
        return;
    }
/*** end dybal ***/
    this.$ShipVersion_SLCost();//save cost of increasing sercive level on current player ship
    player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
    this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel;
    if( this.$ShipVersion_Debug )
        log("ShipVersion", "Service level when bought: "+player.ship.serviceLevel+"%"); //debug
    var shname = this.$ShipVersion_PlayerShipName();
    if( (this.$ShipVersion_PlayerShipDataKey && this.$ShipVersion_PlayerShipDataKey.length > 0
        || this.$ShipVersion_storedShipString && this.$ShipVersion_storedShipString.length > 0)
        && !this.$ShipVersion_ReplaceShip ) { //there are stored old ship and not in buy loop
        var options = {
            "1_NO" : "No",
            "2_YES" : "Yes, I Confirm"
        };
        var desc = "";
        if(worldScripts["rocketmenu"]) //detailed ship info newest version
            desc=worldScripts["rocketmenu"].$RocketMenu_GetPlayerShipData();
        else if(worldScripts["hardmenu"]) //detailed ship info new version
            desc=worldScripts["hardmenu"].$HardMenu_GetPlayerShipData();
        else desc=this.$ShipVersion_GetPlayerShipData(); //old version
        if(desc.length > 0) desc+="\n\n";
        mission.runScreen({
            title: shname ,
                  message: desc+"You have "+formatCredits(this.$ShipVersion_PlayerCredits, false, true)+", "+
                      "if you buy you will have "+formatCredits(player.credits, false, true)+".\n"+
                      "Are you sure to buy "+shname+" ?",
            choices: options,
            model: "["+ship.dataKey+"]",
            background: "start_choices_bg.png"
        },
        function (choice) {
            if (choice === "1_NO") { //get back the old ship and money
                player.credits = this.$ShipVersion_PlayerCredits;
                this.$ShipVersion_ReplaceShip = true;//prevent buy loop
                if(worldScripts["Ship_Storage_Helper.js"]) { //new restore method
                    worldScripts["Ship_Storage_Helper.js"].restoreStoredShip(this.$ShipVersion_storedShipString);
                } else { //old restore method
                    player.replaceShip( this.$ShipVersion_PlayerShipDataKey, //the old ship
                        this.$ShipVersion_PlayerShipPersonality ); //and old version
                    var i=-1;
                    var eq = player.ship.equipment; //local variable, faster
                    while(eq[++i]) {
                        if( eq[i].name.length > 0 ) {
                            player.ship.removeEquipment(eq[i].equipmentKey);
                        } else {//skip noname eqs awarded by NumericHUD to avoid timeLimit error
                            for( var j = i + 10; j < eq.length; j+=10 ) {
                                if( eq[j].name.length != 0 ) {
                                    i = j - 10; //end of skip
                                    j = eq.length;
                                }
                            }
                            while( i < eq.length && eq[i].name.length == 0 ) i++;
                            i--;
                        }
                    }
                    i=-1;
                    while(this.$ShipVersion_PlayerShipEqs[++i]) {//give back equipments
                        player.ship.awardEquipment(this.$ShipVersion_PlayerShipEqs[i]);
                        if(this.$ShipVersion_PlayerShipEqDmg[i])
                            player.ship.setEquipmentStatus(
                                this.$ShipVersion_PlayerShipEqs[i],
                                "EQUIPMENT_DAMAGED");
                    }
                    i=this.$ShipVersion_PlayerShipPCap;
                    while(i-- > 1) player.ship.awardEquipment("EQ_PASSENGER_BERTH");
                    player.ship.fuel = this.$ShipVersion_PlayerShipFuel;
                    i=-1;
                    while(this.$ShipVersion_PlayerShipPass[++i]) //give back passengers
                        player.ship.addPassenger(this.$ShipVersion_PlayerShipPass[i][1],
                            this.$ShipVersion_PlayerShipPass[i][2],
                            this.$ShipVersion_PlayerShipPass[i][3],
                            this.$ShipVersion_PlayerShipPass[i][4] + clock.seconds,
                            this.$ShipVersion_PlayerShipPass[i][5],
                            this.$ShipVersion_PlayerShipPass[i][6]);
                    var c;
                    for (i = 0; i < this.$ShipVersion_PlayerShipMan.length; i++) {
                        c = this.$ShipVersion_PlayerShipMan[i].commodity;
                        player.ship.manifest[c] = this.$ShipVersion_PlayerShipMan[i].quantity;
                    }
                    player.ship.serviceLevel = this.$ShipVersion_PlayerShipServiceLevel;
                    player.ship.forwardWeapon = this.$ShipVersion_PlayerShipWeapons[0];
                    player.ship.aftWeapon = this.$ShipVersion_PlayerShipWeapons[1];
                    player.ship.portWeapon = this.$ShipVersion_PlayerShipWeapons[2];
                    player.ship.starboardWeapon = this.$ShipVersion_PlayerShipWeapons[3];
                }
                this.$ShipVersion_ReplaceShip = false;//end of replace
                shname = this.$ShipVersion_PlayerShipName();
                mission.exitScreen = "GUI_SCREEN_SHIPYARD";//back to the shipyard if not bought
            } else {
                var shipVersion = this.$ShipVersion_Int(player.ship);
                if(player.ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion) === "EQUIPMENT_UNAVAILABLE")
                    player.ship.awardEquipment("EQ_SHIP_VERSION_"+shipVersion);
/*** begin dybal ***/
                // if HyperSpaceHangar is installed, call its $offerOldShipStorage function
                // that function would have been be called in its playerBoughtNewShip event handler if we 
                // had not "hijacked" its screen opportunity
                if (worldScripts.HyperspaceHangar) {
                    // first we must setup what was cleaned by HH up while the player looked at our ship buying detail screen
                    worldScripts.HyperspaceHangar._actuallyBought = 1;
                    var _oldShip = JSON.parse(this.$ShipVersion_storedShipString);
                    _oldShip[8].length = 0; // clear ship's Cargo Hold
                    _oldShip[10].length = 0; // "space" ship's passengers :-)
                    _oldShip[31] = ""; // clear ship's Smuggler Hold
                    worldScripts.HyperspaceHangar._currentShip = JSON.stringify(_oldShip);
                    worldScripts.HyperspaceHangar.$offerOldShipStorage(ship, price);
                }
/*** end dybal ***/
            }
        });
        
    }
//    if( this.$ShipVersion_Debug ) player.consoleMessage("The "+shname+" is yours!", 10);
    this.$ShipVersion_Ships[0] = player.ship;
    this.$ShipVersion_ServiceLevelCounter = 0;
    if( worldScripts["detectors"] ) worldScripts["detectors"].$Detectors_NameSave(player.ship);
    else if( ship.script ) ship.script.$Detectors_Origname = shname; //log support without Detectors
}
this.shipSpawned = function(ship) {  //do not give version numbers to cargo, etc.
/*** begin dybal ***/
    /*** small performance improvement, worthwhile since this de-referencing would be done a lot of times when
         this event triggers, and it triggers a lot and in a bunch at system populating time ***/
    var wsHardShips = worldScripts["hardships"]; // de-referencing hardShips worldScript just once
/*** end dybal ***/
    if( !ship.isPiloted || ship.isStation || ship.isThargoid || ship.primaryRole === "escape-capsule" ) return;
        if( ship.scriptInfo ) {
                if(ship.scriptInfo.ccl_missionShip ) return;//mission ship by Svengali
                var v = parseInt( ship.scriptInfo.shipversion );
                if( v && v < 0 ) return; //disabled by shipversion = -1
        }
    if( this.$ShipVersion_BasicRulesOnly //skip custom roles
        && ship.primaryRole != "trader" && ship.primaryRole != "scavenger"
        && ship.primaryRole != "hunter" && ship.primaryRole != "miner"
        && ship.primaryRole != "pirate" && ship.primaryRole != "police"
        && ship.primaryRole != "wingman" && ship.primaryRole != "interceptor"
        && ship.primaryRole != "escort" && ship.primaryRole != "shuttle" ) return;
    var shipVersion = this.$ShipVersion_Int(ship); // 10-100
    if(ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion) === "EQUIPMENT_UNAVAILABLE")
        ship.awardEquipment("EQ_SHIP_VERSION_"+shipVersion); //need to made damageable the timed bonuses
//    if( this.$ShipVersion_Debug )
//        player.consoleMessage(ship.name+" "+ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion));
    
    if( !ship.isPlayer && this.$ShipVersion_Ships.indexOf(ship) < 0) {//only new NPC ships
        this.$ShipVersion_Ships[this.$ShipVersion_Ships.length] = ship;//store the new ship
        if( !worldScripts["detectors"] ) {//detectors use different function to show the version number
            ship.displayName += this.$ShipVersion_Show(ship); // 1 - 10 or 1.0 - 10.0
            //support to show (and log) names with version number without Detectors OXP also
            if( ship.script ) ship.script.$Detectors_Origname = ship.displayName;
        }
        //Ship Version one-time changes, do only once
        ship.accuracy += shipVersion / 20; //AI level tweak, +0.5 - +5
        //ship.thrust = ship.maxThrust*(0.5+shipVersion/100); //only weakenable, 0.1:half, 5.0-10.0:normal
        var v = Math.floor( shipVersion / 10 );//the first number of the version
        v = Math.min( v, this.$ShipVersion_AwardMax ); //limit the award
        if( ship.mass < 3000 ) v = 0; //drones not awarded with eqs, but get recharge and AI bonus
        var small = true; //Adder, Rocket Fighter and Gunship, but Runner and Corvette is not small
        if( ship.mass >= 30000 ) small = false;
        var largebounty = 0;
        var large = true; //from Cobra III and Rocket Miner
        if( ship.mass < 130000 ) large = false;
        else switch( v ) {  //add more bounty for large ships with high versions
            case 10: largebounty = 100; break; //100 from v10 and 100 from large means 200 in total
            case 9: largebounty = 60; break; //90+60=150 in total
            case 8: largebounty = 20; break; //80+20=100 in total
        }
        if( !small && ship.bounty > 0 ) //add version*10% for non-small ships
            ship.setBounty( ship.bounty + v * 10 + largebounty, "ShipVersion" + v );
/*** begin dybal ***/
        //if( !small && worldScripts["hardships"] )
        if( !small && wsHardShips )
/*** end dybal ***/
            ship.awardEquipment("EQ_HARDSHIELD_"+v);//but do not award Hard Armour
        switch( v ) {
        case 10:
            ship.awardEquipment("EQ_CLOAKING_DEVICE");
            ship.cloakAutomatic = true;
            if(ship.missiles && ship.missiles.length == ship.missileCapacity)
                    ship.removeEquipment("EQ_MISSILE"); //make room for QC_Mine
            if(ship.missiles && ship.missiles.length >= ship.missileCapacity - 1)
                    ship.removeEquipment("EQ_MISSILE");
            if(ship.missiles && ship.missiles.length == ship.missileCapacity)
                    ship.removeEquipment("EQ_HARDENED_MISSILE");
            if(ship.missiles && ship.missiles.length >= ship.missileCapacity - 1)
                    ship.removeEquipment("EQ_HARDENED_MISSILE");
            ship.awardEquipment("EQ_QC_MINE"); 
            ship.awardEquipment("EQ_QC_MINE"); 
            //NPCs often destroy yourself by your own QC_Mine so do not award to lower versions
            //will get 1.5x Military IronHide Armour from HardShips OXP directly, not here
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"])
            if( !small && wsHardShips)
/*** end dybal ***/
                ship.awardEquipment("EQ_ANTISAPPER"); //required HardShield10, awarded above
            //no break means get all of the following equipments also
        case 9:
            if( large ) ship.awardEquipment("EQ_MILITARY_JAMMER");
            if( large ) ship.awardEquipment("EQ_NAVAL_ENERGY_UNIT");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"] ) {
            if( !small && wsHardShips ) {
/*** end dybal ***/
                if( ship.bounty > 0 ) {
                    ship.awardEquipment("EQ_EEG");
                    ship.awardEquipment("EQ_HTCAT"); //requires EEG and clean status
                }
                if( large && worldScripts["shieldequalizercapacitors"]
                    && worldScripts["customshields"] )
                    ship.awardEquipment("EQ_BIGSHCAP");
            }
        case 8:
            if( large && worldScripts["customshields"] )
                ship.awardEquipment("EQ_SHIELD_ENHANCER"); //give increased recharge rate also
            //will get Military IronHide Armour from HardShips OXP automatically, do not award here
/*** begin dybal ***/
            //if( large && worldScripts["hardships"]) ship.awardEquipment("EQ_VERYHARDNOSE");
            if( large && wsHardShips) ship.awardEquipment("EQ_VERYHARDNOSE");
/*** end dybal ***/
        case 7:
            if( large ) ship.awardEquipment("EQ_MILITARY_SCANNER_FILTER");
            //ship.awardEquipment("EQ_NAVAL_SHIELD_BOOSTER"); //not used by CustomShields OXP
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"] ) {
            if( !small && wsHardShips ) {
/*** end dybal ***/
                ship.awardEquipment("EQ_ADDITIONAL_AFT_ARMOUR");
                if(ship.equipmentStatus("EQ_EEG")!="EQUIPMENT_OK") ship.awardEquipment("EQ_EEG");
                ship.awardEquipment("EQ_ESG"); //requires EEG
            }
            if( worldScripts["shieldequalizercapacitors"] ) {
                ship.awardEquipment("EQ_SHIELD_EQUALIZER");
                if( !small ) {
                    ship.awardEquipment("EQ_FORWARD_SHIELD_CAPACITOR");
                    ship.awardEquipment("EQ_AFT_SHIELD_CAPACITOR");
                }
            }
        case 6:
            ship.awardEquipment("EQ_FUEL_INJECTION");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"]) {
            if( !small && wsHardShips) {
/*** end dybal ***/
                ship.awardEquipment("EQ_ADDITIONAL_FRONT_ARMOUR");
                ship.awardEquipment("EQ_INJECTOR_BOX");
                if( ship.equipmentStatus("EQ_EEG")!="EQUIPMENT_OK")
                    ship.awardEquipment("EQ_EEG");
            }
            if( !small && worldScripts["Q-Charger"]) worldScripts["Q-Charger"].awardQCharger(ship);
        case 5:
            if(worldScripts["Shield Cycler"]) ship.awardEquipment("EQ_SC_SHIELD_CYCLER_INTERNAL");
            //will get IronHide Armour from HardShips OXP automatically, do not award here
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"] &&
            if( !small && wsHardShips &&
/*** end dybal ***/
                ship.equipmentStatus("EQ_VERYHARDNOSE") != "EQUIPMENT_OK" )
                ship.awardEquipment("EQ_HARDNOSE");
            var i = 0;
            while(i++ < ship.missileCapacity) //missiles get ECM hardening
                if(ship.equipmentStatus("EQ_MISSILE") == "EQUIPMENT_OK")
                    ship.removeEquipment("EQ_MISSILE");
            i = 0;
            if(ship.missiles) i = ship.missiles.length;
            if( !small ) while (i++ < ship.missileCapacity) ship.awardEquipment("EQ_HARDENED_MISSILE");
        case 4:
            if( !small && worldScripts["customshields"] ) ship.awardEquipment("EQ_SHIELD_BOOSTER");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"] ) {
            if( !small && wsHardShips ) {
/*** end dybal ***/
                ship.awardEquipment("EQ_ADDITIONAL_CORE_HULL");
                ship.awardEquipment("EQ_SHIELD_BOX");
                if( ship.bounty > 0 ) ship.awardEquipment("EQ_REG"); //requires not clean status
            }
        case 3:
            ship.awardEquipment("EQ_ENERGY_UNIT");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"] ) ship.awardEquipment("EQ_EQUIPMENT_BOX");
            if( !small && wsHardShips ) ship.awardEquipment("EQ_EQUIPMENT_BOX");
/*** end dybal ***/
        case 2:
            ship.awardEquipment("EQ_ECM");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"]) {
            if( !small && wsHardShips ) {
/*** end dybal ***/
                ship.awardEquipment("EQ_ELECTRONICS_BOX");
                if( ship.cargoSpaceCapacity > 1660 ) ship.awardEquipment("EQ_DUMMY100"); //and smallers also
                if( ship.cargoSpaceCapacity > 660 ) ship.awardEquipment("EQ_DUMMY50");
                if( ship.cargoSpaceCapacity > 150 ) ship.awardEquipment("EQ_DUMMY10");
                if( ship.cargoSpaceCapacity > 50 ) ship.awardEquipment("EQ_DUMMY5");
                if( ship.cargoSpaceCapacity > 10 ) ship.awardEquipment("EQ_DUMMY1");
            }
        case 1:
//            ship.awardEquipment("EQ_MILITARY_JAMMER");//debug
            ship.awardEquipment("EQ_ESCAPE_POD");
/*** begin dybal ***/
            //if( !small && worldScripts["hardships"]) ship.awardEquipment("EQ_LIFE_BOX");
            if( !small && wsHardShips ) ship.awardEquipment("EQ_LIFE_BOX");
/*** end dybal ***/
        default: //do not give anything if AwardMax 0
        }
        
        var br = shipVersion * 2 / 256; //higher version brighter (0 - 0.78)
        var br2 = 0.5 + br / 2; //brighter purple and orange (0.5 - 0.89)
        if( this.$ShipVersion_Colors ) { //color adjustments on IFF
            if(ship.primaryRole === "police" || ship.primaryRole === "interceptor"
                || ship.primaryRole === "wingman" || ship.isPolice) { //police ship
                ship.scannerDisplayColor1 = br2+" 0 "+br2; //bright purple
                        if(ship.target === player.ship) {//works automatic, not needed
                            ship.scannerDisplayColor2 = null;//not flashing
                        } else ship.scannerDisplayColor2 = "1 0 1";//magenta flash
            } else this.$ShipVersion_TimedColors(ship, shipVersion);
        }
    }
    ship.missileLoadTime /= ( shipVersion + 5 ) / 50; //10.0: half, 4.5: normal, 2.0: double
//    if( worldScripts["customshields"] ) worldScripts["customshields"].shipSpawned(ship); //surely add shield bonuses - removed due to cause InternalError: too much recursion
//    if( this.$ShipVersion_Debug ) player.consoleMessage(ship.displayName+" spawned.", 5);
}
this.shipWillEnterWitchspace = function(cause) {
    if( this.$ShipVersion_Debug )
        player.consoleMessage("Deleted "+this.$ShipVersion_Ships.length+" ship, doing "+cause+".", 10);
    delete this.$ShipVersion_Ships;
    this.$ShipVersion_Ships = [ player.ship ]; //start a new array in the new system
}
this.$ShipVersion_EQLimiter = function( eq, equipmentKey, mass ) {
//    player.consoleMessage( equipmentKey+" "+mass+" "+player.ship.mass );//debug
    if( equipmentKey == eq && player.ship.mass < mass ) {
        player.ship.removeEquipment( eq );
        player.consoleMessage("Your ship can not hold "+
            EquipmentInfo.infoForKey( eq ).name+", removed.", 5);
        player.credits += EquipmentInfo.infoForKey( eq ).price/10;
        return false;
    }
    return true;
}
this.$ShipVersion_GetPlayerShipData = function() {
    //detailed ship info old version, new can be found in rocketmenu.js of the RocketShips OXP
    var equip="";
    var s=""; 
    s=player.ship.forwardWeapon;
    if(s!=null) equip+="Forward "+s.name;
    s=player.ship.aftWeapon;
    if(s!=null) equip+=", Aft "+s.name;
    s=player.ship.portWeapon;
    if(s!=null) equip+="\nPort "+s.name;
    s=player.ship.starboardWeapon;
    if(s!=null) equip+=", Starboard "+s.name;
    if(equip.length > 0) equip+="\n";
    else equip+="No Laser\n";
    var eqs = "";
    var n = "";
    var eq = player.ship.equipment;
    var i=-1;
    while(eq[++i]) {
        n = eq[i].name;
        if( n.length > 0 ) eqs += n + ", ";
        else {    //skip noname eqs awarded by NumericHUD to avoid timeLimit error
            for( var j = i + 10; j < eq.length; j+=10 ) {
                if( eq[j].name.length != 0 ) {
                    i = j - 10; //end of skip
                    j = eq.length;
                }
            }
            while( i < eq.length && eq[i].name.length == 0 ) i++;
            i--;
        }
    }
    eqs = eqs.substr(0, eqs.length - 2); //cut last comma
    if(eqs.length > 0) equip+=eqs+"\n\n";
    else equip+="\n";//empty line before desc if no eqs
    var desc = "";
    if(player.ship.scriptInfo && player.ship.scriptInfo.buydesc)//show ship description if any
        desc="\""+player.ship.scriptInfo.buydesc+"\"\n\n";
    var sh = player.ship.maxForwardShield;
    if( worldScripts["shieldequalizercapacitors"] ) {
        if( player.ship.equipmentStatus("EQ_BIGSHCAP") == "EQUIPMENT_OK"
            && player.ship.equipmentStatus("EQ_FORWARD_SHIELD_CAPACITOR") == "EQUIPMENT_OK" )
            sh = sh + "+256";
        else if( player.ship.equipmentStatus("EQ_FORWARD_SHIELD_CAPACITOR") == "EQUIPMENT_OK" )
            sh = sh + "+64";
    }
    var psd = "";//player.ship.displayName+
    psd += "Speed: "+(player.ship.maxSpeed)+" mLS  Thrust: "+player.ship.maxThrust+
        "  Pitch: "+Math.round(player.ship.maxPitch*100)/100+
        "  Roll: "+Math.round(player.ship.maxRoll*100)/100+
        "  Yaw: "+Math.round(player.ship.maxYaw*100)/100+
//        "  Version: "+this.$shipVersion(player.ship)+
//        "  Service "+player.ship.serviceLevel+
        "\nCargo Space: "+player.ship.cargoSpaceCapacity;
    if(player.ship.scriptInfo && player.ship.scriptInfo.cargoext && parseInt(player.ship.scriptInfo.cargoext) > 0)
        psd += "+"+parseInt(player.ship.scriptInfo.cargoext);
    psd += "t  Mass: "+Math.max(Math.round(player.ship.mass/1000), 10)+ //min.10t
        "t  Size: "+Math.round(player.ship.boundingBox.x)+"*"+
        Math.round(player.ship.boundingBox.y)+"*"+Math.round(player.ship.boundingBox.z)+"m Radius:"+
        Math.round(player.ship.collisionRadius)+
        "m\nEnergy: "+player.ship.maxEnergy+"  Shield: "+sh+"  Max.Missile: "+player.ship.missileCapacity;
    psd += "\n"+equip+desc+"Price: "+formatCredits(player.ship.price, false, true);
    return(psd);
}
this.$ShipVersion_Int = function(ship) { //integer version number
    if( !ship ) return( 10 ); //default
    if( ship.scriptInfo ) {
        var v = parseInt( ship.scriptInfo.shipversion );
        if( v ) {
            if( v <= 1 ) return( 10 );
            if( v >= 10 ) return( 100 );
            if( v > 1 && v < 10 ) return( 10 * v );
        }
    }
    return( Math.max( 10, Math.floor( ship.entityPersonality/328 ) + 1 ) ); // 10 - 100
}
this.$ShipVersion_Display = function(shipVersion, simple) { //format the integer version to displayable string
    var st = shipVersion / 10;
    var sv = " "; //start with a space to easily add to the original name
    if( simple )  sv += Math.floor( st ); // 1 - 10
    else {
        sv += st;
        if( st ==  Math.floor( st ) )  sv += ".0";
    }
    return( sv ); // 1.0 - 10.0
}
this.$ShipVersion_PlayerShipName = function() { //with full version number
    return(player.ship.displayName + this.$ShipVersion_Display( this.$ShipVersion_Int( player.ship ), false ));
}
this.$ShipVersion_Show = function(ship) { //string version number of a ship
    if( this.$ShipVersion_Simple )
        return( this.$ShipVersion_Display( this.$ShipVersion_Int( ship ), true ) );
    else
        return( this.$ShipVersion_Display( this.$ShipVersion_Int( ship ), false ) );
}
this.$ShipVersion_SL = function( sl ) { //name of the service level
    if( sl >= 100 ) return("Perfect");
    if( sl >= 90 ) return("Good");
    if( sl >= 80 ) return("Weak");
    if( sl >= 76 ) return("Bad");
    if( sl < 76 ) return("Minimal");
    return("Unknown");
}
this.$ShipVersion_SLCost = function() { //cost of 1% service level repair
        if(player.ship.docked) { //next line works in docks only
            this.$ShipVersion_SLCr = Math.round( player.ship.price * 0.005 ); //0.5% of the new price
        }
    return( this.$ShipVersion_SLCr );
}
this.$ShipVersion_Timed = function() { //Ship Version repeated changes, called once in a second
//    player.ship.setEquipmentStatus("EQ_SHIP_VERSION_"+this.$ShipVersion_Int(player.ship), "EQUIPMENT_DAMAGED");//debug
    if( !player.ship || !player.ship.isValid ) return;
    if( this.$ShipVersion_ServiceLevelPrev == 0 ) {
        player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
        this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel; //save
        if( this.$ShipVersion_Debug )
            log("ShipVersion", "Service level "+player.ship.serviceLevel+"%"); //debug
    }
    
    if( this.$ShipVersion_ServiceLevelPrev != player.ship.serviceLevel ) {
        var st = player.ship.equipmentStatus("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
        player.ship.removeEquipment("EQ_SERVICE_LEVEL_"+this.$ShipVersion_ServiceLevelPrev);
        player.ship.awardEquipment("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel);
        if( st == "EQUIPMENT_DAMAGED" ) //set to damaged
            player.ship.setEquipmentStatus("EQ_SERVICE_LEVEL_"+player.ship.serviceLevel, st);
        if( this.$ShipVersion_ServiceLevelPrev > player.ship.serviceLevel
            && !player.ship.docked ) { //to avoid message when buy new ship with different SL
            //if( player.ship.serviceLevel > 76) {
                player.consoleMessage("Service level degraded ("+
                    this.$ShipVersion_SL(player.ship.serviceLevel)+")", 5);
            //} else if( player.ship.serviceLevel > 75)
                        //    player.consoleMessage("Weapons almost destroyed, repair the ship!", 5);
        } //if SL increased then no message to avoid it when another OXP increase it
          //for example HardShips OXP after hauler is not confirmed in the start menu
        if( this.$ShipVersion_Debug )
            log("ShipVersion", "Service level changed from "+
                this.$ShipVersion_ServiceLevelPrev+"% to "+
                player.ship.serviceLevel+"%"); //debug
        else {
            var c = "decreased";
            if( this.$ShipVersion_ServiceLevelPrev < player.ship.serviceLevel )
                c = "increased";
            log("ShipVersion", "Service level "+c+" ("+
                this.$ShipVersion_SL(player.ship.serviceLevel)+")");
        }
        this.$ShipVersion_ServiceLevelPrev = player.ship.serviceLevel; //save
    }
    
//    if( player.ship.serviceLevel > 86 ) player.ship.serviceLevel = 76; //debug
    
    if( player.ship.serviceLevel <= 75 ) {
        var w1 = player.ship.forwardWeapon;
        var w2 = player.ship.aftWeapon;
        var w3 = player.ship.portWeapon;
        var w4 = player.ship.starboardWeapon;
    //    if( w1 ) player.ship.forwardWeapon = null;
    //    if( w2 ) player.ship.aftWeapon = null;
    //    if( w3 ) player.ship.portWeapon = null;
    //    if( w4 ) player.ship.starboardWeapon = null;
    //    if(player.ship.subEntities)
        //            for(var i = 0; i < player.ship.subEntities.length; i++) //remove turrets
    //        player.ship.subEntities[i].remove();
        //      for(var j = 0; j < player.ship.subEntities.length; j++) { //remove missiles
    //        player.ship.removeEquipment("EQ_MISSILE");
    //        player.ship.removeEquipment("EQ_HARDENED_MISSILE");
    //        player.ship.removeEquipment("EQ_QC_MINE");
    //    }
    //    if( w1 || w2 || w3 || w4 || i > 0 || j > 0 ) //no docked check, need to show in dock also
    //        player.consoleMessage("Weapons destroyed! Do ship maintenance before buy anothers!", 5);
    }
    
    if( player.ship.docked ) return; //no recharges in dock
    for( var i = 0; i < this.$ShipVersion_Ships.length; i++) {
        var ship = this.$ShipVersion_Ships[i];//all version-equipped ship
        if( ship && ship.isValid && ship.isVisible && ship.isPiloted
            && !ship.isThargoid  && !ship.isDerelict ) { //to save CPU time
            var shipVersion = this.$ShipVersion_Int(ship); // 10-100
            if( this.$ShipVersion_Colors ) this.$ShipVersion_TimedColors(ship, shipVersion);
            if( ship.equipmentStatus("EQ_SHIP_VERSION_"+shipVersion)!="EQUIPMENT_OK") return;
            var recharge = ( shipVersion - 10 ) / 100; // 0 - +0.9/s
            if( ship.energy < ship.maxEnergy )  ship.energy += recharge * 2; // 0 - +1.8/s
                        //if( ship.temperature > recharge && shipVersion > 10 )  ship.temperature -= recharge;
                        //0 - -0.9/s, removed because cause buggy temperature drops with RocketShips OXP
            if( ship.isPlayer ) { //player only
                if( ship.forwardShield < ship.maxForwardShield )  ship.forwardShield += recharge;
                if( ship.aftShield < ship.maxAftShield )  ship.aftShield += recharge;
                if( shipVersion > 79 //repair bot working from v8.0, only integer possible
                    && ship.equipmentStatus("EQ_SERVICE_LEVEL_"+ship.serviceLevel)
                        == "EQUIPMENT_OK" )  {
                    if( ship.serviceLevel < 80 ) {//Bad levels only
                                                var cost = 100; //must be equal with cost of small fix in equipment.plist
                        this.$ShipVersion_ServiceLevelCounter++;
//                        player.consoleMessage(this.$ShipVersion_ServiceLevelCounter);//debug
                        if( this.$ShipVersion_ServiceLevelCounter > 300
                            && cost <= player.credits ) {
                            ship.serviceLevel++; //+1% in every 5 minutes of flying
                            player.credits -= cost;
                            this.$ShipVersion_ServiceLevelCounter = 0;
                            ship.removeEquipment("EQ_SERVICE_LEVEL_"
                                +this.$ShipVersion_ServiceLevelPrev);
                            ship.awardEquipment("EQ_SERVICE_LEVEL_"
                                +ship.serviceLevel);
                            this.$ShipVersion_ServiceLevelPrev = ship.serviceLevel; //save
                            var msg = "Service level repaired a bit for "+cost+" credits ("+
                                this.$ShipVersion_SL(ship.serviceLevel)+
                                ")";
                            if( this.$ShipVersion_Debug )
                                log("ShipVersion", "Service level repaired to "
                                    +ship.serviceLevel+"% for "
                                    +cost+" credits"); //debug
                            else log("ShipVersion", msg);
                            player.consoleMessage(msg, 10);
                        } else if( this.$ShipVersion_ServiceLevelCounter > 300 ) {
                            this.$ShipVersion_ServiceLevelCounter = 0;
                            var msg = "Service level "+
                                this.$ShipVersion_SL(ship.serviceLevel)+
                                " but repair need "+cost+" credits";
                            if( this.$ShipVersion_Debug )
                                log("ShipVersion", "Service level "
                                    +ship.serviceLevel+"% but repair need "
                                    +cost+" credits");
                            else log("ShipVersion", msg);
                            player.consoleMessage(msg, 10);
                        }
                    }
                }
            } else { //NPC only
                if(worldScripts["customshields"]) { //check if CustomShields installed
                    if( ship.script.customshieldsforwardshieldlevel <
                        ship.script.customshieldsmaxforwardshieldlevel )
                        ship.script.customshieldsforwardshieldlevel += recharge + 1;
                    if( ship.script.customshieldsaftshieldlevel <
                        ship.script.customshieldsmaxaftshieldlevel )
                        ship.script.customshieldsaftshieldlevel += recharge + 1;
                }
            }
        }
    }
}
this.$ShipVersion_TimedColors = function(ship, shipVersion) {
    //must check continually to turn a yellow netural ship to red when decided to attack the player
    if( !ship || ship.isPolice || ship.isDerelict ) return; //not need to refresh
    //var br = shipVersion * 2 / 256; //higher version brighter (0 - 0.78)
    //var br2 = 0.5 + br / 2; //brighter purple and orange (0.5 - 0.89)
        if( ship.hasHostileTarget && ship.target === player.ship ) { //hostile ship
                if(!ship.script) si.script = "oolite-default-ship-script.js"; //for sure
                if(ship.script && !ship.script.$SV_oldscannerDisplayColor1)
                    ship.script.$SV_oldscannerDisplayColor1 = ship.scannerDisplayColor1;
        if( ship.mass >= 130000 ) ship.scannerDisplayColor1 = "1 0 0";//(2*br+0.2)+" 0 0"; //red
                else ship.scannerDisplayColor1 = "1 0.2 0.2";//"1 "+(br-0.39)+" "+(br-0.39); //bright red
    } else if( ship.bounty > 0 ) {
                if( ship.mass >= 130000 ) ship.scannerDisplayColor1 = "1 0.5 0";//dark orange
                else ship.scannerDisplayColor1 = "1 0.6 0";//"1 "+br2+" "+br; //orange
    } else if( ship.isStation ) //friendly base
        ship.scannerDisplayColor1 = greenColor;
        else if( ship.script && ship.script.$SV_oldscannerDisplayColor1 ) {
            ship.scannerDisplayColor1 = ship.script.$SV_oldscannerDisplayColor1; //restore non-hostile color
            ship.script.$SV_oldscannerDisplayColor1 = null;
        }
    //else if( shipVersion >= 5 )
    //    ship.scannerDisplayColor1 = "1 1 "+(br-0.39); //bright yellow
    //else ship.scannerDisplayColor1 = (2*br+0.2)+" "+(2*br+0.2)+" 0"; //dark yellow
}
 |