"use strict";
this.name = "military_fiasco";
this.author = "Written by Murgh";
this.copyright = "� 2008 the Oolite team.";
this.description = "Translation into JS by Eric Walch";
this.version = "3.0";

this.restartAllowed = false;

// configuration settings for use in Lib_Config
this._fiascoConfig = {
    Name: this.name, 
    Alias: expandMissionText("fiasco_config_alias"), 
    Display: expandMissionText("fiasco_config_display"), 
    Alive: "_fiascoConfig",
    Bool: {
        B0: { Name: "restartAllowed", Def: false, Desc: expandMissionText("fiasco_restart_allowed") },
        Info: expandMissionText("fiasco_config_bool_info")
    },
};

//-------------------------------------------------------------------------------------------------------------
this.startUp = function () {
    if (!missionVariables.military_fiasco) missionVariables.military_fiasco = "MIL_START";
    if (!missionVariables.military_fiasco_progresscount) missionVariables.military_fiasco_progresscount = 0;
    if (!missionVariables.military_fiasco_killcount) missionVariables.military_fiasco_killcount = 0;

    if (missionVariables.military_fiasco_restart) this.restartAllowed = (missionVariables.military_fiasco_restart === "Y" ? true : false);
}

//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function() {
    // register our settings, if Lib_Config is present
    if (worldScripts.Lib_Config) worldScripts.Lib_Config._registerSet(this._fiascoConfig);
	if (worldScripts.Lib_GUI) {
		worldScripts.Lib_GUI.$IDRules["military_fiasco_music"] = { pic: 1, snd: 1, mus: 0 };
		worldScripts.Lib_GUI.$IDRules["military_fiasco"] = { pic: 1, snd: 1, mus: 1 };
	}
}

//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function() {
    missionVariables.military_fiasco_restart = (this.restartAllowed ? "Y" : "N");
}

//-------------------------------------------------------------------------------------------------------------
this.missionScreenOpportunity = function () {
    if (player.ship.docked) {
        this.missionOffers();
    }
}

//-------------------------------------------------------------------------------------------------------------
this.missionOffers = function () {
    var fiascoStatus = missionVariables.military_fiasco;
    if (fiascoStatus === "MIL_COMPLETE" || fiascoStatus === "MIL_DELAY" || fiascoStatus === "MIL_NEVER") return;
    if (player.ship.dockedStation.isMainStation) {
        if ((fiascoStatus === "MIL_START" || fiascoStatus === "MIL_RESTART")) {
            if (player.score > 1000 && player.bounty < 5) {
                missionVariables.military_fiasco_old_killcount = missionVariables.military_fiasco_killcount;
                mission.runScreen({
                    screenID: "military_fiasco_music",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_intro",
                    choicesKey: "military_fiasco_offer1_yesnonever",
                    initialChoicesKey: "YesFiasco",
                    music: "military_fiasco_theme.ogg"
                },
                    this.choiceEvaluation);
                this.offer = "military_fiasco_first";
                missionVariables.military_fiasco_progresscount = 0;
            }
            return;
        }
        var killCount = missionVariables.military_fiasco_killcount - missionVariables.military_fiasco_old_killcount;
        if (fiascoStatus === "MIL_ACTIVE" && killCount > 0) {
            mission.runScreen({
                screenID: "military_fiasco_music",
                titleKey: "military_fiasco_title",
                messageKey: "military_fiasco_disp1",
                overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                choicesKey: "military_fiasco_disp1_yesno",
                initialChoicesKey: "YesFiasco",
                music: "military_fiasco_teaser.ogg"
            }, this.choiceEvaluation);
            this.offer = "military_fiasco_second";
            missionVariables.military_fiasco_progresscount = 0;
        }
        if (fiascoStatus === "MIL_STAGE1" && killCount > 8) {
            mission.runScreen({
                screenID: "military_fiasco_music",
                titleKey: "military_fiasco_title",
                messageKey: "military_fiasco_disp1",
                overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                choicesKey: "military_fiasco_disp2_yesno",
                initialChoicesKey: "YesFiasco",
                music: "military_fiasco_teaser.ogg"
            }, this.choiceEvaluation);
            this.offer = "military_fiasco_third";
            missionVariables.military_fiasco_progresscount = 0;
        }
        if (fiascoStatus === "MIL_STAGE2" && killCount > 19) {
            mission.runScreen({
                screenID: "military_fiasco_music",
                titleKey: "military_fiasco_title",
                messageKey: "military_fiasco_disp1",
                overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                choicesKey: "military_fiasco_disp3_yesno",
                initialChoicesKey: "YesFiasco",
                music: "military_fiasco_teaser.ogg"
            }, this.choiceEvaluation);
            this.offer = "military_fiasco_fourth";
            missionVariables.military_fiasco_progresscount = 0;
        }
        if (fiascoStatus === "MIL_LEVI_DESTROYED") {
            mission.runScreen({
                screenID: "military_fiasco_music",
                titleKey: "military_fiasco_title",
                messageKey: "military_fiasco_disp1",
                overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                choicesKey: "military_fiasco_disp4_justyes",
                music: "military_fiasco_theme.ogg"
            }, this.choiceEvaluation);
            this.offer = "military_fiasco_fifth";
            missionVariables.military_fiasco_progresscount = 0;
        }
        if (missionVariables.military_fiasco_progresscount > 4) {
            mission.runScreen({
                screenID: "military_fiasco_music",
                titleKey: "military_fiasco_title",
                messageKey: "military_fiasco_progress",
                overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                music: "military_fiasco_teaser.ogg"
            });
            missionVariables.military_fiasco_progresscount = 0;
        }
    }
}

