475 lines
18 KiB
C++
475 lines
18 KiB
C++
#include "game/World.h"
|
|
#include "protocol/PacketDispatcher.h"
|
|
#include "protocol/Protocol.h"
|
|
#include "game/BaseGame.h"
|
|
#include "misc/Random.h"
|
|
|
|
#include <cmath>
|
|
#include "misc/Compression.h"
|
|
|
|
#include <iostream>
|
|
|
|
#define WRITE_MAP 0
|
|
|
|
namespace td {
|
|
namespace game {
|
|
|
|
World::World(Game* game) : m_Game(game) {
|
|
#if WRITE_MAP
|
|
loadMapFromFile("");
|
|
#endif
|
|
}
|
|
|
|
TilePtr World::getTile(std::int32_t x, std::int32_t y) const {
|
|
std::int16_t chunkX = x / Chunk::ChunkWidth;
|
|
std::int16_t chunkY = y / Chunk::ChunkHeight;
|
|
|
|
std::uint16_t subChunkX = x % Chunk::ChunkWidth;
|
|
std::uint16_t subChunkY = y % Chunk::ChunkHeight;
|
|
|
|
auto chunkIt = m_Chunks.find({ chunkX, chunkY });
|
|
if (chunkIt == m_Chunks.end())
|
|
return nullptr;
|
|
|
|
ChunkPtr chunk = chunkIt->second;
|
|
|
|
return getTilePtr(chunk->getTileIndex(subChunkY * Chunk::ChunkWidth + subChunkX));
|
|
}
|
|
|
|
bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader) {
|
|
m_TowerPlacePalette = worldHeader->getTowerTilePalette();
|
|
m_WalkablePalette = worldHeader->getWalkableTileColor();
|
|
m_DecorationPalette = worldHeader->getDecorationPalette();
|
|
|
|
getRedTeam().getSpawn() = worldHeader->getRedSpawn();
|
|
getBlueTeam().getSpawn() = worldHeader->getBlueSpawn();
|
|
|
|
m_SpawnColorPalette = worldHeader->getSpawnPalette();
|
|
|
|
getRedTeam().getCastle() = worldHeader->getRedCastle();
|
|
getBlueTeam().getCastle() = worldHeader->getBlueCastle();
|
|
|
|
m_TilePalette = worldHeader->getTilePalette();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool World::loadMap(const protocol::WorldDataPacket* worldData) {
|
|
m_Chunks = worldData->getChunks();
|
|
return true;
|
|
}
|
|
|
|
bool World::loadMapFromFile(const std::string& fileName) {
|
|
#if !WRITE_MAP
|
|
DataBuffer buffer;
|
|
if (!buffer.ReadFile(fileName)) {
|
|
std::cerr << "Failed to load map from file " << fileName << " !\n";
|
|
}
|
|
|
|
std::cout << "Read file : " << fileName << " (File size : " << buffer.GetSize() << ")" << std::endl;
|
|
|
|
DataBuffer mapHeaderPacketBuffer = utils::Decompress(buffer);
|
|
DataBuffer mapDataPacketBuffer = utils::Decompress(buffer);
|
|
|
|
protocol::PacketType packetType;
|
|
|
|
mapHeaderPacketBuffer >> packetType;
|
|
|
|
protocol::WorldBeginDataPacket headerPacket;
|
|
headerPacket.Deserialize(mapHeaderPacketBuffer);
|
|
|
|
mapDataPacketBuffer >> packetType;
|
|
|
|
protocol::WorldDataPacket dataPacket;
|
|
dataPacket.Deserialize(mapDataPacketBuffer);
|
|
|
|
loadMap(&headerPacket);
|
|
loadMap(&dataPacket);
|
|
|
|
return true;
|
|
|
|
#else
|
|
m_WalkablePalette = { 102, 102, 153 };
|
|
|
|
m_TowerPlacePalette[0] = { 204, 51, 0 };
|
|
m_TowerPlacePalette[1] = { 255, 153, 0 };
|
|
|
|
WalkableTile walkableTileDown; // 1
|
|
walkableTileDown.direction = Direction::PositiveY;
|
|
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileDown));
|
|
|
|
WalkableTile walkableTileUp; // 2
|
|
walkableTileUp.direction = Direction::NegativeY;
|
|
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileUp));
|
|
|
|
WalkableTile walkableTileLeft; // 3
|
|
walkableTileLeft.direction = Direction::NegativeX;
|
|
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileLeft));
|
|
|
|
WalkableTile walkableTileRight; // 4
|
|
walkableTileRight.direction = Direction::PositiveX;
|
|
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileRight));
|
|
|
|
TowerTile redTile0; // 5
|
|
redTile0.color_palette_ref = 0;
|
|
redTile0.team_owner = TeamColor::Red;
|
|
m_TilePalette.push_back(std::make_shared<TowerTile>(redTile0));
|
|
|
|
TowerTile redTile1; // 6
|
|
redTile1.color_palette_ref = 1;
|
|
redTile1.team_owner = TeamColor::Red;
|
|
m_TilePalette.push_back(std::make_shared<TowerTile>(redTile1));
|
|
|
|
TowerTile blueTile0; // 7
|
|
blueTile0.color_palette_ref = 0;
|
|
blueTile0.team_owner = TeamColor::Blue;
|
|
m_TilePalette.push_back(std::make_shared<TowerTile>(blueTile0));
|
|
|
|
TowerTile blueTile1; // 8
|
|
blueTile1.color_palette_ref = 1;
|
|
blueTile1.team_owner = TeamColor::Blue;
|
|
m_TilePalette.push_back(std::make_shared<TowerTile>(blueTile1));
|
|
|
|
getRedTeam().getCastle().x = 58;
|
|
getRedTeam().getCastle().y = 43;
|
|
|
|
getRedTeam().getSpawn().x = 13;
|
|
getRedTeam().getSpawn().y = 13;
|
|
getRedTeam().getSpawn().direction = Direction::PositiveY;
|
|
|
|
m_SpawnColorPalette[(unsigned int)TeamColor::Red] = { 255, 0, 0 };
|
|
|
|
//Chunks
|
|
|
|
Chunk chunk0;
|
|
for (int i = 0; i <= 6; i++) {
|
|
chunk0.palette.push_back(i);
|
|
}
|
|
chunk0.tiles = {
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,5,5,6,6,6,5,5,5,6,6,6,5,5,5,6,
|
|
0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
};
|
|
|
|
m_Chunks.insert({ {0, 0}, std::make_shared<Chunk>(chunk0) });
|
|
m_Chunks.insert({ {1, 0}, std::make_shared<Chunk>(chunk1) });
|
|
m_Chunks.insert({ {1, 1}, std::make_shared<Chunk>(chunk2) });
|
|
|
|
|
|
// blue map = same but with offset of 64 (in x coordinate)
|
|
|
|
getBlueTeam().getCastle().x = 58 + 64;
|
|
getBlueTeam().getCastle().y = 43;
|
|
|
|
getBlueTeam().getSpawn().x = 13 + 64;
|
|
getBlueTeam().getSpawn().y = 13;
|
|
getBlueTeam().getSpawn().direction = Direction::PositiveY;
|
|
|
|
m_SpawnColorPalette[(unsigned int)TeamColor::Blue] = { 0, 0, 255 };
|
|
|
|
Chunk chunk01;
|
|
chunk01.palette = { 0, 1, 2, 3, 4, 7, 8 };
|
|
|
|
chunk01.tiles = chunk0.tiles; // the tiles indicies are the same, only the palette has changed
|
|
|
|
|
|
Chunk chunk11;
|
|
chunk11.palette = { 0, 1, 2, 3, 4, 7, 8 };
|
|
|
|
chunk11.tiles = chunk1.tiles; // the tiles indicies are the same, only the palette has changed
|
|
|
|
|
|
Chunk chunk21;
|
|
chunk21.palette = { 0, 1, 2, 3, 4, 7, 8 };
|
|
|
|
chunk21.tiles = chunk2.tiles; // the tiles indicies are the same, only the palette has changed
|
|
|
|
m_Chunks.insert({ {2, 0}, std::make_shared<Chunk>(chunk01) });
|
|
m_Chunks.insert({ {3, 0}, std::make_shared<Chunk>(chunk11) });
|
|
m_Chunks.insert({ {3, 1}, std::make_shared<Chunk>(chunk21) });
|
|
|
|
return saveMap("tdmap_debug.tdmap");
|
|
#endif
|
|
|
|
}
|
|
|
|
bool World::saveMap(const std::string& fileName) const {
|
|
protocol::WorldBeginDataPacket headerPacket(this);
|
|
protocol::WorldDataPacket dataPacket(this);
|
|
|
|
DataBuffer mapHeaderCompressed = utils::Compress(headerPacket.Serialize());
|
|
DataBuffer mapDataCompressed = utils::Compress(dataPacket.Serialize());
|
|
|
|
std::cout << "Header Packet Size : " << mapHeaderCompressed.GetSize() << std::endl;
|
|
std::cout << "World Data Packet Size : " << mapDataCompressed.GetSize() << std::endl;
|
|
|
|
DataBuffer buffer = mapHeaderCompressed << mapDataCompressed;
|
|
std::cout << "Total Size : " << buffer.GetSize() << std::endl;
|
|
return buffer.WriteFile(fileName);
|
|
}
|
|
|
|
void World::tick(std::uint64_t delta) {
|
|
moveMobs(delta);
|
|
for(TowerPtr tower : m_Towers){
|
|
tower->tick(delta);
|
|
}
|
|
}
|
|
|
|
void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir) {
|
|
MobPtr mob = MobFactory::createMob(id, type, level, sender);
|
|
mob->setX(x);
|
|
mob->setY(y);
|
|
mob->setDirection(dir);
|
|
m_Mobs.push_back(mob);
|
|
}
|
|
|
|
void World::placeTowerAt(TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) {
|
|
TowerPtr tower = TowerFactory::createTower(type, x, y, builder);
|
|
m_Towers.push_back(tower);
|
|
}
|
|
|
|
void World::moveMobs(std::uint64_t delta) {
|
|
for (MobPtr mob : m_Mobs) {
|
|
TilePtr tile = getTile(mob->getX(), mob->getY());
|
|
|
|
if (tile != nullptr && tile->getType() == TileType::Walk) {
|
|
WalkableTile* walkTile = dynamic_cast<WalkableTile*>(tile.get());
|
|
mob->setDirection(walkTile->direction);
|
|
}
|
|
|
|
float mobWalkSpeed = mob->getStats()->getMovementSpeed();
|
|
|
|
float walkAmount = mobWalkSpeed * ((float)delta / 1000.0f);
|
|
|
|
switch (mob->getDirection()) {
|
|
case Direction::NegativeX: {
|
|
mob->setX(mob->getX() - walkAmount);
|
|
break;
|
|
}
|
|
case Direction::PositiveX: {
|
|
mob->setX(mob->getX() + walkAmount);
|
|
break;
|
|
}
|
|
case Direction::NegativeY: {
|
|
mob->setY(mob->getY() - walkAmount);
|
|
break;
|
|
}
|
|
case Direction::PositiveY: {
|
|
mob->setY(mob->getY() + walkAmount);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const Color* World::getTileColor(TilePtr tile) const {
|
|
switch (tile->getType()) {
|
|
case TileType::Tower: {
|
|
TowerTile* towerTile = (TowerTile*)tile.get();
|
|
return &m_TowerPlacePalette[towerTile->color_palette_ref];
|
|
}
|
|
case TileType::Walk: {
|
|
return &m_WalkablePalette;
|
|
}
|
|
case TileType::Decoration: {
|
|
DecorationTile* towerTile = (DecorationTile*)tile.get();
|
|
return &m_DecorationPalette[towerTile->color_palette_ref];
|
|
break;
|
|
}
|
|
case TileType::None: {
|
|
return nullptr;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) const {
|
|
TilePtr tile = getTile(worldPos.x, worldPos.y);
|
|
const Player& player = m_Game->getPlayers()[playerID];
|
|
|
|
if (tile == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
if (tile->getType() == game::TileType::Tower) {
|
|
const TowerTile* towerTile = (const TowerTile*) tile.get();
|
|
if(towerTile->team_owner != player.getTeamColor())
|
|
return false;
|
|
for (int x = -1; x < 2; x++) {
|
|
for (int y = -1; y < 2; y++) {
|
|
game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y);
|
|
if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const {
|
|
TilePtr tile = getTile(worldPos.x, worldPos.y);
|
|
const Player& player = m_Game->getPlayers()[playerID];
|
|
|
|
if (tile == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
if (tile->getType() == game::TileType::Tower) {
|
|
const TowerTile* towerTile = (const TowerTile*) tile.get();
|
|
if(towerTile->team_owner != player.getTeamColor())
|
|
return false;
|
|
for (int x = -2; x < 3; x++) {
|
|
for (int y = -2; y < 3; y++) {
|
|
game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y);
|
|
if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Team& World::getRedTeam() {
|
|
return m_Game->getRedTeam();
|
|
}
|
|
|
|
const Team& World::getRedTeam() const {
|
|
return m_Game->getRedTeam();
|
|
}
|
|
|
|
Team& World::getBlueTeam() {
|
|
return m_Game->getBlueTeam();
|
|
}
|
|
|
|
const Team& World::getBlueTeam() const {
|
|
return m_Game->getBlueTeam();
|
|
}
|
|
|
|
Team& World::getTeam(TeamColor team) {
|
|
return m_Game->getTeam(team);
|
|
}
|
|
|
|
const Team& World::getTeam(TeamColor team) const {
|
|
return m_Game->getTeam(team);
|
|
}
|
|
|
|
} // namespace game
|
|
} // namespace td
|