This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseentity.cpp
266 lines (257 loc) · 8.49 KB
/
baseentity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "baseentity.h"
#include "engine.h"
#include "utils.h"
#include "player.h"
#include <iostream> // std::cout
#include <algorithm> // std::count
std::vector<_BaseEntityInfo> baseentityinf;
bool CBaseEntity::IsItemOnBaseEntityTable(int edict) {
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); it++) {
if(it->edictptr == edict) {
return true;
}
}
return false;
}
int CBaseEntity::GetBaseEntityTableIndex(int edict) {
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); it++) {
if(it->edictptr == edict) {
return i;
}
i++;
}
return -1;
}
void CBaseEntity::AddEntityToTable(int edict, const char* entityname, int owneredict) {
_BaseEntityInfo baseentity;
memset(&baseentity, 0, sizeof(BaseEntityInfo));
Utils *utils = new Utils();
baseentity.edictptr = edict;
baseentity.owneredict = owneredict;
sprintf(baseentity.entityname, "%s", entityname);
baseentityinf.push_back(baseentity);
delete utils;
}
void CBaseEntity::RemoveEntitiesByOwner( int owneredict ) { //Removes entities by the owner of it
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); ) {
if(it->owneredict == owneredict) {
StopFollowingEntity(it->edictptr);
SUB_RemoveEx(it->edictptr);
printf("CBaseEntity::RemoveEntitiesByOwner: Removed 0x%.8X edict from table with index %d!\n", it->edictptr, i);
it = baseentityinf.erase(it);
} else {
++it;
}
i++;
}
}
void CBaseEntity::RemoveEntitiesByOwnerAndName( int owneredict, const char* szName ) { //Removes entities by the owner of it, including the name of the entity
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end();) {
if(it->owneredict == owneredict) {
if(CountEntitiesByOwner( owneredict, szName, false ) != 0) {
StopFollowingEntity(it->edictptr);
SUB_RemoveEx(it->edictptr);
printf("CBaseEntity::RemoveEntitiesByOwnerAndName: Removed 0x%.8X edict from table with index %d!\n", it->edictptr, i);
it = baseentityinf.erase(it);
}
} else {
++it;
}
i++;
}
}
void CBaseEntity::RemoveAllEntitiesFromTable( void ) {
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); it++) {
StopFollowingEntity(it->edictptr);
SUB_RemoveEx(it->edictptr);
printf("CBaseEntity::RemoveAllEntitiesFromTable: Removed 0x%.8X edict from table with index %d!\n", it->edictptr, i);
baseentityinf.erase(it);
i++;
}
}
void CBaseEntity::ShowEntityTable ( void ) {
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); it++) {
printf("Index: %d Edict: 0x%.8X Owner: 0x%.8X\n", i, it->edictptr, it->owneredict);
i++;
}
}
void CBaseEntity::OnServerChangeMap( void ) {
//We'll call this function from OnServerChangeMap
RemoveAllEntitiesFromTable();
}
void CBaseEntity::EntitiesOnClientDisconnect(int playerid) {
CBasePlayer *pPlayer = new CBasePlayer;
int edictptr;
edictptr = pPlayer->GetEdictNum(playerid);
if(!edictptr)
return;
if(CountEntitiesByOwner(edictptr, "", true) != 0) {
RemoveEntitiesByOwner(edictptr);
}
ShowEntityTable();
delete pPlayer;
}
void CBaseEntity::RemoveEntityFromTable( int edict ) {
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end();) {
if(it->edictptr == edict) {
printf("CBaseEntity::RemoveEntityFromTable: Removed 0x%.8X edict from table with index %d!\n", edict, i);
SUB_RemoveEx(edict);
baseentityinf.erase(it);
} else {
++it;
}
i++;
}
}
int CBaseEntity::FindRandomEntityPointer() {
int entityptr = FindEntityByClassname(0, "info_player_start");
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "game_player_equip"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "func_wall"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "func_breakable"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "func_button"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "item_breakable"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "item_grappletarget"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "env_sprite"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "trigger_multiple"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "trigger_once"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "func_ladder"); }
if ( entityptr == 0 ) { entityptr = FindEntityByClassname(0, "weapon_dukes"); }
//new string[128];
//strformat(string, sizeof(string), false, "Entity Ptr: %d", entityptr);
//print(string);
return entityptr;
}
int CBaseEntity::CreateEntityEx(const char *szName, int Origin, int Angles, int owneredict) {
int ptr = CreateEntity(szName, Origin, Angles, owneredict);
AddEntityToTable(ptr, szName, owneredict);
return ptr;
}
int CBaseEntity::CreateSpectatorEntity(const char *szName, int Origin, int Angles, int owneredict, int spectatoredict) {
engineModule *engine = new engineModule;
int ptr = CreateEntity(szName, Origin, Angles, owneredict);
if ( !ptr ){
delete engine;
return 0;
}
int edictptr = engine->EDICT_NUM(ptr);
AddEntityToTable(edictptr, szName, spectatoredict); //Because the spectator is the "owner" of the entity attached to that specific player
delete engine;
return ptr;
}
int CBaseEntity::CountEntitiesByOwner( int owneredict, const char *szName, bool all ) { //Counts the amount of entities someone owns (if they're on the table)
int i = 0;
for (std::vector<_BaseEntityInfo>::iterator it = baseentityinf.begin(); it != baseentityinf.end(); it++) {
if(it->owneredict == owneredict) {
if(!all && strcmp(szName, it->entityname)) { //If we're searching for how much entities they have based on name
i++; //Increment
continue; //Continue to the next loop
}
i++; //We're searching for all, this will only be reached if all is set to 1
}
}
return i;
}
void CBaseEntity::SUB_RemoveEx(int edictptr) {
if(FNullEnt(edictptr) != 0) {
UTIL_Remove(edictptr);
}
}
void CBaseEntity::StopFollowingEntity(int edictptr) {
int newentptr = Instance(edictptr);
if ( newentptr == 0) return;
newentptr = ReadInt32(newentptr + 0x4);
WriteInt32((newentptr + 0x108), MOVETYPE_NONE ); //set the entity's movetype
WriteInt32((newentptr + 0x10C), NULL); //solid flags
WriteInt32((newentptr + 0x194), NULL); //aiment Follow
WriteInt32((newentptr + 0x198), NULL); //Owner
}
/* -------------------------------------------------- Assembly / Game Engine Calls -------------------------------------------------- */
int CBaseEntity::CreateEntity(const char *szName, int Origin, int Angles, int owneredict) {
DWORD adr = ADR_CreateEntity;
int newptr;
__asm{
push owneredict
push Angles
push Origin
push szName
call adr
mov newptr, eax
add esp, 0x10
}
return newptr;
}
int CBaseEntity::FindEntityByX(const char *szName, const char *classtype, int pStartEntity) { //classname, targetname, globalname, ?
int edictptr = 0;
DWORD adr = ADR_FindEntityByX;
__asm push szName
__asm push classtype
__asm push pStartEntity
__asm call adr
__asm add esp, 0x0C
__asm mov edictptr, eax
return edictptr;
}
int CBaseEntity::FindEntityByClassname( int pStartEntity, const char *szName ) {
int edictptr = 0;
DWORD adr = ADR_FindEntityByClassname;
__asm push szName
__asm push pStartEntity
__asm call adr
__asm mov edictptr, eax
__asm add esp, 0x8
return edictptr;
}
void CBaseEntity::SUB_Remove(DWORD entityptr) {
DWORD adr;
adr = ADR_SUBREMOVE;
__asm mov ecx, entityptr
__asm call adr
//__asm add esp, 4
}
void CBaseEntity::UTIL_Remove(DWORD edictptr) {
DWORD adr;
adr = ADR_UTIL_REMOVE;
int entptr = Instance(edictptr);
__asm {
push entptr
call adr
add esp, 0x4
}
}
int CBaseEntity::GetBaseEntity(int edictptr){
int base = GET_BASE_ENTITY;
__asm {
push edictptr
call base
mov base, eax
add esp, 0x4
}
return base;
}
int CBaseEntity::Instance(int edictptr){
//int inst;
//__asm mov ebx, edictptr
//__asm mov eax, dword ptr ds:[ebx + 0x11c]
//__asm mov inst, eax
if(!edictptr)
return 0;
return ReadInt32(edictptr + 0x11C); //retrieves entity pointer for this edict
}
int CBaseEntity::FNullEnt(int edictptr){ //Is this entity's edict null? returns 0 if null
//MOV EAX,DWORD PTR SS:[entityptr]
//SUB EAX,DWORD PTR DS:[44A7B9A4]
int isnull = FNULLENT;
__asm {
push edictptr
call isnull
mov isnull, eax
add esp, 0x4
}
return isnull;
}