2009-06-20 09:13:29 -07:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. 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 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "MessageHandler.h"
|
|
|
|
|
#include "../GameLoop.h"
|
|
|
|
|
|
2006-06-09 09:44:16 -07:00
|
|
|
#include "graphics/GameView.h"
|
|
|
|
|
#include "graphics/MapWriter.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "graphics/Patch.h"
|
2006-06-09 09:44:16 -07:00
|
|
|
#include "graphics/Terrain.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "graphics/TextureEntry.h"
|
2006-06-09 09:44:16 -07:00
|
|
|
#include "graphics/TextureManager.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "ps/Game.h"
|
|
|
|
|
#include "ps/GameAttributes.h"
|
|
|
|
|
#include "ps/Loader.h"
|
2007-01-13 14:44:42 -08:00
|
|
|
#include "ps/World.h"
|
2006-06-11 00:03:59 -07:00
|
|
|
#include "renderer/Renderer.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "simulation/LOSManager.h"
|
|
|
|
|
#include "simulation/Simulation.h"
|
2010-01-22 12:03:14 -08:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
#include "simulation2/components/ICmpPlayer.h"
|
|
|
|
|
#include "simulation2/components/ICmpPlayerManager.h"
|
|
|
|
|
#include "simulation2/components/ICmpPosition.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
namespace
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-01-24 12:17:28 -08:00
|
|
|
void InitGame(const CStrW& map)
|
|
|
|
|
{
|
|
|
|
|
if (g_Game)
|
|
|
|
|
{
|
|
|
|
|
delete g_Game;
|
|
|
|
|
g_Game = NULL;
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
// Set attributes for the game:
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
g_GameAttributes.m_MapFile = map;
|
|
|
|
|
// Make all players locally controlled
|
|
|
|
|
for (int i = 1; i < 8; ++i)
|
|
|
|
|
g_GameAttributes.GetSlot(i)->AssignLocal();
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
// Make the whole world visible
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
g_GameAttributes.m_LOSSetting = LOS_SETTING_ALL_VISIBLE;
|
2007-01-24 12:17:28 -08:00
|
|
|
g_GameAttributes.m_FogOfWar = false;
|
2006-10-08 10:39:46 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
// Don't use screenshot mode, because we want working AI for the
|
|
|
|
|
// simulation-testing. Outside that simulation-testing, we avoid having
|
|
|
|
|
// the units move into attack mode by never calling CEntity::update.
|
|
|
|
|
g_GameAttributes.m_ScreenshotMode = false;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
// Initialise the game:
|
|
|
|
|
g_Game = new CGame();
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
void AddDefaultPlayers()
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpPlayerManager> cmpPlayerMan(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
|
|
|
|
|
debug_assert(!cmpPlayerMan.null());
|
|
|
|
|
|
|
|
|
|
// TODO: pick a sensible number, give them names and colours etc
|
|
|
|
|
size_t numPlayers = 4;
|
|
|
|
|
for (size_t i = 0; i < numPlayers; ++i)
|
|
|
|
|
{
|
|
|
|
|
entity_id_t ent = g_Game->GetSimulation2()->AddEntity(L"special/player");
|
|
|
|
|
cmpPlayerMan->AddPlayer(ent);
|
|
|
|
|
}
|
|
|
|
|
// Also TODO: Maybe it'd be sensible to load this from a map XML file via CMapReader,
|
|
|
|
|
// rather than duplicating the creation code here?
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
void StartGame()
|
|
|
|
|
{
|
|
|
|
|
PSRETURN ret = g_Game->StartGame(&g_GameAttributes);
|
|
|
|
|
debug_assert(ret == PSRETURN_OK);
|
|
|
|
|
LDR_NonprogressiveLoad();
|
|
|
|
|
ret = g_Game->ReallyStartGame();
|
|
|
|
|
debug_assert(ret == PSRETURN_OK);
|
|
|
|
|
|
|
|
|
|
// Make sure entities get rendered in the correct location
|
|
|
|
|
g_Game->GetSimulation()->Interpolate(0.0);
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2007-01-24 12:17:28 -08:00
|
|
|
namespace AtlasMessage {
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
MESSAGEHANDLER(GenerateMap)
|
|
|
|
|
{
|
|
|
|
|
InitGame(L"");
|
|
|
|
|
|
|
|
|
|
// Convert size in patches to number of vertices
|
|
|
|
|
int vertices = msg->size * PATCH_SIZE + 1;
|
|
|
|
|
|
|
|
|
|
// Generate flat heightmap
|
|
|
|
|
u16* heightmap = new u16[vertices*vertices];
|
|
|
|
|
for (int z = 0; z < vertices; ++z)
|
|
|
|
|
for (int x = 0; x < vertices; ++x)
|
|
|
|
|
heightmap[x + z*vertices] = 16384;
|
|
|
|
|
|
|
|
|
|
// Initialise terrain using the heightmap
|
|
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
|
terrain->Initialize(msg->size, heightmap);
|
|
|
|
|
|
|
|
|
|
delete[] heightmap;
|
|
|
|
|
|
2010-02-01 12:28:48 -08:00
|
|
|
if (g_UseSimulation2)
|
|
|
|
|
AddDefaultPlayers();
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
// Start the game, load data files - this must be done before initialising
|
|
|
|
|
// the terrain texture below, since the terrains must be loaded before being
|
|
|
|
|
// used.
|
|
|
|
|
StartGame();
|
|
|
|
|
|
|
|
|
|
// Cover terrain with default texture
|
|
|
|
|
// TODO: split into fCoverWithTexture
|
|
|
|
|
CTextureEntry* texentry = g_TexMan.FindTexture("grass1_spring"); // TODO: make default customisable
|
|
|
|
|
Handle tex = texentry ? texentry->GetHandle() : 0;
|
|
|
|
|
|
2009-11-08 08:49:52 -08:00
|
|
|
int patchesPerSide = terrain->GetPatchesPerSide();
|
|
|
|
|
for (int pz = 0; pz < patchesPerSide; ++pz)
|
2007-01-24 12:17:28 -08:00
|
|
|
{
|
2009-11-08 08:49:52 -08:00
|
|
|
for (int px = 0; px < patchesPerSide; ++px)
|
2007-01-24 12:17:28 -08:00
|
|
|
{
|
2009-11-08 08:49:52 -08:00
|
|
|
CPatch* patch = terrain->GetPatch(px, pz); // can't fail
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2008-07-12 03:45:11 -07:00
|
|
|
for (ssize_t z = 0; z < PATCH_SIZE; ++z)
|
2007-01-24 12:17:28 -08:00
|
|
|
{
|
2008-07-12 03:45:11 -07:00
|
|
|
for (ssize_t x = 0; x < PATCH_SIZE; ++x)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
patch->m_MiniPatches[z][x].Tex1 = tex;
|
|
|
|
|
patch->m_MiniPatches[z][x].Tex1Priority = 0;
|
|
|
|
|
}
|
2007-01-24 12:17:28 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(LoadMap)
|
|
|
|
|
{
|
|
|
|
|
InitGame(*msg->filename);
|
|
|
|
|
StartGame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(SaveMap)
|
|
|
|
|
{
|
|
|
|
|
CMapWriter writer;
|
2009-11-03 13:46:35 -08:00
|
|
|
const VfsPath pathname = VfsPath(L"maps/scenarios/") / *msg->filename;
|
|
|
|
|
writer.SaveMap(pathname,
|
2007-01-07 17:56:46 -08:00
|
|
|
g_Game->GetWorld()->GetTerrain(), &g_Game->GetWorld()->GetUnitManager(),
|
2006-06-21 15:37:31 -07:00
|
|
|
g_Renderer.GetWaterManager(), g_Renderer.GetSkyManager(),
|
2006-08-21 19:24:44 -07:00
|
|
|
&g_LightEnv, g_Game->GetView()->GetCamera(), g_Game->GetView()->GetCinema());
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|