Page 5 of 11

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 2:48 pm
by Reval
How to pass an array to a function - is that possible in JS?

Edit: I just want the function to return the total of the elements in the array.

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 2:56 pm
by dybal
Reval wrote: Fri Nov 13, 2020 2:48 pm
How to pass an array to a function - is that possible in JS?
When you assign an array (or an object) to a variable, you are assigning a reference for the array (or object) to that variable.

Function parameters in JS are, if memory serves, passed by value, i.e., copied, but for parameters that are arrays or objects that means copying the reference to the array or object.

Beware that if the copy or the original variable are "reset" by assigning an empty array to it, the original and copy will not be referencing the same array any more... "reset" the array by assigning 0 to its length property to avoid that.

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 3:04 pm
by Reval
OK. So would this behave as expected? (it doesn't seem to be doing so)

Code: Select all

// Total price - passes price array
this._etGetTotPrice = function(which) {
	var result = 0;
	for (var t=0; t<20; t++) result += which[t];
	return result;
}
so I'm not changing the passed array in the function, just using its contents.

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 3:43 pm
by dybal
It should, I don't see anything wrong with it

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 6:10 pm
by Reval
How do I get the current system name as a string? - I've tried System.name and it returns "System". Also, System.mainPlanet returns "undefined".

Edit: Even Station.name only returns "Station".

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 6:22 pm
by dybal

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 6:27 pm
by Reval
Thanks. They're very confusing. I infer from what there is on the Wiki that first you must somehow get the system ID and then use that ID to get the name of the system (Inera, Raale, Tionisla or whatever), correct?

Edit: System.systemNameForID(System.ID) gives me errors, as did just systemNameForID(System.ID)

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 6:42 pm
by dybal
Reval wrote: Fri Nov 13, 2020 6:27 pm
Thanks. They're very confusing. I infer from what there is on the Wiki that first you must somehow get the system ID and then use that ID to get the name of the system (Inera, Raale, Tionisla or whatever), correct?

Edit: System.systemNameForID(System.ID) gives me errors, as did just systemNameForID(System.ID)
Try System.systemNameForID(system.ID)

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 7:12 pm
by Reval
That little 's' fixed it :) So could you explain the difference between System and system. When should I use the first and when the second? And does this difference apply to all the other objects and properties?

Re: (WIP) Elite Trader

Posted: Fri Nov 13, 2020 7:30 pm
by dybal
I infer system is a global variable (a property of the global object) with a reference to the current system object, while System is the class name of that object, used to access the static methods (there are similar Class names to access static methods of Ship, EquipmentInfo and SistemInfo)

Keep a bookmark for [EliteWiki] Oolite JavaScript Reference, it will come in handy time and time again.

Re: (WIP) Elite Trader

Posted: Sat Nov 14, 2020 6:56 am
by Reval
Elite Trader, version 0.2 Beta

OK. I think I've taken this little proof-of-concept about as far as I care to for now. Any more would just be overcomplicating things. Probably time for a version 1.0 release.

If anyone would like to give it a last-minute test, I'd be grateful for any bug reports...

http://wiki.alioth.net/img_auth.php/e/e ... Trader.oxz

Code: Select all

"use strict";

this.name = "Elite Trader";
this.author = "Reval";
this.description = "A profitable Market trade scores toward Elite";

/*  Version 0.2 Beta
	
	This OXP takes no account of cargo contracts,
	special deals, scoops of drifting cargo in space, 
	or of gems or precious metals.
	
	NB. If the game loads with full holds, the first score is missed.
*/

	
this.playerSoldCargo = function(commodity, units, price) {

	// selling preliminaries - update quantity and price
	if (this.$etLastPrice[commodity]>0) {
		this.$etSoldUnits += units;
		this.$etSoldPrice += price * units;
	}
	
	// see if we made a profit
	this.$etMadeProfit = ((this.$etSoldPrice > this.$etBoughtPrice) && (this.$etLastPrice[commodity] > 0));
	
	// conditions for scoring
	this.$etOKtoScore = ((units > 2) && (!this.$etDone) && (this.$etBoughtUnits>0) && (this.$etMadeProfit));
	
	// decrement the manifest by # of units and price
	if (this.$etLastPrice[commodity]>0) {
		this.$etBoughtUnits -= units;
		this.$etBoughtPrice -= this.$etLastPrice[commodity] * units;
	}
	
	// display ship's cargo and its purchase value
	if (price==this.$etLastPrice[commodity])
		player.consoleMessage("In hold: "+this.$etBoughtUnits+" pods ( "+formatCredits(((this.$etBoughtPrice)/10),true,true)+" )");

	// credit a "market killing" on first profitable sell
	if (this.$etOKtoScore) {
		player.score ++;
		player.consoleMessage("You made a killing!");
		// for simplicity, only one score per station
		this.$etDone = true;
	// otherwise acknowledge a skillful trade if we're not just unloading
	} else if (this.$etMadeProfit && (price!=this.$etLastPrice[commodity]) && (this.$etStation!=this.$etLastStation)) 
		player.consoleMessage("Nice trade!");
}
	

