-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBotWaypointScript.as
More file actions
482 lines (392 loc) · 12.8 KB
/
BotWaypointScript.as
File metadata and controls
482 lines (392 loc) · 12.8 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "BotWaypoint"
BotWaypointScript g_WaypointScripts;
enum ScriptOperator
{
Equals,
LessThan,
GreaterThan,
NotEqual
}
bool CheckScriptOperator ( float check, ScriptOperator op, float value )
{
bool ret = false;
switch ( op )
{
case Equals:
ret = (check == value);
//BotMessage("CheckScriptOperator " + check + "==" + value);
break;
case LessThan:
ret = (check < value);
//BotMessage("CheckScriptOperator " + check + "<" + value);
break;
case GreaterThan:
ret = (check > value);
//BotMessage("CheckScriptOperator " + check + ">" + value);
break;
case NotEqual:
ret = (check != value);
//BotMessage("CheckScriptOperator " + check "!=" + value);
default:
break;
}
//BotMessage("CheckScriptOperator result is " + (ret ? "TRUE" : "FALSE") );
return ret;
}
/*
abstract class BotObjectiveFlag
{
string m_name;
BotObjectiveFlag ( string name )
{
m_name = name;
}
bool isName ( string name )
{
return m_name == name;
}
bool CanDo ( CBaseEntity@ bot )
{
return false;
}
}
final class BotObjectiveFlag_NeedFullHealth : BotObjectiveFlag
{
BotObjectiveFlag_NeedFullHealth ()
{
super("need_full_health");
}
bool CanDo ( CBaseEntity@ bot )
{
return bot.pev.health >= 100.0;
}
}
final class BotObjectiveFlag_NeedFullArmor : BotObjectiveFlag
{
BotObjectiveFlag_NeedFullArmor ()
{
super("need_full_armor");
}
bool CanDo ( CBaseEntity@ bot )
{
return bot.pev.armorvalue >= 100.0;
}
}
class BotObjectiveFlags
{
array<BotObjectiveFlag@> m_flags;
BotObjectiveFlags()
{
m_flags = {};
}
void addFlag ( BotObjectiveFlag@ flag )
{
m_flags.push_back(flag);
}
bool CanDo ( CBaseEntity@ bot )
{
for ( uint i = 0; i < m_flags.length() ; i ++ )
{
if ( m_flags[i].CanDo(bot) == false )
return false;
}
return true;
}
};
final class BotObjectiveFlagManager : BotObjectiveFlags
{
BotObjectiveFlagManager()
{
addFlag(BotObjectiveFlag_NeedFullHealth());
addFlag(BotObjectiveFlag_NeedFullArmor());
}
BotObjectiveFlag@ getFlag ( string name )
{
for ( uint i = 0; i < m_flags.length() ; i ++ )
{
if ( m_flags[i].isName(name) )
return m_flags[i];
}
return null;
}
void Parse ( string flags, BotObjectiveFlags@ flags_object )
{
array<string> flags_split = flags.Split("|");
for ( uint i = 0; i < flags_split.length() ; i ++ )
{
BotObjectiveFlag@ flag_object = getFlag(flags_split[i]);
if ( flags_object !is null )
flags_object.addFlag(flag_object);
}
}
};
BotObjectiveFlagManager m_BotObjectiveFlagManager = BotObjectiveFlagManager();
*/
// Example
//#WID, prev, entity search, parameter, operator, value, extra_flags
//[0], [1], [2], [3], [4], [5], [6]
//12, -1, env_sprite, distance, >, 100, need_armor|need_health
//32, 12, null, null, null, null, null
BotObjectiveScript@ ObjectiveScriptRead ( string line )
{
// comma separated
array<string> args = line.Split( "," );
if ( args.length() < 6 )
return null;
for ( uint i = 0; i < args.length() ; i ++ )
{
args[i].Trim();
}
int id = atoi(args[0]);
int prev = atoi(args[1]);
int ent = atoi(args[2]);
string param = args[3];
string op = args[4];
// string flags = "";
/*if ( args.length() > 6 )
flags = args[6];*/
ScriptOperator sop;
if ( op == ">" )
sop = GreaterThan;
else if ( op == "<" )
sop = LessThan;
else if ( op == "=" )
sop = Equals;
else if ( op == "!=" )
sop = NotEqual;
else if ( op == "null" )
sop = Equals;
else
return null; // Invalid operation
float value = atof(args[5]);
return BotObjectiveScript ( id, prev,
ent, param, sop, value/*, flags*/ );
}
class BotObjectiveScript
{
int id;
int previous_id;
int entity_id;
string parameter;
ScriptOperator operator;
float value;
EHandle entity;
//BotObjectiveFlags@ m_flags;
BotObjectiveScript ( int wptid, int prev_id,
int ent_id, string param, ScriptOperator op, float val/*, string flags*/ )
{
id = wptid;
previous_id = prev_id;
// entity ID is the script value take away 8
// all entity IDs must be taken when maxplayers is 8 (default)
// then maxClients is added on (so if it is 8 there is no change)
// script Entity offset is used for dedicated servers
entity_id = ent_id + g_ScriptEntityOffset - 8 + g_Engine.maxClients;
parameter = param;
operator = op;
value = val;
entity = null;
// @m_flags = BotObjectiveFlags();
// m_BotObjectiveFlagManager.Parse(flags,m_flags);
initEntity();
}
bool isForWaypoint ( int wptid )
{
return id == wptid;
}
void initEntity ()
{
if ( entity_id >= 0 && entity_id < g_Engine.maxEntities )
{
edict_t@ edict = g_EntityFuncs.IndexEnt(entity_id);
if ( edict !is null )
{
entity = g_EntityFuncs.Instance(edict);
}
if ( entity.GetEntity() is null )
BotMessage("SCRIPT ENTITY " + entity_id + " NOT FOUND" );
}
}
}
enum BotWaypointScriptResult
{
BotWaypointScriptResult_Error,
BotWaypointScriptResult_Previous_Incomplete,
BotWaypointScriptResult_Incomplete,
BotWaypointScriptResult_Complete
}
class BotWaypointScript
{
array<BotObjectiveScript@> m_scripts;
bool ScriptExists ()
{
return m_scripts.length() > 0;
}
void Read ()
{
string filename = "" + g_Engine.mapname + ".ini";
File@ f;
m_scripts = { };
@f = g_FileSystem.OpenFile( "scripts/plugins/store/" + filename , OpenFile::READ);
if ( f is null )
@f = g_FileSystem.OpenFile( "scripts/plugins/BotManager/rcw/" + filename , OpenFile::READ);
// Open the file in 'read' mode
if( f !is null )
{
while ( !f.EOFReached() )
{
string fileLine;
f.ReadLine( fileLine );
if ( fileLine[0] == "#" )
continue;
BotObjectiveScript@ script = ObjectiveScriptRead(fileLine);
if ( script !is null )
{
m_scripts.insertLast(script);
}
else
{
BotMessage("ERROR Reading waypoint script line '"+fileLine+"'");
}
}
f.Close();
}
}
BotWaypointScriptResult canDoObjective ( CBaseEntity@ pBot, int wptid )
{
BotObjectiveScript@ script = getScript(wptid);
CWaypoint@ pWpt;
if ( script is null )
{
//BotMessage("SCRIPT no script found for wpt id " + wptid);
return BotWaypointScriptResult_Error;
}
//if ( script.m_flags.CanDo(pBot) == false )
// return BotWaypointScriptResult_Error; // need pre-requisite
if ( script.previous_id >= 0 )
{
if ( canDoObjective(pBot,script.previous_id) != BotWaypointScriptResult_Complete )
{
// BotMessage("SCRIPT isObjectiveComplete(script.previous_id) != BotWaypointScriptResult_Complete" );
return BotWaypointScriptResult_Previous_Incomplete;
}
}
@pWpt = g_Waypoints.getWaypointAtIndex(wptid);
if ( pWpt is null )
{
// BotMessage("SCRIPT pWpt is null, BotWaypointScriptResult_Error");
return BotWaypointScriptResult_Error;
}
Vector vOrigin = pWpt.m_vOrigin;
if ( script.entity_id == 0 ) // self
{
CBaseEntity@ pent = pBot;
if ( script.parameter == "health" )
{
if ( CheckScriptOperator(pent.pev.health,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "armor" )
{
if ( CheckScriptOperator(pent.pev.armorvalue,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
}
else if ( script.entity_id > 0 )
{
CBaseEntity@ pent = script.entity.GetEntity();
if ( pent is null )
{
if ( script.parameter == "null" )
{
// BotMessage("SCRIPT script.parameter == 'null' BotWaypointScriptResult_Complete");
return BotWaypointScriptResult_Complete;
}
// BotMessage("SCRIPT pent is null BotWaypointScriptResult_Incomplete");
return BotWaypointScriptResult_Incomplete;
}
//BotMessage("ID: " + wptid + " " + script.parameter);
if ( script.parameter == "active" )
{
float isActive = UTIL_ToggleIsActive(pent,pBot) ? 1.0 : 0.0;
BotMessage("isActive == " + isActive);
if ( CheckScriptOperator(isActive,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "distance" )
{
float distance = (UTIL_EntityOrigin(pent) - vOrigin).Length();
if ( CheckScriptOperator(distance,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "frame" )
{
// BotMessage("CHECKING FRAME");
if ( CheckScriptOperator(pent.pev.frame,script.operator,script.value) )
{
// BotMessage("OK!");
return BotWaypointScriptResult_Complete;
}
}
else if ( script.parameter == "x" )
{
Vector pentOrigin = UTIL_EntityOrigin(pent);
if ( CheckScriptOperator(pentOrigin.x,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "y" )
{
Vector pentOrigin = UTIL_EntityOrigin(pent);
if ( CheckScriptOperator(pentOrigin.y,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "z" )
{
Vector pentOrigin = UTIL_EntityOrigin(pent);
if ( CheckScriptOperator(pentOrigin.z,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "angle.x" )
{
if ( CheckScriptOperator(pent.pev.angles.x,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "angle.y" )
{
if ( CheckScriptOperator(pent.pev.angles.y,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "solid" )
{
if ( CheckScriptOperator(pent.pev.solid,script.operator,script.value) )
return BotWaypointScriptResult_Complete;
}
else if ( script.parameter == "visible" )
{
float isVisible = 1;
if ( pent.pev.effects & EF_NODRAW == EF_NODRAW )
isVisible = 0;
if ( CheckScriptOperator(isVisible,script.operator,script.value) )
{
// BotMessage("VISIBLE CHECK OK");
return BotWaypointScriptResult_Complete;
}
//else
// BotMessage("VISIBLE CHECK FAIL");
}
}
return BotWaypointScriptResult_Incomplete;
}
BotObjectiveScript@ getScript ( int wptid )
{
for ( uint i = 0; i < m_scripts.length(); i ++ )
{
BotObjectiveScript@ scr = m_scripts[i];
if ( scr.isForWaypoint(wptid) )
{
return scr;
}
}
return null;
}
}