Repository navigation
Expand file tree
/
Copy pathguardscript.cpp
More file actions
executable file
·89 lines (68 loc) · 1.98 KB
/
Copy pathguardscript.cpp
File metadata and controls
executable file
·89 lines (68 loc) · 1.98 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
#include "guardscript.h"
#include "../xegame/chit.h"
#include "../xegame/spatialcomponent.h"
#include "../xegame/chitbag.h"
#include "../xegame/chitcontext.h"
#include "../game/lumosmath.h"
#include "../game/lumoschitbag.h"
#include "../game/team.h"
#include "../game/aicomponent.h"
using namespace grinliz;
static const int RAD = 2;
static const int NOTIFY_RAD = 8;
GuardScript::GuardScript() : timer(1000)
{
}
void GuardScript::Serialize(XStream* xs)
{
BeginSerialize(xs, Name());
timer.Serialize(xs, "timer");
EndSerialize(xs);
}
void GuardScript::OnAdd(Chit* chit, bool init)
{
super::OnAdd(chit, init);
if (init) {
timer.Randomize(parentChit->random.Rand());
}
}
int GuardScript::DoTick(U32 delta)
{
if (timer.Delta(delta)) {
// Check for enemies.
Vector2I pos2i = ToWorld2I(parentChit->Position());
Vector2I sector = ToSector(pos2i);
Rectangle2I innerSector = InnerSectorBounds(sector);
Rectangle2I r;
r.min = r.max = pos2i;
r.Outset(RAD);
r.DoIntersection(innerSector);
Rectangle2F rf = ToWorld2F(r);
CChitArray enemyArr;
MOBIshFilter enemyFilter;
CArray<int, 8> enemyID;
enemyFilter.CheckRelationship(parentChit, ERelate::ENEMY);
Context()->chitBag->QuerySpatialHash(&enemyArr, rf, 0, &enemyFilter);
for (int i = 0; i < enemyArr.Size() && enemyID.HasCap(); ++i) {
// convert to IDs for MakeAware()
enemyID.Push(enemyArr[i]->ID());
}
if (enemyID.Size()) {
r.min = r.max = pos2i;
r.Outset(NOTIFY_RAD);
r.DoIntersection(innerSector);
rf = ToWorld2F(r);
CChitArray friendArr;
MOBIshFilter friendFilter;
friendFilter.CheckRelationship(parentChit, ERelate::FRIEND);
Context()->chitBag->QuerySpatialHash(&friendArr, rf, 0, &friendFilter);
for (int i = 0; i < friendArr.Size(); ++i) {
AIComponent* ai = friendArr[i]->GetAIComponent();
if (ai) {
ai->MakeAware(enemyID.Mem(), enemyID.Size());
}
}
}
}
return timer.Next();
}