From e4ed1d52b90bf7507e8e9e31e246a2acf6a977a9 Mon Sep 17 00:00:00 2001 From: janwas Date: Wed, 12 Jul 2006 14:36:59 +0000 Subject: [PATCH] # CSimulation cleanup and optimization (1) moved some fields (e.g. size of health bar) out of CEntity and into BaseEntity so they can be shared between entities (instead of duplicated in each). TODO: is it ok to remove AddProperty for those properties in CEntity? (BaseEntity is still adding them) This was SVN commit r4078. --- source/gui/MiniMap.cpp | 4 +- source/main.cpp | 2 +- source/simulation/BaseEntity.cpp | 3 + source/simulation/BaseEntity.h | 1 + source/simulation/Entity.cpp | 141 ++++++++++---------- source/simulation/Entity.h | 70 ++++++---- source/simulation/EntityManager.cpp | 4 +- source/simulation/EntityStateProcessing.cpp | 5 +- 8 files changed, 126 insertions(+), 104 deletions(-) diff --git a/source/gui/MiniMap.cpp b/source/gui/MiniMap.cpp index 9b10a782f3..463edb33e5 100644 --- a/source/gui/MiniMap.cpp +++ b/source/gui/MiniMap.cpp @@ -367,7 +367,7 @@ void CMiniMap::Draw() if(unit && unit->GetEntity() && losMgr->GetUnitStatus(unit, g_Game->GetLocalPlayer()) != UNIT_HIDDEN) { CEntity* entity = unit->GetEntity(); - CStrW& type = entity->m_minimapType; + CStrW& type = entity->m_base->m_minimapType; if(type==L"Unit" || type==L"Structure" || type==L"Hero") { // Use the player colour @@ -375,7 +375,7 @@ void CMiniMap::Draw() glColor3f(colour.r, colour.g, colour.b); } else { - glColor3f(entity->m_minimapR/255.0f, entity->m_minimapG/255.0f, entity->m_minimapB/255.0f); + glColor3f(entity->m_base->m_minimapR/255.0f, entity->m_base->m_minimapG/255.0f, entity->m_base->m_minimapB/255.0f); } diff --git a/source/main.cpp b/source/main.cpp index 031f5deebb..ea5d5197c2 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -367,7 +367,7 @@ int main(int argc, char* argv[]) Init(argc, argv, 0); MainControllerInit(); -//debug_filter_add("LOADER"); +debug_filter_add("LOADER"); //trace_gen_random(5000); //trace_write_to_file("../logs/trace.txt"); diff --git a/source/simulation/BaseEntity.cpp b/source/simulation/BaseEntity.cpp index 84a6c8e46d..3fce2a3e90 100644 --- a/source/simulation/BaseEntity.cpp +++ b/source/simulation/BaseEntity.cpp @@ -84,6 +84,9 @@ CBaseEntity::CBaseEntity( CPlayer* player ) m_socket = CStrW(); m_passThroughAllies = false; m_sectorDivs = 4; + m_sectorValues.resize(m_sectorDivs); + for ( int i=0; i m_sectorValues; int m_sectorDivs; int m_pitchDivs; float m_anchorConformX; diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index eddc28dba1..99dd86e77b 100644 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -68,38 +68,38 @@ CEntity::CEntity( CBaseEntity* base, CVector3D position, float orientation, cons AddProperty( L"player", &m_player, false, (NotifyFn)&CEntity::playerChanged ); AddProperty( L"traits.health.curr", &m_healthCurr ); AddProperty( L"traits.health.max", &m_healthMax ); - AddProperty( L"traits.health.bar_height", &m_healthBarHeight ); - AddProperty( L"traits.health.bar_size", &m_healthBarSize ); - AddProperty( L"traits.health.bar_width", &m_healthBarWidth ); - AddProperty( L"traits.health.border_height", &m_healthBorderHeight); - AddProperty( L"traits.health.border_width", &m_healthBorderWidth ); - AddProperty( L"traits.health.border_name", &m_healthBorderName ); + //AddProperty( L"traits.health.bar_height", &m_base->m_healthBarHeight ); + //AddProperty( L"traits.health.bar_size", &m_base->m_healthBarSize ); + //AddProperty( L"traits.health.bar_width", &m_base->m_healthBarWidth ); + //AddProperty( L"traits.health.border_height", &m_base->m_healthBorderHeight); + //AddProperty( L"traits.health.border_width", &m_base->m_healthBorderWidth ); + //AddProperty( L"traits.health.border_name", &m_base->m_healthBorderName ); AddProperty( L"traits.health.regen_rate", &m_healthRegenRate ); AddProperty( L"traits.health.regen_start", &m_healthRegenStart ); AddProperty( L"traits.health.decay_rate", &m_healthDecayRate ); AddProperty( L"traits.stamina.curr", &m_staminaCurr ); AddProperty( L"traits.stamina.max", &m_staminaMax ); - AddProperty( L"traits.stamina.bar_height", &m_staminaBarHeight ); - AddProperty( L"traits.stamina.bar_size", &m_staminaBarSize ); - AddProperty( L"traits.stamina.bar_width", &m_staminaBarWidth ); - AddProperty( L"traits.stamina.border_height", &m_staminaBorderHeight); - AddProperty( L"traits.stamina.border_width", &m_staminaBorderWidth ); - AddProperty( L"traits.stamina.border_name", &m_staminaBorderName ); + //AddProperty( L"traits.stamina.bar_height", &m_base->m_staminaBarHeight ); + //AddProperty( L"traits.stamina.bar_size", &m_base->m_staminaBarSize ); + //AddProperty( L"traits.stamina.bar_width", &m_base->m_staminaBarWidth ); + //AddProperty( L"traits.stamina.border_height", &m_base->m_staminaBorderHeight); + //AddProperty( L"traits.stamina.border_width", &m_base->m_staminaBorderWidth ); + //AddProperty( L"traits.stamina.border_name", &m_base->m_staminaBorderName ); AddProperty( L"traits.rally.name", &m_rallyTexture ); AddProperty( L"traits.rally.width", &m_rallyWidth ); AddProperty( L"traits.rally.height", &m_rallyHeight ); - AddProperty( L"traits.flank_penalty.sectors", &m_sectorDivs); - AddProperty( L"traits.pitch.sectors", &m_pitchDivs ); - AddProperty( L"traits.rank.width", &m_rankWidth ); - AddProperty( L"traits.rank.height", &m_rankHeight ); + //AddProperty( L"traits.flank_penalty.sectors", &m_base->m_sectorDivs); + //AddProperty( L"traits.pitch.sectors", &m_base->m_pitchDivs ); + //AddProperty( L"traits.rank.width", &m_base->m_rankWidth ); + //AddProperty( L"traits.rank.height", &m_base->m_rankHeight ); AddProperty( L"traits.rank.name", &m_rankName ); - AddProperty( L"traits.minimap.type", &m_minimapType ); - AddProperty( L"traits.minimap.red", &m_minimapR ); - AddProperty( L"traits.minimap.green", &m_minimapG ); - AddProperty( L"traits.minimap.blue", &m_minimapB ); - AddProperty( L"traits.anchor.type", &m_anchorType ); - AddProperty( L"traits.anchor.conformx", &m_anchorConformX ); - AddProperty( L"traits.anchor.conformz", &m_anchorConformZ ); + //AddProperty( L"traits.minimap.type", &m_base->m_minimapType ); + //AddProperty( L"traits.minimap.red", &m_base->m_minimapR ); + //AddProperty( L"traits.minimap.green", &m_base->m_minimapG ); + //AddProperty( L"traits.minimap.blue", &m_base->m_minimapB ); + //AddProperty( L"traits.anchor.type", &m_base->m_anchorType ); + //AddProperty( L"traits.anchor.conformx", &m_base->m_anchorConformX ); + //AddProperty( L"traits.anchor.conformz", &m_base->m_anchorConformZ ); AddProperty( L"traits.vision.los", &m_los ); AddProperty( L"traits.vision.permanent", &m_permanent ); AddProperty( L"traits.is_territory_centre", &m_isTerritoryCentre ); @@ -132,10 +132,6 @@ CEntity::CEntity( CBaseEntity* base, CVector3D position, float orientation, cons m_actorSelections = actorSelections; loadBase(); - m_sectorValues.resize(m_sectorDivs); - for ( int i=0; isetPosition( m_position.X, m_position.Z ); @@ -262,7 +258,12 @@ void CEntity::initAttributes(const CEntity* _this) #define getoffset_action(member, mem2) \ (size_t) ((unsigned char*)&_this->member.mem2 - (unsigned char*)_this) - + +// JW: temporarily disabled (-> techs are dead) until we replace getoffset with CJSComplex. +// it is no longer safe to use this because some fields are no longer present inside CEntity. +// they must be accessed via m_base instead. +#if 0 + //Add the attribute name and the variable that holds it CEntity::m_AttributeTable["actions.move.speed_curr"] = getoffset(m_speed); CEntity::m_AttributeTable["actions.move.run.speed"] = getoffset(m_runSpeed); @@ -307,6 +308,8 @@ void CEntity::initAttributes(const CEntity* _this) CEntity::m_AttributeTable["last_combat_time"] = getoffset(m_lastCombatTime); CEntity::m_AttributeTable["last_run_time"] = getoffset(m_lastRunTime); CEntity::m_AttributeTable["building"] = getoffset(m_building); +#endif + #undef getoffset } void CEntity::kill() @@ -1097,7 +1100,7 @@ float CEntity::getAnchorLevel( float x, float z ) { CTerrain *pTerrain = g_Game->GetWorld()->GetTerrain(); float groundLevel = pTerrain->getExactGroundLevel( x, z ); - if( m_anchorType==L"Ground" ) + if( m_base->m_anchorType==L"Ground" ) { return groundLevel; } @@ -1256,17 +1259,17 @@ void CEntity::renderBarBorders() if( !m_visible ) return; - if ( m_staminaBarHeight >= 0 && - g_Selection.m_unitUITextures.find(m_healthBorderName) != g_Selection.m_unitUITextures.end() ) + if ( m_base->m_staminaBarHeight >= 0 && + g_Selection.m_unitUITextures.find(m_base->m_healthBorderName) != g_Selection.m_unitUITextures.end() ) { - ogl_tex_bind( g_Selection.m_unitUITextures[m_healthBorderName] ); - CVector2D pos = getScreenCoords( m_healthBarHeight ); + ogl_tex_bind( g_Selection.m_unitUITextures[m_base->m_healthBorderName] ); + CVector2D pos = getScreenCoords( m_base->m_healthBarHeight ); - float left = pos.x - m_healthBorderWidth/2; - float right = pos.x + m_healthBorderWidth/2; + float left = pos.x - m_base->m_healthBorderWidth/2; + float right = pos.x + m_base->m_healthBorderWidth/2; pos.y = g_yres - pos.y; - float bottom = pos.y + m_healthBorderHeight/2; - float top = pos.y - m_healthBorderHeight/2; + float bottom = pos.y + m_base->m_healthBorderHeight/2; + float top = pos.y - m_base->m_healthBorderHeight/2; glBegin(GL_QUADS); @@ -1277,17 +1280,17 @@ void CEntity::renderBarBorders() glEnd(); } - if ( m_staminaBarHeight >= 0 && - g_Selection.m_unitUITextures.find(m_staminaBorderName) != g_Selection.m_unitUITextures.end() ) + if ( m_base->m_staminaBarHeight >= 0 && + g_Selection.m_unitUITextures.find(m_base->m_staminaBorderName) != g_Selection.m_unitUITextures.end() ) { - ogl_tex_bind( g_Selection.m_unitUITextures[m_staminaBorderName] ); + ogl_tex_bind( g_Selection.m_unitUITextures[m_base->m_staminaBorderName] ); - CVector2D pos = getScreenCoords( m_staminaBarHeight ); - float left = pos.x - m_staminaBorderWidth/2; - float right = pos.x + m_staminaBorderWidth/2; + CVector2D pos = getScreenCoords( m_base->m_staminaBarHeight ); + float left = pos.x - m_base->m_staminaBorderWidth/2; + float right = pos.x + m_base->m_staminaBorderWidth/2; pos.y = g_yres - pos.y; - float bottom = pos.y + m_staminaBorderHeight/2; - float top = pos.y - m_staminaBorderHeight/2; + float bottom = pos.y + m_base->m_staminaBorderHeight/2; + float top = pos.y - m_base->m_staminaBorderHeight/2; glBegin(GL_QUADS); @@ -1303,30 +1306,30 @@ void CEntity::renderHealthBar() { if( !m_bounds || !m_visible ) return; - if( m_healthBarHeight < 0 ) + if( m_base->m_healthBarHeight < 0 ) return; // negative bar height means don't display health bar float fraction; if(m_healthMax == 0) fraction = 1.0f; else fraction = clamp(m_healthCurr / m_healthMax, 0.0f, 1.0f); - CVector2D pos = getScreenCoords( m_healthBarHeight ); - float x1 = pos.x - m_healthBarSize/2; - float x2 = pos.x + m_healthBarSize/2; + CVector2D pos = getScreenCoords( m_base->m_healthBarHeight ); + float x1 = pos.x - m_base->m_healthBarSize/2; + float x2 = pos.x + m_base->m_healthBarSize/2; float y = g_yres - pos.y; - glLineWidth( m_healthBarWidth ); + glLineWidth( m_base->m_healthBarWidth ); glBegin(GL_LINES); // green part of bar glColor3f( 0, 1, 0 ); glVertex3f( x1, y, 0 ); glColor3f( 0, 1, 0 ); - glVertex3f( x1 + m_healthBarSize*fraction, y, 0 ); + glVertex3f( x1 + m_base->m_healthBarSize*fraction, y, 0 ); // red part of bar glColor3f( 1, 0, 0 ); - glVertex3f( x1 + m_healthBarSize*fraction, y, 0 ); + glVertex3f( x1 + m_base->m_healthBarSize*fraction, y, 0 ); glColor3f( 1, 0, 0 ); glVertex3f( x2, y, 0 ); @@ -1340,30 +1343,30 @@ void CEntity::renderStaminaBar() { if( !m_bounds || !m_visible ) return; - if( m_staminaBarHeight < 0 ) + if( m_base->m_staminaBarHeight < 0 ) return; // negative bar height means don't display stamina bar float fraction; if(m_staminaMax == 0) fraction = 1.0f; else fraction = clamp(m_staminaCurr / m_staminaMax, 0.0f, 1.0f); - CVector2D pos = getScreenCoords( m_staminaBarHeight ); - float x1 = pos.x - m_staminaBarSize/2; - float x2 = pos.x + m_staminaBarSize/2; + CVector2D pos = getScreenCoords( m_base->m_staminaBarHeight ); + float x1 = pos.x - m_base->m_staminaBarSize/2; + float x2 = pos.x + m_base->m_staminaBarSize/2; float y = g_yres - pos.y; - glLineWidth( m_staminaBarWidth ); + glLineWidth( m_base->m_staminaBarWidth ); glBegin(GL_LINES); // blue part of bar glColor3f( 0.1f, 0.1f, 1 ); glVertex3f( x1, y, 0 ); glColor3f( 0.1f, 0.1f, 1 ); - glVertex3f( x1 + m_staminaBarSize*fraction, y, 0 ); + glVertex3f( x1 + m_base->m_staminaBarSize*fraction, y, 0 ); // purple part of bar glColor3f( 0.3f, 0, 0.3f ); - glVertex3f( x1 + m_staminaBarSize*fraction, y, 0 ); + glVertex3f( x1 + m_base->m_staminaBarSize*fraction, y, 0 ); glColor3f( 0.3f, 0, 0.3f ); glVertex3f( x2, y, 0 ); @@ -1374,7 +1377,7 @@ void CEntity::renderRank() { if( !m_bounds || !m_visible ) return; - if( m_rankHeight < 0 ) + if( m_base->m_rankHeight < 0 ) return; // negative height means don't display rank //Check for valid texture if( g_Selection.m_unitUITextures.find( m_rankName ) == g_Selection.m_unitUITextures.end() ) @@ -1386,12 +1389,12 @@ void CEntity::renderRank() CVector3D above; above.X = m_position.X; above.Z = m_position.Z; - above.Y = getAnchorLevel(m_position.X, m_position.Z) + m_rankHeight; + above.Y = getAnchorLevel(m_position.X, m_position.Z) + m_base->m_rankHeight; g_Camera->GetScreenCoordinates(above, sx, sy); - int size = m_rankWidth/2; + int size = m_base->m_rankWidth/2; - float x1 = sx + m_healthBarSize/2; - float x2 = sx + m_healthBarSize/2 + 2*size; + float x1 = sx + m_base->m_healthBarSize/2; + float x2 = sx + m_base->m_healthBarSize/2 + 2*size; float y1 = g_yres - (sy - size); //top float y2 = g_yres - (sy + size); //bottom @@ -2176,8 +2179,8 @@ jsval CEntity::RegisterDamage( JSContext* cx, uintN argc, jsval* argv ) float angle = acosf( up.dot(posDelta) ); //Find what section it is between and "activate" it - int sector = findSector(m_sectorDivs, angle, DEGTORAD(360.0f))-1; - m_sectorValues[sector]=true; + int sector = findSector(m_base->m_sectorDivs, angle, DEGTORAD(360.0f))-1; + m_base->m_sectorValues[sector]=true; return JS_TRUE; } jsval CEntity::RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ) @@ -2195,15 +2198,15 @@ jsval CEntity::RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ) float angle = acosf( up.dot(posDelta) ); //Find what section it is between and "deactivate" it - int sector = MAX( 0.0, findSector(m_sectorDivs, angle, DEGTORAD(360.0f)) ); - m_sectorValues[sector]=false; + int sector = MAX( 0.0, findSector(m_base->m_sectorDivs, angle, DEGTORAD(360.0f)) ); + m_base->m_sectorValues[sector]=false; return JS_TRUE; } jsval CEntity::GetAttackDirections( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { int directions=0; - for ( std::vector::iterator it=m_sectorValues.begin(); it != m_sectorValues.end(); it++ ) + for ( std::vector::iterator it=m_base->m_sectorValues.begin(); it != m_base->m_sectorValues.end(); it++ ) { if ( *it ) ++directions; diff --git a/source/simulation/Entity.h b/source/simulation/Entity.h index e23e86ca48..83441a9856 100644 --- a/source/simulation/Entity.h +++ b/source/simulation/Entity.h @@ -37,8 +37,13 @@ #include "EntitySupport.h" #include "scripting/DOMEvent.h" +// JW: must be pulled in because CEntity no longer mirrors CBaseEntity exactly. +// some fields have been moved out of CEntity and are accessed via m_base, +// so CBaseEntity must be fully defined. +#include "BaseEntity.h" + class CAura; -class CBaseEntity; +//class CBaseEntity; // see comment above class CBoundingObject; class CPlayer; class CProductionQueue; @@ -122,28 +127,12 @@ public: //SP properties float m_staminaCurr; float m_staminaMax; - float m_staminaBarHeight; - int m_staminaBarSize; - float m_staminaBarWidth; - - int m_staminaBorderWidth; - int m_staminaBorderHeight; - CStr m_staminaBorderName; // HP properties float m_healthCurr; float m_healthMax; - float m_healthBarHeight; - int m_healthBarSize; - float m_healthBarWidth; - - int m_healthBorderWidth; - int m_healthBorderHeight; - CStr m_healthBorderName; //Rank properties - float m_rankHeight; - float m_rankWidth; CStr m_rankName; //Rally texture CStr m_rallyTexture; @@ -152,16 +141,6 @@ public: bool m_healthDecay; - // Minimap properties - CStrW m_minimapType; - int m_minimapR; - int m_minimapG; - int m_minimapB; - - // Y anchor - CStrW m_anchorType; - float m_anchorConformX; - float m_anchorConformZ; // LOS int m_los; @@ -232,11 +211,46 @@ public: int m_currentRequest; //Notification we our notifiers are sending bool m_destroyNotifiers; //True: we destroy them. False: the script does. + /* JW: these have all been 'moved' (1) into BaseEntity: + 1: were already present there, just removed from here std::vector m_sectorValues; int m_sectorDivs; - + int m_pitchDivs; + float m_staminaBarHeight; + int m_staminaBarSize; + float m_staminaBarWidth; + + float m_healthBarHeight; + int m_healthBarSize; + float m_healthBarWidth; + + int m_staminaBorderWidth; + int m_staminaBorderHeight; + CStr m_staminaBorderName; + + float m_rankHeight; + float m_rankWidth; + + // Minimap properties + CStrW m_minimapType; + int m_minimapR; + int m_minimapG; + int m_minimapB; + + // Y anchor + CStrW m_anchorType; + float m_anchorConformX; + float m_anchorConformZ; + + int m_healthBorderWidth; + int m_healthBorderHeight; + CStr m_healthBorderName; + */ + + + private: CEntity( CBaseEntity* base, CVector3D position, float orientation, const std::set& actorSelections, CStrW building = L"" ); diff --git a/source/simulation/EntityManager.cpp b/source/simulation/EntityManager.cpp index ff5a5d10ab..97063ec3c3 100644 --- a/source/simulation/EntityManager.cpp +++ b/source/simulation/EntityManager.cpp @@ -311,8 +311,8 @@ void CEntityManager::conformAll() while( targetXZ.y > PI ) targetXZ.y -= 2 * PI; while( targetXZ.y < -PI ) targetXZ.y += 2 * PI; - entity->m_orientation.X = clamp( targetXZ.x, -entity->m_anchorConformX, entity->m_anchorConformX ); - entity->m_orientation.Z = clamp( targetXZ.y, -entity->m_anchorConformZ, entity->m_anchorConformZ ); + entity->m_orientation.X = clamp( targetXZ.x, -entity->m_base->m_anchorConformX, entity->m_base->m_anchorConformX ); + entity->m_orientation.Z = clamp( targetXZ.y, -entity->m_base->m_anchorConformZ, entity->m_base->m_anchorConformZ ); entity->m_orientation_unclamped.x = targetXZ.x; entity->m_orientation_unclamped.y = targetXZ.y; entity->updateActorTransforms(); diff --git a/source/simulation/EntityStateProcessing.cpp b/source/simulation/EntityStateProcessing.cpp index 0062b89155..338bcdb5de 100644 --- a/source/simulation/EntityStateProcessing.cpp +++ b/source/simulation/EntityStateProcessing.cpp @@ -143,8 +143,9 @@ uint CEntity::processGotoHelper( CEntityOrder* current, size_t timestep_millis, while( targetXZ.y > PI ) targetXZ.y -= 2 * PI; while( targetXZ.y < -PI ) targetXZ.y += 2 * PI; - m_orientation.X = clamp( targetXZ.x, -m_anchorConformX, m_anchorConformX ); - m_orientation.Z = clamp( targetXZ.y, -m_anchorConformZ, m_anchorConformZ ); + float cX = m_base->m_anchorConformX, cZ = m_base->m_anchorConformZ; + m_orientation.X = clamp( targetXZ.x, -cX, cX ); + m_orientation.Z = clamp( targetXZ.y, -cZ, cZ ); m_orientation_unclamped.x = targetXZ.x; m_orientation_unclamped.y = targetXZ.y;