about summary refs log tree commit diff
path: root/src/game/server/gs_common.hpp
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2008-07-06 11:21:21 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2008-07-06 11:21:21 +0000
commit9d632dd826c8a312095de0f56df66b2580d336cb (patch)
tree3fdde543c94323d6c698d278a58bf18e3c385776 /src/game/server/gs_common.hpp
parent3705064b109580103a3d13f44693503da9927281 (diff)
downloadzcatch-9d632dd826c8a312095de0f56df66b2580d336cb.tar.gz
zcatch-9d632dd826c8a312095de0f56df66b2580d336cb.zip
major update. server clean up and much added documentation
Diffstat (limited to 'src/game/server/gs_common.hpp')
-rw-r--r--src/game/server/gs_common.hpp411
1 files changed, 322 insertions, 89 deletions
diff --git a/src/game/server/gs_common.hpp b/src/game/server/gs_common.hpp
index b6c809fa..ff939116 100644
--- a/src/game/server/gs_common.hpp
+++ b/src/game/server/gs_common.hpp
@@ -2,16 +2,15 @@
 #include "../g_game.hpp"
 #include "../generated/gs_data.hpp"
 
-
 extern TUNING_PARAMS tuning;
 
-void create_sound_global(int sound, int target=-1);
-
 inline int cmask_all() { return -1; }
 inline int cmask_one(int cid) { return 1<<cid; }
 inline int cmask_all_except_one(int cid) { return 0x7fffffff^cmask_one(cid); }
 inline bool cmask_is_set(int mask, int cid) { return (mask&cmask_one(cid)) != 0; }
 
+
+
 //
 class EVENT_HANDLER
 {
@@ -33,57 +32,94 @@ public:
 	void snap(int snapping_client);
 };
 
-extern EVENT_HANDLER events;
-
-// a basic entity
+/*
+	Class: Entity
+		Basic entity class.
+*/
 class ENTITY
 {
 private:
 	friend class GAMEWORLD; // thy these?
-	friend class PLAYER;
 	ENTITY *prev_entity;
 	ENTITY *next_entity;
 
 	ENTITY *prev_type_entity;
 	ENTITY *next_type_entity;
 protected:
+	bool marked_for_destroy;
 	int id;
-public:
-	float proximity_radius;
-	unsigned flags;
 	int objtype;
-	vec2 pos;
-
-	enum
-	{
-		FLAG_DESTROY=0x00000001,
-		FLAG_PHYSICS=0x00000002,
-	};
+public:
 	
 	ENTITY(int objtype);
 	virtual ~ENTITY();
 	
-	virtual void reset() {}
-	virtual void post_reset() {}
-	
-	void set_flag(unsigned flag) { flags |= flag; }
-	void clear_flag(unsigned flag) { flags &= ~flag; }
+	ENTITY *typenext() { return next_type_entity; }
+	ENTITY *typeprev() { return prev_type_entity; }
 
+	/*
+		Function: destroy
+		Destorys the entity.
+	*/
 	virtual void destroy() { delete this; }
+		
+	/*
+		Function: reset
+		Called when the game resets the map. Puts the entity
+		back to it's starting state or perhaps destroys it.
+	*/
+	virtual void reset() {}
+	
+	/*
+		Function: tick
+		Called progress the entity to the next tick. Updates
+		and moves the entity to it's new state and position.
+	*/
 	virtual void tick() {}
+
+	/*
+		Function: tick
+		Called after all entities tick() function has been called.
+	*/
 	virtual void tick_defered() {}
+	
+	/*
+		Function: snap
+		Called when a new snapshot is being generated for a specific
+		client.
 		
+		Arguments:
+			snapping_client - ID of the client which snapshot is
+				being generated. Could be -1 to create a complete
+				snapshot of everything in the game for demo
+				recording.
+	*/
 	virtual void snap(int snapping_client) {}
-		
-	virtual bool take_damage(vec2 force, int dmg, int from, int weapon) { return true; }
+
+	/*
+		Variable: proximity_radius
+		Contains the physical size of the entity.
+	*/
+	float proximity_radius;
+	
+	/*
+		Variable: pos
+		Contains the current posititon of the entity.
+	*/
+	vec2 pos;
 };
 
 
