-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathPlayerBot.cs
556 lines (502 loc) · 22.6 KB
/
PlayerBot.cs
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCForge)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.osedu.org/licenses/ECL-2.0
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace MCLawl
{
public sealed class PlayerBot
{
public static List<PlayerBot> playerbots = new List<PlayerBot>(64);
public bool hunt = false;
public bool kill = false;
public string AIName = "";
public string name;
public byte id;
public string color;
public Level level;
public int currentPoint = 0;
public int countdown = 0;
public bool nodUp = false;
public List<Pos> Waypoints = new List<Pos>();
public struct Pos { public string type, newscript; public int seconds, rotspeed; public ushort x, y, z; public byte rotx, roty; }
public ushort[] pos = new ushort[3] { 0, 0, 0 };
ushort[] oldpos = new ushort[3] { 0, 0, 0 };
ushort[] basepos = new ushort[3] { 0, 0, 0 };
public byte[] rot = new byte[2] { 0, 0 };
byte[] oldrot = new byte[2] { 0, 0 };
ushort[] foundPos = new ushort[3] { 0, 0, 0 };
byte[] foundRot = new byte[2] { 0, 0 };
bool movement = false;
public int movementSpeed = 24;
bool jumping = false;
int currentjump = 0;
public System.Timers.Timer botTimer = new System.Timers.Timer(100);
public System.Timers.Timer moveTimer = new System.Timers.Timer(100 / 24);
public System.Timers.Timer jumpTimer = new System.Timers.Timer(95);
#region == constructors ==
public PlayerBot(string n, Level l)
{
Server.s.Log("adding " + n + " bot");
name = n;
color = "&1";
id = FreeId();
level = l;
ushort x = (ushort)((0.5 + level.spawnx) * 32);
ushort y = (ushort)((1 + level.spawny) * 32);
ushort z = (ushort)((0.5 + level.spawnz) * 32);
pos = new ushort[3] { x, y, z }; rot = new byte[2] { level.rotx, level.roty };
GlobalSpawn();
}
public PlayerBot(string n, Level l, ushort x, ushort y, ushort z, byte rotx, byte roty)
{
name = n;
color = "&1";
id = FreeId();
level = l;
pos = new ushort[3] { x, y, z }; rot = new byte[2] { rotx, roty };
GlobalSpawn();
foreach (Player p in Player.players)
{
if (p.level == level)
{
Player.SendMessage(p, color + name + Server.DefaultColor + ", the bot, has been added.");
}
}
botTimer.Elapsed += delegate
{
int currentNum, foundNum = (32 * 75);
Random rand = new Random();
x = (ushort)Math.Round((decimal)pos[0] / (decimal)32);
y = (ushort)((pos[1] - 33) / 32);
z = (ushort)Math.Round((decimal)pos[2] / (decimal)32);
if (kill)
{
foreach (Player p in Player.players)
{
if ((ushort)(p.pos[0] / 32) == x)
{
if (Math.Abs((ushort)(p.pos[1] / 32) - y) < 2)
{
if ((ushort)(p.pos[2] / 32) == z)
{
p.HandleDeath(Block.Zero);
}
}
}
}
}
if (Waypoints.Count < 1)
{
if (hunt)
Player.players.ForEach(delegate(Player p)
{
if (p.level == level && !p.invincible)
{
currentNum = Math.Abs(p.pos[0] - pos[0]) + Math.Abs(p.pos[1] - pos[1]) + Math.Abs(p.pos[2] - pos[2]);
if (currentNum < foundNum)
{
foundNum = currentNum;
foundPos = p.pos;
foundRot = p.rot;
movement = true;
rot[1] = (byte)(255 - foundRot[1]);
if (foundRot[0] < 128) rot[0] = (byte)(foundRot[0] + 128);
else rot[0] = (byte)(foundRot[0] - 128);
}
}
});
}
else
{
bool skip = false;
movement = false;
retry: switch (Waypoints[currentPoint].type)
{
case "walk":
foundPos[0] = Waypoints[currentPoint].x;
foundPos[1] = Waypoints[currentPoint].y;
foundPos[2] = Waypoints[currentPoint].z;
movement = true;
if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[currentPoint].x / 32))
{
if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[currentPoint].z / 32))
{
rot[0] = Waypoints[currentPoint].rotx;
rot[1] = Waypoints[currentPoint].roty;
currentPoint++;
movement = false;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
}
}
break;
case "teleport":
pos[0] = Waypoints[currentPoint].x;
pos[1] = Waypoints[currentPoint].y;
pos[2] = Waypoints[currentPoint].z;
rot[0] = Waypoints[currentPoint].rotx;
rot[1] = Waypoints[currentPoint].roty;
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
return;
case "wait":
if (countdown != 0)
{
countdown--;
if (countdown == 0)
{
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
}
}
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "nod":
if (countdown != 0)
{
countdown--;
if (nodUp)
{
if (rot[1] > 32 && rot[1] < 128) nodUp = !nodUp;
else
{
if (rot[1] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[1] = 0;
else rot[1] += (byte)Waypoints[currentPoint].rotspeed;
}
}
else
{
if (rot[1] > 128 && rot[1] < 224) nodUp = !nodUp;
else
{
if (rot[1] - (byte)Waypoints[currentPoint].rotspeed < 0) rot[1] = 255;
else rot[1] -= (byte)Waypoints[currentPoint].rotspeed;
}
}
if (countdown == 0)
{
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
}
}
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "spin":
if (countdown != 0)
{
countdown--;
if (rot[0] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[0] = 0;
else if (rot[0] + (byte)Waypoints[currentPoint].rotspeed < 0) rot[0] = 255;
else rot[0] += (byte)Waypoints[currentPoint].rotspeed;
if (countdown == 0)
{
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
}
}
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "speed":
movementSpeed = (int)Math.Round((decimal)((decimal)24 / (decimal)100 * (decimal)Waypoints[currentPoint].seconds));
if (movementSpeed == 0) movementSpeed = 1;
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
return;
case "reset":
currentPoint = 0;
return;
case "remove":
removeBot();
return;
case "linkscript":
if (File.Exists("bots/" + Waypoints[currentPoint].newscript))
{
Command.all.Find("botset").Use(null, this.name + " " + Waypoints[currentPoint].newscript);
return;
}
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
return;
case "jump":
jumpTimer.Elapsed += delegate
{
currentjump++;
switch (currentjump)
{
case 1:
case 2: pos[1] += 24; break;
case 3: break;
case 4: pos[1] -= 24; break;
case 5: pos[1] -= 24; jumping = false; currentjump = 0; jumpTimer.Stop(); break;
}
};
jumpTimer.Start();
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
break;
}
if (currentPoint == Waypoints.Count) currentPoint = 0;
}
if (!movement)
{
if (rot[0] < 245) rot[0] += 8;
else rot[0] = 0;
if (rot[1] > 32 && rot[1] < 64) rot[1] = 224;
else if (rot[1] > 250) rot[1] = 0;
else rot[1] += 4;
}
};
botTimer.Start();
moveTimer.Elapsed += delegate
{
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed;
if (!movement) return;
int newNum; Random rand = new Random();
if ((pos[1] - 19) % 32 != 0 && !jumping)
{
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
}
x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32);
y = (ushort)((pos[1] - 64) / 32);
z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32);
byte b = Block.Convert(level.GetTile(x, y, z));
byte b1, b2, b3;//, b4;
if (Block.Walkthrough(b) && !jumping)
{
pos[1] = (ushort)(pos[1] - 32);
}
y = (ushort)((pos[1] - 64) / 32); //Block below feet
newNum = level.PosToInt((ushort)(x + Math.Sign(foundPos[0] - pos[0])), y, (ushort)(z + Math.Sign(foundPos[2] - pos[2])));
b = Block.Convert(level.GetTile(newNum));
b1 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 1, 0)));
b2 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 2, 0)));
b3 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 3, 0)));
if (Block.Walkthrough(b2) && Block.Walkthrough(b3) && !Block.Walkthrough(b1))
{ //Get ready to go up step
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[1] += (ushort)32;
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
else if (Block.Walkthrough(b1) && Block.Walkthrough(b2))
{ //Stay on current level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
else if (Block.Walkthrough(b) && Block.Walkthrough(b1))
{ //Drop a level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[1] -= (ushort)32;
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32);
y = (ushort)((pos[1] - 64) / 32);
z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32);
b1 = Block.Convert(level.GetTile(x, (ushort)(y + 1), z));
b2 = Block.Convert(level.GetTile(x, (ushort)(y + 2), z));
b3 = Block.Convert(level.GetTile(x, y, z));
/*
if ((ushort)(foundPos[1] / 32) > y) {
if (b1 == Block.water || b1 == Block.waterstill || b1 == Block.lava || b1 == Block.lavastill) {
if (Block.Walkthrough(b2)) {
pos[1] = (ushort)(pos[1] + (Math.Sign(foundPos[1] - pos[1])));
}
} else if (b2 == Block.water || b2 == Block.waterstill || b2 == Block.lava || b2 == Block.lavastill) {
pos[1] = (ushort)(pos[1] + (Math.Sign(foundPos[1] - pos[1])));
}
} else if ((ushort)(foundPos[1] / 32) < y) {
if (b3 == Block.water || b3 == Block.waterstill || b3 == Block.lava || b3 == Block.lavastill) {
pos[1] = (ushort)(pos[1] + (Math.Sign(foundPos[1] - pos[1])));
}
}*/
};
moveTimer.Start();
}
#endregion
#region ==Input ==
public void SetPos(ushort x, ushort y, ushort z, byte rotx, byte roty)
{
pos = new ushort[3] { x, y, z };
rot = new byte[2] { rotx, roty };
}
#endregion
public void removeBot()
{
this.botTimer.Stop();
GlobalDie();
PlayerBot.playerbots.Remove(this);
}
public void GlobalSpawn()
{
Player.players.ForEach(delegate(Player p) //bots dont need to be informed of other bots here
{
if (p.level != level) { return; }
p.SendSpawn(id, color + name, pos[0], pos[1], pos[2], rot[0], rot[1]);
});
}
public void GlobalDie()
{
Server.s.Log("removing " + name + " bot");
Player.players.ForEach(delegate(Player p)
{
if (p.level != level) { return; }
p.SendDie(id);
});
playerbots.Remove(this); //dont know if this is allowed really calling itself to kind of die
}
public void Update()
{
//pos[0] += 1;
}
void UpdatePosition() //Im going to avoid touching this unless nessesary
{
//pingDelayTimer.Stop();
// Shameless copy from JTE's Server
byte changed = 0;
if (oldpos[0] != pos[0] || oldpos[1] != pos[1] || oldpos[2] != pos[2]) { changed |= 1; }
if (oldrot[0] != rot[0] || oldrot[1] != rot[1]) { changed |= 2; }
if (Math.Abs(pos[0] - basepos[0]) > 32 || Math.Abs(pos[1] - basepos[1]) > 32 ||
Math.Abs(pos[2] - basepos[2]) > 32) { changed |= 4; }
if ((oldpos[0] == pos[0] && oldpos[1] == pos[1] && oldpos[2] == pos[2]) &&
(basepos[0] != pos[0] || basepos[1] != pos[1] || basepos[2] != pos[2])) { changed |= 4; }
byte[] buffer = new byte[0]; byte msg = 0;
if ((changed & 4) != 0)
{
msg = 8; buffer = new byte[9]; buffer[0] = id;
HTNO(pos[0]).CopyTo(buffer, 1);
HTNO(pos[1]).CopyTo(buffer, 3);
HTNO(pos[2]).CopyTo(buffer, 5);
buffer[7] = rot[0]; buffer[8] = rot[1];
}
else if (changed == 1)
{
msg = 10; buffer = new byte[4]; buffer[0] = id;
buffer[1] = (byte)(pos[0] - oldpos[0]);
buffer[2] = (byte)(pos[1] - oldpos[1]);
buffer[3] = (byte)(pos[2] - oldpos[2]);
}
else if (changed == 2)
{
msg = 11; buffer = new byte[3]; buffer[0] = id;
buffer[1] = rot[0]; buffer[2] = rot[1];
}
else if (changed == 3)
{
msg = 9; buffer = new byte[6]; buffer[0] = id;
buffer[1] = (byte)(pos[0] - oldpos[0]);
buffer[2] = (byte)(pos[1] - oldpos[1]);
buffer[3] = (byte)(pos[2] - oldpos[2]);
buffer[4] = rot[0]; buffer[5] = rot[1];
}
try
{
if (changed != 0) foreach (Player p in Player.players)
{
if (p.level == level)
{
p.SendRaw(msg, buffer);
}
}
} catch { }
oldpos = pos;
oldrot = rot;
}
#region == Misc ==
static byte FreeId()
{
for (byte i = 64; i < 128; ++i)
{
foreach (PlayerBot b in playerbots)
{
if (b.id == i) { goto Next; }
} return i;
Next: continue;
} unchecked { return (byte)-1; }
}
public static PlayerBot Find(string name)
{
PlayerBot tempPlayer = null; bool returnNull = false;
foreach (PlayerBot pB in PlayerBot.playerbots)
{
if (pB.name.ToLower() == name.ToLower()) return pB;
if (pB.name.ToLower().IndexOf(name.ToLower()) != -1)
{
if (tempPlayer == null) tempPlayer = pB;
else returnNull = true;
}
}
if (returnNull == true) return null;
if (tempPlayer != null) return tempPlayer;
return null;
}
public static bool ValidName(string name)
{
string allowedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890_";
foreach (char ch in name) { if (allowedchars.IndexOf(ch) == -1) { return false; } } return true;
}
#endregion
#region == True Global ==
public static void GlobalUpdatePosition()
{
playerbots.ForEach(delegate(PlayerBot b) { b.UpdatePosition(); });
}
public static void GlobalUpdate()
{
while (true)
{
Thread.Sleep(100);
playerbots.ForEach(delegate(PlayerBot b) { b.Update(); });
}
}
#endregion
#region == Host <> Network ==
byte[] HTNO(ushort x) //Is used currently, the rest are not and may not be needed at all
{
byte[] y = BitConverter.GetBytes(x); Array.Reverse(y); return y;
}
ushort NTHO(byte[] x, int offset)
{
byte[] y = new byte[2];
Buffer.BlockCopy(x, offset, y, 0, 2); Array.Reverse(y);
return BitConverter.ToUInt16(y, 0);
}
byte[] HTNO(short x)
{
byte[] y = BitConverter.GetBytes(x); Array.Reverse(y); return y;
}
#endregion
}
}