Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
World Class Reference

Pure game state - no networking, no scheduling. More...

#include <World.hpp>

+ Collaboration diagram for World:

Public Member Functions

 World (int width, int height, const std::vector< std::string > &teamNames, unsigned int seed)
 
Tileat (int x, int y)
 Access a tile by position. Map is toroidal, coordinates wrap.
 
const Tileat (int x, int y) const
 
std::vector< std::pair< int, int > > spawnResources ()
 
int addPlayer (int connectionId, const std::string &teamName, int x, int y, Orientation orientation)
 
void removePlayer (int id)
 
void movePlayer (int id, int x, int y)
 
void turnPlayer (int id, Orientation orientation)
 
bool takeResource (int playerId, ResourceType type)
 
bool setResource (int playerId, ResourceType type)
 
bool consumeFood (int playerId)
 Consume one unit of food from playerId (starvation tick). Fires onPlayerInventoryChanged so observers stay in sync.
 
PlayergetPlayer (int id)
 
void playerBroadcast (int playerId, const std::string &message)
 Fire onBroadcast to observers (GUI animation, logging). No state change.
 
EjectResult ejectPlayers (int ejectorId)
 
void spawnInitialEggs (int countPerTeam)
 Spawn countPerTeam eggs for every team at random tiles (server startup).
 
int addEgg (int playerId)
 
bool hatchEgg (int eggId)
 
std::optional< EggpopEggForTeam (const std::string &teamName)
 
int teamEggCount (const std::string &team) const
 
std::optional< std::vector< int > > startIncantation (int playerId)
 Validate and start an incantation for playerId. Returns the list of participant IDs on success, nullopt if prerequisites fail. Prerequisites are checked again in finalizeIncantation() after the ritual delay.
 
bool finalizeIncantation (int x, int y, const std::vector< int > &participantIds)
 Finalize an incantation after the 300/f second delay. Re-checks prerequisites. On success, levels up all participants and consumes stones. Returns false if any participant died or moved off the tile.
 
std::optional< std::string > checkWin () const
 
bool isGameEnded () const
 True once a team has won. Game logic stops reacting to AI commands.
 
const std::optional< std::string > & winner () const
 Winning team name, set when isGameEnded() becomes true.
 
int teamPlayerCount (const std::string &team) const
 
int width () const
 
int height () const
 
const std::unordered_map< int, Player > & getPlayers () const
 
const std::unordered_map< int, Egg > & getEggs () const
 
void addWorldObserver (IWorldObserver *observer)
 

Private Member Functions

int _spawnEgg (const std::string &teamName, int x, int y, int parentPlayerId)
 

Private Attributes

int _width
 
int _height
 
std::vector< Tile_tiles
 
std::unordered_map< int, Player_players
 
std::unordered_map< int, Egg_eggs
 
std::vector< std::string > _teamNames
 
std::mt19937 _rng
 
int _nextPlayerId = 0
 
int _nextEggId = 0
 
bool _gameEnded = false
 
std::optional< std::string > _winner
 
std::vector< IWorldObserver * > _observers
 

Detailed Description

Pure game state - no networking, no scheduling.

Owns the map, players, eggs, and teams. The server layer calls mutation methods here after dispatching commands, then notifies GUI clients via GuiNotifier. Keeping World network-free makes it fully unit-testable.

See also
server/doc.md - "core/World" section.
GuiNotifier for GUI notifications after mutations.

Definition at line 47 of file World.hpp.

Constructor & Destructor Documentation

◆ World()

World::World ( int  width,
int  height,
const std::vector< std::string > &  teamNames,
unsigned int  seed 
)
Parameters
seedvalue for the world RNG (egg + resource placement), for reproducible games.

Definition at line 6 of file World.cpp.

References _tiles, height(), and width().

+ Here is the call graph for this function:

Member Function Documentation

◆ _spawnEgg()

int World::_spawnEgg ( const std::string &  teamName,
int  x,
int  y,
int  parentPlayerId 
)
private

Definition at line 194 of file World.cpp.

References _eggs, _nextEggId, _observers, at(), and Tile::eggIds.

Referenced by addEgg(), and spawnInitialEggs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addEgg()

int World::addEgg ( int  playerId)

Definition at line 216 of file World.cpp.

References _players, and _spawnEgg().

Referenced by CommandDispatcher::_handleFork().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addPlayer()

int World::addPlayer ( int  connectionId,
const std::string &  teamName,
int  x,
int  y,
Orientation  orientation 
)

Definition at line 58 of file World.cpp.

References _nextPlayerId, _observers, _players, at(), Player::connectionId, Resources::food, Player::id, id(), Player::inventory, Player::level, Player::orientation, Tile::playerIds, Player::teamName, Player::x, and Player::y.

Referenced by HandshakeHandler::_promoteToAi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addWorldObserver()

