Skip to content

Commit

Permalink
Start server with scenario
Browse files Browse the repository at this point in the history
Allows to define a scenario in ini file
  • Loading branch information
cybiosphere committed Mar 3, 2024
1 parent 461190b commit 34d1bc1
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 77 deletions.
14 changes: 3 additions & 11 deletions WpClanNetwork/inc/clan_server.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
#pragma once

//FRED #include <ClanLib/core.h>
//FRED #include <ClanLib/network.h>

//#include "Network/precomp.h"
//#include "API/Network/NetGame/event.h"
//#include "API/Core/Text/string_format.h"
//#include "API/Network/NetGame/event_dispatcher.h"
//#include "API/Network/NetGame/server.h"
//#include "Network/NetGame/connection_impl.h"

#include "API/core.h"
#include "API/network.h"
#include "CBiotop.h"
#include "CScenarioPlayer.h"
#include "event_manager.h"

using namespace clan;
Expand All @@ -23,7 +14,7 @@ class ServerCoprocessor;
class Server
{
public:
Server(std::string portStr, CBiotop* pBiotop);
Server(std::string portStr, CBiotop* pBiotop, CScenarioPlayer* pScenarioPlayer);
~Server();

// start only
Expand Down Expand Up @@ -96,6 +87,7 @@ class Server

std::string serverPortStr;
CBiotop* m_pBiotop;
CScenarioPlayer* m_pScenarioPlayer;
int next_user_id;
int nb_users_connected;
float m_biotopSpeed; // set 1.0 for real time speed. Biotp update every 1sec
Expand Down
44 changes: 19 additions & 25 deletions WpClanNetwork/src/clan_client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,43 @@
using namespace clan;

#include "clan_client.h"
#include "Helpers.h"

//FRED #ifdef WIN32
//FRED int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
//FRED #else
int main(int argc, char* argv[])
//FRED #endif
{
try
{
ConsoleWindow console("Client Console", 160, 1000);
ConsoleLogger logger;

// Get Server info in ini file
string ServerAddrStr;
string ServerPortStr;
string LogInStr;
char resuBuffer[512];
try
{
ConsoleWindow console("Client Console", 160, 1000);
ConsoleLogger logger;
string fileIni = "Cybiosphere.ini";

if (argc == 2)
{
fileIni = argv[1];
log_event("Start ", "Use specific ini file " + fileIni);
}
int resu = getStringSectionFromFile("CYBIOSPHERE", "ServerAddr", "localhost", resuBuffer, 512, fileIni);
ServerAddrStr = resuBuffer;
resu = getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni);
ServerPortStr = resuBuffer;
resu = getStringSectionFromFile("CYBIOSPHERE", "Login", "Player", resuBuffer, 512, fileIni);
LogInStr = resuBuffer;

Client client(ServerAddrStr, ServerPortStr, LogInStr);
client.exec();
string serverAddrStr = getIpServerAddrFromIniFile(fileIni);
string serverPortStr = getIpPortStrFromIniFile(fileIni);
string logInStr = getIpServerLoginFromIniFile(fileIni);

Client client(serverAddrStr, serverPortStr, logInStr);
client.exec();

return 0;
}
catch (Exception e)
{
return 0;
}
catch (Exception e)
{
#ifdef WIN32
//FRED MessageBox(0, e.get_message_and_stack_trace().c_str(), TEXT("Unhandled Exception"), MB_OK);
#else
Console::write_line("Unhandled exception: %1", e.get_message_and_stack_trace());
Console::write_line("Unhandled exception: %1", e.get_message_and_stack_trace());
#endif

return 1;
}
return 1;
}
}
17 changes: 15 additions & 2 deletions WpClanNetwork/src/clan_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ CommandHandler_t ServerCmdNameList[SERVER_CMD_NUMBER] =
};


