From 77c59070c109ab50a01d974b18930f773f83049b Mon Sep 17 00:00:00 2001 From: Matei Date: Sat, 10 Jun 2006 03:05:16 +0000 Subject: [PATCH] # Various gameplay updates. - Added per-player resource pool and modified the GUI and entity scripts to work with it. - Modified unit creation functions so the unit is always assigned a player before firing its initialize event, so that this event can deduct population, etc from the right player. - Fixed various small bugs, like a unit not cancelling all of its queued items when it dies, or buildings not costing anything. This was SVN commit r3998. --- .../entities/template_entity_full.xml | 1 + .../gui/test/functions_page_pregame_load.js | 7 +- .../gui/test/functions_page_session.js | 3 + .../functions_page_session_status_commands.js | 22 ++- .../official/gui/test/functions_sim_player.js | 181 +++--------------- .../mods/official/scripts/entity_functions.js | 136 ++++++++----- .../mods/official/scripts/game_startup.js | 38 ++++ source/ps/Game.cpp | 4 - source/scripting/ScriptGlue.cpp | 6 +- source/simulation/Entity.cpp | 27 ++- source/simulation/Entity.h | 2 +- source/simulation/EntityManager.cpp | 12 ++ source/simulation/EntityManager.h | 9 +- source/simulation/EntityStateProcessing.cpp | 2 - source/simulation/ProductionQueue.cpp | 7 +- source/simulation/Simulation.cpp | 16 +- 16 files changed, 248 insertions(+), 225 deletions(-) diff --git a/binaries/data/mods/official/entities/template_entity_full.xml b/binaries/data/mods/official/entities/template_entity_full.xml index 6dc1b1ac26..ac1a83ff74 100644 --- a/binaries/data/mods/official/entities/template_entity_full.xml +++ b/binaries/data/mods/official/entities/template_entity_full.xml @@ -23,5 +23,6 @@ + \ No newline at end of file diff --git a/binaries/data/mods/official/gui/test/functions_page_pregame_load.js b/binaries/data/mods/official/gui/test/functions_page_pregame_load.js index 30ea73ebfc..93c65a10d1 100644 --- a/binaries/data/mods/official/gui/test/functions_page_pregame_load.js +++ b/binaries/data/mods/official/gui/test/functions_page_pregame_load.js @@ -44,10 +44,6 @@ function startMap (mapName, losSetting, openWindow) // Set starting UI layout. GUIType=rb; - - // Initialise Resource Pools by attaching them to the Player object. - // (CPlayer code takes care of giving a copy to each player.) - createResources(); // Set session UI sprites to match the skin for the player's civilisation. // (We don't have skins for all civs yet, so we're using the standard menu skin. But it should be settable from here later.) @@ -137,6 +133,9 @@ function setupSession () // Fade out main theme and fade in session theme. crossFade(curr_music, curr_session_playlist_1); // , g_ConfigDB.system["sound.mastergain"]); + // Create the resouce counters + createResourceCounters(); + // Start refreshing the session controls. setInterval( snRefresh, 1, 100 ); } diff --git a/binaries/data/mods/official/gui/test/functions_page_session.js b/binaries/data/mods/official/gui/test/functions_page_session.js index 0b8fa7c00b..01c1965210 100644 --- a/binaries/data/mods/official/gui/test/functions_page_session.js +++ b/binaries/data/mods/official/gui/test/functions_page_session.js @@ -357,6 +357,9 @@ function snRefresh() // Update manual if it's open. refreshManual(); } + + // Always refresh resources + refreshResources(); } // ==================================================================== \ No newline at end of file diff --git a/binaries/data/mods/official/gui/test/functions_page_session_status_commands.js b/binaries/data/mods/official/gui/test/functions_page_session_status_commands.js index 5fa91ef9da..ce68c8a4ce 100644 --- a/binaries/data/mods/official/gui/test/functions_page_session_status_commands.js +++ b/binaries/data/mods/official/gui/test/functions_page_session_status_commands.js @@ -443,7 +443,7 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells) case "structciv": case "structmil": // Select building placement cursor. - startPlacing (Crd[getCrd (this.name, true)].entity); + tryConstruction( Crd[getCrd (this.name, true)].entity ); break; case "garrison": // Remove this item from the entity's garrison inventory. @@ -529,6 +529,26 @@ function updateTab (tab, type, cellSheet, attribute, attribute2, arrayCells) // ==================================================================== +function tryConstruction( name ) +{ + // Start the building placement cursor for the given template, if we have enough resources + + var result = checkEntityReqs( localPlayer, getEntityTemplate( name ) ); + + if (result == true) // If the entry meets requirements to be built (i.e. resources) + { + // Show the building placement cursor + startPlacing( name ); + } + else + { + // If not, output the error message. + console.write( result ); + } +} + +// ==================================================================== + function refreshCommandButtons() { // Reset button counter. diff --git a/binaries/data/mods/official/gui/test/functions_sim_player.js b/binaries/data/mods/official/gui/test/functions_sim_player.js index 9e9ddfb62d..31cd4be2aa 100644 --- a/binaries/data/mods/official/gui/test/functions_sim_player.js +++ b/binaries/data/mods/official/gui/test/functions_sim_player.js @@ -5,171 +5,41 @@ // ==================================================================== -function createResources() +function createResourceCounters() { - // Defines all resource types for future use. - // Assigns the value of game setup resource values as starting values. - - // Numbers for resource modes Low/Normal/High - tweak as needed for balancing - var resLowValue = Array(100,50,0,0); - var resNormalValue = Array(200,200,100,100); - var resHighValue = Array(1000,1000,1000,1000); + resourceUIArray = new Array(); - if (getCurrItemValue ("pgSessionSetupResources") == "Low") { - // Give low resources - var resValue = resLowValue; - } else if (getCurrItemValue("pgSessionSetupResources") == "Normal") { - // Give normal resources - var resValue = resNormalValue; - } else if (getCurrItemValue ("pgSessionSetupResources") == "High") { - // Give high resources - var resValue = resHighValue; - } else { - // Do not give any resources - var resValue = Array(0,0,0,0); - } - - addResource ("Food", Number(resValue[0])); - addResource ("Wood", Number(resValue[1])); - addResource ("Stone", Number(resValue[2])); - addResource ("Ore", Number(resValue[3])); - addResource ("Population", 0); - addResource ("Housing", 0); + addResourceCounter(0, "food"); + addResourceCounter(1, "wood"); + addResourceCounter(2, "stone"); + addResourceCounter(3, "ore"); + addResourceCounter(4, "population"); + addResourceCounter(5, "housing"); } // ==================================================================== -function addResource (resourceName, resourceQty) +function addResourceCounter (index, resourceName) { - // Creates a resource type. + // Creates a resource counter widget. - // Ensure resource name is title-case. - resourceName = toTitleCase (resourceName); - - if (!localPlayer.resource) - { - // Define the base resource group if it does not exist. - localPlayer.resource = new Array(); - // Set the length of the array. - localPlayer.resource.length = 0; - // Create the UI resource array container if it does not exist. - resourceUIArray = new Array; - } - - // Store resource's name and starting value. - localPlayer.resource.valueOf()[resourceName] = resourceQty; - - // The array is now one index longer. - localPlayer.resource.length++; + // Ensure resource name is lower-case. + resourceName = resourceName.toLowerCase(); // We maintain a sorted array of the resource indexes so that the UI counters can be refreshed in centered order. // (We don't include Housing in the list, though, as it does not have its own resource counter.) - if (resourceName != "Housing") + if (resourceName != "housing") { // If it's an even index, - if ((localPlayer.resource.length-1) % 2 == 0) + if (index % 2 == 0) // Add it to the end of the UI array. - resourceUIArray.push ((localPlayer.resource.length-1)); + resourceUIArray.push ( index ); else // Add it to the beginning of the UI array, - resourceUIArray.unshift ((localPlayer.resource.length-1)); + resourceUIArray.unshift ( index ); } - - // Dynamically adjust width of resource counter based on caption length. - refreshResources(); - console.write( "Added " + resourceName + " (" + resourceQty + ")" ); -} - -// ==================================================================== - -function setResources (resourceName, resourceQty) -{ - // Generic function to set the value of a resource in the player's pool. - - // Ensure resource name is title-case. - resourceName = toTitleCase (resourceName); - -// if ( localPlayer.resource.valueOf()[resourceName] ) -// { - // Set resource value. - localPlayer.resource.valueOf()[resourceName] = resourceQty; - - // Dynamically adjust width of resource counter based on caption length. - refreshResources(); - - console.write ("Resource set to " + resourceQty + " " + resourceName + "."); - return ( true ); -// } - - // If the resource wasn't in the list, report an error. -// console.write ("Failed to set resource " + resourceName + " to " + resourceQty); -// return ( false ) ; -} - -// ==================================================================== - -function getResources (resourceName) -{ - // Generic function to get the value of a resource in the player's pool. - - // Ensure resource name is title-case. - resourceName = toTitleCase (resourceName); - - return parseInt(localPlayer.resource.valueOf()[resourceName]); -} -// ==================================================================== - -function giveResources (resourceName, resourceQty) -{ - // Generic function to add resources to the player's Pool. - - // Ensure resource name is title-case. - resourceName = toTitleCase (resourceName); - -// if ( localPlayer.resource.valueOf()[resourceName] ) -// { - // Set resource value. - localPlayer.resource.valueOf()[resourceName] += resourceQty; - - // Dynamically adjust width of resource counter based on caption length. - refreshResources(); - - if (resourceName != "Housing" && resourceName != "Population") - console.write ("Earned " + resourceQty + " " + resourceName + "."); - return ( true ); -// } - -// // If the resource wasn't in the list, report an error. -// console.write ("Failed to add " + resourceQty + " to resource " + resourceName); -// return ( false ); -} - -// ==================================================================== - -function deductResources (resourceName, resourceQty) -{ - // Generic function to remove resources from the player's Pool. - - // Ensure resource name is title-case. - resourceName = toTitleCase (resourceName); - -// if( localPlayer.resource.valueOf()[resourceName] ) -// { - // Set resource value. - localPlayer.resource.valueOf()[resourceName] -= resourceQty; - - // Dynamically adjust width of resource counter based on caption length. - refreshResources(); - - if (resourceName != "Housing" && resourceName != "Population") - console.write("Deducted " + resourceQty + " " + resourceName + "."); - return( true ); -// } - -// // If the resource wasn't in the list, report an error. -// console.write ("Failed to deduct " + resourceQty + " from resource " + resourceName); -// return false; + console.write( "Added " + resourceName /*+ " (" + resourceQty + ")"*/ ); } // ==================================================================== @@ -178,13 +48,16 @@ function refreshResources () { // Refreshes all resource counters after update. - var resourcePool = localPlayer.resource; + var resourcePool = localPlayer.resources; var resourceCount = 0; - for (var currResource in resourcePool) + for (var currResource in resourcePool) { - // Pass the array index of the resource as the second parameter (as we'll need that to determine the centered screen position of each counter). - refreshResource (toTitleCase(currResource), resourceUIArray[resourceCount]); - resourceCount++; + if(currResource != "housing") + { + // Pass the array index of the resource as the second parameter (as we'll need that to determine the centered screen position of each counter). + refreshResource (toTitleCase(currResource), resourceUIArray[resourceCount]); + resourceCount++; + } } } @@ -207,10 +80,10 @@ function refreshResource (resourceName, resourceIndex) var resourceIconObject = getGUIObjectByName ("snResourceCounterIcon_" + (resourceIndex + 1)); // Update counter caption (since we need to have up-to-date text to determine the length of the counter). - var caption = localPlayer.resource.valueOf()[resourceName]; + var caption = localPlayer.resources[resourceName.toLowerCase()]; // The Population counter also lists the amount of available housing. if (resourceName == "Population") - caption += "/" + localPlayer.resource.valueOf()["Housing"]; + caption += "/" + localPlayer.resources["housing"]; resourceObject.caption = caption; // Update counter tooltip. diff --git a/binaries/data/mods/official/scripts/entity_functions.js b/binaries/data/mods/official/scripts/entity_functions.js index 8b44525ace..ee7a17fd3f 100644 --- a/binaries/data/mods/official/scripts/entity_functions.js +++ b/binaries/data/mods/official/scripts/entity_functions.js @@ -32,7 +32,40 @@ function entityInit() { // Initialise an entity when it is first spawned (generate starting hitpoints, etc). - this.addCreateQueue = entityAddCreateQueue; + // If the entity is a foundation, we must deduct resource costs here + if( this.building ) + { + var template = getEntityTemplate( this.building, this.player ); + var result = checkEntityReqs( this.player, template ); + + if (result == true) // If the entry meets requirements to be added to the queue (eg sufficient resources) + { + // Cycle through all costs of this entry. + var pool = template.traits.creation.resource; + for ( resource in pool ) + { + switch ( resource.toString() ) + { + case "population": + case "housing": + break; + default: + // Deduct the given quantity of resources. + this.player.resources[resource.toString()] -= parseInt(pool[resource]); + + console.write ("Spent " + pool[resource] + " " + resource + " to build " + + template.traits.id.generic); + break; + } + } + } + else + { + // Might happen if the player clicks to place 2 buildings really fast + console.write( "Could not begin construction: " + result ); + evt.preventDefault(); + } + } // If this is a foundation, initialize traits from the building we're converting into if( this.building && this.building != "" ) @@ -217,12 +250,15 @@ function entityInit() // If the entity either costs population or adds to it, if (this.traits.population) { + console.write("Here: " + this.traits.population.add + " " + this.traits.population.rem + + " " + this.player.resources.population + " " + this.player); + // If the entity increases the population limit (provides Housing), if (this.traits.population.add) - getGUIGlobal().giveResources ("Housing", this.traits.population.add); + this.player.resources.housing += parseInt(this.traits.population.add); // If the entity occupies population slots (occupies Housing), if (this.traits.population.rem) - getGUIGlobal().giveResources ("Population", this.traits.population.rem); + this.player.resources.population += parseInt(this.traits.population.rem); } // Build Unit AI Stance list, and set default stance. @@ -331,6 +367,22 @@ function attachAuras() // ==================================================================== +function entityDestroyed( evt ) +{ + // If the entity either costs population or adds to it, + if (this.traits.population) + { + // If the entity increases the population limit (provides Housing), + if (this.traits.population.add) + this.player.resources.housing -= parseInt(this.traits.population.add); + // If the entity occupies population slots (occupies Housing), + if (this.traits.population.rem) + this.player.resources.population -= parseInt(this.traits.population.rem); + } +} + +// ==================================================================== + function foundationDestroyed( evt ) { if( this.building != "" ) // Check that we're *really* a foundation since the event handler is kept when we change templates (probably a bug) @@ -344,7 +396,7 @@ function foundationDestroyed( evt ) for( r in resources ) { amount = parseInt( fractionToReturn * parseInt(resources[r]) ); - getGUIGlobal().giveResources( r.toString(), amount ); + this.player.resources[r.toString()] += amount; } } } @@ -510,7 +562,7 @@ function performGather( evt ) // Remove amount from target. s.curr -= gather_amt; // Add extracted resources to player's resource pool. - getGUIGlobal().giveResources(s.type.toString(), parseInt(gather_amt)); + this.player.resources[s.type.toString()] += gather_amt; // Kill the target if it's now out of resources if( s.curr == 0 ) @@ -531,12 +583,12 @@ function performHeal( evt ) } // Cycle through all resources. - pool = this.player.resource; + pool = this.player.resources; for( resource in pool ) { switch( resource.toString() ) { - case "Population" || "Housing": + case "population" || "housing": break; default: // Make sure we have enough resources. @@ -558,17 +610,17 @@ function performHeal( evt ) } // Cycle through all resources. - pool = this.player.resource; + pool = this.player.resources; for( resource in pool ) { switch( resource.toString() ) { - case "Population": - case "Housing": + case "population": + case "housing": break; default: // Deduct resources to pay for healing. - getGUIGlobal().deductResources(toTitleCase (resource.toString()), parseInt(evt.target.actions.heal.cost * evt.target.traits.creation.cost[resource])); + this.player.resources[resource.toString()] -= parseInt(evt.target.actions.heal.cost * evt.target.traits.creation.cost[resource]); break; } } @@ -604,7 +656,7 @@ function performBuild( evt ) t.attachAuras(); if (t.traits.population && t.traits.population.add) - getGUIGlobal().giveResources ("Housing", t.traits.population.add); + this.player.resources.housing += parseInt(t.traits.population.add); } evt.preventDefault(); // Stop performing this action } @@ -637,7 +689,7 @@ function performRepair( evt ) for( r in resources ) { amount = parseInt( fraction * parseInt(resources[r]) ); - getGUIGlobal().deductResources( r.toString(), amount ); + this.player.resources[r.toString()] -= parseInt(amount); } // Heal the building @@ -648,7 +700,7 @@ function performRepair( evt ) for( r in resources ) { amount = parseInt( fraction * parseInt(resources[r]) ); - getGUIGlobal().deductResources( r.toString(), amount ); + this.player.resources[r.toString()] -= parseInt(amount); } } @@ -728,7 +780,7 @@ function damage( dmg, inflictor ) // Notify player. console.write ("Spoils of war! " + this.traits.loot[loot] + " " + loot.toString() + "!"); // Give the inflictor his resources. - getGUIGlobal().giveResources( loot.toString(), parseInt(this.traits.loot[loot]) ); + this.player.resources[loot.toString()] -= parseInt(this.traits.loot[loot]); } break; } @@ -1124,7 +1176,7 @@ function entityStartProduction( evt ) if(evt.productionType == PRODUCTION_TRAIN) { var template = getEntityTemplate( evt.name, this.player ); - var result = entityCheckQueueReq( this, template ); + var result = checkEntityReqs( this.player, template ); if (result == true) // If the entry meets requirements to be added to the queue (eg sufficient resources) { @@ -1132,14 +1184,14 @@ function entityStartProduction( evt ) var pool = template.traits.creation.resource; for ( resource in pool ) { - switch ( getGUIGlobal().toTitleCase(resource.toString()) ) + switch ( resource.toString() ) { - case "Population": - case "Housing": + case "population": + case "housing": break; default: // Deduct the given quantity of resources. - getGUIGlobal().deductResources (resource.toString(), parseInt(pool[resource])); + this.player.resources[resource.toString()] -= parseInt(pool[resource]); console.write ("Spent " + pool[resource] + " " + resource + " to purchase " + template.traits.id.generic); @@ -1174,14 +1226,14 @@ function entityCancelProduction( evt ) var pool = template.traits.creation.resource; for ( resource in pool ) { - switch ( getGUIGlobal().toTitleCase(resource.toString()) ) + switch ( resource.toString() ) { - case "Population": - case "Housing": + case "population": + case "housing": break; default: - // Deduct the given quantity of resources. - getGUIGlobal().addResources (resource.toString(), parseInt(pool[resource])); + // Refund the given quantity of resources. + this.player.resources[resource.toString()] += parseInt(pool[resource]); console.write ("Got back " + pool[resource] + " " + resource + " from cancelling " + template.traits.id.generic); @@ -1214,23 +1266,21 @@ function entityFinishProduction( evt ) } else { - var created = new Entity( template, position ); + var created = new Entity( template, position, 0, this.player ); // Above shouldn't ever fail, but just in case... if( created ) { console.write( "Created: ", template.tag ); - - // Entities start under Gaia control - make the controller - // the same as our controller - created.player = this.player; } } } } -// ==================================================================== +// Old training queue system +// ==================================================================== +/* function entityAddCreateQueue( template, tab, list ) { // Make sure we have a queue to put things in... @@ -1326,7 +1376,7 @@ function entityCreateComplete() function attemptAddToBuildQueue( entity, create_tag, tab, list ) { template = getEntityTemplate( create_tag, entity.player ); - result = entityCheckQueueReq( entity, template ); + result = checkEntityReqs( entity, template ); if (result == true) // If the entry meets requirements to be added to the queue (eg sufficient resources) { @@ -1334,14 +1384,14 @@ function attemptAddToBuildQueue( entity, create_tag, tab, list ) pool = template.traits.creation.resource; for ( resource in pool ) { - switch ( getGUIGlobal().toTitleCase(resource.toString()) ) + switch ( resource.toString() ) { - case "Population": - case "Housing": + case "population": + case "housing": break; default: // Deduct the given quantity of resources. - getGUIGlobal().deductResources (resource.toString(), parseInt(pool[resource])); + this.player.resources[resource.toString()] -= parseInt(pool[resource]); console.write ("Spent " + pool[resource] + " " + resource + " to purchase " + template.traits.id.generic); @@ -1361,10 +1411,10 @@ function attemptAddToBuildQueue( entity, create_tag, tab, list ) return false; } } - +*/ // ==================================================================== -function entityCheckQueueReq( entity, template ) +function checkEntityReqs( player, template ) { // Determines if the given entity meets requirements for production by the player, and returns an appropriate // error string. @@ -1374,14 +1424,14 @@ function entityCheckQueueReq( entity, template ) var resources = template.traits.creation.resource; for( resource in resources ) { - switch( getGUIGlobal().toTitleCase(resource.toString()) ) + switch( resource.toString() ) { - case "Population": - case "Housing": // Ignore housing. It's handled in combination with population. + case "population": + case "housing": // Ignore housing. It's handled in combination with population. break default: // If the item costs more of this resource type than we have, - var cur = getGUIGlobal().getResources(resource); + var cur = player.resources[resource]; var req = parseInt(resources[resource]); if (req > cur) { @@ -1399,7 +1449,7 @@ function entityCheckQueueReq( entity, template ) if(template.traits.population && template.traits.population.rem) { var req = parseInt(template.traits.population.rem); - var space = getGUIGlobal().getResources("Housing") - getGUIGlobal().getResources("Population"); + var space = player.resources.housing - player.resources.population; // If the item costs more of this resource type than we have, if (req > space) diff --git a/binaries/data/mods/official/scripts/game_startup.js b/binaries/data/mods/official/scripts/game_startup.js index e2d752afbc..e1ab20f961 100644 --- a/binaries/data/mods/official/scripts/game_startup.js +++ b/binaries/data/mods/official/scripts/game_startup.js @@ -3,4 +3,42 @@ NOTES : */ +// Assign the players resources + +for(var i=0; iInsertMessage( L"Warning: Invalid tech template name \"%ls\" passed for getTechnology()", name.c_str() ); - if ( rval ) - return JS_TRUE; - return JS_FALSE; + return JS_TRUE; } //----------------------------------------------------------------------------- diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index e3be35f220..dd4295052b 100644 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -164,8 +164,6 @@ CEntity::CEntity( CBaseEntity* base, CVector3D position, float orientation, cons m_building = building; m_player = g_Game->GetPlayer( 0 ); - - Initialize(); } CEntity::~CEntity() @@ -771,10 +769,15 @@ void UpdateAuras_Normal( SAura& aura, CEntity* e ) #endif -void CEntity::Initialize() +bool CEntity::Initialize() { CEventInitialize evt; - DispatchEvent( &evt ); + if( !DispatchEvent( &evt ) ) + { + kill(); + return false; + } + return true; } void CEntity::Tick() @@ -1468,6 +1471,8 @@ JSBool CEntity::Construct( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsva JSObject* jsBaseEntity = JSVAL_TO_OBJECT( argv[0] ); CStrW templateName; + CPlayer* player = g_Game->GetPlayer( 0 ); + CBaseEntity* baseEntity = NULL; if( JSVAL_IS_OBJECT( argv[0] ) ) // only set baseEntity if jsBaseEntity is a valid object baseEntity = ToNative( cx, jsBaseEntity ); @@ -1516,8 +1521,22 @@ JSBool CEntity::Construct( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsva } } + if( argc >= 4 ) + { + try + { + player = ToPrimitive( argv[3] ); + } + catch( PSERROR_Scripting_ConversionFailed ) + { + player = g_Game->GetPlayer( 0 ); + } + } + std::set selections; // TODO: let scripts specify selections? HEntity handle = g_EntityManager.create( baseEntity, position, orientation, selections ); + handle->SetPlayer( player ); + handle->Initialize(); *rval = ToJSVal( *handle ); return( JS_TRUE ); diff --git a/source/simulation/Entity.h b/source/simulation/Entity.h index 9173f30c1d..1ed24eb28b 100644 --- a/source/simulation/Entity.h +++ b/source/simulation/Entity.h @@ -267,7 +267,7 @@ public: void kill(); // Process initialization - void Initialize(); + bool Initialize(); // Process tick. void Tick(); diff --git a/source/simulation/EntityManager.cpp b/source/simulation/EntityManager.cpp index 2b319458c6..4d449d1b61 100644 --- a/source/simulation/EntityManager.cpp +++ b/source/simulation/EntityManager.cpp @@ -93,6 +93,18 @@ HEntity CEntityManager::create( CBaseEntity* base, CVector3D position, float ori return( HEntity( m_nextalloc++ ) ); } +HEntity CEntityManager::create( CStrW templateName, CPlayer* player, CVector3D position, float orientation ) +{ + CBaseEntity* base = g_EntityTemplateCollection.getTemplate( templateName, player ); + debug_assert( base ); + if( !base ) + return HEntity(); + + std::set selections; + + return create( base, position, orientation, selections ); +} + HEntity CEntityManager::createFoundation( CStrW templateName, CPlayer* player, CVector3D position, float orientation ) { CBaseEntity* base = g_EntityTemplateCollection.getTemplate( templateName, player ); diff --git a/source/simulation/EntityManager.h b/source/simulation/EntityManager.h index 92f75bcb1c..bdb9994bdf 100644 --- a/source/simulation/EntityManager.h +++ b/source/simulation/EntityManager.h @@ -53,9 +53,14 @@ public: CEntityManager(); ~CEntityManager(); - HEntity create( CBaseEntity* base, CVector3D position, float orientation, const std::set& actorSelections ); + HEntity create( CBaseEntity* base, CVector3D position, float orientation, + const std::set& actorSelections ); - HEntity createFoundation( CStrW templateName, CPlayer* player, CVector3D position, float orientation ); + HEntity create( CStrW templateName, CPlayer* player, CVector3D position, + float orientation ); + + HEntity createFoundation( CStrW templateName, CPlayer* player, CVector3D position, + float orientation ); HEntity* getByHandle( u16 index ); CHandle *getHandle( int index ); diff --git a/source/simulation/EntityStateProcessing.cpp b/source/simulation/EntityStateProcessing.cpp index ecf56a05dc..0062b89155 100644 --- a/source/simulation/EntityStateProcessing.cpp +++ b/source/simulation/EntityStateProcessing.cpp @@ -654,11 +654,9 @@ bool CEntity::processProduce( CEntityOrder* order ) { int type = order->m_data[1].data; CStrW name = order->m_data[0].string; - debug_printf("Trying to produce %d %ls\n", type, name.c_str() ); CEventStartProduction evt( type, name ); if( DispatchEvent( &evt ) && evt.GetTime() >= 0 ) { - debug_printf("Production started, time is %f\n", evt.GetTime()); m_productionQueue->AddItem( type, name, evt.GetTime() ); } return( false ); diff --git a/source/simulation/ProductionQueue.cpp b/source/simulation/ProductionQueue.cpp index 1d47c9f78e..227c9ad907 100644 --- a/source/simulation/ProductionQueue.cpp +++ b/source/simulation/ProductionQueue.cpp @@ -61,6 +61,12 @@ CProductionQueue::~CProductionQueue() { for( size_t i=0; i < m_items.size(); ++i ) { + // Cancel production of the item + CProductionItem* item = m_items[i]; + CEventCancelProduction evt( item->m_type, item->m_name ); + m_owner->DispatchEvent( &evt ); + + // Free its memory delete m_items[i]; } } @@ -78,7 +84,6 @@ void CProductionQueue::Update( size_t timestep ) front->Update( timestep ); if( front->IsComplete() ) { - debug_printf("Production complete!\n"); CEventFinishProduction evt( front->m_type, front->m_name ); m_owner->DispatchEvent( &evt ); m_items.erase( m_items.begin() ); diff --git a/source/simulation/Simulation.cpp b/source/simulation/Simulation.cpp index 30ebfbfc58..68d66e38e2 100644 --- a/source/simulation/Simulation.cpp +++ b/source/simulation/Simulation.cpp @@ -49,6 +49,10 @@ int CSimulation::Initialize(CGameAttributes* pAttribs) { m_pTurnManager->Initialize(m_pGame->GetNumPlayers()); + // Call the game startup script + // TODO: Maybe don't do this if we're in Atlas + g_ScriptingHost.RunScript( "scripts/game_startup.js" ); + g_EntityManager.InitializeAll(); // 2006-03-04: ~33ms @@ -348,11 +352,13 @@ uint CSimulation::TranslateMessage(CNetMessage* pMsg, uint clientMask, void* UNU CVector3D pos(msg->m_X/1000.0f, msg->m_Y/1000.0f, msg->m_Z/1000.0f); HEntity newObj = g_EntityManager.createFoundation( msg->m_Template, player, pos, msg->m_Angle/1000.0f ); newObj->SetPlayer(player); - - // Order all the selected units to work on the new object using the given action - order.m_type = CEntityOrder::ORDER_START_CONSTRUCTION; - order.m_data[0].entity = newObj; - QueueOrder(order, msg->m_Entities, false); + if( newObj->Initialize() ) + { + // Order all the selected units to work on the new object using the given action + order.m_type = CEntityOrder::ORDER_START_CONSTRUCTION; + order.m_data[0].entity = newObj; + QueueOrder(order, msg->m_Entities, false); + } } while(0) break;