//-------------------------------------------------------------------------------------------------------------
this.choiceEvaluation = function (choice) {
    if (!player.ship.docked) return;
    if (choice === "YesFiasco") {
        switch (this.offer) {
            case "military_fiasco_first":
                player.credits -= 5;
                if (missionVariables.military_fiasco == "MIL_START")
                    mission.runScreen({
                        screenID: "military_fiasco",
                        titleKey: "military_fiasco_title",
                        messageKey: "military_fiasco_brief1a",
                        overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                        choicesKey: "military_fiasco_cont1_yesno",
                        initialChoicesKey: "YesFiasco",
                        model: "mil-rattle"
                    }, this.choiceEvaluation)
                else
                    mission.runScreen({
                        screenID: "military_fiasco",
                        titleKey: "military_fiasco_title",
                        messageKey: "military_fiasco_brief1aBis",
                        overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                        choicesKey: "military_fiasco_cont1_yesno",
                        initialChoicesKey: "YesFiasco",
                        model: "mil-rattle"
                    }, this.choiceEvaluation);
                this.offer = "military_fiasco_first2";
                break;
            case "military_fiasco_first2":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_brief1b",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                    choicesKey: "military_fiasco_cont1_yesno",
                    initialChoicesKey: "YesFiasco",
                    model: "mil-basilisk"
                }, this.choiceEvaluation);
                this.offer = "military_fiasco_first3";
                break;
            case "military_fiasco_first3":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_brief1c",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 },
                    choicesKey: "military_fiasco_cont1_yesno",
                    initialChoicesKey: "YesFiasco",
                    model: "mil-hydra1"
                }, this.choiceEvaluation);
                this.offer = "military_fiasco_first4";
                break;
            case "military_fiasco_first4":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_brief1d",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 }
                });
                player.credits += 100;
                mission.setInstructionsKey("mission_military_desc1");
                missionVariables.military_fiasco = "MIL_ACTIVE";
                this.offer = null;
                break;
            case "military_fiasco_second":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_message1",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 }
                });
                mission.setInstructionsKey("mission_military_desc1");
                missionVariables.military_fiasco = "MIL_STAGE1";
                this.offer = null;
                break;
            case "military_fiasco_third":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_message2",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 }
                });
                mission.setInstructionsKey("mission_military_desc1");
                missionVariables.military_fiasco = "MIL_STAGE2";
                this.offer = null;
                break;
            case "military_fiasco_fourth":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_message3",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 }
                });
                mission.setInstructionsKey("mission_military_desc2");
                missionVariables.military_fiasco = "MIL_STAGE3";
                missionVariables.military_fiasco_helpers = Math.round(missionVariables.military_fiasco_killcount / 100) + 3;
                this.offer = null;
                break;
            case "military_fiasco_fifth":
                mission.runScreen({
                    screenID: "military_fiasco",
                    titleKey: "military_fiasco_title",
                    messageKey: "military_fiasco_debrief",
                    overlay: { name: "military_fiasco_navy_star.png", height: 546 }
                });
                mission.setInstructionsKey(null);
                missionVariables.military_fiasco = "MIL_COMPLETE";
                missionVariables.military_fiasco_old_killcount = null;
                missionVariables.military_fiasco_helpers = null;
                player.ship.manifest["gold"] += 100;
                this.offer = null;
                break;
            default:
                break;
        };
    }
    if (choice === "NoFiasco") {
        missionVariables.military_fiasco_delay = missionVariables.military_fiasco;
        missionVariables.military_fiasco = "MIL_DELAY";
        mission.setInstructionsKey("mission_military_desc3");
        this.offer = null;
    }
    if (choice === "NeverFiasco") {
        missionVariables.military_fiasco = "MIL_NEVER";
        missionVariables.military_fiasco_old_killcount = null;
        this.offer = null;
    }
}

