Weirdness with JS API and Research (or is it just me?)

For AI and campaign script related discussions and questions
Post Reply
User avatar
Goth Zagog-Thou
Regular
Regular
Posts: 1582
Joined: 06 Jan 2007, 08:08
Location: Delta Base
Contact:

Weirdness with JS API and Research (or is it just me?)

Post by Goth Zagog-Thou » 31 Jul 2013, 07:15

I have been working on implementing a workaround for research topics not unlocking as they should in Cam 4. All seemed to be going well until I got a little more complex with my code.

The subroutine I've been working on is as follows:

Code: Select all

function eventResearched(research, structure)
{
	if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-Turret01" &&research.done == true) // Sensors path
	{
		enableResearch("R-Sys-Sensor-TurretHvy", selectedPlayer);
		enableResearch("R-Sys-Sensor-Tower01", selectedPlayer);
		{
			if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-TurretHvy" && research.done == true)
			{
				enableResearch("R-Sys-Sensor-Tower02", selectedPlayer);
				{
					if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-Tower02" && research.done == true)
					{
						enableResearch("R-Sys-Sensor-Upgrade01", selectedPlayer);
						{
							if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-Upgrade01" && research.done == true)
							{
								enableResearch("R-Sys-RadarDetector01", selectedPlayer); // Make sure their structures are unlocked after researching each one!
								enableResearch("R-Sys-CBSensor-Turret01", selectedPlayer);
								enableResearch("R-Sys-VTOLStrike-Turret01", selectedPlayer);
								enableResearch("R-Sys-VTOLCBS-Turret01", selectedPlayer);
								enableResearch("R-Sys-Sensor-Upgrade02", selectedPlayer);
								{
									if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-Upgrade02" && research.done == true)
									{
										enableResearch("R-Sys-RadarDetectorHvy", selectedPlayer);
										enableResearch("R-Sys-CBSensor-TurretHvy", selectedPlayer);
										enableResearch("R-Sys-VTOLStrike-TurretHvy", selectedPlayer);
										enableResearch("R-Sys-VTOLCBS-TurretHvy", selectedPlayer);
										enableResearch("R-Sys-Sensor-Upgrade03", selectedPlayer);
										{
											if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-Upgrade03" && research.done == true)
											{
												enableResearch("R-Sys-Sensor-WS", selectedPlayer);
												{
													if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-WS" && research.done == true)
													{
														enableResearch("R-Sys-Sensor-WSTower", selectedPlayer);
														{
															if (structure.player == selectedPlayer && research.name == "R-Sys-Sensor-WSTower" && research.done == true)
															{
																enableResearch("R-Sys-Sensor-UpLink", selectedPlayer);
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Defense-TankTrap01" && research.done == true) // Walls and Construction items
	{
		enableResearch("R-Defense-TowerConst01", selectedPlayer);
		enableResearch("R-Struc-Materials01", selectedPlayer);
		{
			if (structure.player == selectedPlayer && research.name == "R-Defense-TowerConst01" && research.done == true)
			{
				enableResearch("R-Defense-HardcreteWall", selectedPlayer);
				{
					if (structure.player == selectedPlayer && research.name == "R-Defense-HardcreteWall" && research.done == true)
					{
						enableResearch("R-Defense-HardcreteGate", selectedPlayer);
						enableResearch("R-Defense-TowerConst02", selectedPlayer);
						enableResearch("R-Defense-BunkerConst01", selectedPlayer);
						enableResearch("R-Defense-WallTowerConst01", selectedPlayer);
						enableResearch("R-Defense-EmplConst01", selectedPlayer);
						enableResearch("R-Defense-TankTrapGate", selectedPlayer);
						enableResearch("R-Defense-WallUpgrade01", selectedPlayer);
						{
							if (structure.player == selectedPlayer && research.name == "R-Defense-WallUpgrade03" && research.done == true)
							{
								enableResearch("R-Defense-SupercreteWall", selectedPlayer);
								{
									if (structure.player == selectedPlayer && research.name == "R-Defense-SupercreteWall" && research.done == true)
									{
										enableResearch("R-Defense-SupercreteGate", selectedPlayer);
										enableResearch("R-Defense-TowerConst03", selectedPlayer);
										enableResearch("R-Defense-EmplConst02", selectedPlayer);
										enableResearch("R-Defense-WallTowerConst02", selectedPlayer);
										enableResearch("R-Defense-BunkerConst02", selectedPlayer);
										enableResearch("R-Defense-SuperEmplConst01", selectedPlayer);
										enableResearch("R-Defense-FortConst01", selectedPlayer);
										enableResearch("R-Defense-WallUpgrade04", selectedPlayer);
										enableResearch("R-Struc-Materials04", selectedPlayer);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Wpn-Artillery-ROF03" && research.done == true) // Mortars
	{
		enableResearch("R-Wpn-Mortar3", selectedPlayer);
		{
			if (structure.player == selectedPlayer && research.name == "R-Defense-Empl-MortarRot" && research.done == true)
			{
				enableResearch("R-Wpn-Mortar-Ramjet", selectedPlayer);
				enableResearch("R-Wpn-Artillery-Accuracy04", selectedPlayer);
				enableResearch("R-Wpn-Artillery-Damage04", selectedPlayer);
				enableResearch("R-Wpn-Artillery-ROF04", selectedPlayer);
			}
		}
	}
	
	// Rockets
	// Machineguns
	// Cannons
	// Flamers
	// Arty
	// Lasers
	// Railguns
	// Special Weapons
}
It was working until I got two or three branches in .. and the next test run later, it refused to work past that. The game simply does not execute the code past the second branch, when it should.

Yes, it's a bit hacky. No, I probably shouldn't be doing things this way. It's a workaround, as stated.

Any idea what could be going wrong? Guidance on this problem would be very much appreciated.
rules.js
(14.11 KiB) Downloaded 240 times

Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3753
Joined: 03 Aug 2006, 19:39

Re: Weirdness with JS API and Research (or is it just me?)

Post by Per » 31 Jul 2013, 11:20

The second conditional can never be true, since the first conditional ensures that you only get there if research.name == "R-Sys-Sensor-Turret01". Not sure why you add curly braces after some enableResearch() calls...? This should have no effect other than make the code harder to read, I think.

The 'research' object is not updated dynamically. It is just a copy of some information that is passed to eventResearched() to be handled.

User avatar
Goth Zagog-Thou
Regular
Regular
Posts: 1582
Joined: 06 Jan 2007, 08:08
Location: Delta Base
Contact:

Re: Weirdness with JS API and Research (or is it just me?)

Post by Goth Zagog-Thou » 31 Jul 2013, 20:02

Hmm.. so I should:

a) Not use the WZScript style of coding (with the braces in a procedural method) but use a Javascript method instead

b) Not use more than one conditional per check (makes sense)

c) treat the object as static rather than dynamic (makes sense also)

Thanks Per. I'll try it the other way after doing a little bit of homework on Javascript. Looks like I'm missing a few pointers.

User avatar
Goth Zagog-Thou
Regular
Regular
Posts: 1582
Joined: 06 Jan 2007, 08:08
Location: Delta Base
Contact:

Re: Weirdness with JS API and Research (or is it just me?)

Post by Goth Zagog-Thou » 01 Aug 2013, 03:25

Alrighty, just a little status update to close this subject out (and to possibly assist anyone else who needs to simulate Research in a Campaign).

The problem was indeed my implementation. Thanks to a little bit of homework on the matter, I've got things moving in the right direction again.

Rule #1: Thou shalt not go further than 2 brackets deep.

Rule #2: Thou shalt not specify more than one research topic at a time in the check.

Rule #3: Thou shall not assume that the code is dynamic.

Here's an example of the new and improved code .. and it works very well if I say so myself. Still have some things to put in.

Code: Select all

	// Engineering & Construction - This is really the main research topic that controls all the other topics.
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering01" && research.done == true)
	{
		enableResearch("R-Sys-Engineering02", selectedPlayer);
		enableResearch("R-Struc-PowerModuleMk1", selectedPlayer);
		enableResearch("R-Struc-Research-Module", selectedPlayer);
		enableResearch("R-Struc-RepairFacility", selectedPlayer);
		enableResearch("R-Struc-Factory-Module", selectedPlayer);
		enableResearch("R-Struc-Factory-Cyborg", selectedPlayer);
		enableResearch("R-Sys-Spade1Hvy", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering02" && research.done == true)
	{
		enableResearch("R-Sys-Engineering03", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering03" && research.done == true)
	{
		enableResearch("R-Sys-Engineering04", selectedPlayer);
		enableResearch("R-Sys-Spade2Mk1", selectedPlayer);
		enableResearch("R-Sys-Spade2Hvy", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering04" && research.done == true)
	{
		enableResearch("R-Sys-Engineering05", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering05" && research.done == true)
	{
		enableResearch("R-Sys-Engineering06", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering06" && research.done == true)
	{
		enableResearch("R-Sys-Engineering07", selectedPlayer);
		enableResearch("R-Sys-Spade3Mk1", selectedPlayer);
		enableResearch("R-Sys-Spade3Hvy", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering07" && research.done == true)
	{
		enableResearch("R-Sys-Engineering08", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering08" && research.done == true)
	{
		enableResearch("R-Sys-Engineering09", selectedPlayer);
	}
	
	if (structure.player == selectedPlayer && research.name == "R-Sys-Engineering09" && research.done == true)
	{
		enableResearch("R-Sys-Engineering10", selectedPlayer);
		enableResearch("R-Sys-Spade4Mk1", selectedPlayer);
		enableResearch("R-Sys-Spade4Hvy", selectedPlayer);
	}
Yay! :D

[EDIT] I should also note the reason for needing to simulate research in the first place -- it appears that prresearch.txt is NOT functioning in Campaign mode. The exact same file works perfectly in Multiplayer. This is the root cause of ALL of the Research problems I've experienced in developing Cam 4. Anyone who needs to should feel free to grab any part or all of the Simulated Research script they need. It's GPL-2 after all. :)

Post Reply