void World::addWorldObserver ( IWorldObserver observer)

Definition at line 420 of file World.cpp.

References _observers.

Referenced by Server::Server().

+ Here is the caller graph for this function:

◆ at() [1/2]

Tile & World::at ( int  x,
int  y 
)

Access a tile by position. Map is toroidal, coordinates wrap.

Definition at line 12 of file World.cpp.

References _height, _tiles, and _width.

Referenced by CommandDispatcher::_handleBct(), CommandDispatcher::_handleLook(), CommandDispatcher::_handleMct(), HandshakeHandler::_promoteToGui(), _spawnEgg(), addPlayer(), ejectPlayers(), finalizeIncantation(), hatchEgg(), movePlayer(), popEggForTeam(), removePlayer(), setResource(), spawnResources(), startIncantation(), and takeResource().

+ Here is the caller graph for this function:

◆ at() [2/2]

const Tile & World::at ( int  x,
int  y 
) const

Definition at line 20 of file World.cpp.

References _height, _tiles, and _width.

◆ checkWin()

std::optional< std::string > World::checkWin ( ) const

Definition at line 405 of file World.cpp.

References _players.

Referenced by finalizeIncantation().

+ Here is the caller graph for this function:

◆ consumeFood()

bool World::consumeFood ( int  playerId)

Consume one unit of food from playerId (starvation tick). Fires onPlayerInventoryChanged so observers stay in sync.

Returns
true if the player is still alive (food > 0), false if it starved.

Definition at line 135 of file World.cpp.

References _observers, _players, and Resources::food.

◆ ejectPlayers()

EjectResult World::ejectPlayers ( int  ejectorId)

Definition at line 154 of file World.cpp.

References _eggs, _observers, _players, at(), and movePlayer().

Referenced by CommandDispatcher::_handleEject().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ finalizeIncantation()

bool World::finalizeIncantation ( int  x,
int  y,
const std::vector< int > &  participantIds 
)

Finalize an incantation after the 300/f second delay. Re-checks prerequisites. On success, levels up all participants and consumes stones. Returns false if any participant died or moved off the tile.

Definition at line 331 of file World.cpp.

References _checkReqs(), _gameEnded, _observers, _players, _winner, at(), checkWin(), DERAUMERE, INCANTATION_REQS, LINEMATE, IncantationReq::linemate, MENDIANE, PHIRAS, SIBUR, THYSTAME, and winner().

Referenced by CommandDispatcher::_handleIncantation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getEggs()

const std::unordered_map< int, Egg > & World::getEggs ( ) const

Definition at line 403 of file World.cpp.

References _eggs.

Referenced by HandshakeHandler::_promoteToGui().

+ Here is the caller graph for this function:

◆ getPlayer()

◆ getPlayers()

const std::unordered_map< int, Player > & World::getPlayers ( ) const

Definition at line 402 of file World.cpp.

References _players.

Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleConnectNbr(), Server::_handleGameOver(), CommandDispatcher::_handleIncantation(), CommandDispatcher::_onAiJoined(), HandshakeHandler::_promoteToGui(), and CommandDispatcher::onDisconnect().

+ Here is the caller graph for this function:

◆ hatchEgg()

bool World::hatchEgg ( int  eggId)

Definition at line 230 of file World.cpp.

References _eggs, _observers, at(), and Tile::eggIds.

+ Here is the call graph for this function:

◆ height()

int World::height ( ) const

Definition at line 401 of file World.cpp.

References _height.

Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleEject(), CommandDispatcher::_handleMct(), CommandDispatcher::_handleMsz(), and World().

+ Here is the caller graph for this function:

◆ isGameEnded()

bool World::isGameEnded ( ) const

True once a team has won. Game logic stops reacting to AI commands.

Definition at line 417 of file World.cpp.

References _gameEnded.

Referenced by CommandDispatcher::_dispatchAi(), HandshakeHandler::onLine(), and Server::run().

+ Here is the caller graph for this function:

◆ movePlayer()

void World::movePlayer ( int  id,
int  x,
int  y 
)

Definition at line 91 of file World.cpp.

References _height, _observers, _players, _width, at(), and Tile::playerIds.

Referenced by CommandDispatcher::_handleForward(), and ejectPlayers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ playerBroadcast()

void World::playerBroadcast ( int  playerId,
const std::string &  message 
)

Fire onBroadcast to observers (GUI animation, logging). No state change.

Definition at line 149 of file World.cpp.

References _observers.

Referenced by CommandDispatcher::_handleBroadcast().

+ Here is the caller graph for this function:

◆ popEggForTeam()

std::optional< Egg > World::popEggForTeam ( const std::string &  teamName)

Definition at line 244 of file World.cpp.

References _eggs, _observers, at(), Tile::eggIds, Egg::id, Egg::x, and Egg::y.