Server::Server(std::string portStr, CBiotop* pBiotop)
Server::Server(std::string portStr, CBiotop* pBiotop, CScenarioPlayer* pScenarioPlayer)
: serverPortStr{ portStr },
m_pBiotop{ pBiotop },
m_pScenarioPlayer{ pScenarioPlayer },
next_user_id(1),
nb_users_connected(0),
m_biotopSpeed(1.0),
Expand Down Expand Up @@ -106,6 +107,7 @@ void Server::exec()
{
std::chrono::time_point<std::chrono::system_clock> curTick;
std::chrono::time_point<std::chrono::system_clock> lastRunTick = std::chrono::system_clock::now();
int currentScenarioScore = 0;

network_server.start(serverPortStr);

Expand All @@ -129,7 +131,18 @@ void Server::exec()
{
send_event_new_second_start();
// Next second in biotop
m_pBiotop->nextSecond();
if ((m_pScenarioPlayer != nullptr) && m_pScenarioPlayer->NextCmdNextSecond())
{
if (m_pScenarioPlayer->m_totalScore > currentScenarioScore)
{
currentScenarioScore = m_pScenarioPlayer->m_totalScore;
log_event(labelServer, "Scenario score success: %1 / %2", m_pScenarioPlayer->m_successScore, m_pScenarioPlayer->m_totalScore);
}
}
else
{
m_pBiotop->nextSecond();
}
send_event_new_second_end();
}
}
Expand Down
51 changes: 13 additions & 38 deletions WpClanNetwork/src/clan_server_main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "clan_server.h"
#include "event_definitions.h"
#include "CBiotop.h"
#include "CScenarioPlayer.h"
#include "Helpers.h"

//#ifdef WIN32
//int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
Expand All @@ -12,55 +14,28 @@ int main(int argc, char* argv[])
{
ConsoleWindow console("Server Console", 160, 1000);
ConsoleLogger logger;

CBiotop* pBiotop;
char resuBuffer[512];
string resuStr;
CBiotop* pBiotop{nullptr};
CScenarioPlayer* pScenarioPlayer{nullptr};
string fileIni = "Cybiosphere.ini";

if (argc == 2)
{
fileIni = argv[1];
log_event(labelServer, "Open ini file " + fileIni);
}
string serverPortStr;
if (getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni) > 0)
{
serverPortStr = resuBuffer;
}
else
{
serverPortStr = "4556";
}
string serverPortStr = getIpPortStrFromIniFile(fileIni);
createBiotopAndScenarioFromIniFile(fileIni, &pBiotop, &pScenarioPlayer);

int resu = getStringSectionFromFile("CYBIOSPHERE", "Biotop", "", resuBuffer, 512, fileIni);
resuStr = resuBuffer;

if (resuStr != "")
{
string resuDataPath;
pBiotop = new CBiotop(0,0,0);
bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni);
resuDataPath = resuBuffer;
if (resuDataPath != "")
pBiotop->loadFromXmlFile(resuStr, resuDataPath);
else
pBiotop->loadFromXmlFile(resuStr, "..\\dataXml\\");
log_event(labelServer, "Biotop loaded");
}
else
{
pBiotop = new CBiotop(80,40,3);
pBiotop->initGridDefaultLayerType();
pBiotop->initGridDefaultAltitude();
pBiotop->initGridEntity();
pBiotop->setDefaultEntitiesForTest();
log_event(labelServer, "Default empty biotop created");
}
if (pBiotop != nullptr)
log_event(labelServer, "Biotop loaded: %1", pBiotop->getLabel());
if (pScenarioPlayer != nullptr)
log_event(labelServer, "Scenario loaded");

Server server(serverPortStr, pBiotop);
Server server(serverPortStr, pBiotop, pScenarioPlayer);
server.exec();

delete pBiotop;
delete pScenarioPlayer;
return 0;
}
catch (Exception e)
Expand Down
84 changes: 84 additions & 0 deletions WpCommon/inc/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,90 @@ distribution.
// Functions
//===========================================================================

inline string getIpServerAddrFromIniFile(const string& fileIni)
{
string ServerAddrStr;
char resuBuffer[512];
getStringSectionFromFile("CYBIOSPHERE", "ServerAddr", "localhost", resuBuffer, 512, fileIni);
ServerAddrStr = resuBuffer;
return ServerAddrStr;
}

inline string getIpServerLoginFromIniFile(const string& fileIni)
{
string LogInStr;
char resuBuffer[512];
getStringSectionFromFile("CYBIOSPHERE", "Login", "Player", resuBuffer, 512, fileIni);
LogInStr = resuBuffer;
return LogInStr;
}

