Skip to content

Commit

Permalink
Auto add entity coming from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
cybiosphere committed Oct 3, 2024
1 parent c038e4e commit b16e1fa
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 74 deletions.
2 changes: 1 addition & 1 deletion WpClanNetwork/inc/clan_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Client
void send_event_change_biotop_speed(const float newBiotopSpeed, const bool isManualMode, const bool isMaxSpeedMode);
void send_event_force_entity_action(const entityIdType entityId, const int actionIndex);
void send_event_create_measure(CMeasure* pMeasure);
void send_event_request_entity_refresh(CBasicEntity* pEntity);
void send_event_request_entity_refresh(entityIdType entityId, std::string entityLabel, bool needToAddEntity = false);
void send_event_add_entity_spawner(int index, BiotopRandomEntitiyGeneration_t& generator);
void send_event_create_specie_map(CGeoMapPopulation* pGeoMapSpecie);
void send_event_mark_entities_with_gene(CGene& modelGene, bool markDominantAlleleOnly);
Expand Down
20 changes: 17 additions & 3 deletions WpClanNetwork/inc/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ namespace clan
DataBuffer buffer[SENT_BUFFER_MAX_NB_BLOCKS];
} LongBufferEvent_t;

typedef struct
{
int entityId;
bool isEntityFound;
bool isValidCoord;
CBasicEntity* pEntity;
} UpdatedEntityInfo_t;

typedef struct
{
bool needToAddEntity;
CBasicEntity* pEntity;
} EntityRefreshInfo_t;

class event_manager
{
public:
Expand All @@ -32,7 +46,7 @@ namespace clan
void handleEvenUpdateEntityData(const NetGameEvent& e, CBiotop* pBiotop);

static NetGameEvent buildEventUpdateEntityPos(CBasicEntity* pEntity, std::string eventLabel);
static CBasicEntity* handleEventUpdateEntityPosition(const NetGameEvent& e, CBiotop* pBiotop, bool forceEntityUpdate, bool updatePhysic);
static UpdatedEntityInfo_t handleEventUpdateEntityPosition(const NetGameEvent& e, CBiotop* pBiotop, bool forceEntityUpdate, bool updatePhysic);

static NetGameEvent buildEventRemoveEntity(CBasicEntity* pEntity, entityIdType entityId);
static bool handleEventRemoveEntity(const NetGameEvent& e, CBiotop* pBiotop);
Expand All @@ -46,8 +60,8 @@ namespace clan
static bool buildEventsCreateMeasure(CMeasure* pMeasure, std::vector<NetGameEvent>& eventVector);
void handleEventCreateMeasure(const NetGameEvent& e, CBiotop* pBiotop);

static NetGameEvent buildEventReqEntityRefresh(CBasicEntity* pEntity);
static CBasicEntity* handleEventReqEntityRefresh(const NetGameEvent& e, CBiotop* pBiotop);
static NetGameEvent buildEventReqEntityRefresh(entityIdType entityId, std::string entityLabel, bool needToAddEntity);
static EntityRefreshInfo_t handleEventReqEntityRefresh(const NetGameEvent& e, CBiotop* pBiotop);

static bool buildEventsAddEntitySpawner(int index, BiotopRandomEntitiyGeneration_t& generator, std::vector<NetGameEvent>& eventVector);
void handleEventAddEntitySpawner(const NetGameEvent& e, CBiotop* pBiotop);
Expand Down
24 changes: 17 additions & 7 deletions WpClanNetwork/src/clan_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,22 @@ void Client::on_event_biotop_updatefullentity(const NetGameEvent &e)

void Client::on_event_biotop_updateentityposition(const NetGameEvent &e)
{
CBasicEntity* pEntity = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, false);
UpdatedEntityInfo_t updateInfo = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, false);
if ((!updateInfo.isEntityFound) && (updateInfo.isValidCoord))
{
log_event(labelEvent, "updateentityposition: pEntity is NULL for ID%1. Request entity update", updateInfo.entityId);
send_event_request_entity_refresh(updateInfo.entityId, "Unset", true);
}
}

void Client::on_event_biotop_updateentityphysic(const NetGameEvent& e)
{
CBasicEntity* pEntity = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, true);
UpdatedEntityInfo_t updateInfo = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, true);
if ((!updateInfo.isEntityFound) && (updateInfo.isValidCoord))
{
log_event(labelEvent, "updateentityphysic: pEntity is NULL for ID%1. Request entity update", updateInfo.entityId);
send_event_request_entity_refresh(updateInfo.entityId, "Unset", true);
}
}