Referenced by HandshakeHandler::_promoteToAi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removePlayer()

void World::removePlayer ( int  id)

Definition at line 78 of file World.cpp.

References _observers, _players, at(), and Tile::playerIds.

Referenced by CommandDispatcher::onDisconnect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setResource()

bool World::setResource ( int  playerId,
ResourceType  type 
)

Definition at line 123 of file World.cpp.

References _observers, _players, at(), and Tile::resources.

Referenced by CommandDispatcher::_handleSet().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ spawnInitialEggs()

void World::spawnInitialEggs ( int  countPerTeam)

Spawn countPerTeam eggs for every team at random tiles (server startup).

Definition at line 208 of file World.cpp.

References _height, _rng, _spawnEgg(), _teamNames, and _width.

Referenced by Server::Server().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ spawnResources()

std::vector< std::pair< int, int > > World::spawnResources ( )

Definition at line 28 of file World.cpp.

References _height, _observers, _rng, _tiles, _width, at(), Resources::density(), Tile::resources, and Resources::TYPE_COUNT.

Referenced by Server::Server().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startIncantation()

std::optional< std::vector< int > > World::startIncantation ( int  playerId)

Validate and start an incantation for playerId. Returns the list of participant IDs on success, nullopt if prerequisites fail. Prerequisites are checked again in finalizeIncantation() after the ritual delay.

Definition at line 304 of file World.cpp.

References _checkReqs(), _observers, _players, at(), and INCANTATION_REQS.

Referenced by CommandDispatcher::_handleIncantation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ takeResource()

bool World::takeResource ( int  playerId,
ResourceType  type 
)

Definition at line 110 of file World.cpp.

References _observers, _players, and at().

Referenced by CommandDispatcher::_handleTake().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ teamEggCount()

int World::teamEggCount ( const std::string &  team) const

Definition at line 222 of file World.cpp.

References _eggs.

Referenced by CommandDispatcher::_handleConnectNbr(), HandshakeHandler::_promoteToAi(), and HandshakeHandler::onLine().

+ Here is the caller graph for this function:

◆ teamPlayerCount()

int World::teamPlayerCount ( const std::string &  team) const

Definition at line 392 of file World.cpp.

References _players.

◆ turnPlayer()

void World::turnPlayer ( int  id,
Orientation  orientation 
)

Definition at line 103 of file World.cpp.

References _observers, and _players.

Referenced by CommandDispatcher::_handleLeft(), and CommandDispatcher::_handleRight().

+ Here is the caller graph for this function:

◆ width()

int World::width ( ) const

Definition at line 400 of file World.cpp.

References _width.

Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleEject(), CommandDispatcher::_handleMct(), CommandDispatcher::_handleMsz(), and World().

+ Here is the caller graph for this function:

◆ winner()

const std::optional< std::string > & World::winner ( ) const

Winning team name, set when isGameEnded() becomes true.

Definition at line 418 of file World.cpp.

References _winner.

Referenced by Server::_handleGameOver(), and finalizeIncantation().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _eggs

std::unordered_map<int, Egg> World::_eggs
private

Definition at line 126 of file World.hpp.

Referenced by _spawnEgg(), ejectPlayers(), getEggs(), hatchEgg(), popEggForTeam(), and teamEggCount().

◆ _gameEnded

bool World::_gameEnded = false
private

Definition at line 131 of file World.hpp.

Referenced by finalizeIncantation(), and isGameEnded().

◆ _height

int World::_height
private

Definition at line 123 of file World.hpp.

Referenced by at(), at(), height(), movePlayer(), spawnInitialEggs(), and spawnResources().

◆ _nextEggId

int World::_nextEggId = 0
private

Definition at line 130 of file World.hpp.

Referenced by _spawnEgg().

◆ _nextPlayerId

int World::_nextPlayerId = 0
private

Definition at line 129 of file World.hpp.

Referenced by addPlayer().

◆ _observers

◆ _players

◆ _rng

std::mt19937 World::_rng
private

Definition at line 128 of file World.hpp.

Referenced by spawnInitialEggs(), and spawnResources().

◆ _teamNames

std::vector<std::string> World::_teamNames
private

Definition at line 127 of file World.hpp.

Referenced by spawnInitialEggs().

◆ _tiles

std::vector<Tile> World::_tiles
private

Definition at line 124 of file World.hpp.

Referenced by at(), at(), spawnResources(), and World().

◆ _width

int World::_width
private

Definition at line 122 of file World.hpp.

Referenced by at(), at(), movePlayer(), spawnInitialEggs(), spawnResources(), and width().

◆ _winner

std::optional<std::string> World::_winner
private

Definition at line 132 of file World.hpp.

Referenced by finalizeIncantation(), and winner().


The documentation for this class was generated from the following files: