| Scripts/tca_script.js | "use strict";
this.name			= "Tionisla Chronicle Array OXP";
this.author			= "Storm";
this.copyright		= "Creative Commons Attribution - Non-Commercial - Share Alike 3.0 license";
this.description	= "Addition of a Broadcast Array station to each galactic chart";
// Arrays for the Broadcast Arrays locations by planet, their shipdata.plist names, and 'channel' name 
this.broadcastArrayPlanet = [124, 103, 197, 58, 211, 225, 184, 206];
this.broadcastArrayName = ["Tionisla Chronicle", "Views of the Worlds", "GNN", "Daily Wail", "G5TVN", "Rooters", "StarSports", "Snoopers"];
this.broadcastChannel = ["tca", "votw", "gnn", "dailywail", "g5tvn", "rooters", "starsports", "snoopers"];
this._debug = false;
this.systemWillPopulate = function() {
	// Check if in a system where a Broadcast Array is based, and add if so...
	if(system.ID == this.broadcastArrayPlanet[galaxyNumber] || this._debug === true) {
		if(system.countShipsWithRole("broadcast_array") == 0) {
			// Work out the location for placement of the Broadcast Array and it's beacon...
			var vector = system.mainPlanet.position.subtract(system.sun.position).direction();
			var arraypos = system.mainPlanet.position.add(vector.multiply(system.mainPlanet.radius * 4));
			system.setPopulator("tca", {
				priority: 200,
				location: "COORDINATES",
				groupCount: 1,
				callback: function(pos) {
					var tca = worldScripts["Tionisla Chronicle Array OXP"];
					// Add Broadcast Array to system...
					var broadcastArray = system.addShips("broadcast_array", 1, pos, 0)[0];
					broadcastArray.orientation = system.mainPlanet.position.subtract(broadcastArray.position).direction().rotationTo([0, 0, 1]);
					broadcastArray.scannerDisplayColor1 = "25 100 175";
					broadcastArray.scannerDisplayColor2 = "50 150 255";
					var vect = system.mainPlanet.position.subtract(system.sun.position).direction();
							
					// Add the Broadcast Array's Beacon to system...
					var buoyPos = system.mainPlanet.position.add(vect.multiply((system.mainPlanet.radius * 4) - 10000));
					var navbuoy = system.addShips("broadcast_array_beacon", 1, buoyPos, 0)[0];
					navbuoy.orientation = system.mainPlanet.position.subtract(broadcastArray.position).direction().rotationTo([0,0,1]);
					// Change the Broadcast Array's docking display / beacon to match the appropriate 'channel'...
					broadcastArray.displayName = (tca.broadcastArrayName[galaxyNumber] + " Array");
					broadcastArray.beaconLabel = (tca.broadcastArrayName[galaxyNumber] + " Array");
					var broadcastDisplayEmissionPng = (tca.broadcastChannel[galaxyNumber] + "_display_emission_map.png");
					broadcastArray.subEntities[4].setMaterials(
						{"broadcast_array_display.png":{diffuse_map: "broadcast_array_display.png", emission_map: broadcastDisplayEmissionPng}}
					);
					// Can't pass galaxy number to shader so hack to pass it via 'fuel' instead... this will allow
					// the broadcast arrays scrolling display in dock to 'change channel' to appropriate galaxy
					broadcastArray.fuel = galaxyNumber / 10;
					navbuoy.displayName = (tca.broadcastArrayName[galaxyNumber] + " Navigation Buoy");
					var navbuoyPng = (tca.broadcastChannel[galaxyNumber] + "_beacon.png");
					navbuoy.setMaterials({"broadcast_array_beacon.png": { diffuse_map: navbuoyPng }});
				},
				coordinates:arraypos,
				deterministic:true
			});
		}		
	}
}
this.shipWillLaunchFromStation = function(station) {
	if(station.hasRole("broadcast_array")) {
		missionVariables.trumbles = missionVariables.broadcastArray_storeTrumble; 
		// reactivate offer of trumble mission if it was available before docking
		missionVariables.broadcastArray_storeTrumble = null;
	}
}
this.shipWillDockWithStation = function(station) {
	if(station.hasRole("broadcast_array")) {
		// delay offer of trumble mission whilst docked at a broadcast array
		missionVariables.broadcastArray_storeTrumble = missionVariables.trumbles;
		missionVariables.trumbles = null;
	} 
}
/* Monkey-patch for TR - added by Alnivel and therefore and therefore all the blame on him */
this.startUp = function startUp() {
    const tr = worldScripts["Tionisla Reporter"];
    if (tr) {
        tr.$TCA_addErewhonPADPages = this.$TCA_addErewhonPADPages;
        tr.$TCA_missionOffers_original = tr.missionOffers;
        tr.missionOffers = this.$TCA_missionOffers_replacement;
        tr.$TCA_choiceEvaluation_preamble = this.$TCA_choiceEvaluation_preamble;
    }
    this.$trScript = tr;
};
this.startUpComplete = function startUpComplete() {
	if (this.$trScript) {
        if (!missionVariables.tca_tr_erehwonVisited) {
            missionVariables.tca_tr_erehwonVisited = +(
                missionVariables.tca_tr_finale === "SUCCESS" ||
                missionVariables.erehwonGotStory === "YES"
            );
        }        
    }
    const Lib_PAD = this.$Lib_PAD = worldScripts.Lib_PAD;
	if(Lib_PAD) {
		Lib_PAD._addPageInCategory("SYSTEMS.Tionisla G1",
			{
				name: "Tionisla G1",
				info: [
					"Located in the south-west of Galaxy 1.",
					"The Lizards of Tionisla wears mechanical masks in public.",
					"Home of the Tionisla Orbital GraveYard (TOGY).",
					"Home of the Tionisla Chronicle and its Array (TCA)."
				]
			});
		if (missionVariables.tca_tr_erehwonVisited) {
			this.$TCA_addErewhonPADPages();
		}
        if(missionVariables.tca_intro) {
            this.$addTCAPADPages();
        }
	}    
};
this.$addTCAPADPages = function $addTCAPADPages() {
    const Lib_PAD = this.$Lib_PAD;
    if (!Lib_PAD)
        return;
    Lib_PAD._addPageInCategory("INFOS.Tionisla Chronicle Array",
        { // Page content
            name: "Tionisla Chronicle Array",
            beacon: "Tionisla Chronicle Array",
            location: "In high orbit on the dark side of Tionisla",
            purpose: "Massive communications platform",
            special: [
                // Results of the missions?
            ],
            t1: "tca_lib_pad_tca.png"
        }, 
        [ // Page parents
            "SYSTEMS.Tionisla G1"
        ]);
}
this.missionScreenOpportunity = function missionScreenOpportunity() {
    if (galaxyNumber === 0 && system.ID == 124) {
        const dockedStation = player.ship.dockedStation;
        if (dockedStation.dataKey === "broadcast_array" && !missionVariables.tca_intro) {
            missionVariables.tca_intro = 1;
            mission.runScreen({
                screenID: "tca_intro",
                title: "Tionisla Chronicle Array",
                messageKey: "tca_intro"
            });
            this.$addTCAPADPages();
        }
    }
};
this.shipDockedWithStation = function (station) {
    if (station.displayName === "Erehwon Station") {
        missionVariables.tca_tr_erehwonVisited = 1;
        this.$TCA_addErewhonPADPages();
    }
};
this.$TCA_addErewhonPADPages = function $TCA_addErewhonPADPages() {
	const Lib_PAD = worldScripts.Lib_PAD;
	if(!Lib_PAD)
		return
	Lib_PAD._addPageInCategory("SYSTEMS.Erewhon G1",
        {
            name: "Erewhon G1",
            info: [
                "Located in the centre-west of Galaxy 1.",
                "rare metals etc. Imports food, liquor, narcotics & slaves.",
                "Planet is inhabited by lizards & rich in mineral deposits,",
                "'Lost' planet in interstellar space between Biorle & Usle."
            ]
        });
};
this.$TCA_missionOffers_replacement = function missionOffers_TionislaChronicleArray() {
    // this === worldScripts["Tionisla Reporter"]
    const dockedStation = player.ship.dockedStation;
    const erehwonReconnaissance = missionVariables.erehwonReconnaissance;
    let passToOriginalHandler = true;
    if (system.ID == 124) { // Tionisla
        passToOriginalHandler = false;
        
        if (dockedStation.isMainStation &&
            erehwonReconnaissance === "START" &&
            !missionVariables.tca_tr_lureWasShown) {
            mission.runScreen({
                screenID: "tionisla_reporter",
                title: "Tionisla Chronicle",
                messageKey: "tca_erehwonReconnaissance_lure"
            });
            missionVariables.tca_tr_lureWasShown = 1;
        }
        else if (dockedStation.dataKey === "broadcast_array") {
            // Do not show scene on main station if we already was at broadcast array
            missionVariables.tca_tr_lureWasShown = 1;
            if (erehwonReconnaissance === "START") {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_preamble",
                    choicesKey: "tca_erehwonReconnaissance_choice0"
                }, this.$TCA_choiceEvaluation_preamble
                );
            }
            else if (erehwonReconnaissance === "SUCCESS") {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_debriefing",
                    model: "erehwonStation"
                }
                );
                mission.displayModel.orientation = [0.75, 0, -0.6, 0];
                delete missionVariables.erehwonGotStory;
                delete missionVariables.erehwonFotoShot;
                delete missionVariables.erehwonPictureCount;
                delete missionVariables.erehwomMisJumpInfo;
                missionVariables.erehwonReconnaissance = "EPILOGUE";
                missionVariables.erehwonEpilogue = 1;
                missionVariables.erehwonActiontime = clock.days + 30;
                player.ship.removeEquipment("EQ_EREHWON_PHOTOCAMERA");
                mission.setInstructionsKey(null);
                this.$unmarkSystem(124);
                player.credits += 500;
                // 
                missionVariables.tca_tr_finale = "SUCCESS";
                //
            }
            else if (erehwonReconnaissance === "ACCEPTED" &&
                missionVariables.erehwonPictureCount == 0 && missionVariables.erehwonFotoShot === "NO") {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_failure",
                    choicesKey: "erehwonReconnaissance_choice2"
                }, this.choiceEvaluation
                );
            }
            else if (erehwonReconnaissance === "ACCEPTED" &&
                player.ship.equipmentStatus("EQ_EREHWON_PHOTOCAMERA") === "EQUIPMENT_DAMAGED") {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_damaged",
                    choicesKey: "erehwonReconnaissance_choice2"
                }, this.choiceEvaluation
                );
            }
        }
        else if(erehwonReconnaissance === "FINISHED" || erehwonReconnaissance === "EPILOGUE"){
            passToOriginalHandler = true;
        }
    }
    if(passToOriginalHandler) {
        // "DECLINED" is set in mission screen callback, so check at next mission screen opportunity
        if (missionVariables.erehwonReconnaissance === "DECLINED") {
            missionVariables.tca_tr_finale = "DECLINED";
        }
        this.$TCA_missionOffers_original();
        if (dockedStation.displayName === "Erehwon Station" && missionVariables.erehwonReconnaissance === "SUCCESS") {
            mission.setInstructionsKey("tca_erehwonReconnaissance_missioninfo2");
        }
    }
};
this.$TCA_choiceEvaluation_preamble = function (choice) {
    switch (choice) {
        case "OFFER_ACCEPTED":
            // If the mission screen has no choices then the callback's choice argument is null
            // Can (and should) I somehow distinguish if screen was ended with Space or interupted with F1?
            // If yes, then F1 == OFFER_DECLINED?
            let runPart3Callback = function (choice) {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_briefing_p3",
                    choicesKey: "erehwonReconnaissance_choice"
                }, this.choiceEvaluation
                );
            };
            let runPart2Callback = function (choice) {
                mission.runScreen({
                    screenID: "tionisla_reporter",
                    title: "Tionisla Chronicle",
                    messageKey: "tca_erehwonReconnaissance_briefing_p2"
                }, runPart3Callback
                );
            };
            mission.runScreen({
                screenID: "tionisla_reporter",
                title: "Tionisla Chronicle",
                messageKey: "tca_erehwonReconnaissance_briefing_p1"
            }, runPart2Callback
            );
            break;
        case "OFFER_DECLINED":
            missionVariables.erehwonReconnaissance = "DECLINED";
            missionVariables.erehwonActiontime = clock.days + 30;
            break;
    }
};
/*** Limited shipyard with discount ***/
this.$oxpEqPostfix = "_TCA";
this.$oxpEqPostfixOffset = -this.$oxpEqPostfix.length;
this.$removalEqInfix = "_REMOVAL";
this.$removalEqInfixOffset = -this.$removalEqInfix.length + this.$oxpEqPostfixOffset;
this.playerBoughtEquipment = function playerBoughtEquipment(equipmentKey) {
    const postfix = equipmentKey.slice(this.$oxpEqPostfixOffset);
    if (postfix !== this.$oxpEqPostfix) {
        return;
    }
    const infix = equipmentKey.slice(this.$removalEqInfixOffset, this.$oxpEqPostfixOffset);
    if (infix === this.$removalEqInfix) {
        let originalEquipmentKey = equipmentKey.slice(0, this.$removalEqInfixOffset);
        let removalEquipmentKey = equipmentKey.slice(0, this.$oxpEqPostfixOffset);
        let ps = player.ship;
        switch (originalEquipmentKey) { // there may be some nontrivial cases
            case "EQ_GCM_RANGE_FINDER":
                originalEquipmentKey = "EQ_GCM_RANGE_FINDER_MFD";
            case "EQ_GCM_RANGE_FINDER_EXT":
                let ws = worldScripts.GalCopBB_RangeFinder_MFD;
                ps.removeEquipment(equipmentKey);
                ws.playerBoughtEquipment(removalEquipmentKey);
                break;
            default:
                ps.removeEquipment(equipmentKey);
                ps.removeEquipment(originalEquipmentKey);
                // No money back?
                break;
        }
    }
    else {
        let originalEquipmentKey = equipmentKey.slice(0, this.$oxpEqPostfixOffset);
        let success = false;
        switch (originalEquipmentKey) { // there may be some nontrivial cases
            default:
                player.ship.removeEquipment(equipmentKey);
                success = player.ship.awardEquipment(originalEquipmentKey);
                break;
        }
        if (!success) {
            log("Tionisla Chronicle Array", "Failed to award player with \"" + originalEquipmentKey + "\" when processing \"" + equipmentKey + "\'");
        }
    }
};
 |