+/*
+	Class: Game World
+		Tracks all entities in the game. Propagates tick and
+		snap calls to all entities.
+*/
 class GAMEWORLD
 {
 	void reset();
 	void remove_entities();
-public:
+
 	enum
 	{
 		NUM_ENT_TYPES=10, // TODO: are more exact value perhaps? :)
@@ -92,30 +128,108 @@ public:
 	// TODO: two lists seams kinda not good, shouldn't be needed
 	ENTITY *first_entity;
 	ENTITY *first_entity_types[NUM_ENT_TYPES];
-	bool paused;
+
+public:
 	bool reset_requested;
-	
+	bool paused;
 	WORLD_CORE core;
 	
 	GAMEWORLD();
 	~GAMEWORLD();
-	int find_entities(vec2 pos, float radius, ENTITY **ents, int max);
-	int find_entities(vec2 pos, float radius, ENTITY **ents, int max, const int* types, int maxtypes);
-
-	void insert_entity(ENTITY *ent);
-	void destroy_entity(ENTITY *ent);
-	void remove_entity(ENTITY *ent);
 	
-	//
+	ENTITY *find_first() { return first_entity; }
+	ENTITY *find_first(int type);
+	
+	/*
+		Function: find_entities
+			Finds entities close to a position and returns them in a list.
+		Arguments:
+			pos - Position.
+			radius - How close the entities have to be.
+			ents - Pointer to a list that should be filled with the pointers
+				to the entities.
+			max - Number of entities that fits into the ents array.
+			type - Type of the entities to find. -1 for all types.
+		Returns:
+			Number of entities found and added to the ents array.
+	*/
+	int find_entities(vec2 pos, float radius, ENTITY **ents, int max, int type = -1);
+	
+	/*
+		Function: interserct_character
+			Finds the closest character that intersects the line.
+		Arguments:
+			pos0 - Start position
+			pos2 - End position
+			radius - How for from the line the character is allowed to be.
+			new_pos - Intersection position
+			notthis - Entity to ignore intersecting with
+		Returns:
+			Returns a pointer to the closest hit or NULL of there is no intersection.
+	*/
+	class CHARACTER *intersect_character(vec2 pos0, vec2 pos1, float radius, vec2 &new_pos, class ENTITY *notthis = 0);
+	
+	/*
+		Function: closest_character
+			Finds the closest character to a specific point.
+		Arguments:
+			pos - The center position.
+			radius - How far off the character is allowed to be
+			notthis - Entity to ignore
+		Returns:
+			Returns a pointer to the closest character or NULL if no character is close enough.
+	*/
+	class CHARACTER *closest_character(vec2 pos, float radius, ENTITY *notthis);
+
+	/*
+		Function: insert_entity
+			Adds an entity to the world.
+		Arguments:
+			entity - Entity to add
+	*/
+	void insert_entity(ENTITY *entity);
+
+	/*
+		Function: remove_entity
+			Removes an entity from the world.
+		Arguments:
+			entity - Entity to remove
+	*/
+	void remove_entity(ENTITY *entity);
+
+	/*
+		Function: destroy_entity
+			Destroys an entity in the world.
+		Arguments:
+			entity - Entity to destroy
+	*/
+	void destroy_entity(ENTITY *entity);
+	
+	/*
+		Function: snap
+			Calls snap on all the entities in the world to create
+			the snapshot.
+		Arguments:
+			snapping_client - ID of the client which snapshot
+			is being created.
+	*/
 	void snap(int snapping_client);
+	
+	/*
+		Function: tick
+			Calls tick on all the entities in the world to progress
+			the world to the next tick.
+		
+	*/
 	void tick();
 };
 
-extern GAMEWORLD *world;
-
-// game object
-// TODO: should change name of this one
-class GAMECONTROLLER : public ENTITY
+/*
+	Class: Game Controller
+		Controls the main game logic. Keeping track of team and player score,
+		winning conditions and specific game logic.
+*/
+class GAMECONTROLLER
 {
 protected:
 	void cyclemap();
@@ -145,25 +259,57 @@ public:
 	void endround();
 	
 	bool is_friendly_fire(int cid1, int cid2);
+
+	/*
 	
-	virtual bool on_entity(int index, vec2 pos);
-	
-	virtual void post_reset();
+	*/	
 	virtual void tick();
 	
-	virtual void on_player_spawn(class PLAYER *p) {}
-	virtual int on_player_death(class PLAYER *victim, class PLAYER *killer, int weapon);
+	virtual void snap(int snapping_client);
+	
+	/*
+		Function: on_entity
+			Called when the map is loaded to process an entity
+			in the map.
+		Arguments:
+			index - Entity index.
+			pos - Where the entity is located in the world.
+		Returns:
+			bool?
+	*/
+	virtual bool on_entity(int index, vec2 pos);
 	
+	/*
+		Function: on_character_spawn
+			Called when a character spawns into the game world.
+		Arguments:
+			chr - The character that was spawned.
+	*/
+	virtual void on_character_spawn(class CHARACTER *chr) {}
+	
+	/*
+		Function: on_character_death
+			Called when a character in the world dies.
+		Arguments:
+			victim - The character that died.
+			killer - The player that killed it.
+			weapon - What weapon that killed it. Can be -1 for undefined
+				weapon when switching team or player suicides.
+	*/
+	virtual int on_character_death(class CHARACTER *victim, class PLAYER *killer, int weapon);
+
 	virtual void on_player_info_change(class PLAYER *p);
+
+	/*
 	
-	virtual void snap(int snapping_client);
+	*/	
+	virtual const char *get_team_name(int team);
 	virtual int get_auto_team(int notthisid);
 	virtual bool can_join_team(int team, int notthisid);
 	int clampteam(int team);
-};
-
-extern GAMECONTROLLER *gamecontroller;
 
+	virtual void post_reset();
+};
 
 // TODO: move to seperate file
 class PICKUP : public ENTITY
