Author Topic: Difficulties setting the price of Items/cargo for my market mod!  (Read 174 times)

ninekorn

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Difficulties setting the price of Items/cargo for my market mod!
« on: December 24, 2017, 12:48:15 pm »
Hey,

So I have been working on my market mod for Void Expanse and the good news is I was able to fix the HUGE hang / lag when accessing the market. So right now there is no lag or anything and that is due to how I rewrote the script. Basically, instead of having the whole 2500 lines of code in the same script, I was able to cut out every pieces of it to make 17 different scripts for every different market sections and different weapon types. Every time a step is taken for instance choosing the system to access the market than BAM a new dialogue opens with a hiddenInStationNPC that basically starts from where the other NPC left off right at choosing the stations for the system that was chosen with the first NPC.... So what I have created is a chain of NPCs that just do EXACTLY what the first script did but because there were so much variables and topics in the first 2500 lines script it was hanging the whole server. With this new system it fixes this... But there are issues... Right now spawning 15 NPC hiddenInStation doesnt seem to cause a lag issue with Void Expanse even IF I spawn them in every stations of the whole server. In the future I might reduce the number to 1 market terminal in each station and just 1 station contains the other HiddenInStationNPCs. We will see. But right now I am unable to fix the right prices for the items for the sell or buying functions. I have found pieces of code in the Trading.js and also the Stations.js but it doesn't seem to work properly because I am not getting the right prices unless I am not using them correctly? Here are the functions that I have tried:

Code: [Select]
function CalculateBuyPriceOfCargo(args)
{
var min = 1;
var max = 1.05;
var trade_margin = -ship.GetFinalCacheValue(args, "trade_margin");

var stationInfo = station.GetBaseByID(systemBases);
var fact = relations.GetFactionDispositionToShip(stationInfo.faction, args);
var relation_coef = 0.2 * GetFactionRelationCoef(fact);

var coef = min - (trade_margin + relation_coef) * 0.05;
coef = Clamp(min, coef, max);
return coef;
}


function CalculateBuyPriceOfItemModifier(args) {
var min = 1;
var max = 1.25;
var trade_margin = -ship.GetFinalCacheValue(args, "trade_margin");
var stationInfo = station.GetBaseByID(systemBases);

var fact = relations.GetFactionDispositionToShip(stationInfo.faction, args);
var relation_coef = 0.1 * GetFactionRelationCoef(fact);
var coef = max - trade_margin - relation_coef;
coef = Clamp(min, coef, max);
return coef;
}



function CalculateSellPriceOfItemModifier(args, min, max)
{
var min = 0.4;
var max = 0.9;
var stationInfo = station.GetBaseByID(systemBases);
var fact = relations.GetFactionDispositionToShip(stationInfo.faction, args);

var trade_margin = -ship.GetFinalCacheValue(args, "trade_margin");

var relation_coef = 0.2 * GetFactionRelationCoef(fact);
var coef = min + trade_margin + relation_coef;
coef = Clamp(min, coef, max);
return coef;
}

function GetFactionRelationCoef(relation) {
relation = Clamp(-150.0, relation, 150.0);
return relation / 150.0;
}


function Clamp(min, current, max) {
if (current < min) {
return min;
}
if (current > max) {
return max;
}
return current;
}

I thought that the CalculateBuyPriceOfCargo function was going to work but it doesn't really give me the right price of the cargo. The thing is for all of those function I am just sending the player Ship Id for the "args".

Before the function was

Code: [Select]
var relation_coef = 0.1 * GetFactionRelationCoef(args.faction_relation);

and I replaced it with the following code because the args which was only the player ship ID didn't contain the faction_relation stuff... So I went in the Scripting API Wiki page and found the relations.getfactionDispositi ontoShip but it doesn't give the right price:

Code: [Select]
var stationInfo = station.GetBaseByID(systemBases);
var fact = relations.GetFactionDispositionToShip(stationInfo.faction, args);
var relation_coef = 0.1 * GetFactionRelationCoef(fact);

Do you guys have any ideas how I could go and fetch the real prices of the items/cargo that show on the market page? And that would be with buying and selling stuff. I know you guys must be busy with Cryofall but if you've got a couple minutes to look into this i'd really appreciate it. If I can I will try to release this mod before new years day!

Oh and just trying to fetch the price with:
Code: [Select]
var price = generator.GetItemPrice(item.xml_id);
doesn't really give the price that the market shows... It seems to be the default price before all other modifiers are applied for instance the player level and player relation to faction of the station and whatnot!

Thank you!
nine
« Last Edit: December 24, 2017, 09:44:12 pm by ninekorn »

ai_enabled

  • AtomicTorch Founder
  • Hero Member
  • *****
  • Posts: 1002
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #1 on: December 24, 2017, 10:09:21 pm »
Hi,