void Client::on_event_biotop_removeentity(const NetGameEvent &e)
Expand Down Expand Up @@ -402,8 +412,8 @@ void Client::on_event_biotop_changespeed(const NetGameEvent& e)

void Client::on_event_biotop_requestentityrefresh(const NetGameEvent& e)
{
CBasicEntity* pEntity = event_manager::handleEventReqEntityRefresh(e, m_pBiotop);
send_event_update_entity_data(pEntity);
EntityRefreshInfo_t refreshInfo = event_manager::handleEventReqEntityRefresh(e, m_pBiotop);
send_event_update_entity_data(refreshInfo.pEntity);
}

void Client::displayBiotopEntities()
Expand Down Expand Up @@ -595,10 +605,10 @@ void Client::send_event_create_measure(CMeasure* pMeasure)
}
}

void Client::send_event_request_entity_refresh(CBasicEntity* pEntity)
void Client::send_event_request_entity_refresh(entityIdType entityId, std::string entityLabel, bool needToAddEntity)
{
log_event(labelClient, "Request entity refresh: label %1", pEntity->getLabel());
NetGameEvent bioReqActionRefresh{ event_manager::buildEventReqEntityRefresh(pEntity) };
log_event(labelClient, "Request entity refresh: label %1", entityLabel);
NetGameEvent bioReqActionRefresh{ event_manager::buildEventReqEntityRefresh(entityId, entityLabel, needToAddEntity)};
network_client.send_event(bioReqActionRefresh);
}

Expand Down
36 changes: 20 additions & 16 deletions WpClanNetwork/src/clan_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ void Server::on_event_login(const NetGameEvent &e, ServerUser *user)
}

// "Game-RequestStartGame" event was received
void Server::on_event_game_requeststart(const NetGameEvent &e, ServerUser *user)
void Server::on_event_game_requeststart(const NetGameEvent& e, ServerUser* user)
{
log_event(labelServer, "Client requested game start");
log_event(labelServer, "Client requested game start");
nb_users_connected++;

// Freeze biotop time during new player configuration
Expand All @@ -406,7 +406,7 @@ void Server::on_event_game_requeststart(const NetGameEvent &e, ServerUser *user)
std::string prevEntityLabel = "";
int prevEntityId = -1;
cloneEntitiesMap.clear();
for (i=0; i<m_pBiotop->getNbOfEntities(); i++)
for (i = 0; i < m_pBiotop->getNbOfEntities(); i++)
{
pCurEntity = m_pBiotop->getEntityByIndex(i);
if (pCurEntity->getLabel() != prevEntityLabel)
Expand Down Expand Up @@ -461,7 +461,7 @@ void Server::on_event_game_requeststart(const NetGameEvent &e, ServerUser *user)
}
}

user->send_event(NetGameEvent(labelEventStart, nextHourTimeOffsetForClient));
user->send_event(NetGameEvent(labelEventStart, nextHourTimeOffsetForClient));

if (m_pBiotop->getGeneToMark().getGeneType() != GENE_GENERIC)
{
Expand Down Expand Up @@ -490,24 +490,24 @@ void Server::on_event_biotop_updatefullentity(const NetGameEvent& e, ServerUser*

void Server::on_event_biotop_updateentityposition(const NetGameEvent& e, ServerUser* user)
{
CBasicEntity* pEntity = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, false);
if (pEntity && (m_tCoprocessors.size() > 0))
UpdatedEntityInfo_t updateInfo = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, false);
if (updateInfo.pEntity && (m_tCoprocessors.size() > 0))
{
for (auto coprocess : m_tCoprocessors)
{
coprocess.update_entity_control(pEntity);
coprocess.update_entity_control(updateInfo.pEntity);
}
}
}

