| Config/script.js | "use strict";
this.name        = "trophy_col";
this.author      = "spara";
this.copyright   = "2012-2014 Mika Spara";
this.licence     = "CC BY-NC-SA 3.0";
this.description = "Manage Trophy Collection";
this.version     = "2.3.0";
this.startUp = function() {
	this.$lines = 19; 	//Number of rows printed to screen. Number of entries shown is 2*rows.
	if (!missionVariables.trophyArray) {
		this.$trophyArray = new Array();
	}
	else {
		this.$trophyArray = JSON.parse(missionVariables.trophyArray);
	}
	if (!missionVariables.trophyLog) {
		this.$trophyLog = new Array();
	}
	else {
		this.$trophyLog = JSON.parse(missionVariables.trophyLog);
	}
	this.$nextPage = false;
	this.$printInd = 0;
	this.$showLog = false;
	this.$cleanTrophyCollection();
	this.$trophyArrayFull = this.$trophyArray;
}
this.startUpComplete = function() {
	this.$addTrophyInterface();
	if (this.$isBigGuiActive() == true) this.$lines = 25;
}
//Clean bogus entries from the array.
this.$cleanTrophyCollection = function() {
	var bogusEntries = ["?"];
	for (var j = this.$trophyArray.length - 1; j >= 0; j--) {
		if (bogusEntries.indexOf(this.$trophyArray[j][0])!== -1)
			this.$trophyArray.splice(j,1);
	}
}
this.playerWillSaveGame = function () {
	missionVariables.trophyArray = JSON.stringify(this.$trophyArray);
	missionVariables.trophyLog = JSON.stringify(this.$trophyLog);
}
//Add new trophy to the collection.
this.shipKilledOther = function(whom, damageType) {
	if (!whom.hasRole("escape-capsule") && (whom.isPiloted || whom.isDerelict || whom.hasRole("tharglet") || whom.hasRole("thargon"))) {
		var name = expandMissionText("trophy_"+whom.dataKey);
		if (!name) name = whom.name;
		if (whom.primaryRole == "pirate-cove") //distinguish pirate coves from rock hermits
			name = "Pirate Cove";
		this.$trophyArray.unshift([name, formatCredits(whom.bounty, true, true) +", "+system.name]);
		if (this.$trophyArray.length == 2 * this.$lines + 1)
			this.$trophyArray.pop();
		this.$addTrophyToCollection(name);
		this.$setMFD();
	}
}
//Go to next page, if more than one screen of trophies.
this.missionScreenEnded = function() {
	if (this.$nextPage) {
		this.$showTrophyScreen("trophy_collection");
	}
	//show log after kill pages
	else if (this.$showLog) {
		this.shipWillLaunchFromStation();
		this.$showTrophyScreen("log");
		this.shipDockedWithStation();
		this.$nextPage = false;
		this.$printInd = 0;
	}
}
//Show mission screen of trophies
this.$showTrophyScreen = function(mode) {
	var largeGUI = this.$isBigGuiActive();
	this.$lines = 19;
	if (largeGUI == true) this.$lines = 25;
	//show log after kill pages
	this.$showLog = true;
	this.$nextpage = false;
	var titleText = "Trophy Collection";
	var bgImage = "trophy_bg.png";
	if (largeGUI == true) bgImage = "trophy_bg_lg.png";
	if (mode === "log") {
		titleText = "Trophy Log";
		bgImage = "trophy_inflight.png";
		if (largeGUI == true) bgImage = "trophy_inflight_lg.png";
	}
	var messageText = this.$createColumns();
	mission.runScreen({
		title: titleText,
		screenID:"trophy-collector",
		message: messageText,
		background: bgImage,
		allowInterrupt: true,
		exitScreen:"GUI_SCREEN_INTERFACES"
	});
}
//Swap collection to log on launch. Reset page count.
this.shipWillLaunchFromStation = function(station) {
	this.$trophyArray = this.$trophyLog;
	this.$printInd = 0;
	this.$nextPage = false;
	this.$setMFD();
}
this.$setMFD = function() {
	// no need to set the MFD if we're docked
	if (player.ship.isInSpace == false) return;
	if (player.ship.equipmentStatus("EQ_TROPHY_MFD") === "EQUIPMENT_OK") {
		var message = ""
		var line = 0
		if (this.$trophyArray.length > 0) {
			while (line < 10 && line < this.$trophyArray.length) {
				var details = this.$trophyArray[line][1].split(", ")
				message = message + this.$truncate(this.$trophyArray[line][0], details[0], details[1]) + "\n";
				line++;
			}
		}
		else message = "No trophies logged yet.\n\nGo hunt some trophies, Commander.";
		player.ship.setMultiFunctionText("trophy_mfd", message);
	}
	else player.ship.setMultiFunctionText("trophy_mfd", null);
}
this.equipmentDamaged = this.equipmentDestroyed = function(equipment) {
	if (equipment === "EQ_TROPHY_MFD") {
		player.ship.setMultiFunctionText("trophy_mfd", null);
	}
}
this.equipmentRepaired = function(equipment) {
	if (equipment === "EQ_TROPHY_MFD") {
		this.$setMFD();
	}
}
this.playerBoughtEquipment = function(equipment) {
	if (equipment === "EQ_TROPHY_MFD_REMOVE") {
		player.ship.removeEquipment("EQ_TROPHY_MFD");
		player.ship.removeEquipment("EQ_TROPHY_MFD_REMOVE");
	}
}
//squeezing looks ugly, so I'll truncate instead.
this.$truncate = function(tShip, tBounty, tSystem) {
	var line = tBounty + ", " + tShip + " (" + tSystem + ")";
	//first we chop the system down to 4 chars max
	while (global.defaultFont.measureString(line) > 14 && tSystem.length > 4) {
		tSystem = tSystem.slice(0, -1);
		line = tBounty + ", " + tShip + " (" + tSystem + ".)";
	}
	//if that's not enough, then we chop the shipname
	while (global.defaultFont.measureString(line) > 14) {
		var lastChar = tShip.charAt(tShip.length - 1);
		tShip = tShip.slice(0, -1);
		if (lastChar !== " " && tShip.charAt(tShip.length - 1) !== " ")
			line = tBounty + ", " + tShip + ". (" + tSystem + ".)";
		else
			line = tBounty + ", " + tShip + " (" + tSystem + ".)";
	}
	return line;
}
//Swap log to collection on docking.
this.shipDockedWithStation = function(station) {
	this.$trophyLog = this.$trophyArray;
	this.$trophyArrayFull.sort();
	this.$trophyArray = this.$trophyArrayFull;
	this.$addTrophyInterface();
	this.$showLog = false;
}
//Merge new trophy into the collection.
this.$addTrophyToCollection = function(name) {
	for (var j = 0; j < this.$trophyArrayFull.length; j++) {
		if (this.$trophyArrayFull[j][0] == name) {
			this.$trophyArrayFull[j][1] = this.$trophyArrayFull[j][1] + 1;
			return;
		}
	}
	this.$trophyArrayFull.push([name, 1]);
}
this.$addTrophyInterface = function() {
	player.ship.dockedStation.setInterface("trophy_collection",{
		title: "Trophy collection",
		category: "Statistical",
		summary: "Your collection of Trophies. You get trophies by killing piloted or derelict ships including thargoid drones and excluding escape pods.",
		callback: this.$showTrophyScreen.bind(this)
	});
}
//Format data into two columns.
this.$createColumns = function() {
	var columnized = "";
	var nameWidth = 0;
	var countWidth = 0;
	var spaceWidth = defaultFont.measureString(" ");
	var tabChar = String.fromCharCode(31);//hair-space
	var tabCharWidth = defaultFont.measureString(String.fromCharCode(31));//hair-space
	var entryWidth = 0;
	var start = this.$printInd;
	var lines = this.$lines; //maximum number of rows
	//less entries than rows?
	if (this.$trophyArray.length - start < lines)
		lines = this.$trophyArray.length - start;
	//last page?
	if (this.$trophyArray.length - (start+1) < 2*lines) {
		this.$printInd = 0;
		this.$nextPage = false;
	}
	else {
		this.$printInd = this.$printInd + 2 * lines;
		this.$nextPage = true;
	}
	var trunkName = "";
	for (var i = 0; i < lines ; i++) {
		nameWidth = defaultFont.measureString(this.$trophyArray[start+i][0]);
		countWidth = defaultFont.measureString("("+this.$trophyArray[start+i][1]+")");
		entryWidth = nameWidth + spaceWidth + countWidth;
		//Two column layout
		if (start+i+lines < this.$trophyArray.length) {
			//Left column
			if (entryWidth <= 15) {
				columnized = columnized + this.$trophyArray[start+i][0]+" ("+this.$trophyArray[start+i][1]+")";
				for (var j = 0; j < (17 - entryWidth) / tabCharWidth; j++)//spaceWidth
					columnized = columnized + tabChar;
			}
			else {
			//Entry too wide, truncate
				trunkName = this.$trophyArray[start+i][0];
				while (defaultFont.measureString(trunkName)+countWidth > 15) {
					trunkName = trunkName.substring(0,trunkName.length-1);
				}
				columnized = columnized + trunkName + "("+this.$trophyArray[start+i][1]+")";
				entryWidth = defaultFont.measureString(trunkName + "("+this.$trophyArray[start+i][1]+")");
				for (var j = 0; j < (17 - entryWidth) / tabCharWidth; j++)
					columnized = columnized + tabChar;
			}
			nameWidth = defaultFont.measureString(this.$trophyArray[start+i+lines][0]);
			countWidth = defaultFont.measureString("("+this.$trophyArray[start+i+lines][1]+")");
			entryWidth = nameWidth + spaceWidth + countWidth;
			//Right column
			if (entryWidth <= 15)
				columnized = columnized + this.$trophyArray[start+i+lines][0]+" ("+this.$trophyArray[start+i+lines][1]+")\n";
			else {
			//Entry too wide, truncate
				trunkName = this.$trophyArray[start+i+lines][0];
				while (defaultFont.measureString(trunkName)+countWidth > 15)
					trunkName = trunkName.substring(0,trunkName.length-1);
				columnized = columnized + trunkName + "("+this.$trophyArray[start+i+lines][1]+")\n";
			}
		}
		//One column layout
		else {
			if (entryWidth <= 32)
				columnized = columnized + this.$trophyArray[start+i][0]+" ("+this.$trophyArray[start+i][1]+")\n";
			else {
			//Entry too wide, truncate
				trunkName = this.$trophyArray[start+i][0];
				while (defaultFont.measureString(trunkName)+countWidth > 32)
					trunkName = trunkName.substring(0,trunkName.length-1);
				columnized = columnized + trunkName + "("+this.$trophyArray[start+i][1]+")\n"
			}
		}
	}
	//No trophies?
	if (this.$trophyArray.length == 0) {
		columnized = "No trophies collected yet.\n\nGo hunt some trophies Commander.";
		this.$showLog = false;
	}
	return columnized;
}
//-------------------------------------------------------------------------------------------------------------
// 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 {
		var bigGuiHUD = ["XenonHUD.plist", "coluber_hud_ch01-dock.plist"]; 	// until there is a property we can check, I'll be listing HUD's that have the allow_big_gui property set here
		if (bigGuiHUD.indexOf(player.ship.hud) >= 0) {
			return true;
		} else {
			return false;
		}
	}
}
 |