inline string getIpPortStrFromIniFile(const string& fileIni)
{
string serverPortStr;
char resuBuffer[512];
if (getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni) > 0)
{
serverPortStr = resuBuffer;
}
else
{
serverPortStr = "4556";
}
return serverPortStr;
}

inline void createBiotopAndScenarioFromIniFile(const string& fileIni, CBiotop** pNewBiotop, CScenarioPlayer** pNewScenarioPlayer)
{
CBiotop* pBiotop{ nullptr };
CScenarioPlayer* pScenarioPlayer{ nullptr };
char resuBuffer[512];
string resuStr;

int resu = getStringSectionFromFile("CYBIOSPHERE", "Biotop", "", resuBuffer, 512, fileIni);
resuStr = resuBuffer;
if (resuStr != "")
{
string resuDataPath;
pBiotop = new CBiotop(0, 0, 0);
bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni);
resuDataPath = resuBuffer;
if (resuDataPath != "")
pBiotop->loadFromXmlFile(resuStr, resuDataPath);
else
pBiotop->loadFromXmlFile(resuStr, "..\\dataXml\\");
}
else
{
pBiotop = new CBiotop(50, 50, 3);
pBiotop->initGridDefaultLayerType();
pBiotop->initGridDefaultAltitude();
pBiotop->initGridEntity();
pBiotop->setDefaultEntitiesForTest();
}

resu = getStringSectionFromFile("CYBIOSPHERE", "Scenario", "", resuBuffer, 512, fileIni);
resuStr = resuBuffer;
if (resuStr != "")
{
string resuDataPath;
pScenarioPlayer = new CScenarioPlayer(pBiotop);
bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni);
resuDataPath = resuBuffer;
if (resuDataPath != "")
pScenarioPlayer->ReadScenarioFile(resuStr, resuDataPath);
else
pScenarioPlayer->ReadScenarioFile(resuStr, "..\\dataXml\\");

// Start reading scenario twice to update biotop
pScenarioPlayer->NextCmdNextSecond();
pScenarioPlayer->NextCmdNextSecond();
}

*pNewBiotop = pBiotop;
*pNewScenarioPlayer = pScenarioPlayer;
}

inline size_t computeMaxSpeedStepfactor(CBiotop* pBiotop, bool isMaxSpeed)
{
if (pBiotop == NULL)
Expand Down
1 change: 1 addition & 0 deletions WpEnvironment/inc/CBiotop.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ class DLL_CYBIOCORE_API CBiotop
private:
std::map<entityIdType, BiotopEvent_t>& getBiotopEventMapCurrent();
std::map<entityIdType, BiotopEvent_t>& getBiotopEventMapPrevious();
void clearEventMaps();

//---------------------------------------------------------------------------
// Save/Load in file
Expand Down
10 changes: 9 additions & 1 deletion WpEnvironment/src/CBiotop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,12 @@ std::map<entityIdType, BiotopEvent_t>& CBiotop::getBiotopEventMapPrevious()
}
}

void CBiotop::clearEventMaps()
{
m_tEventsOdd.clear();
m_tEventsEven.clear();
}

//===========================================================================
// Save/Load in file
//===========================================================================
Expand Down Expand Up @@ -2431,7 +2437,6 @@ bool CBiotop::saveInXmlFile(TiXmlDocument *pXmlDoc, string pathNameForEntities,

bool CBiotop::loadFromXmlFile(string fileName, string pathName)
{

TiXmlDocument xmlDoc(pathName + fileName);
if (xmlDoc.LoadFile() == false)
{
Expand All @@ -2445,6 +2450,8 @@ bool CBiotop::loadFromXmlFile(string fileName, string pathName)
loadFromXmlFile(&xmlDoc, pathName);
CYBIOCORE_LOG_TIME(m_BioTime);
CYBIOCORE_LOG("BIOTOP - File load complete\n");

m_Label = fileName;
return true;
}

Expand Down Expand Up @@ -2475,6 +2482,7 @@ bool CBiotop::loadFromXmlFile(TiXmlDocument *pXmlDoc, string pathNameForEntities
deleteAllEntities();
deleteAllMeasures();
deleteGrid();
clearEventMaps();

m_Dimension.x = sizeX;
m_Dimension.y = sizeY;
Expand Down

0 comments on commit 34d1bc1

Please sign in to comment.