Ticket #2180: AIFix_.5.patch

File AIFix_.5.patch, 10.3 KB (added by wraitii, 10 years ago)
  • binaries/data/mods/public/simulation/ai/common-api-v3/shared.js

     
    180180        this._techModifications[o] = state.players[o].techModifications;
    181181   
    182182    this._entities = {};
    183     this.entities = new EntityCollection(this);
    184183
    185     /* (var id in state.entities)
     184    for (var id in state.entities)
    186185    {
    187186        this._entities[id] = new Entity(this, state.entities[id]);
    188187    }
    189188    // entity collection updated on create/destroy event.
    190189    this.entities = new EntityCollection(this, this._entities);
    191     */
    192190   
    193     this.ApplyEntitiesDelta(state);
    194 
    195191    // create the terrain analyzer
    196192    this.terrainAnalyzer = new TerrainAnalysis();
    197193    this.terrainAnalyzer.init(this, state);
     
    214210// applies entity deltas, and each gamestate.
    215211SharedScript.prototype.onUpdate = function(state)
    216212{
    217     if (this.turn !== 0)
    218         this.ApplyEntitiesDelta(state);
     213    this.ApplyEntitiesDelta(state);
    219214
    220215    Engine.ProfileStart("onUpdate");
    221216   
     
    297292            for (var i in this._players)
    298293                delete this._entityMetadata[this._players[i]][evt.msg.entity];
    299294        }
     295        else if (evt.type == "EntityRenamed")
     296        {
     297            //combines destroy and create.
     298
     299            warn(uneval(evt.msg));
     300
     301            // Switch the metadata
     302            for (var i in this._players)
     303            {
     304                warn (uneval(this._entityMetadata[this._players[i]][evt.msg.entity]));
     305                this._entityMetadata[this._players[i]][evt.msg.newentity] = this._entityMetadata[this._players[i]][evt.msg.entity];
     306                this._entityMetadata[this._players[i]][evt.msg.entity] = {};
     307            }
     308        }
    300309        else if (evt.type == "TrainingFinished")
    301310        {
    302311            // Apply metadata stored in training queues
  • binaries/data/mods/public/simulation/components/AIInterface.js

     
    3737    return state;
    3838};
    3939// Intended to be called first, during the map initialization: no caching
    40 AIInterface.prototype.GetFullRepresentation = function()
     40AIInterface.prototype.GetFullRepresentation = function(flushEvents)
    4141{
    4242    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    4343   
     
    4646   
    4747    // Add some extra AI-specific data
    4848    state.events = this.events;
     49    if (flushEvents)
     50    {
     51        state.events = [];
     52        this.events = [];
     53    }
    4954   
    5055    // Add entity representations
    5156    Engine.ProfileStart("proxy representations");
     
    8186    this.events.push({"type": "PlayerDefeated", "msg": msg});
    8287};
    8388
     89AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
     90{
     91    this.events.push({"type": "EntityRenamed", "msg": msg});
     92};
     93
    8494Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface);
  • binaries/data/mods/public/simulation/components/SkirmishReplacer.js

    (this hunk was shorter than expected)  
    2323};
    2424
    2525/**
    26  * Replace this entity with a civ-specific entity on the first turn
     26 * Replace this entity with a civ-specific entity
     27 * Message is sent right before InitGame() is called, in InitGame.js
    2728 */
    28 SkirmishReplacer.prototype.OnUpdate = function(msg)
     29SkirmishReplacer.prototype.OnSkirmishReplace = function(msg)
    2930{
    3031    var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
    3132    var civ = cmpPlayer.GetCiv();
    32 
     33   
    3334    var templateName = "";
    3435    if (civ in this.template)
    3536        templateName = this.template[civ];
    3637    else if ("general" in this.template)
    3738        templateName = this.template.general;
    38 
     39   
    3940    if (!templateName || civ == "gaia")
    4041    {
    4142        Engine.DestroyEntity(this.entity);
     
    4345    }
    4446   
    4547    templateName = templateName.replace(/\{civ\}/g, civ);
    46 
     48   
    4749    var cmpCurPosition = Engine.QueryInterface(this.entity, IID_Position);
    4850    var replacement = Engine.AddEntity(templateName);
    4951    var cmpReplacementPosition = Engine.QueryInterface(replacement, IID_Position)
     
    5456    var cmpCurOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    5557    var cmpReplacementOwnership = Engine.QueryInterface(replacement, IID_Ownership);
    5658    cmpReplacementOwnership.SetOwner(cmpCurOwnership.GetOwner());
    57 
     59   
     60    Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: replacement});
    5861    Engine.DestroyEntity(this.entity);
    5962};
    6063
  • binaries/data/mods/public/simulation/components/interfaces/SkirmishReplacer.js

     
    11Engine.RegisterInterface("SkirmishReplacer");
     2Engine.RegisterMessageType("SkirmishReplace");
  • binaries/data/mods/public/simulation/helpers/InitGame.js

     
    1 function InitGame(settings)
     1function ReplaceSkirmishGlobals()
    22{
    33    // This will be called after the map settings have been loaded,
    44    // before the simulation has started.
    55    // This is only called at the start of a new game, not when loading
    66    // a saved game.
     7    Engine.BroadcastMessage(MT_SkirmishReplace, {});
     8}
    79
     10function InitGame(settings)
     11{
     12    // This is called right after ReplaceSkirmishGlobals
     13
    814    // No settings when loading a map in Atlas, so do nothing
    915    if (!settings)
    1016        return;
     17   
     18    Engine.BroadcastMessage(MT_SkirmishReplace, {});
    1119
    1220    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    1321    var cmpAIManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIManager);
     
    3745    cmpAIManager.RunGamestateInit();
    3846}
    3947
     48Engine.RegisterGlobal("ReplaceSkirmishGlobals", ReplaceSkirmishGlobals);
    4049Engine.RegisterGlobal("InitGame", InitGame);
  • source/ps/Game.cpp

     
    4848#include "simulation2/components/ICmpPlayerManager.h"
    4949#include "soundmanager/ISoundManager.h"
    5050
     51#include "tools/atlas/GameInterface/GameLoop.h"
     52
    5153extern bool g_GameRestarted;
     54extern GameLoopState* g_AtlasGameLoop;
    5255
    5356/**
    5457 * Globally accessible pointer to the CGame object.
     
    200203    // Call the script function InitGame only for new games, not saved games
    201204    if (!m_IsSavedGame)
    202205    {
     206        if (!g_AtlasGameLoop->running)
     207        {
     208            m_Simulation2->ReplaceSkirmishGlobals();
     209            m_Simulation2->FlushDestroyedEntities();
     210        }
    203211        CScriptVal settings;
    204212        m_Simulation2->GetScriptInterface().GetProperty(m_Simulation2->GetInitAttributes().get(), "settings", settings);
    205213        m_Simulation2->InitGame(settings);
  • source/simulation2/Simulation2.cpp

     
    647647    return m->m_ComponentManager.GetScriptInterface();
    648648}
    649649
     650void CSimulation2::ReplaceSkirmishGlobals()
     651{
     652    GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "ReplaceSkirmishGlobals");
     653}
     654
    650655void CSimulation2::InitGame(const CScriptVal& data)
    651656{
    652657    GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "InitGame", data);
  • source/simulation2/Simulation2.h

     
    147147    void ResetState(bool skipScriptedComponents = false, bool skipAI = false);
    148148
    149149    /**
     150     * Send a message to replace skirmish entities with real ones
     151     * Called right before InitGame, on CGame instantiation.
     152     * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
     153     * This calls the ReplaceSkirmishGlobals function defined in helpers/InitGame.js.
     154     */
     155    void ReplaceSkirmishGlobals();
     156
     157    /**
    150158     * Initialise a new game, based on some script data. (Called on CGame instantiation)
    151159     * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
    152160     * This calls the InitGame function defined in helpers/InitGame.js.
  • source/simulation2/components/CCmpAIManager.cpp

     
    994994        ENSURE(cmpAIInterface);
    995995       
    996996        // Get the game state from AIInterface
    997         CScriptVal state = cmpAIInterface->GetFullRepresentation();
     997        CScriptVal state = cmpAIInterface->GetFullRepresentation(true);
    998998
    999999        // Get the passability data
    10001000        Grid<u16> dummyGrid;
  • source/simulation2/components/ICmpAIInterface.cpp

     
    3434    {
    3535        return m_Script.Call<CScriptVal> ("GetRepresentation");
    3636    }
    37     virtual CScriptVal GetFullRepresentation()
     37    virtual CScriptVal GetFullRepresentation(bool flushEvents = false)
    3838    {
    39         return m_Script.Call<CScriptVal> ("GetFullRepresentation");
     39        return m_Script.Call<CScriptVal> ("GetFullRepresentation",flushEvents);
    4040    }
    4141   
    4242};
  • source/simulation2/components/ICmpAIInterface.h

     
    3232     * Returns a script object that represents the current world state,
    3333     * to be passed to AI scripts. No caching for initialization
    3434     */
    35     virtual CScriptVal GetFullRepresentation() = 0;
     35    virtual CScriptVal GetFullRepresentation(bool flushEvents) = 0;
    3636
    3737    DECLARE_INTERFACE_TYPE(AIInterface)
    3838};