System Populator editable by Javascript?

An area for discussing new ideas and additions to Oolite.

Moderators: winston, another_commander

Post Reply
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

System Populator editable by Javascript?

Post by Pleb »

An interesting concept was mentioned in this thread about the system populator being editable by Javascript. I tried playing around with the code a bit and managed to get it so that the system populator could be editable via the planetinfo.plist file.

The code could be modified to make these functions editable in the planetinfo.plist file. Using the source code from version 1.76.1, the original code in the Universe.m file from line 9079 reads like this:

Code: Select all

	// traders
	// TODO: consider using floating point for this stuff, giving a greater variety of possible results while maintaining the same range.
	unsigned trading_parties = (9 - economy);			// 2 .. 9
	if (government == 0) trading_parties *= 1.25;	// 25% more trade where there are no laws!
	if (trading_parties > 0)
		trading_parties = 1 + trading_parties * (randf()+randf());   // randomize 0..2
	while (trading_parties > 15)
		trading_parties = 1 + (Ranrot() % trading_parties);   // reduce
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@trading vessels", trading_parties, @"");
	
	unsigned skim_trading_parties = (Ranrot() & 3) + trading_parties * (Ranrot() & 31) / 120;	// about 12%
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@trading vessels", skim_trading_parties, @"sun skim ");
	
	// pirates
	int anarchy = (8 - government);
	unsigned raiding_parties = (Ranrot() % anarchy) + (Ranrot() % anarchy) + anarchy * trading_parties / 3;	// boosted
	raiding_parties *= randf() + randf();   // randomize
	while (raiding_parties > 25)
		raiding_parties = 12 + (Ranrot() % raiding_parties);   // reduce
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@pirate vessels", raiding_parties, @"");
	
	unsigned skim_raiding_parties = ((randf() < 0.14 * economy)? 1:0) + raiding_parties * (Ranrot() & 31) / 120;	// about 12%
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@pirate vessels", skim_raiding_parties, @"sun skim ");
	
	// bounty-hunters and the law
	unsigned hunting_parties = (1 + government) * trading_parties / 8;
	if (government == 0) hunting_parties *= 1.25;   // 25% more bounty hunters in an anarchy
	hunting_parties *= (randf()+randf());   // randomize
	while (hunting_parties > 15)
		hunting_parties = 5 + (Ranrot() % hunting_parties);   // reduce
	
	//debug
	if (hunting_parties < 1)
		hunting_parties = 1;
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@law/bounty-hunter vessels", hunting_parties, @"");
	
	unsigned skim_hunting_parties = ((randf() < 0.14 * government)? 1:0) + hunting_parties * (Ranrot() & 31) / 160;	// about 10%
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@law/bounty-hunter vessels", skim_hunting_parties, @"sun skim ");
	
	unsigned thargoid_parties = 0;
	while ((Ranrot() % 100) < thargoidChance && thargoid_parties < 16)
		thargoid_parties++;
	
	OOLog(kOOLogUniversePopulate, @"... adding %d Thargoid warships", thargoid_parties);
	
	unsigned rockClusters = Ranrot() % 3;
	if (trading_parties + raiding_parties + hunting_parties < 10)
		rockClusters += 1 + (Ranrot() % 3);
	
	rockClusters *= 2;
But if the previous code is changed with the following code:

