-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightBrain.as
More file actions
221 lines (180 loc) · 4.95 KB
/
Copy pathKnightBrain.as
File metadata and controls
221 lines (180 loc) · 4.95 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
// Knight brain
#define SERVER_ONLY
#include "BrainCommon.as"
#include "BrainPathing.as"
// Gingerbeard @ March 6th, 2025
// here is an example of a pathing implementation for a bot
void onInit(CBrain@ this)
{
InitBrain(this);
this.server_SetActive(true);
CBlob@ blob = this.getBlob();
BrainPath pather(blob, Path::GROUND);
blob.set("brain_path", @pather);
}
void onTick(CBrain@ this)
{
CBlob@ blob = this.getBlob();
BrainPath@ pather;
if (!blob.get("brain_path", @pather)) return;
if (blob.getPlayer() !is null && !blob.isBot())
{
pather.EndPath();
this.server_SetActive(false);
return;
}
pather.Tick();
pather.SetSuggestedKeys();
pather.SetSuggestedAimPos();
CBlob@ target = this.getTarget();
if (target is null || XORRandom(20) == 0)
{
@target = getNewTarget(blob);
this.SetTarget(target);
}
u8 strategy = blob.get_u8("strategy");
if (target !is null)
{
f32 distance;
const bool visibleTarget = isVisible(blob, target, distance);
if (visibleTarget && distance < 50.0f)
{
strategy = Strategy::attacking;
}
if (strategy == Strategy::idle)
{
strategy = Strategy::chasing;
}
else if (strategy == Strategy::chasing && (getGameTime() + blob.getNetworkID() * 10) % 20 == 0)
{
pather.SetPath(blob.getPosition(), target.getPosition());
}
else if (strategy == Strategy::attacking)
{
if (!visibleTarget || distance > 120.0f)
{
strategy = Strategy::chasing;
}
}
/*if (strategy == Strategy::chasing)
{
DefaultChaseBlob(blob, target);
}*/
if (strategy == Strategy::attacking)
{
pather.EndPath();
AttackBlob(blob, target);
}
// lose target if its killed (with random cooldown)
if (LoseTarget(this, target))
{
pather.EndPath();
strategy = Strategy::idle;
}
blob.set_u8("strategy", strategy);
}
else if (strategy == Strategy::idle)
{
// wander around the map
if (!pather.isPathing())
{
CMap@ map = getMap();
Vec2f dim = map.getMapDimensions();
pather.SetPath(blob.getPosition(), Vec2f(XORRandom(dim.x), XORRandom(dim.y)));
}
}
}
void AttackBlob(CBlob@ blob, CBlob @target)
{
Vec2f mypos = blob.getPosition();
Vec2f targetPos = target.getPosition();
Vec2f targetVector = targetPos - mypos;
f32 targetDistance = targetVector.Length();
const s32 difficulty = blob.get_s32("difficulty");
if (targetDistance > blob.getRadius() + 15.0f)
{
Chase(blob, target);
}
JumpOverObstacles(blob);
// aim always at enemy
blob.setAimPos(targetPos);
const u32 gametime = getGameTime();
bool shieldTime = gametime - blob.get_u32("shield time") < uint(8 + difficulty * 1.33f + XORRandom(20));
bool backOffTime = gametime - blob.get_u32("backoff time") < uint(1 + XORRandom(20));
if (target.isKeyPressed(key_action1)) // enemy is attacking me
{
int r = XORRandom(35);
if (difficulty > 2 && r < 2 && (!backOffTime || difficulty > 4))
{
blob.set_u32("shield time", gametime);
shieldTime = true;
}
else if (difficulty > 1 && r > 32 && !shieldTime)
{
// raycast to check if there is a hole behind
Vec2f raypos = mypos;
raypos.x += targetPos.x < mypos.x ? 32.0f : -32.0f;
Vec2f col;
if (getMap().rayCastSolid(raypos, raypos + Vec2f(0.0f, 32.0f), col))
{
blob.set_u32("backoff time", gametime); // base on difficulty
backOffTime = true;
}
}
}
else
{
// start attack
if (XORRandom(Maths::Max(3, 30 - (difficulty + 4) * 2)) == 0 && (getGameTime() - blob.get_u32("attack time")) > 10)
{
// base on difficulty
blob.set_u32("attack time", gametime);
}
}
if (shieldTime) // hold shield for a while
{
blob.setKeyPressed(key_action2, true);
}
else if (backOffTime) // back off for a bit
{
Runaway(blob, target);
}
else if (targetDistance < 40.0f && getGameTime() - blob.get_u32("attack time") < (Maths::Min(13, difficulty + 3))) // release and attack when appropriate
{
if (!target.isKeyPressed(key_action1))
{
blob.setKeyPressed(key_action2, false);
}
blob.setKeyPressed(key_action1, true);
}
}
CBlob@ getNewTarget(CBlob@ blob)
{
CBlob@[] players;
getBlobsByTag("player", @players);
CBlob@ closest = null;
f32 closestDist = 600.0f;
for (uint i = 0; i < players.length; i++)
{
CBlob@ potential = players[i];
if (blob.getTeamNum() == potential.getTeamNum()) continue;
if (potential.hasTag("dead") || potential.hasTag("migrant")) continue;
const f32 dist = (potential.getPosition() - blob.getPosition()).Length();
if (dist < closestDist)
{
@closest = potential;
closestDist = dist;
}
}
return closest;
}
/// PATHING DEBUG
void onRender(CSprite@ this)
{
if ((!render_paths && g_debug == 0) || g_debug == 5) return;
CBlob@ blob = this.getBlob();
if (blob.hasTag("dead")) return;
BrainPath@ pather;
if (!blob.get("brain_path", @pather)) return;
pather.Render();
}