@@ -221,25 +367,28 @@ class LASER : public ENTITY
 	float energy;
 	int bounces;
 	int eval_tick;
-	PLAYER *owner;
+	CHARACTER *owner;
 	
-	bool hit_player(vec2 from, vec2 to);
+	bool hit_character(vec2 from, vec2 to);
 	void do_bounce();
 	
 public:
 	
-	LASER(vec2 pos, vec2 direction, float start_energy, PLAYER *owner);
+	LASER(vec2 pos, vec2 direction, float start_energy, CHARACTER *owner);
 	
 	virtual void reset();
 	virtual void tick();
 	virtual void snap(int snapping_client);
 };
 
-// player entity
-class PLAYER : public ENTITY
+
+class CHARACTER : public ENTITY
 {
 public:
-	static const int phys_size = 28;
+	// player controlling this character
+	class PLAYER *player;
+	
+	bool alive;
 
 	// weapon info
 	ENTITY *hitobjects[10];
@@ -264,14 +413,13 @@ public:
 	int emote_type;
 	int emote_stop;
 
-	int last_action; // last tick that the player took any action ie some input
-	
-	//
-	int client_id;
+	// TODO: clean this up
 	char skin_name[64];
 	int use_custom_color;
 	int color_body;
 	int color_feet;
+	
+	int last_action; // last tick that the player took any action ie some input
 
 	// these are non-heldback inputs
 	NETOBJ_PLAYER_INPUT latest_previnput;
@@ -298,40 +446,19 @@ public:
 	} ninja;
 
 	//
-	int score;
+	//int score;
 	int team;
 	int player_state; // if the client is chatting, accessing a menu or so
-	
-	bool spawning;
-	bool dead;
-	int die_tick;
-	vec2 die_pos;
-	
-	// latency calculations
-	int latency_accum;
-	int latency_accum_min;
-	int latency_accum_max;
-	int latency_avg;
-	int latency_min;
-	int latency_max;
 
 	// the player core for the physics	
