Currently, each script is associated with a specific player:
* scavfact.js -- scavenger faction
* rules.js -- human player
* AI's -- their associated player
What if there was an additional script that was "multiplayer"? It would work like this...
1. Whenever a game event is fired, WZ sets the 'me' of the multiplayer script to the id of the player the event is for, then triggers the event handler in the multiplayer script and also the individual player script.
This will allow a single script to receive events from all players, it will just look at the value of 'me' when the event is fired to work out which player that event was for.
2. The value of 'me' in the multiplayer script is not locked, it can be changed by the script at runtime.
It's my understanding that the first thing any of the JS API C++ functions do is work out which payer is invoking them by looking at the value of 'me' in the JS env that invoked the function. So if the JS script in the multiplayer script is able to alter the value of 'me' it can pretend to be any player and thus invoke functions as if it were that player.
Some possible uses for the multiplayer script environment:
In regards to Shadow's idea, the multiplayer script would be able to invoke getResearch() for each player in turn to work out if they've researched it, or just hook to eventResearched() to be notified when players research the various techs.Shadow Wolf TJC wrote:Lately, I've been trying to add wind farms to Contingency, and while I've figured out how to successfully implement them for 3.1 rc2 (via having the rules.js script check all players, instead of just the host player, when using the setPower() function every second), I'm not yet able to adjust their power generation rates (based on what power upgrades each player has researched) on account that I believe that eventResearched() is only called when the player running the script researches the given item. Instead, I wish that it was called whenever ANY player researches the item (though AI scripts could still check if the research lab that completed the research belongs to them or one of their allies).
Another scenario is where the same AI is in multiple slots. The AI could live in the multiplayer script so there's only one copy of most of it's functions, internal data, etc. It could create objects to store data associated with each player, but use the same instantiated code to process that data.
Yet another scenario is for campaigns and challenges. I know there's already the ability to have an 'extra' script with challenges, which is assigned to player 0, maybe that could become the multiplayer script (by having it receive all events and have an editable 'me' global). As the plot unfolds, the multiplayer script could change alliances between AI players, launch co-ordinated attacks, etc.
Obviously, a great deal of care would be required in the multiplayer script. It will be all too easy to forget to change 'me', and data relevant to each player would need to be carefully managed. Developers would need to give extra thought to all their internal/private variables, data structures, etc., to ensure they are "multiplayer aware". However, such an environment could offer a lot of new opportunities.
It's not something I'm desperate to see implemented any time soon. There are dozens of other more vital things IMHO, but just wanted to put the idea out there to see what people think of it.