void Server::on_event_biotop_updateentityphysic(const NetGameEvent& e, ServerUser* user)
{
CBasicEntity* pEntity = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, true);
if (pEntity && (m_tCoprocessors.size() > 0))
UpdatedEntityInfo_t updateInfo = event_manager::handleEventUpdateEntityPosition(e, m_pBiotop, m_bManualMode, true);
if (updateInfo.pEntity && (m_tCoprocessors.size() > 0))
{
for (auto coprocess : m_tCoprocessors)
{
coprocess.update_entity_control(pEntity);
coprocess.update_entity_control(updateInfo.pEntity);
}
}
}
Expand Down Expand Up @@ -548,15 +548,19 @@ void Server::on_event_biotop_addEntitySpawner(const NetGameEvent& e, ServerUser*

void Server::on_event_biotop_requestentityrefresh(const NetGameEvent& e, ServerUser* user)
{
CBasicEntity* pEntity = event_manager::handleEventReqEntityRefresh(e, m_pBiotop);
ServerUser* pOwnerUser = getCoprocessorOwnerUser(pEntity);
if (pOwnerUser != NULL)
EntityRefreshInfo_t refreshInfo = event_manager::handleEventReqEntityRefresh(e, m_pBiotop);
ServerUser* pOwnerUser = getCoprocessorOwnerUser(refreshInfo.pEntity);
if (refreshInfo.needToAddEntity)
{
send_event_add_entity(refreshInfo.pEntity, user);
}
else if (pOwnerUser != NULL)
{
send_event_request_entity_refresh(pEntity, pOwnerUser);
send_event_request_entity_refresh(refreshInfo.pEntity, pOwnerUser);
}
else
{
send_event_update_entity_data(pEntity, user);
send_event_update_entity_data(refreshInfo.pEntity, user);
}
}

Expand Down Expand Up @@ -863,7 +867,7 @@ void Server::send_event_new_second_end(ServerUser* user)
void Server::send_event_request_entity_refresh(CBasicEntity* pEntity, ServerUser* user)
{
log_event(labelServer, "Request entity refresh: label %1", pEntity->getLabel());
NetGameEvent bioReqActionRefresh{ event_manager::buildEventReqEntityRefresh(pEntity) };
NetGameEvent bioReqActionRefresh{ event_manager::buildEventReqEntityRefresh(pEntity->getId(), pEntity->getLabel(), false) };
if (user == NULL) // If user not define, broadcast info to all
network_server.send_event(bioReqActionRefresh);
else
Expand Down
90 changes: 49 additions & 41 deletions WpClanNetwork/src/event_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,34 @@ namespace clan
return (std::move(newEvent));
}

CBasicEntity* event_manager::handleEventUpdateEntityPosition(const NetGameEvent& e, CBiotop* pBiotop, bool forceEntityUpdate, bool updatePhysic)
UpdatedEntityInfo_t event_manager::handleEventUpdateEntityPosition(const NetGameEvent& e, CBiotop* pBiotop, bool forceEntityUpdate, bool updatePhysic)
{
UpdatedEntityInfo_t resultInfo{ 0, false, false, NULL };
if (e.get_argument_count() < 8)
{
log_event(labelEvent, "handleEventUpdateEntityPosition: ERROR bad number of arguments: %1", e.get_argument_count());
return NULL;
return resultInfo;
}
int entityId = e.get_argument(0);

CBasicEntity* pEntity = pBiotop->getEntityById(entityId);
if (pEntity == NULL)
{
log_event(labelEvent, "handleEventUpdateEntityPosition: ERROR pEntity is NULL for ID%1", entityId);
return NULL;
}

std::string entityLabel = e.get_argument(1);
int status = e.get_argument(2);
Point_t position;
int positionx = e.get_argument(3);
int positiony = e.get_argument(4);
position.x = positionx;
position.y = positiony;
resultInfo.entityId = entityId;
resultInfo.pEntity = pEntity;
resultInfo.isEntityFound = (pEntity != NULL);
resultInfo.isValidCoord = pBiotop->isCoordValid(pBiotop->getGridCoordFromGlobalGridCoord(pBiotop->getGridCoordFromStepCoord(position)));

if (pEntity == NULL)
{
return resultInfo;
}

int layer = e.get_argument(5);
int direction = e.get_argument(6);
int isImmortal = e.get_argument(7);
Expand All @@ -254,7 +259,7 @@ namespace clan
if ((int)pEntity->getStatus() > status)
{
log_event(labelEvent, "Biotop update entity position %1: Unexpected status transition from %2 to %3", pEntity->getLabel(), (int)pEntity->getStatus(), status);
return pEntity;
return resultInfo;
}
pEntity->setStatus((StatusType_e)status);
pEntity->jumpToGlobalStepCoord(position, true, layer, true);
Expand Down Expand Up @@ -291,7 +296,7 @@ namespace clan
pAnimal->setCurrentLifeStages((LifeStage_e)lifeStage);
}
}
return pEntity;
return resultInfo;
}

NetGameEvent event_manager::buildEventRemoveEntity(CBasicEntity* pEntity, entityIdType entityId)
Expand Down Expand Up @@ -387,20 +392,23 @@ namespace clan
}
}

NetGameEvent event_manager::buildEventReqEntityRefresh(CBasicEntity* pEntity)
NetGameEvent event_manager::buildEventReqEntityRefresh(entityIdType entityId, std::string entityLabel, bool needToAddEntity)
{
NetGameEvent newEvent(labelEventReqEntityRefresh);
newEvent.add_argument((int)pEntity->getId());
newEvent.add_argument(pEntity->getLabel());
newEvent.add_argument((int)entityId);
newEvent.add_argument(entityLabel);
newEvent.add_argument((int)needToAddEntity);
return (std::move(newEvent));
}