-	PLAYER_CORE core;
-	
-	//
-	int64 last_chat;
+	CHARACTER_CORE core;
 
 	//
-	PLAYER();
-	void init();
+	CHARACTER();
+	
 	virtual void reset();
 	virtual void destroy();
 		
-	void try_respawn();
-	void respawn();
-	
-	void set_team(int team);
-	
 	bool is_grounded();
 	
 	void set_weapon(int w);
@@ -341,17 +468,123 @@ public:
 	
 	int handle_weapons();
 	int handle_ninja();
-	
-	void on_direct_input(NETOBJ_PLAYER_INPUT *input);
+
+	void on_predicted_input(NETOBJ_PLAYER_INPUT *new_input);
+	void on_direct_input(NETOBJ_PLAYER_INPUT *new_input);
 	void fire_weapon();
 
+	void die(int killer, int weapon);
+
+	bool take_damage(vec2 force, int dmg, int from, int weapon);	
+
+	
+	bool spawn(PLAYER *player, vec2 pos, int team);
+	//bool init_tryspawn(int team);
+	bool remove();
+
+	static const int phys_size = 28;
+
 	virtual void tick();
 	virtual void tick_defered();
+	virtual void snap(int snaping_client);
 	
-	void die(int killer, int weapon);
+	bool increase_health(int amount);
+	bool increase_armor(int amount);
+};
+
+// player object
+class PLAYER
+{
+public:
+	PLAYER();
+
+	// TODO: clean this up
+	char skin_name[64];
+	int use_custom_color;
+	int color_body;
+	int color_feet;
 	
-	virtual bool take_damage(vec2 force, int dmg, int from, int weapon);
-	virtual void snap(int snaping_client);
+	//
+	bool spawning;
+	int client_id;
+	int team;
+	int score;
+
+	//
+	int64 last_chat;
+
+	// network latency calculations	
+	struct
+	{
+		int accum;
+		int accum_min;
+		int accum_max;
+		int avg;
+		int min;
+		int max;	
+	} latency;
+	
+	CHARACTER character;
+	
+	// this is used for snapping so we know how we can clip the view for the player
+	vec2 view_pos;
+
+	void init(int client_id);
+	
+	CHARACTER *get_character();
+	
+	void kill_character();
+
+	void try_respawn();
+	void respawn();
+	void set_team(int team);
+	
+	void tick();
+	void snap(int snaping_client);
+
+	void on_direct_input(NETOBJ_PLAYER_INPUT *new_input);
+	void on_predicted_input(NETOBJ_PLAYER_INPUT *new_input);
+	void on_disconnect();
+};
+
+class GAMECONTEXT
+{
+public:
+	GAMECONTEXT();
+	void clear();
+	
+	EVENT_HANDLER events;
+	PLAYER players[MAX_CLIENTS];
+	
+	GAMECONTROLLER *controller;
+	GAMEWORLD world;
+
+	void tick();
+	void snap(int client_id);
+
+	// helper functions
+	void create_damageind(vec2 p, float angle_mod, int amount);
+	void create_explosion(vec2 p, int owner, int weapon, bool bnodamage);
+	void create_smoke(vec2 p);
+	void create_playerspawn(vec2 p);
+	void create_death(vec2 p, int who);
+	void create_sound(vec2 pos, int sound, int mask=-1);
+	void create_sound_global(int sound, int target=-1);	
+
+	// network
+	void send_chat(int cid, int team, const char *text);
+	void send_emoticon(int cid, int emoticon);
+	void send_weapon_pickup(int cid, int weapon);
+	void send_broadcast(const char *text, int cid);
+	void send_info(int who, int to_who);
 };
 
-extern PLAYER *players;
+extern GAMECONTEXT game;
+
+enum
+{
+	CHAT_ALL=-2,
+	CHAT_SPEC=-1,
+	CHAT_RED=0,
+	CHAT_BLUE=1
+};