about summary refs log tree commit diff
path: root/src/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/game')
-rw-r--r--src/game/server/entities/character.cpp3
-rw-r--r--src/game/server/entities/character.hpp13
-rw-r--r--src/game/server/entity.hpp46
-rw-r--r--src/game/server/gamecontext.cpp15
-rw-r--r--src/game/server/gamecontext.hpp1
-rw-r--r--src/game/server/hooks.cpp2
-rw-r--r--src/game/server/player.cpp10
-rw-r--r--src/game/server/player.hpp3
8 files changed, 64 insertions, 29 deletions
diff --git a/src/game/server/entities/character.cpp b/src/game/server/entities/character.cpp
index 60acdca8..e79962c2 100644
--- a/src/game/server/entities/character.cpp
+++ b/src/game/server/entities/character.cpp
@@ -32,6 +32,9 @@ static INPUT_COUNT count_input(int prev, int cur)
 	return c;
 }
 
+
+MACRO_ALLOC_POOL_ID_IMPL(CHARACTER, MAX_CLIENTS)
+
 // player
 CHARACTER::CHARACTER()
 : ENTITY(NETOBJTYPE_CHARACTER)
diff --git a/src/game/server/entities/character.hpp b/src/game/server/entities/character.hpp
index 9bd441ba..c25b4b5f 100644
--- a/src/game/server/entities/character.hpp
+++ b/src/game/server/entities/character.hpp
@@ -11,19 +11,8 @@
 
 class CHARACTER : public ENTITY
 {
-	/*static CHARACTER pool_data[MAX_CLIENTS];
-	static int pool_used[MAX_CLIENTS];*/
+	MACRO_ALLOC_POOL_ID()
 public:
-/*
-	void operator delete(void *character)
-	{
-		(CHARACTER *)character 
-		int id = (CHARACTER *)character - (CHARACTER *)pool_data;
-		dbg_assert(pool_used[id], "");
-		pool_used[id] = 0;
-		mem_zero(&pool_data[id], sizeof(CHARACTER));
-	}*/
-
 	// player controlling this character
 	class PLAYER *player;
 	
diff --git a/src/game/server/entity.hpp b/src/game/server/entity.hpp
index d3ae3a1c..8ccb2d9a 100644
--- a/src/game/server/entity.hpp
+++ b/src/game/server/entity.hpp
@@ -1,14 +1,59 @@
 #ifndef GAME_SERVER_ENTITY_H
 #define GAME_SERVER_ENTITY_H
 
+#include <new>
 #include <base/vmath.hpp>
 
+#define MACRO_ALLOC_HEAP() \
+	public: \
+	void *operator new(size_t size) \
+	{ \
+		void *p = mem_alloc(size, 1); \
+		/*dbg_msg("", "++ %p %d", p, size);*/ \
+		mem_zero(p, size); \
+		return p; \
+	} \
+	void operator delete(void *p) \
+	{ \
+		/*dbg_msg("", "-- %p", p);*/ \
+		mem_free(p); \
+	} \
+	private:
+
+#define MACRO_ALLOC_POOL_ID() \
+	public: \
+	void *operator new(size_t size, int id); \
+	void operator delete(void *p); \
+	private:
+	
+#define MACRO_ALLOC_POOL_ID_IMPL(POOLTYPE, poolsize) \
+	static char pool_data_##POOLTYPE[poolsize][sizeof(POOLTYPE)] = {{0}}; \
+	static int pool_used_##POOLTYPE[poolsize] = {0}; \
+	void *POOLTYPE::operator new(size_t size, int id) \
+	{ \
+		dbg_assert(sizeof(POOLTYPE) == size, "size error"); \
+		dbg_assert(!pool_used_##POOLTYPE[id], "already used"); \
+		/*dbg_msg("pool", "++ %s %d", #POOLTYPE, id);*/ \
+		pool_used_##POOLTYPE[id] = 1; \
+		mem_zero(pool_data_##POOLTYPE[id], size); \
+		return pool_data_##POOLTYPE[id]; \
+	} \
+	void POOLTYPE::operator delete(void *p) \
+	{ \
+		int id = (POOLTYPE*)p - (POOLTYPE*)pool_data_##POOLTYPE; \
+		dbg_assert(pool_used_##POOLTYPE[id], "not used"); \
+		/*dbg_msg("pool", "-- %s %d", #POOLTYPE, id);*/ \
+		pool_used_##POOLTYPE[id] = 0; \
+		mem_zero(pool_data_##POOLTYPE[id], sizeof(POOLTYPE)); \
+	}
+	
 /*
 	Class: Entity
 		Basic entity class.
 */
 class ENTITY
 {
+	MACRO_ALLOC_HEAP()
 private:
 	friend class GAMEWORLD; // thy these?
 	ENTITY *prev_entity;
@@ -21,7 +66,6 @@ protected:
 	int id;
 	int objtype;
 public:
-	
 	ENTITY(int objtype);
 	virtual ~ENTITY();
 	
diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp
index 7866e0f0..987ce64e 100644
--- a/src/game/server/gamecontext.cpp
+++ b/src/game/server/gamecontext.cpp
@@ -7,15 +7,13 @@ GAMECONTEXT game;
 GAMECONTEXT::GAMECONTEXT()
 {
 	for(int i = 0; i < MAX_CLIENTS; i++)
-	{
 		players[i] = 0;
-		/*players = new PLAYER();
-		players[i].init(-1);*/
-	}
 }
 
 GAMECONTEXT::~GAMECONTEXT()
 {
+	for(int i = 0; i < MAX_CLIENTS; i++)
+		delete players[i];
 }
 
 void GAMECONTEXT::clear()
@@ -23,15 +21,6 @@ void GAMECONTEXT::clear()
 	this->~GAMECONTEXT();
 	mem_zero(this, sizeof(*this));
 	new (this) GAMECONTEXT();
-	// reset all players
-	/*
-	for(int i = 0; i < MAX_CLIENTS; i++)
-		players[i].init(-1);
-	
-	world.~GAMEWORLD();
-	mem_zero(&world, sizeof(world));
-	world.GAMEWORLD();
-	*/
 }
 
 
diff --git a/src/game/server/gamecontext.hpp b/src/game/server/gamecontext.hpp
index 4c7079c8..dd10ec4d 100644
--- a/src/game/server/gamecontext.hpp
+++ b/src/game/server/gamecontext.hpp
@@ -27,7 +27,6 @@
 			All players (PLAYER::snap)
 
 */
-
 class GAMECONTEXT
 {
 public:
diff --git a/src/game/server/hooks.cpp b/src/game/server/hooks.cpp
index 0cd6d6e1..494f4f62 100644
--- a/src/game/server/hooks.cpp
+++ b/src/game/server/hooks.cpp
@@ -73,7 +73,7 @@ void mods_client_enter(int client_id)
 
 void mods_connected(int client_id)
 {
-	game.players[client_id] = new PLAYER(client_id);
+	game.players[client_id] = new(client_id) PLAYER(client_id);
 	//game.players[client_id].init(client_id);
 	//game.players[client_id].client_id = client_id;
 	
diff --git a/src/game/server/player.cpp b/src/game/server/player.cpp
index a31cf21c..252e23e3 100644
--- a/src/game/server/player.cpp
+++ b/src/game/server/player.cpp
@@ -5,12 +5,20 @@
 #include "player.hpp"
 #include "gamecontext.hpp"
 
+MACRO_ALLOC_POOL_ID_IMPL(PLAYER, MAX_CLIENTS)
+
 PLAYER::PLAYER(int client_id)
 {
 	character = 0;
 	this->client_id = client_id;
 }
 
+PLAYER::~PLAYER()
+{
+	delete character;
+	character = 0;
+}
+
 /*
 void PLAYER::init(int client_id)
 {
@@ -168,7 +176,7 @@ void PLAYER::try_respawn()
 	if(num_ents == 0)
 	{
 		spawning = false;
-		character = new CHARACTER();
+		character = new(client_id) CHARACTER();
 		character->spawn(this, spawnpos, team);
 	}
 }
diff --git a/src/game/server/player.hpp b/src/game/server/player.hpp
index 9836bffc..c92265a8 100644
--- a/src/game/server/player.hpp
+++ b/src/game/server/player.hpp
@@ -7,9 +7,12 @@
 // player object
 class PLAYER
 {
+	MACRO_ALLOC_POOL_ID()
+private:
 	CHARACTER *character;
 public:
 	PLAYER(int client_id);
+	~PLAYER();
 
 	// TODO: clean this up
 	char skin_name[64];