| Config/script.js | "use strict";
this.name        = "andromeda";
this.author      = "Norby";
this.copyright   = "2014 Norbert Nagy";
this.licence     = "CC BY-NC-SA 4.0";
this.description = "Create an Andromeda spaceship next to main station.";
this.version     = "1.4";
//customizable properties
this.$AndromedaMaxFuelReserve = 50; //in light years
this.$AndromedaFuelCost = 3; //credits for each light year fuel
this.$AndromedaReFuelDistance = 5000; //m far from the main station
//internal properties, should not touch
this.$AndromedaCargoShip = null; //store the created CargoShip ship during docking
this.$AndromedaFuelReserve = 0;
this.$AndromedaShip = null; //store the created sunskimmer ship to prevent recreates
this.$AndromedaTimer = null;
//world script events
this.startUp = function() {	
//	player.replaceShip("andromeda-player", 0); //debug: player will start in an Andromeda
	this.$AndromedaFuelReserve = missionVariables.$AndromedaFuelReserve;
	if( !this.$AndromedaFuelReserve ) this.$AndromedaFuelReserve = this.$AndromedaMaxFuelReserve;
	this.$AndromedaSoundRefuel = new SoundSource;
	this.$AndromedaSoundRefuel.sound = "buy.ogg"; //refuel sound
	this.$AndromedaSoundRefuel.loop = false;
	this.$AndromedaSoundRefuel.repeatCount = 1;	
	this.$AndromedaTimer = new Timer(this, this.$Andromeda_Timed.bind(this), 2, 2);
}
this.playerWillSaveGame = function(message) {
        missionVariables.$AndromedaFuelReserve = this.$AndromedaFuelReserve;
}
this.shipCollided = this.shipScoopedOther = function(otherShip) {
	if(player.ship.dataKey == "andromeda-player" && otherShip
		&& otherShip == this.$AndromedaCargoShip ) { //scoop back
		player.consoleMessage("CargoShip is scooped back", 5);
		this.$AndromedaCargoShip = null;
		otherShip.remove(true);
	}
}
//Andromeda functions
this.$Andromeda_GetStation = function(p) {
	var s = null;
	if( this.$Andromeda_isStation(p.target) ) {
		s = p.target;
	} else if( p.checkScanner ) { //much faster than filteredEntities, need Oolite 1.79
		var targets = p.checkScanner(true);
		if( targets && targets.length > 0 )
			for( var i = 0; !s && i < targets.length; i++ ) {
				var t = targets[i];
				if( this.$Andromeda_isStation( t ) ) s = t;
			}
	} else { //before Oolite 1.79
		var targets = system.filteredEntities(this,
			this.$Andromeda_isStation, p, p.scannerRange);
		if( targets.length > 0 ) s = targets[0];
	}
	return(s);
}
this.$Andromeda_isStation = function(entity) {
	return (entity && entity.isValid && entity.isStation
		&& entity.target != player.ship
		&& entity.dataKey.indexOf("carrier") == -1 );
}
this.$Andromeda_LaunchCargoShip = function(p) {
	var s = p.spawnOne("[andromeda-CargoShip]");
	if( s && s.isValid ) {
		this.$AndromedaCargoShip = s;
		s.target = this.$Andromeda_GetStation(p);
		var msg = "";
		if( !s.target ) {
			msg = "CargoShip is not launched due to no friendly station nearby";
			s.remove(true);
		} else {
			msg = "CargoShip is going to "+s.target.name;
			s.position = p.position.add(p.vectorForward.multiply(600))
				.add(p.vectorUp.multiply(-100)); //starting position
			s.orientation = p.orientation;
			s.velocity = p.velocity.add(p.vectorForward.multiply(200)); //initial push
			p.target = s;
		}
		if(p == player.ship) player.consoleMessage(msg, 5);
	}
}
this.$Andromeda_Timed = function() {
	var p = player.ship;
	if( !p || !p.isValid ) return;
	
	var m = system.mainStation;
	if( m && m.isValid && ( !this.$AndromedaShip || !this.$AndromedaShip.isValid ) ) { //delayed create
		this.$AndromedaShip = system.addShips("andromeda", 1, m.position, 500000)[0]; //sunskimmer
//		this.$AndromedaShip.setAI("nullAI.plist");//debug
	}
	if(p.dataKey == "andromeda-player") {
		if( p.fuel <= 6 ) { //refuel from reserves
			if( this.$AndromedaFuelReserve > 0 ) {
				p.fuel += 1;
				this.$AndromedaFuelReserve -= 1;
				if( this.$AndromedaFuelReserve <= 0 ) {
					player.consoleMessage("Your fuel reserves are empty, "
						+"you should target a station and stop within "
						+(this.$AndromedaReFuelDistance/1000)
						+"km for refuel.", 10);
				} else player.consoleMessage(this.$AndromedaFuelReserve
					+" ly fuel left in reserves.", 10);
			}
		}
		var t = p.target;
		var c = this.$AndromedaCargoShip;
//		log(this.name, "station:"+this.$Andromeda_isStation( t )+t+c);//debug
		if( c && c.isValid ) {
			if( !this.$Andromeda_isStation(c.target) )
				c.target = this.$Andromeda_GetStation(p);
			if( c.position.distanceTo(p.position) > p.scannerRange )
				c = this.$AndromedaCargoShip = null;
		}
		if( this.$Andromeda_isStation( t ) ) {
			m = t; //else main station
			if( c && c.target != t ) c.target = t;
			if( !p.missilesOnline && !c )
				this.$Andromeda_LaunchCargoShip(p);
		}
//		log(this.name, "station:"+this.$Andromeda_isStation( t )+t+c+" ct"+c.target);//debug
		
		if( m && m.isValid && p.speed < 0.1 //stop near a main station for refuel
			&& this.$AndromedaFuelReserve < this.$AndromedaMaxFuelReserve ) {
			var mdist = m.position.distanceTo(p.position) - m.collisionRadius;
			//player.consoleMessage(mdist,1);//debug
			if( mdist < this.$AndromedaReFuelDistance ) {
				var maxcost = this.$AndromedaMaxFuelReserve * this.$AndromedaFuelCost;
				if( player.credits >= maxcost ) {
					var f = this.$AndromedaMaxFuelReserve - this.$AndromedaFuelReserve;
					var cr = f * this.$AndromedaFuelCost;
					this.$AndromedaFuelReserve = this.$AndromedaMaxFuelReserve; //up to full
					player.credits -= cr; //cost of the refill
					this.$AndromedaSoundRefuel.play(); //refuel sound
					player.consoleMessage("Payed "+cr+" credits for "+f+" ly reserve fuel.", 10);
				} else {
					player.consoleMessage("You must have at least "+maxcost
						+" credits before you can get reserve fuel.", 10);
				}
			}
		}
	}
}
 |