| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // FILE: OCLUpdate.cpp /////////////////////////////////////////////////////////////////////////
- // Author: Graham Smallwood, August2002
- // Desc: Update Spits out an OCL on a timer
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
- #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
- #include "Common/RandomValue.h"
- #include "Common/Xfer.h"
- #include "Common/Player.h"
- #include "Common/PlayerTemplate.h"
- #include "Common/UnicodeString.h"
- #include "GameLogic/GameLogic.h"
- #include "GameLogic/Object.h"
- #include "GameLogic/ObjectCreationList.h"
- #include "GameLogic/PartitionManager.h"
- #include "GameLogic/Module/OCLUpdate.h"
- #include "GameLogic/TerrainLogic.h"
- //-------------------------------------------------------------------------------------------------
- void parseFactionObjectCreationList( INI *ini, void *instance, void *store, const void *userData )
- {
- OCLUpdateModuleData::FactionOCLInfo info;
- info.m_factionName = "";
- info.m_ocl = 0;
- const char *token = ini->getNextToken( ini->getSepsColon() );
- if ( stricmp(token, "Faction") == 0 )
- {
- token = ini->getNextTokenOrNull( ini->getSepsColon() );
- if (!token) throw INI_INVALID_DATA;
- info.m_factionName = token;
- }
- else
- throw INI_INVALID_DATA;
- token = ini->getNextTokenOrNull( ini->getSepsColon() );
- if ( stricmp(token, "OCL") == 0 )
- ini->parseObjectCreationList( ini, instance, &info.m_ocl, NULL );
- else
- throw INI_INVALID_DATA;
- // Insert the info into the ocl hashmap
- OCLUpdateModuleData::FactionOCLList * theList = (OCLUpdateModuleData::FactionOCLList*)store;
- theList->push_back(info);
-
- } // end parseFactionObjectCreationList
- //-------------------------------------------------------------------------------------------------
- OCLUpdateModuleData::OCLUpdateModuleData()
- {
- m_minDelay = 0;
- m_maxDelay = 0;
- m_ocl = NULL;
- m_factionOCL.clear();
- m_isCreateAtEdge = FALSE;
- m_isFactionTriggered = FALSE;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ void OCLUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
- {
- UpdateModuleData::buildFieldParse(p);
- static const FieldParse dataFieldParse[] =
- {
- { "OCL", INI::parseObjectCreationList, NULL, offsetof( OCLUpdateModuleData, m_ocl ) },
- { "FactionOCL", parseFactionObjectCreationList, NULL, offsetof( OCLUpdateModuleData, m_factionOCL ) },
- { "MinDelay", INI::parseDurationUnsignedInt, NULL, offsetof( OCLUpdateModuleData, m_minDelay ) },
- { "MaxDelay", INI::parseDurationUnsignedInt, NULL, offsetof( OCLUpdateModuleData, m_maxDelay ) },
- { "CreateAtEdge", INI::parseBool, NULL, offsetof( OCLUpdateModuleData, m_isCreateAtEdge ) },
- { "FactionTriggered", INI::parseBool, NULL, offsetof( OCLUpdateModuleData, m_isFactionTriggered ) },
- { 0, 0, 0, 0 }
- };
- p.add(dataFieldParse);
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- OCLUpdate::OCLUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
- {
- m_nextCreationFrame = 0;
- m_timerStartedFrame = 0;
- m_isFactionNeutral = TRUE;
- m_currentPlayerColor = 0;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- OCLUpdate::~OCLUpdate( void )
- {
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- UpdateSleepTime OCLUpdate::update( void )
- {
- if( getObject()->isDisabled() )
- {
- m_nextCreationFrame++;
- return UPDATE_SLEEP_NONE;
- }
- const OCLUpdateModuleData *data = getOCLUpdateModuleData();
- // Test if the OCL update is faction dependant. If so, check for faction changes
- if (data->m_isFactionTriggered)
- {
- Player *player = getObject()->getControllingPlayer();
- // Test for when a player captures the building
- if (m_isFactionNeutral)
- {
- if( player && player->isPlayableSide() )
- {
- m_currentPlayerColor = player->getPlayerColor();
- m_isFactionNeutral = FALSE;
- setNextCreationFrame();
- }
- }
- // Test for when the building has been made neutral or when it changes faction
- else
- {
- // If this is no longer under player control, then we set the faction to neutral
- if( !player || !player->isPlayableSide() )
- {
- m_isFactionNeutral = TRUE;
- }
- // If another player has taken control, reset the timer
- else if( player && player->getPlayerColor() != m_currentPlayerColor)
- {
- m_currentPlayerColor = player->getPlayerColor();
- setNextCreationFrame();
- }
- }
- // If the building is neutal, skip futher update
- if (m_isFactionNeutral)
- return UPDATE_SLEEP_NONE;
- }
- /// @todo srj use SLEEPY_UPDATE here
- if( shouldCreate() )
- {
- if( m_nextCreationFrame == 0 )
- {
- // You don't get to actually spread the first try, you start on a timer, then go
- setNextCreationFrame();
- return UPDATE_SLEEP_NONE;
- }
- setNextCreationFrame();
- Coord3D creationCoord;
- if( getOCLUpdateModuleData()->m_isCreateAtEdge )
- creationCoord = TheTerrainLogic->findClosestEdgePoint( getObject()->getPosition() );
- else
- creationCoord = *getObject()->getPosition();
- // If this is faction triggered, search through the faction specific OCLs to find the match
- if (data->m_isFactionTriggered)
- {
- std::string playerFactionName("");
- Player *player = getObject()->getControllingPlayer();
- if (!player) return UPDATE_SLEEP_NONE;
-
- const PlayerTemplate *playerT = player->getPlayerTemplate();
- if (!playerT) return UPDATE_SLEEP_NONE;
- // Get and store the faction side to compare with the faction ocl list
- if (playerT->getSide().str()) playerFactionName = playerT->getSide().str();
- // Loop through the list of faction ocls to find the matching faction that triggeres the specific ocls
- for (OCLUpdateModuleData::FactionOCLList::const_iterator it = data->m_factionOCL.begin(); it != data->m_factionOCL.end(); ++it)
- {
- OCLUpdateModuleData::FactionOCLInfo info = *it;
- if (playerFactionName == info.m_factionName)
- {
- ObjectCreationList::create( info.m_ocl, getObject(), &creationCoord, getObject()->getPosition(), getObject()->getOrientation() );
- break;
- }
- }
- }
- // Use the non faction OCL information
- else
- {
- ObjectCreationList::create( data->m_ocl, getObject(), &creationCoord, getObject()->getPosition(), getObject()->getOrientation() );
- }
- }
- return UPDATE_SLEEP_NONE;
- }
- //-------------------------------------------------------------------------------------------------
- void OCLUpdate::resetTimer()
- {
- setNextCreationFrame();
- }
- // ------------------------------------------------------------------------------------------------
- // ------------------------------------------------------------------------------------------------
- Bool OCLUpdate::shouldCreate()
- {
- if( TheGameLogic->getFrame() < m_nextCreationFrame )
- return FALSE;//too soon
- if( getObject()->getStatusBits().test( OBJECT_STATUS_UNDER_CONSTRUCTION ) )
- return FALSE;// not built yet
- return TRUE;
- }
- // ------------------------------------------------------------------------------------------------
- // ------------------------------------------------------------------------------------------------
- void OCLUpdate::setNextCreationFrame()
- {
- UnsignedInt delay = GameLogicRandomValue( getOCLUpdateModuleData()->m_minDelay,
- getOCLUpdateModuleData()->m_maxDelay );
- m_timerStartedFrame = TheGameLogic->getFrame();
- m_nextCreationFrame = m_timerStartedFrame + delay;
- }
- // ------------------------------------------------------------------------------------------------
- // ------------------------------------------------------------------------------------------------
- Real OCLUpdate::getCountdownPercent() const
- {
- UnsignedInt now = TheGameLogic->getFrame();
- return 1.0f - (( m_nextCreationFrame - now ) / (float)( m_nextCreationFrame - m_timerStartedFrame ));
- }
- // ------------------------------------------------------------------------------------------------
- // ------------------------------------------------------------------------------------------------
- UnsignedInt OCLUpdate::getRemainingFrames() const
- {
- UnsignedInt now = TheGameLogic->getFrame();
- return ( m_nextCreationFrame - now );
- }
- // ------------------------------------------------------------------------------------------------
- /** CRC */
- // ------------------------------------------------------------------------------------------------
- void OCLUpdate::crc( Xfer *xfer )
- {
- // extend base class
- UpdateModule::crc( xfer );
- } // end crc
- // ------------------------------------------------------------------------------------------------
- /** Xfer method
- * Version Info:
- * 1: Initial version */
- // ------------------------------------------------------------------------------------------------
- void OCLUpdate::xfer( Xfer *xfer )
- {
- // version
- XferVersion currentVersion = 1;
- XferVersion version = currentVersion;
- xfer->xferVersion( &version, currentVersion );
- // extend base class
- UpdateModule::xfer( xfer );
- // next creation frame
- xfer->xferUnsignedInt( &m_nextCreationFrame );
- // timer stated frame
- xfer->xferUnsignedInt( &m_timerStartedFrame );
- // faction status
- xfer->xferBool( &m_isFactionNeutral );
-
- // current owning player color
- xfer->xferInt( &m_currentPlayerColor );
- } // end xfer
- // ------------------------------------------------------------------------------------------------
- /** Load post process */
- // ------------------------------------------------------------------------------------------------
- void OCLUpdate::loadPostProcess( void )
- {
- // extend base class
- UpdateModule::loadPostProcess();
- } // end loadPostProcess
|