1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _GLEST_GAME_WORLD_H_
13 #define _GLEST_GAME_WORLD_H_
14 
15 #include "vec.h"
16 #include "math_util.h"
17 #include "resource.h"
18 #include "tech_tree.h"
19 #include "tileset.h"
20 #include "console.h"
21 #include "map.h"
22 #include "scenario.h"
23 #include "minimap.h"
24 #include "logger.h"
25 #include "stats.h"
26 #include "time_flow.h"
27 #include "upgrade.h"
28 #include "water_effects.h"
29 #include "faction.h"
30 #include "unit_updater.h"
31 #include "random.h"
32 #include "game_constants.h"
33 
34 namespace Glest{ namespace Game{
35 
36 using Shared::Graphics::Quad2i;
37 using Shared::Graphics::Rect2i;
38 using Shared::Util::Random;
39 
40 class Faction;
41 class Unit;
42 class Config;
43 class Game;
44 class GameSettings;
45 class ScriptManager;
46 
47 // =====================================================
48 // 	class World
49 //
50 ///	The game world: Map + Tileset + TechTree
51 // =====================================================
52 
53 class World{
54 private:
55 	typedef vector<Faction> Factions;
56 
57 public:
58 	static const int generationArea= 100;
59 	static const float airHeight;
60 	static const int indirectSightRange= 5;
61 
62 private:
63 
64 	Map map;
65 	Tileset tileset;
66 	TechTree techTree;
67 	TimeFlow timeFlow;
68 	Scenario scenario;
69 
70 	UnitUpdater unitUpdater;
71     WaterEffects waterEffects;
72 	Minimap minimap;
73     Stats stats;	//BattleEnd will delete this object
74 
75 	Factions factions;
76 
77 	Random random;
78 
79 	ScriptManager* scriptManager;
80 
81 	int thisFactionIndex;
82 	int thisTeamIndex;
83 	int frameCount;
84 	int nextUnitId;
85 
86 	//config
87 	bool fogOfWar;
88 	int fogOfWarSmoothingFrameSkip;
89 	bool fogOfWarSmoothing;
90 
91 public:
92 	World();
93 	void end(); //to die before selection does
94 
95 	//get
getMaxPlayers()96 	int getMaxPlayers() const						{return map.getMaxPlayers();}
getThisFactionIndex()97 	int getThisFactionIndex() const					{return thisFactionIndex;}
getThisTeamIndex()98 	int getThisTeamIndex() const					{return thisTeamIndex;}
getThisFaction()99 	const Faction *getThisFaction() const			{return &factions[thisFactionIndex];}
getFactionCount()100 	int getFactionCount() const						{return factions.size();}
getMap()101 	const Map *getMap() const 						{return &map;}
getTileset()102 	const Tileset *getTileset() const 				{return &tileset;}
getTechTree()103 	const TechTree *getTechTree() const 			{return &techTree;}
getScenario()104 	const Scenario *getScenario() const 			{return &scenario;}
getTimeFlow()105 	const TimeFlow *getTimeFlow() const				{return &timeFlow;}
getTileset()106 	Tileset *getTileset() 							{return &tileset;}
getMap()107 	Map *getMap() 									{return &map;}
getFaction(int i)108 	const Faction *getFaction(int i) const			{return &factions[i];}
getFaction(int i)109 	Faction *getFaction(int i) 						{return &factions[i];}
getMinimap()110 	const Minimap *getMinimap() const				{return &minimap;}
getStats()111 	const Stats *getStats() const					{return &stats;};
getStats()112 	Stats *getStats()								{return &stats;};
getWaterEffects()113 	const WaterEffects *getWaterEffects() const		{return &waterEffects;}
getNextUnitId()114 	int getNextUnitId()								{return nextUnitId++;}
getFrameCount()115 	int getFrameCount() const						{return frameCount;}
116 
117 	//init & load
118 	void init(Game *game, bool createUnits);
119 	void loadTileset(const string &dir, Checksum* checksum);
120 	void loadTech(const string &dir, Checksum* checksum);
121 	void loadMap(const string &path, Checksum* checksum);
122 	void loadScenario(const string &path, Checksum* checksum);
123 
124 	//misc
125 	void update();
126 	Unit* findUnitById(int id);
127 	const UnitType* findUnitTypeById(const FactionType* factionType, int id);
128 	bool placeUnit(const Vec2i &startLoc, int radius, Unit *unit, bool spaciated= false);
129 	void moveUnitCells(Unit *unit);
130 	bool toRenderUnit(const Unit *unit, const Quad2i &visibleQuad) const;
131 	bool toRenderUnit(const Unit *unit) const;
132 	Unit *nearestStore(const Vec2i &pos, int factionIndex, const ResourceType *rt);
133 
134 	//scripting interface
135 	void createUnit(const string &unitName, int factionIndex, const Vec2i &pos);
136 	void givePositionCommand(int unitId, const string &commandName, const Vec2i &pos);
137 	void giveProductionCommand(int unitId, const string &producedName);
138 	void giveUpgradeCommand(int unitId, const string &upgradeName);
139 	void giveResource(const string &resourceName, int factionIndex, int amount);
140 	int getResourceAmount(const string &resourceName, int factionIndex);
141 	Vec2i getStartLocation(int factionIndex);
142 	Vec2i getUnitPosition(int unitId);
143 	int getUnitFactionIndex(int unitId);
144 	int getUnitCount(int factionIndex);
145 	int getUnitCountOfType(int factionIndex, const string &typeName);
146 
147 private:
148 
149 	void initCells();
150 	void initSplattedTextures();
151 	void initFactionTypes(GameSettings *gs);
152 	void initMinimap();
153 	void initUnits();
154 	void initMap();
155 	void initExplorationState();
156 
157 	//misc
158 	void tick();
159 	void computeFow();
160 	void exploreCells(const Vec2i &newPos, int sightRange, int teamIndex);
161 };
162 
163 }}//end namespace
164 
165 #endif
166