Code: Select all

	// traders
	// TODO: consider using floating point for this stuff, giving a greater variety of possible results while maintaining the same range.
	unsigned trading_parties = (9 - economy);			// 2 .. 9
	if (government == 0) trading_parties *= 1.25;	// 25% more trade where there are no laws!
	if (trading_parties > 0)
		trading_parties = 1 + trading_parties * (randf()+randf());   // randomize 0..2
	while (trading_parties > 15)
		trading_parties = 1 + (Ranrot() % trading_parties);   // reduce
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_trading_parties = trading_parties;
	trading_parties = [systeminfo oo_unsignedIntForKey:@"trading_parties" defaultValue:backup_trading_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@trading vessels", trading_parties, @"");
	
	unsigned skim_trading_parties = (Ranrot() & 3) + trading_parties * (Ranrot() & 31) / 120;	// about 12%
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_skim_trading_parties = skim_trading_parties;
	skim_trading_parties = [systeminfo oo_unsignedIntForKey:@"skim_trading_parties" defaultValue:backup_skim_trading_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@trading vessels", skim_trading_parties, @"sun skim ");
	
	// pirates
	int anarchy = (8 - government);
	unsigned raiding_parties = (Ranrot() % anarchy) + (Ranrot() % anarchy) + anarchy * trading_parties / 3;	// boosted
	raiding_parties *= randf() + randf();   // randomize
	while (raiding_parties > 25)
		raiding_parties = 12 + (Ranrot() % raiding_parties);   // reduce
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_raiding_parties = raiding_parties;
	raiding_parties = [systeminfo oo_unsignedIntForKey:@"raiding_parties" defaultValue:backup_raiding_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@pirate vessels", raiding_parties, @"");
	
	unsigned skim_raiding_parties = ((randf() < 0.14 * economy)? 1:0) + raiding_parties * (Ranrot() & 31) / 120;	// about 12%
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_skim_raiding_parties = skim_raiding_parties;
	skim_raiding_parties = [systeminfo oo_unsignedIntForKey:@"skim_raiding_parties" defaultValue:backup_skim_raiding_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@pirate vessels", skim_raiding_parties, @"sun skim ");
	
	// bounty-hunters and the law
	unsigned hunting_parties = (1 + government) * trading_parties / 8;
	if (government == 0) hunting_parties *= 1.25;   // 25% more bounty hunters in an anarchy
	hunting_parties *= (randf()+randf());   // randomize
	while (hunting_parties > 15)
		hunting_parties = 5 + (Ranrot() % hunting_parties);   // reduce
	
	//debug
	if (hunting_parties < 1)
		hunting_parties = 1;
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_hunting_parties = hunting_parties;
	hunting_parties = [systeminfo oo_unsignedIntForKey:@"hunting_parties" defaultValue:backup_hunting_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@law/bounty-hunter vessels", hunting_parties, @"");
	
	unsigned skim_hunting_parties = ((randf() < 0.14 * government)? 1:0) + hunting_parties * (Ranrot() & 31) / 160;	// about 10%
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_skim_hunting_parties = skim_hunting_parties;
	skim_hunting_parties = [systeminfo oo_unsignedIntForKey:@"skim_hunting_parties" defaultValue:backup_skim_hunting_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d %@law/bounty-hunter vessels", skim_hunting_parties, @"sun skim ");
	
	unsigned thargoid_parties = 0;
	while ((Ranrot() % 100) < thargoidChance && thargoid_parties < 16)
		thargoid_parties++;
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_thargoid_parties = thargoid_parties;
	thargoid_parties = [systeminfo oo_unsignedIntForKey:@"thargoid_parties" defaultValue:backup_thargoid_parties];
	
	OOLog(kOOLogUniversePopulate, @"... adding %d Thargoid warships", thargoid_parties);
	
	unsigned rockClusters = Ranrot() % 3;
	if (trading_parties + raiding_parties + hunting_parties < 10)
		rockClusters += 1 + (Ranrot() % 3);
		
	// Check whether there is an entry in planetinfo.plist for party:
	unsigned backup_rockClusters = rockClusters;
	rockClusters = [systeminfo oo_unsignedIntForKey:@"rockClusters" defaultValue:backup_rockClusters];
	
	rockClusters *= 2;
	
	OOLog(kOOLogUniversePopulate, @"... adding %d asteroid clusters", rockClusters);
Now the game will check whether there is an entry in the planetinfo.list file that can overide the number of ships in each category that are generated in that system. So if an entry was added to the planetinfo.plist file that reads:

Code: Select all

"0 7" =
	{
		thargoid_parties = 100;
	};
Now every time the player enters the Lave system in Galaxy 1 there will be 100 Thargoid Warships in that system. This could also be used to always ensure that there are no police in a system, as the code will currently always make sure there is at least 1 police/bounty hunter in a system (see code above under the //debug comment).

So using the above as a template, would it be much more work to make this editable by Javascript? You can already use commands in Javascript to change system properties like the tech level, name, productivity, etc... so could this be expanded upon in the same way?
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2453
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: System Populator editable by Javascript?

Post by Wildeblood »

Pleb wrote:
I tried playing around with the code a bit and managed to get it so that the system populator could be editable via the planetinfo.plist file.
Thank you. I will be compiling and trying that out tonight. When I do, I'll prefix each of the planetinfo keys with ships_ because compulsive types like me like to have things grouped both logically and alphabetically simultaneously. I'll also use ships_rock_clusters, not ships_rockClusters.
Pleb wrote:
So using the above as a template, would it be much more work to make this editable by Javascript?
That should be all that is necessary. Any key-value pairs loaded from planetinfo.plist should be visible in system.info, and read/write by default. Did you try it?

Code: Select all

System.infoForSystem(galaxyNumber, player.ship.targetSystem).ships_thargoid_parties = 100;
Jump and see if they're waiting for you. :wink: :mrgreen: :twisted: (Have some random smilies.)
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2453
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: System Populator editable by Javascript?

Post by Wildeblood »

Wildeblood wrote:
That should be all that is necessary. Any key-value pairs loaded from planetinfo.plist should be visible in system.info, and read/write by default. Did you try it?

Code: Select all

System.infoForSystem(galaxyNumber, player.ship.targetSystem).ships_thargoid_parties = 100;
Jump and see if they're waiting for you.
Yes, it works. 100 is rather a lot of Thargoids, by the way. :(

Image
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: System Populator editable by Javascript?

Post by Smivs »

Wildeblood wrote:
Yes, it works. 100 is rather a lot of Thargoids, by the way. :(
It's called a swarm! :wink: :lol:
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: System Populator editable by Javascript?

Post by Thargoid »

No, this is a Thargoid swarm :twisted:
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

Re: System Populator editable by Javascript?

Post by Pleb »

Lol I chose 100 as an extreme example just so it would show up easier if anyone tested this with other OXPs running. But I'm glad this does actually work in Javascript as I did (unsuccessfully) attempt to code in a more precise command like system.thargoidParties = 100 into the code but couldn't figure out how to get it to work. Still cim already pointed out he has some ideas of his own concerning the system populator.

Another idea I had, which did work, was to make the system populator add Thargoid ships to route 2 as well as route 1, but this can be achieved via an OXP I guess...
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2453
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: System Populator editable by Javascript?

Post by Wildeblood »

Pleb wrote:
Lol I chose 100 as an extreme example just so it would show up easier if anyone tested this with other OXPs running. But I'm glad this does actually work in Javascript as I did (unsuccessfully) attempt to code in a more precise command like system.thargoidParties = 100 into the code but couldn't figure out how to get it to work.
That is just misguided. How could you set values for the populator to use after the system has already been set up? system.anyThing refers to the current system. The right place to put values used to create a system is exactly where you've put them. Give yourself a pat on the back.
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

Re: System Populator editable by Javascript?

Post by Pleb »

Wildeblood wrote:
That is just misguided. How could you set values for the populator to use after the system has already been set up? system.anyThing refers to the current system. The right place to put values used to create a system is exactly where you've put them. Give yourself a pat on the back.
I was trying to simplify what I was attempting to do but it works all the same which I hadn't expected. Another source code hack successful! :lol:
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
Post Reply