-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathscrtimers.cpp
302 lines (264 loc) · 7.25 KB
/
scrtimers.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
SA:MP Multiplayer Modification
Copyright 2004-2006 SA:MP Team
file:
scrtimers.cpp
desc:
Gamemode script timers.
Version: $Id: scrtimers.cpp,v 1.7 2006/03/25 09:12:48 kyeman Exp $
*/
//----------------------------------------------------------------------------------
#include "main.h"
//----------------------------------------------------------------------------------
CScriptTimers::CScriptTimers()
{
m_dwTimerCount = 0;
}
//----------------------------------------------------------------------------------
CScriptTimers::~CScriptTimers()
{
DwordTimerMap::iterator itor;
for (itor = m_Timers.begin(); itor != m_Timers.end(); itor++)
{
FreeMem(itor->second);
SAFE_DELETE(itor->second);
}
m_Timers.clear();
}
//----------------------------------------------------------------------------------
void CScriptTimers::FreeMem(ScriptTimer_s* Timer)
{
if (Timer->cellParams != NULL)
{
free(Timer->cellParams);
Timer->cellParams = NULL;
}
}
//----------------------------------------------------------------------------------
// Kills only the timers found in one mode
void CScriptTimers::DeleteForMode(AMX* pEndedAMX)
{
bool bRestart = false;
DwordTimerMap::iterator itor;
for (itor = m_Timers.begin(); itor != m_Timers.end(); bRestart?(itor=m_Timers.begin()):(itor++))
{
bRestart = false;
if (itor->second->pAMX == pEndedAMX)
{
FreeMem(itor->second);
SAFE_DELETE(itor->second);
m_Timers.erase(itor);
// Can't continue iteration if a node is deleted, start iteration from start again.
bRestart = true;
}
}
}
//----------------------------------------------------------------------------------
DWORD CScriptTimers::New(char* szScriptFunc, int iInterval, bool bRepeating, AMX* pAMX)
{
m_dwTimerCount++;
ScriptTimer_s* NewTimer = new ScriptTimer_s;
//if(iInterval < 500) iInterval = 500;
strncpy(NewTimer->szScriptFunc, szScriptFunc, 255);
NewTimer->iTotalTime = iInterval;
NewTimer->iRemainingTime = iInterval;
NewTimer->bRepeating = bRepeating;
NewTimer->iParamCount = 0;
NewTimer->bKilled = false;
NewTimer->pAMX = pAMX;
NewTimer->cellParams = NULL;
// Checks if it's called from a filterscript, if not, mark it for destruction at gamemode end
/*if (pAMX == pNetGame->GetGameMode()->GetGameModePointer())
{
NewTimer->bFilterscript = false;
//print("GM");
}
else
{
NewTimer->bFilterscript = true;
//print("FS");
}*/
m_Timers.insert(DwordTimerMap::value_type(m_dwTimerCount, NewTimer));
return m_dwTimerCount;
}
//----------------------------------------------------------------------------------
// Same as new only with parameters to be passed to the called function
cell* get_amxaddr(AMX *amx, cell amx_addr);
DWORD CScriptTimers::NewEx(char* szScriptFunc, int iInterval, bool bRepeating, cell *params, AMX* pAMX)
{
m_dwTimerCount++;
ScriptTimer_s* NewTimer = new ScriptTimer_s;
strncpy(NewTimer->szScriptFunc, szScriptFunc, 255);
NewTimer->iTotalTime = iInterval;
NewTimer->iRemainingTime = iInterval;
NewTimer->bRepeating = bRepeating;
NewTimer->bKilled = false;
NewTimer->pAMX = pAMX;
cell amx_addr[256];
char* szParamList;
amx_StrParam(pAMX, params[4], szParamList);
int j, numstr, iOff = 5; // Count, func, interval, repeat, map
if (szParamList == NULL) j = 0;
else j = strlen(szParamList);
numstr = 0;
while (j)
{
j--;
cell *paddr = NULL;
if (*(szParamList + j) == 'a')
{
int numcells = *get_amxaddr(pAMX, params[j + iOff + 1]);
if (amx_Allot(pAMX, numcells, &amx_addr[numstr], &paddr) == AMX_ERR_NONE)
{
memcpy(paddr, get_amxaddr(pAMX, params[j + iOff]), numcells * sizeof (cell));
numstr++;
}
}
else if (*(szParamList + j) == 's')
{
char* szParamText;
amx_StrParam(pAMX, params[j + iOff], szParamText);
if (szParamText != NULL && strlen(szParamText) > 0)
{
int numcells = strlen(szParamText) + 1;
if (amx_Allot(pAMX, numcells, &amx_addr[numstr], &paddr) == AMX_ERR_NONE)
{
amx_SetString(paddr, szParamText, 0, 0, UNLIMITED);
numstr++;
}
}
else
{
*szParamText = 1;
*(szParamText + 1) = 0;
if (amx_Allot(pAMX, 1, &amx_addr[numstr], &paddr) == AMX_ERR_NONE)
{
amx_SetString(paddr, szParamText, 0, 0, UNLIMITED);
numstr++;
}
}
}
else
{
amx_addr[numstr] = *get_amxaddr(pAMX, params[j + iOff]);
numstr++;
}
}
void* mem = NULL;
if (numstr)
{
mem = malloc(numstr * sizeof (cell));
memcpy(mem, &amx_addr, numstr * sizeof (cell));
NewTimer->cellParams = mem;
}
else
{
NewTimer->cellParams = NULL;
}
NewTimer->iParamCount = numstr;
m_Timers.insert(DwordTimerMap::value_type(m_dwTimerCount, NewTimer));
return m_dwTimerCount;
}
//----------------------------------------------------------------------------------
void CScriptTimers::Delete(DWORD dwTimerId)
{
DwordTimerMap::iterator itor;
itor = m_Timers.find(dwTimerId);
if (itor != m_Timers.end())
{
FreeMem(itor->second);
SAFE_DELETE(itor->second);
m_Timers.erase(itor);
//Delete(itor->first);
}
}
//----------------------------------------------------------------------------------
void CScriptTimers::Kill(DWORD dwTimerId)
{
DwordTimerMap::iterator itor;
itor = m_Timers.find(dwTimerId);
if (itor != m_Timers.end())
{
//SAFE_DELETE(itor->second);
//m_Timers.erase(itor);
itor->second->iRemainingTime = 0;
itor->second->bKilled = true;
itor->second->bRepeating = false;
//Delete(itor->first);
}
}
//-----------------------------------------------------------
//int AMXAPI amx_Push(AMX *amx, cell value);
void CScriptTimers::Process(int iElapsedTime)
{
DwordTimerMap::iterator itor;
CGameMode *pGameMode;
for (itor = m_Timers.begin(); itor != m_Timers.end(); itor++)
{
itor->second->iRemainingTime -= iElapsedTime;
if (itor->second->iRemainingTime <= 0)
{
DwordTimerMap::iterator itor_tmp = ++itor; itor--;
if (!itor->second->bKilled)
{
pGameMode = pNetGame->GetGameMode();
if (pGameMode)
{
int idx;
AMX* amx = itor->second->pAMX;
if (amx && !amx_FindPublic(amx, itor->second->szScriptFunc, &idx))
{
cell ret;
//cell releases[16];
int count = itor->second->iParamCount;
int i = 0;
if (count > 0)
{
//strings = PushList(amx, (cell*)itor->second->cellParams, count);
cell* pars = (cell*)itor->second->cellParams;
while (i < count)
{
amx_Push(amx, pars[i]);
i++; // Go forwards to maintain push order
}
}
amx_Exec(amx, &ret, idx);
/*if (itor->second->iParamCount > 0)
{
while (strings)
{
strings--;
amx_Release(amx, releases[strings]);
}
}*/
}
}
}
if (itor->second->bRepeating)
{
itor->second->iRemainingTime = itor->second->iTotalTime;
}
else
{
// Release parameter memory
FreeMem(itor->second);
Delete(itor->first);
}
itor = itor_tmp;
}
if (itor == m_Timers.end()) break;
}
}
//----------------------------------------------------------------------------------
/*int CScriptTimers::PushList(AMX* amxFile, cell* params, int count)
{
//cell amx_addr[16]; // = NULL, *phys_addr[16];
int j = 0;
while (j < count)
{
j++; // Go forwards to maintain push order
amx_push(amxFile, params[j]);
}
}*/
//----------------------------------------------------------------------------------
// EOF