CBasicEntity* event_manager::handleEventReqEntityRefresh(const NetGameEvent& e, CBiotop* pBiotop)
EntityRefreshInfo_t event_manager::handleEventReqEntityRefresh(const NetGameEvent& e, CBiotop* pBiotop)
{
int entityId = e.get_argument(0);
string label = e.get_argument(1);
log_event(labelEvent, "Reqest entity refresh: entity Id=%1 label %2", entityId, label);
return (pBiotop->getEntityById(entityId));
int needToAddEntity = e.get_argument(2);
log_event(labelEvent, "Reqest entity refresh: entity Id=%1 label %2 need Add %3", entityId, label, needToAddEntity);
EntityRefreshInfo_t refreshInfo{ needToAddEntity, pBiotop->getEntityById(entityId) };
return refreshInfo;
}

bool event_manager::buildEventsAddEntitySpawner(int index, BiotopRandomEntitiyGeneration_t& generator, std::vector<NetGameEvent>& eventVector)
Expand Down Expand Up @@ -704,49 +712,49 @@ namespace clan
return pNewEntity;
}


bool event_manager::updateEntityWithZipBuffer(const DataBuffer& xmlZipBuffer, entityIdType entityId, CBiotop* pBiotop)
{
DataBuffer xmlBuffer = ZLibCompression::decompress(xmlZipBuffer, false);
TiXmlDocument xmlDoc;
xmlDoc.Parse(xmlBuffer.get_data());

// Update entity with same Id
CBasicEntity* pCurEntity = pBiotop->getEntityById(entityId);
if (pCurEntity == NULL)
{
log_event(labelEvent, "ERROR Biotop update full entity with unknow entity ID %1", (int)entityId);
return false;
}

CBasicEntity* pNewEntity = CEntityFactory::createEntity(&xmlDoc);
if (pNewEntity == NULL)
{
log_event(labelEvent, "ERROR Biotop update full entity with NULL entity ID %1", (int)entityId);
log_event(labelEvent, "ERROR Biotop update full entity with invalid data. entity ID %1", (int)entityId);
return false;
}

log_event(labelEvent, "Biotop update full entity: %1 ID %2", pNewEntity->getLabel(), (int)entityId);
CBasicEntity* pCurEntity;
bool bFound = false;
int curStepDirection;
bool curRemoteCtrl;
// Update entity with same Id
pCurEntity = pBiotop->getEntityById(entityId);

if (pCurEntity != NULL)
// Do not update entity for pregnant animals
if (pCurEntity->isLocalAutoControlled() && (pCurEntity->getClass() == CLASS_MAMMAL) && (((CAnimMammal*)pCurEntity)->getGestationBabyNumber() > 0))
{
// Do not update entity for pregnant animals
if (pCurEntity->isLocalAutoControlled() && (pCurEntity->getClass() == CLASS_MAMMAL) && (((CAnimMammal*)pCurEntity)->getGestationBabyNumber() > 0))
{
log_event(labelEvent, "Skip update entity for pregnant animal %1", pCurEntity->getLabel());
return false;
}
curStepDirection = pCurEntity->getStepDirection();
curRemoteCtrl = pCurEntity->isRemoteControlled();
pNewEntity->setRemoteControlled(curRemoteCtrl);
pNewEntity->setStepDirection(curStepDirection);
if (!pBiotop->replaceEntityByAnother(pCurEntity->getId(), pNewEntity))
{
delete pNewEntity;
return false;
}
pNewEntity->loadPurposeMapFromXmlFile(&xmlDoc);
bFound = true;
log_event(labelEvent, "Skip update entity for pregnant animal %1", pCurEntity->getLabel());
return false;
}
return (bFound);
curStepDirection = pCurEntity->getStepDirection();
curRemoteCtrl = pCurEntity->isRemoteControlled();
pNewEntity->setRemoteControlled(curRemoteCtrl);
pNewEntity->setStepDirection(curStepDirection);
if (!pBiotop->replaceEntityByAnother(pCurEntity->getId(), pNewEntity))
{
delete pNewEntity;
return false;
}
pNewEntity->loadPurposeMapFromXmlFile(&xmlDoc);

return true;
}

bool event_manager::createMeasureWithZipBuffer(const DataBuffer& dataZipBuffer, CBiotop* pBiotop, const int measureId,
Expand Down
Loading

0 comments on commit b16e1fa

Please sign in to comment.