Skip to content

Commit b856434

Browse files
committed
Pull entity manager from Sol project.
1 parent 8c0a8b9 commit b856434

16 files changed

+822
-151
lines changed

game/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1818
]]
1919

2020
add_library(chronon-game STATIC
21+
entity/entity.cpp
22+
entity/entity_manager.cpp
23+
entity/entity_player.cpp
24+
entity/entity_player_start.cpp
25+
2126
CinematicScript.cpp
2227

2328
g_ai.cpp

game/entity/entity.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <hogsy@oldtimes-software.com>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
20+
21+
#include "../g_local.h"
22+
23+
#include "entity.h"
24+
25+
Entity::Entity( edict_t *edict ) : edict( edict )
26+
{
27+
}
28+
29+
void Entity::SetModel( const std::string &path ) const
30+
{
31+
gi.setmodel( edict, path.c_str() );
32+
}
33+
34+
void Entity::SetSolid( const solid_t solid ) const
35+
{
36+
edict->solid = solid;
37+
}
38+
39+
void Entity::SetOrigin( const vec3_t &origin ) const
40+
{
41+
VectorCopy( edict->s.origin, edict->s.old_origin );
42+
VectorCopy( origin, edict->s.origin );
43+
}
44+
45+
void Entity::SetAngles( const vec3_t &angles ) const
46+
{
47+
VectorCopy( angles, edict->s.angles );
48+
}
49+
50+
void Entity::SetSize( const vec3_t &mins, const vec3_t &maxs ) const
51+
{
52+
VectorCopy( mins, edict->mins );
53+
VectorCopy( maxs, edict->maxs );
54+
}
55+
56+
void Entity::Link() const
57+
{
58+
gi.linkentity( edict );
59+
}
60+
61+
void Entity::Unlink() const
62+
{
63+
gi.unlinkentity( edict );
64+
}

game/entity/entity.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <hogsy@oldtimes-software.com>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
20+
#pragma once
21+
22+
#include "entity_manager.h"
23+
24+
#define IMPLEMENT_SUPER( PARENT ) typedef PARENT Super;
25+
#define IMPLEMENT_ENTITY( BASE, PARENT ) \
26+
IMPLEMENT_SUPER( PARENT ) \
27+
public: \
28+
const char *GetClassName() override { return #BASE; } \
29+
\
30+
private:
31+
32+
class Entity
33+
{
34+
public:
35+
explicit Entity( edict_t *edict );
36+
virtual ~Entity() = default;
37+
38+
/**
39+
* Returns the internal classname (not to be confused with edict classname!)
40+
*/
41+
virtual const char *GetClassName() = 0;
42+
43+
virtual void Spawn( const EntityManager::SpawnVariables &variables ) = 0;
44+
45+
void SetModel( const std::string &path ) const;
46+
void SetSolid( solid_t solid ) const;
47+
48+
void SetOrigin( const vec3_t &origin ) const;
49+
void SetAngles( const vec3_t &angles ) const;
50+
51+
void SetSize( const vec3_t &mins, const vec3_t &maxs ) const;
52+
53+
void Link() const;
54+
void Unlink() const;
55+
56+
protected:
57+
edict_t *edict{};
58+
59+
public:
60+
[[nodiscard]] edict_t *GetEdict() const
61+
{
62+
return edict;
63+
}
64+
};

game/entity/entity_manager.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <hogsy@oldtimes-software.com>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
20+
21+
#include "../g_local.h"
22+
23+
#include "entity_manager.h"
24+
#include "entity.h"
25+
26+
std::map< std::string, EntityManager::constructor > EntityManager::classRegistry __attribute__( ( init_priority( 1000 ) ) );
27+
28+
bool EntityManager::ParseSpawnVariables( const char **buf, SpawnVariables &variables )
29+
{
30+
while ( true )
31+
{
32+
const char *token = COM_Parse( buf );
33+
if ( *token == '}' )
34+
{
35+
break;
36+
}
37+
38+
if ( *buf == nullptr )
39+
{
40+
gi.dprintf( "EOF without closing brace!\n" );
41+
return false;
42+
}
43+
44+
std::string key = token;
45+
46+
token = COM_Parse( buf );
47+
if ( *buf == nullptr )
48+
{
49+
gi.dprintf( "EOF without closing brace!\n" );
50+
return false;
51+
}
52+
53+
if ( *token == '}' )
54+
{
55+
gi.dprintf( "Closing brace without data!\n" );
56+
return false;
57+
}
58+
59+
// keynames with a leading underscore are used for utility comments,
60+
// and are immediately discarded by quake
61+
if ( key[ 0 ] == '_' )
62+
{
63+
continue;
64+
}
65+
66+
variables[ key ] = { key, token };
67+
}
68+
69+
return !variables.empty();
70+
}
71+
72+
Entity *EntityManager::CreateEntity( edict_t *edict, const std::string &classname )
73+
{
74+
const auto spawn = classRegistry.find( classname );
75+
if ( spawn == classRegistry.end() )
76+
{
77+
return nullptr;
78+
}
79+
80+
return spawn->second( edict );
81+
}

game/entity/entity_manager.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/******************************************************************************
2+
Copyright © 1997-2001 Id Software, Inc.
3+
Copyright © 2020-2025 Mark E Sowden <hogsy@oldtimes-software.com>
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
as published by the Free Software Foundation; either version 2
8+
of the License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
14+
See the GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, write to the Free Software
18+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
******************************************************************************/
20+
#pragma once
21+
22+
#include <map>
23+
#include <memory>
24+
25+
class Entity;
26+
class EntityManager
27+
{
28+
protected:
29+
typedef Entity *( *constructor )( edict_t *edict );
30+
static std::map< std::string, constructor > classRegistry;
31+
32+
public:
33+
class ClassRegistration
34+
{
35+
public:
36+
ClassRegistration( const std::string &name, constructor constructor ) : name( name )
37+
{
38+
classRegistry[ name ] = constructor;
39+
}
40+
ClassRegistration() = delete;
41+
42+
private:
43+
std::string name;
44+
};
45+
46+
struct SpawnVariable
47+
{
48+
std::string key;
49+
std::string value;
50+
};
51+
typedef std::map< std::string, SpawnVariable > SpawnVariables;
52+
53+
static bool ParseSpawnVariables( const char **buf, SpawnVariables &variables );
54+
55+
static EntityManager *GetInstance()
56+
{
57+
static EntityManager instance;
58+
return &instance;
59+
}
60+
61+
static Entity *CreateEntity( edict_t *edict, const std::string &classname );
62+
63+
private:
64+
EntityManager() = default;
65+
~EntityManager() = default;
66+
};
67+
68+
#define REGISTER_ENTITY_CLASS( NAME, CLASS ) \
69+
static Entity *NAME##_make( edict_t *edict ) { return new CLASS( edict ); } \
70+
static EntityManager::ClassRegistration __attribute__( ( init_priority( 2000 ) ) ) \
71+
_register_entity_##NAME##_name( #NAME, NAME##_make );

0 commit comments

Comments
 (0)