1 /*************************************************************************** 2 session.h - Game session manager 3 ------------------- 4 begin : Sat May 3 2003 5 copyright : (C) 2003 by Gabor Torok 6 email : cctorok@yahoo.com 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 #ifndef SESSION_H 19 #define SESSION_H 20 #pragma once 21 22 #include <vector> 23 #include <set> 24 #include "preferences.h" 25 #include "party.h" 26 #include "net/server.h" 27 #include "net/client.h" 28 #include "net/gamestatehandler.h" 29 #include "net/commands.h" 30 #include "gameadapter.h" 31 #include "terraingenerator.h" 32 #include "render/texture.h" 33 34 #ifdef HAVE_SDL_NET 35 class Server; 36 class Client; 37 #endif 38 class Mission; 39 class Board; 40 class Party; 41 class Map; 42 class Item; 43 class Creature; 44 class GameAdapter; 45 class Preferences; 46 class RpgItem; 47 class Spell; 48 class Characters; 49 class Character; 50 class Monster; 51 class GLShape; 52 class SqBinding; 53 class ShapePalette; 54 class Sound; 55 class Cutscene; 56 class TerrainGenerator; 57 58 /** 59 *@author Gabor Torok 60 */ 61 62 /// Manages a game session (basically the whole "real" ingame part). 63 class Session { 64 private: 65 Sound *sound; 66 ShapePalette *shapePal; 67 GameAdapter *adapter; 68 Party *party; 69 Map *map; 70 Board *board; 71 Cutscene *cutscene; 72 #ifdef HAVE_SDL_NET 73 Server *server; 74 Client *client; 75 #endif 76 bool multiplayerGame; 77 Mission *currentMission; 78 TextureData chapterImage; 79 Texture chapterImageTexture; 80 int chapterImageWidth, chapterImageHeight; 81 bool showChapterIntro; 82 std::vector<Item*> newItems; 83 std::vector<Creature*> creatures; 84 std::set<Creature*> nonVisibleCreatures; 85 86 SqBinding *squirrel; 87 std::map<RpgItem*, Item*> special; 88 std::string savegame; 89 std::string loadgame; 90 std::string savetitle; 91 std::string loadtitle; 92 char scoreid[40]; 93 bool autosave; 94 95 char interruptFunction[255]; 96 Characters* characters; 97 bool exiting; 98 99 // private constructor: call startGame instead. 100 Session( GameAdapter *adapter ); 101 102 int dataInitialized; 103 TerrainGenerator *terrainGenerator; 104 105 public: 106 107 enum { 108 NOT_INITIALIZED = 0, 109 INIT_STARTED, 110 INIT_DONE 111 }; 112 static Session *instance; 113 114 /** 115 * The main method for a project to run a game. 116 * Pass in a GameAdapter implementation (eg.: Scourge class) 117 * @return the value when the game exits. You can return this 118 * from your main() function back to the os. 119 */ 120 static int runGame( GameAdapter *adapter, int argc, char *argv[] ); 121 122 123 virtual ~Session(); 124 125 void initialize(); 126 setExiting(bool b)127 inline void setExiting( bool b ) { exiting = b; } isExiting()128 inline bool isExiting() { return exiting; } 129 130 virtual void start(); 131 virtual void quit( int value ); 132 #ifdef HAVE_SDL_NET 133 virtual void runClient( char const* host, int port, char const* userName ); 134 virtual void runServer( int port ); getServer()135 virtual inline Server *getServer() { 136 return server; 137 } getClient()138 virtual inline Client *getClient() { 139 return client; 140 } 141 virtual void startServer( GameStateHandler *gsh, int port ); 142 virtual void startClient( GameStateHandler *gsh, CommandInterpreter *ci, char const* host, int port, char const* username ); 143 virtual void stopClientServer(); 144 #endif 145 isMultiPlayerGame()146 inline bool isMultiPlayerGame() { 147 return multiplayerGame; 148 } setMultiPlayerGame(bool b)149 inline void setMultiPlayerGame( bool b ) { 150 multiplayerGame = b; 151 } getGameAdapter()152 inline GameAdapter *getGameAdapter() { 153 return adapter; 154 } 155 void playSound( const std::string& sound, int panning ); 156 std::string& getAmbientSoundName(); 157 setInterruptFunction(char * s)158 inline void setInterruptFunction( char *s ) { 159 strcpy( this->interruptFunction, s ); 160 } getInterruptFunction()161 inline char *getInterruptFunction() { 162 return this->interruptFunction; 163 } 164 165 /** 166 Creat a new item for use on this story. Calling this method instead of new Item() 167 directly ensures that the item will be cleaned up properly when the story is 168 exited. Only items in a party member's backpack are not deleted. 169 170 @param rpgItem if not NULL, the RpgItem template for the item to create. 171 @param spell if not NULL, the spell to associate with the created scroll. 172 @return the item created. 173 */ 174 virtual Item *newItem( RpgItem *rpgItem, int level = 1, Spell *spell = NULL, bool loading = false ); 175 176 virtual Item *addItemFromScript( char *name, int x, int y, int z, bool isContainer = false, int level = 1, int depth = 1 ); 177 178 /** 179 Create a new creature for use on this story. Calling this method instead of new Creature() 180 directly ensures that the creature will be cleaned up properly when the story is 181 exited. 182 183 @param character the character class to use for the new creature. 184 @param name the name of the new creature 185 @return the creature created. 186 */ 187 //virtual Creature *newCreature(Character *character, char *name); 188 189 /** 190 Create a new creature for use on this story. Calling this method instead of new Creature() 191 directly ensures that the creature will be cleaned up properly when the story is 192 exited. 193 194 @param monster the monster template to use for the new creature. 195 @return the creature created. 196 */ 197 virtual Creature *newCreature( Monster *monster, GLShape *shape, bool loaded = false ); 198 virtual Creature *newCreature( Character *character, char const* name, int sex, int model ); 199 virtual Creature *replaceCreature( Creature *creature, char *newCreatureType ); 200 virtual Creature *addCreatureFromScript( char *creatureType, int cx, int cy, int *fx = NULL, int *fy = NULL ); 201 virtual bool removeCreatureRef( Creature *creature, int index ); 202 virtual bool removeCreature( Creature *creature ); 203 virtual void addCreatureRef( Creature *creature, int index ); 204 void setVisible( Creature *creature, bool b ); 205 bool isVisible( Creature *creature ); 206 getCreatureCount()207 inline int getCreatureCount() { 208 return creatures.size(); 209 } getCreature(int index)210 inline Creature *getCreature( int index ) { 211 return creatures[index]; 212 } 213 Creature *getCreatureByName( char const* name ); getItemCount()214 inline int getItemCount() { 215 return newItems.size(); 216 } getItem(int index)217 inline Item *getItem( int index ) { 218 return newItems[index]; 219 } 220 virtual void deleteCreaturesAndItems( bool missionItemsOnly = false ); 221 getSound()222 inline Sound *getSound() { 223 return sound; 224 } getShapePalette()225 inline ShapePalette *getShapePalette() { 226 return shapePal; 227 } getMap()228 inline Map *getMap() { 229 return map; 230 } getBoard()231 inline Board *getBoard() { 232 return board; 233 } getParty()234 inline Party *getParty() { 235 return party; 236 } getPreferences()237 inline Preferences *getPreferences() { 238 return getGameAdapter()->getPreferences(); 239 } getCurrentMission()240 inline Mission *getCurrentMission() { 241 return currentMission; 242 } getCutscene()243 inline Cutscene *getCutscene() { 244 return cutscene; 245 } 246 void setCurrentMission( Mission *mission ); 247 void setChapterImage( char *image ); getChapterImage()248 inline TextureData const& getChapterImage() { 249 return chapterImage; 250 } getChapterImageTexture()251 inline Texture getChapterImageTexture() { 252 return chapterImageTexture; 253 } getChapterImageWidth()254 inline int getChapterImageWidth() { 255 return chapterImageWidth; 256 } getChapterImageHeight()257 inline int getChapterImageHeight() { 258 return chapterImageHeight; 259 } setShowChapterIntro(bool b)260 inline void setShowChapterIntro( bool b ) { 261 this->showChapterIntro = b; 262 } isShowingChapterIntro()263 inline bool isShowingChapterIntro() { 264 return showChapterIntro; 265 } 266 267 virtual Creature *getClosestMonster( int x, int y, int w, int h, int radius ); 268 virtual Creature *getClosestGoodGuy( int x, int y, int w, int h, int radius ); 269 virtual Creature *getRandomNearbyMonster( int x, int y, int w, int h, int radius ); 270 virtual Creature *getRandomNearbyGoodGuy( int x, int y, int w, int h, int radius ); 271 virtual void creatureDeath( Creature *creature ); 272 getSquirrel()273 inline SqBinding *getSquirrel() { 274 return squirrel; 275 } 276 setSpecialItem(RpgItem * rpgItem,Item * item)277 inline void setSpecialItem( RpgItem *rpgItem, Item *item ) { 278 special[ rpgItem ] = item; 279 } getSpecialItem(RpgItem * rpgItem)280 inline Item *getSpecialItem( RpgItem *rpgItem ) { 281 if ( special.find( rpgItem ) == special.end() ) return NULL; 282 else return special[ rpgItem ]; 283 } 284 285 /** 286 * How many times did this 'key' occur in the last hour? 287 * if withinLastHour is false, a day is used. 288 */ 289 int getCountForDate( char *key, bool withinLastHour = true ); 290 291 /** 292 * Store a key with data+value 293 */ 294 void setCountForDate( char *key, int value ); 295 296 void setSavegameName( std::string& s ); getSavegameName()297 inline std::string& getSavegameName() { 298 return savegame; 299 } setLoadgameName(const std::string & s)300 inline void setLoadgameName( const std::string& s ) { 301 loadgame = s; 302 } getLoadgameName()303 inline std::string& getLoadgameName() { 304 return loadgame; 305 } setLoadAutosave(bool b)306 inline void setLoadAutosave( bool b ) { 307 autosave = b; 308 } getLoadAutosave()309 inline bool getLoadAutosave() { 310 return autosave; 311 } 312 void setSavegameTitle( std::string& s ); getSavegameTitle()313 inline std::string& getSavegameTitle() { 314 return savetitle; 315 } setLoadgameTitle(const std::string & s)316 inline void setLoadgameTitle( const std::string& s ) { 317 loadtitle = s; 318 } getLoadgameTitle()319 inline std::string& getLoadgameTitle() { 320 return loadtitle; 321 } willLoadGame()322 inline bool willLoadGame() { 323 return( loadgame.length() ? true : false ); 324 } 325 getScoreid()326 inline char *getScoreid() { 327 return scoreid; 328 } 329 330 virtual void initData(); 331 virtual void doInitData(); isDataInitialized()332 inline bool isDataInitialized() { 333 return dataInitialized == INIT_DONE; 334 } 335 setTerrainGenerator(TerrainGenerator * tg)336 inline void setTerrainGenerator( TerrainGenerator *tg ) { 337 this->terrainGenerator = tg; 338 } getTerrainGenerator()339 inline TerrainGenerator *getTerrainGenerator() { 340 return this->terrainGenerator; 341 } 342 }; 343 344 #endif 345 346