this.playerBoughtCargo = function (commodity, units, price) {
	// buying preliminaries - record quantity and price
	this.$etBoughtUnits += units;
	this.$etBoughtPrice += price * units;
	this.$etLastPrice[commodity] = price;
	// display ship's cargo and its purchase value
	player.consoleMessage("In hold: "+this.$etBoughtUnits+" pods ( "+formatCredits(((this.$etBoughtPrice)/10),true,true)+" )");
}


this.guiScreenChanged = function(to, from) {
	if (to == "GUI_SCREEN_MARKET") {
		// suggest a G.E.T. presence on the market floor
		player.consoleMessage("The "+System.systemNameForID(system.ID)+" Guild of Elite Traders", 5);
		// show hold-status on entering Market
		var pods = this._etPodCount();
		player.consoleMessage("In hold: "+pods+" pods ( "+formatCredits(((this.$etBoughtPrice)/10),true,true)+" )");
		// advise player to unload and re-buy cargo after game-load
		if ((pods>0) && (this.$etBoughtPrice<=0)) player.consoleMessage("Elite Trader: Please sell cargo and re-buy.",5); 
	} else
	if (to == "GUI_SCREEN_MANIFEST") {
		// suggest the G.E.T. presence on the Manifest screen
		player.consoleMessage("Under the aegis of the "+System.systemNameForID(system.ID)+" G.E.T", 10);
	} 		
}


this.shipDockedWithStation = function(station) {
	this.$etLastStation = this.$etStation;
	this.$etStation = station;
	this.$etLastSystem = this.$etSystem;
	this.$etSystem = System.systemNameForID(system.ID);
	// System's G.E.T. welcomes the commander on arrival
	player.consoleMessage("The "+this.$etSystem+" Guild of Elite Traders welcomes you!"/* We trust your passage from "+this.$etLastSystem+" passed peacefully."*/, 5);
	// clear flags and sold-quantities on docking
	this.$etDone = false;
	this.$etMadeProfit = false;
	this.$etSoldUnits = 0;
	this.$etSoldPrice = 0;
}


this.startUp = function() {
    log(this.name, "Initialising OXP " + this.name);
	// declare & initialize globals
	this.$etDone = false;
	this.$etMadeProfit = false;
	this.$etOKtoScore = false;
	this.$etSoldUnits = 0;
	this.$etSoldPrice = 0;
	this.$etBoughtPrice = 0;
	this.$etBoughtUnits = 0;
	this.$etSystem = "";
	this.$etLastSystem = "";
	this.$etNextSystem = "";
	this.$etStation=0;
	this.$etLastStation=0;
	this.$etLastPrice = new Array();
	for (var c=0; c<20; c++) this.$etLastPrice[c]=0;
}


// # of cargo pods in ship's hold
this._etPodCount = function() {
	var result = 0;
	for (var i=0; i<manifest.list.length; i++) {
			var q = manifest.list[i].quantity;
			if (q > 0) result+= q;
	}	
	return result;
}
CHANGES:

There's a little bit more immersion now with some of the messages.
Implemented an inventory check on starting the game.
Player is asked to unload and re-buy stock if the initial bought price does not tally.

Re: (WIP) Elite Trader

Posted: Sun Nov 15, 2020 6:33 am
by Reval
Elite Trader, version 1.0

Now in the Manager and on the Wiki.

More info HERE.

Trade well!

Re: (WIP) Elite Trader

Posted: Mon Nov 16, 2020 12:48 pm
by Reval
Elite Trader, version 1.1

Now in the Manager and on the Wiki.

More info HERE.

NEW IN VERSION 1.1:

Immersive G.E.T. messages at every stage of the voyage.
Successful trading brings useful rewards.

Trade well!

Re: (WIP) Elite Trader

Posted: Tue Nov 17, 2020 9:51 am
by Reval
Elite Trader, version 1.2

Now in the Manager and on the Wiki.

More info HERE.

NEW IN VERSION 1.2:

Minor cosmetic enhancements.

On Arrival Screen, speed-reward status message is not shown if another OXP resets the ship's maxSpeed (Masslock Compensator is a known culprit).

Trade well!

Re: (WIP) Elite Trader

Posted: Wed Nov 18, 2020 10:05 am
by Reval
Elite Trader, version 1.3

Now in the Manager and on the Wiki.

More info HERE.

NEW IN VERSION 1.3:

Cosmetic enhancements (contextual comms messages).

Destroying other ships--as opposed to damaging them to aid escape--brings System G.E.T. disapprobation and a bout of penance (no Elite score or rewards for that system).

Trade well!