//-------------------------------------------------------------------------------------------------------------
this.shipExitedWitchspace = function () {
    if (system.isInterstellarSpace) {
        if (Math.random() < 0.10) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 23.5 + 1.5);
        return;
    }
    // Scanning for offenders does nothing in anarchies and only little in low governments. So don't add the real military vessel there.
    // keep those governments dangerous
    switch (missionVariables.military_fiasco) {
        case "MIL_ACTIVE":
            if (Math.random() < 0.17) system.addShipsToRoute("smship", 1, 0.2 + Math.random() * 0.5);
            if (Math.random() < 0.06 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_STAGE1":
            if (Math.random() < 0.26) system.addShipsToRoute("smship", 2, 0.3 + Math.random() * 0.5);
            if (Math.random() < 0.11 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_STAGE2":
            if (Math.random() < 0.33) system.addShipsToRoute("smship", 3, 0.3 + Math.random() * 0.5);
            if (Math.random() < 0.11 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_STAGE3":
            if (Math.random() < 0.21) {
                var sunVector = system.sun.position.direction().multiply(30E3);
                var mom = system.addShips("leviathan-pirate", 1, sunVector, 5000)[0];
                var helpers = missionVariables.military_fiasco_helpers;
                if (helpers < 1) helpers = 1;
                var smships = system.addShips("smship", helpers, mom.position, 8000);
                if (smships) {
                    for (var i = 0; i < smships.length; i++)
                        if (smships[i].isValid) { //could already have been destroyed
                            smships[i].script.isHelper = true;
                            smships[i].script.mom = mom;
                        }
                }
            }
            if (Math.random() < 0.11 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_DELAY":
            if (Math.random() < 0.02 && missionVariables.military_fiasco_progresscount > 1) {
                if (missionVariables.military_fiasco_delay) missionVariables.military_fiasco = missionVariables.military_fiasco_delay
                else missionVariables.military_fiasco = "MIL_START";
                missionVariables.military_fiasco_delay = null;
            }
            if (Math.random() < 0.06 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_NEVER":
            if (Math.random() < 0.001 && missionVariables.military_fiasco_progresscount > 25) missionVariables.military_fiasco = "MIL_START";
            if (Math.random() < 0.035 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        case "MIL_COMPLETE":
            if (this.restartAllowed && Math.random() < 0.001 && missionVariables.military_fiasco_progresscount > 10) missionVariables.military_fiasco = "MIL_RESTART";
            if (Math.random() < 0.035 && system.government > Math.random() * 3) this.leviathan = new Timer(this, this.addCarrier.bind(this), Math.random() * 8 + 0.5);
            break;
        default:
            break;
    }
}

//-------------------------------------------------------------------------------------------------------------
// add carrier with short delay.
this.addCarrier = function addCarrier() {
    if (!system.isInterstellarSpace) system.addShips("leviathan", 1);
}