The price is adjusted for every player as you think. It's not clear from the scripting as this code is called from C#.
Here are methods which we're calling:
Station.js - CalculateBuyPriceOfItemModifi er and CalculateSellPriceOfItemModif ier - for calculation of item price.
Station.js - CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo - for calculation of cargo price.

Unfortunately, these methods require quite a complex argument structure. For items:
Code: [Select]
int base_id;
int faction_relation; // this is a pre-calculated relation between the player's ship and the station faction
int item_id;
int ship_id;

For cargo it's more complicated:
Code: [Select]

[code]
string base_faction; // station faction ID
int base_id; // station ID
string cargo_id; // XML ID of the cargo
double cargo_price; // base price of cargo (from XML, I'm very unsure if this can be get from scripting API now - we might need an update to the game server for this API)
int faction_relation; // relation between the player's ship and the station faction
string ship_faction; // player ship faction ID
int ship_id; // player ship
int status; // cargo status

Alas, it's really complicated. Maybe a better approach would be to implement new scripting API methods accepting ship ID, station ID and item ID or cargo XML ID and returning the calculated price (by reusing the C# code).

Regards!

ninekorn

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #2 on: December 25, 2017, 01:08:23 am »
Yeah AI,

It's pretty much only the cargo_price that I needed. I debugged that price in the console inside the station.js script and its beeing calculated internally with c# script I believe because NO javascripts are making the call to the station.js so it must come from internal c# calculations. Even worse is that it's already calculated the moment that it enters the function of the station.js and then some more modifiers are calculated inside that station.js script. I was thinking of a workaround, like when accessing a station from the market script that it would regenerate/update the stations stock and debug me something from the station.js file but the thing is my market mod is not updating the whole station itself and it seems that even the update on the stations stock is beeing called internally because when I enter a station or click on the shop UI button once inside a station i can see messages in the console like "regenerated stock items for station blablabla" and that debug is not called from any js script so even that debug is beeing called internally in c#. I'm gonna have to think of something else.

ai_enabled

  • AtomicTorch Founder
  • Hero Member
  • *****
  • Posts: 1002
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #3 on: December 25, 2017, 01:25:43 am »
I verified the C# code and can definitely say that it calling Station.js file, methods CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo, to calculate the prices. Have you tried to add the console logging to these two methods?
The game calculates the prices for a player during docking, when the items are added/removed from the shop items list (such as during stock items regeneration) and when a player tries to buy/sell to calculate the current price.

"args.cargo_price" is the price of cargo/resource from the XML file.
It seems there is no API for getting the price by cargo/resource XML ID so you're stuck with this for now, alas.

ninekorn

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #4 on: December 25, 2017, 01:37:18 am »
Yes AI I debugged those CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo cargo_price and I can totally confirm that something is done with the default price internally and then BAM it is modified and cargo_price doesnt show the default price... its already modified upon arrival inside the station.js functions CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo. I'll try to find another workaround. Don't worry about it. Work on Cryofall ;)

edit: in fact i removed everything in the CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo and just returned the cargo_price and it still shows the modified price and not the default price...
« Last Edit: December 25, 2017, 01:43:35 am by ninekorn »

ai_enabled

  • AtomicTorch Founder
  • Hero Member
  • *****
  • Posts: 1002
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #5 on: December 25, 2017, 01:44:11 am »
You're right, the prices are modified (and their statuses too). The price for cargo is calculated by SetResourceBasePrice method at Station.js. This is implemented this way to adjust prices randomly with 10% deviation. Yeah, that's really overcomplicated. Each station generates items/cargo for sale and calls this method for each of them to calculate the price, then this price is stored on server, but players receive another price which is calculated by CalculateBuyPriceOfCargo and CalculateSellPriceOfCargo (or same methods for items).
The only solution will be to implement new API methods for getting the final price of item/cargo by providing player's ship ID and station ID.

ninekorn

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #6 on: December 25, 2017, 02:13:24 am »
Ok Yeah I just tested the  SetResourceBasePrice method at Station.js and this function runs once at the start of the server it seems so the prices are set once by that function. I work tomorrow so I gotta go sleep but Ill think about how I can use that function if I can. I was thinking of making a global array and put all those prices in it and see where that leads me. Anyway talk to you later and thank you for the help!

nine

ninekorn

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Difficulties setting the price of Items/cargo for my market mod!
« Reply #7 on: December 26, 2017, 07:01:39 pm »
Hi,

I have tested again this crash and found out the problem was on my market mod. I had put in the function SetResourceBasePrice that when the price was NOT defined to ALSO put the cargo items in the global arrays... So at every station shop updates in the game all the items were added again and again and again... After a couple hours there must have been thousands and more in that global array and everytime that I added something I also fetched the array to put items into it and then set back the array in the global array.... Even after 5minutes with the game running the game was skipping frames like crazy... So that's my bad. sorry

nine
« Last Edit: December 26, 2017, 07:04:58 pm by ninekorn »