/* $Id: world.hpp,v 1.58.2.2 2006/01/20 11:33:53 chfreund Exp $ */ #ifndef _WORLD_HPP_ #define _WORLD_HPP_ #include #include #include #include "global.hpp" #include "serializable.hpp" #include "map.hpp" #include "particles.hpp" #include "player.hpp" #include "collidableobject.hpp" #include "noncollidableobject.hpp" #include "wopsprites.hpp" #include "scorekeeper.hpp" #include "bonusmanager.hpp" #include "messages.hpp" #include "noncollidablepool.hpp" struct EchoMessage; class SkwoermZone; ////////////////////////////////////////////////////////////////////// // A world base class ////////////////////////////////////////////////////////////////////// //! the world ... should beeeeee enough :) class World : public Serializable { private: //! \name members, that have to be serialized //@{ Map* m_map; //!< the world's map Particles m_particles; //!< the world's particles std::vector m_collidables; //!< the collidable objects BonusManager m_bonusmanager; //! manages the generation of bonus objects Scorekeeper m_scorekeeper; Uint32 m_frame; Random m_random; real m_szProbability; std::vector m_skwoermZones; //@} std::vector m_noncollidables; //!< the uncollidable objects NonCollidablePool m_noncollidablePool; std::vector m_player; //!< The players in the world //! pointer to the world's interface to WopSprites WopSpritesWorldInterface* m_spriteInterface; Uint8 m_nextPlayerID; CollidableObject* m_focusedObject; //! a mutex for controlling access to the world SDL_mutex* m_mutex; public: World( int sizeX = 0, int sizeY = 0 ); virtual ~World(); //! \name creation and preparation //@{ //! creates the world from the themes specified in the settings /*! Uses the singleton object of type WopSettings to receive the * name of the theme. * \return true, if the creation was successful, otherwise false */ bool createFromTheme( void ); //! creates a map from exactly four image files bool createFromThemeMaps( const String& background, const String& diggable, const String& undiggable, const String& indestructible ); /*! Optimizes the map for displaying on the client * \param format Display format of the client * \param flags Flags for the SDL Surface */ void setupGameMode( void ); void optimizeForClient( SDL_PixelFormat* format, Uint32 flags ); //@} //! \name member access //@{ //! returns the frame counter Uint32 getFrame() const { return m_frame; } //! returns a pointer to the map Map* getMap() { return m_map; } //! returns the particle object Particles* getParticles() { return &m_particles; } //! returns vector of collidable objects std::vector* getCollidables() { return &m_collidables; } //! returns a collidable object at the passed position, if there is one CollidableObject* getCollidableAt( const int x, const int y ); //! returns vector of non-collidable objects std::vector* getNoncollidables() { return &m_noncollidables; } //! returns a reference to the player list const std::vector& getPlayerList() const { return m_player; } //! returns a pointer to the world's bonus manager BonusManager* getBonusManager() { return &m_bonusmanager; } Scorekeeper* getScorekeeper() { return &m_scorekeeper; } Random& getRandom() { return m_random; } //@} //! \name sprite interface //@{ //! returns the sprite interface const WopSpritesWorldInterface* getSpriteInterface() const { return m_spriteInterface; } //! sets the sprite interface of the world void setSpriteInterface( WopSpritesWorldInterface* const sif ) { m_spriteInterface = sif; } //@} //! \name administration of players //@{ //! adds the player and its avatar in the world /*! Adds the passed player in the world. This causes the world * to create an avatar and attach it to the player. This should * be the only way for a player to enter class World. The player * leaves the world with a call of removePlayer. addPlayer is * called * - on the client, if a new client connects and adds a new * player. Then the new Player object enters the world via * this function called on the client. * - on the server, if a new client connects. Then the server * adds the new player to the world, kept on the server, by * a call of addPlayer. After that the server's world is sent * to the new client. */ void addPlayer( Player* p ); //! removes the player and its avatar from the world /*! Removes the player with the given id from the world. This means, * that also its avatar is removed from the world and detached from * the Player object. */ void removePlayer( const Uint8 id ); Player* getPlayer( const Uint8 c ) const { return m_player[c]; } Player* getPlayerByID( const Uint8 id ) const; Uint8 getPlayerIndex( const Uint8 id ) const; Uint8 getNumberPlayers() const { return m_player.size(); } Uint8 getNextPlayerID() { //return m_nextPlayerID++; for( Uint8 p = FIRST_VALID_PLAYER_ID; p <= LAST_VALID_PLAYER_ID; p++ ) { if( getPlayerByID( p ) == NULL ) return p; } ASSERT( false, "World::getNextPlayerID: no free player IDs\n" ); return 255; } std::vector& getAllPlayers() { return m_player; } //@} //! \name administration of objects in the world //@{ //! returns pointer to a new object of passed type Object* newObject( const int objectID ); //! seeks the object in the world and deletes it void deleteObject( Object* const object ); //! unhooks object from the worlds administration void unhookObject( const Object* const object ); //@} void scanObjectsForFocus( void ); const CollidableObject* getFocusedObject( void ) const { return m_focusedObject; } void focusNextAvatar( void ); //! \name procedures for "driving" the game //@{ void incrementFrame() { m_frame++; } void applyEvents( const EchoMessage* echoMessage ); void damageObject( const Particles::ParticleData& p ); void revampSkwoermZone( const unsigned int localSZ ); void doTimestep(); real getDT( const Vector& pos ) const; //@} /*! Draws the part of the world specified by \em mapRect on the given * surface using the sprite interface \em sprites */ void draw( SDL_Surface* surface, ClientWopSprites& sprites, SDL_Rect& mapRect ) const; //! \name (de)serialization //@{ virtual Uint32 getSerializeBufferSize() const; virtual void serialize( Uint8*& bufferPointer ) const; virtual void deserialize( Uint8*& bufferPointer ); // static World* createAndDeserialize( Uint8*& bufferPointer ); //@} //! cleans the contents of the world completely void reset(); void setMessageSink( MessageSink* sink ) { m_scorekeeper.setMessageSink( sink ); } void dump(); void dump( std::ostream& out ) const; void printPlayerVectorInfo( void ); }; // World #endif // _WORLD_HPP_