forked from MCForge/MCForge-Vanilla-Redux
/
Mob.cs
495 lines (410 loc) · 18.8 KB
/
Mob.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
/* Copyright (c) <2014> <LeChosenOne, DingusBungus>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
//AStarPathFinder - A* algorithm encoded in C# (c) http://www.csharpcity.com/reusable-code/a-path-finding-library/
using DeenGames.Utils;
using DeenGames.Utils.AStarPathFinder;
namespace MCForge
{
public class Bot
{
/// <summary>
/// Name of the bot.
/// </summary>
public String Name;
/// <summary>
/// Current world the bot is on.
/// </summary>
public Level World;
/// <summary>
/// Position of bot.
/// </summary>
public Position Position;
/// <summary>
/// Entity ID of the bot (-1 = default)
/// </summary>
public int ID = -1;
/// <summary>
/// Current model of the bot
/// </summary>
public string Model = "humanoid";
/// <summary>
/// Current skin of the bot
/// </summary>
public string Skin = "steve";
/// <summary>
/// Running thread for all bots
/// </summary>
private SchedulerTask thread;
//movement
public Player followTarget = null;
public bool isMoving = false;
public bool isFlying = false;
public Position OldPosition;
public Position NewPosition;
public Stopwatch timeCheck = new Stopwatch();
public static readonly double speed = 5.2; //Measured frequency in blocks per second
public static readonly double period = (1 / (speed)) * 1000; //Invert speed and multiply by 1000 to get period in milliseconds
public bool beganMoving;
public List<Vector3I> PositionList = new List<Vector3I>();//Vectors in this list are connected, but not neccessarily in a line
#region Public Methods
/// <summary>
/// Sets a bot, as well as the bot values. Must be called before any other bot classes.
/// </summary>
public void setBot(String botName, Level botWorld, Position pos, int entityID)
{
Name = botName;
World = botWorld;
Position = pos;
ID = entityID;
thread = Scheduler.NewTask(t => NetworkLoop());
thread.RunForever(TimeSpan.FromSeconds(0.1));//run the network loop every 0.1 seconds
Server.Bots.Add(this);
}
/// <summary>
/// Main IO loop, handles the networking of the bot.
/// </summary>
private void NetworkLoop()
{
if (isMoving)
{
if (followTarget != null)
{
Player p = Player.Find(followTarget.name, false)[0];
Server.s.Log("Chaning pos to " + p.name + " at " + p.pos.ToString());
NewPosition = p.pos;
beganMoving = false;
}
//move bot 1 block after every period
if (timeCheck.ElapsedMilliseconds > period)
{
Move();
timeCheck.Restart();
}
}
//If bot is not flying, drop down to nearest solid block
if (!isFlying)
{
//Drop();
}
}
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);
});
pl.mobs.Remove(this); //dont know if this is allowed really calling itself to kind of die
}
public static Player pl;
/// <summary>
/// Creates only the bot entity, not the bot data. Bot data is created from setBot.
/// </summary>
public void createBot()
{
pl.SendSpawn(pl.id, pl.color + pl.name, pl.pos[0], pl.pos[1], pl.pos[2], pl.rot[0], pl.rot[1]);
}
/// <summary>
/// Removes the bot entity, however the bot data remains. Intended as for temp bot changes.
/// </summary>
public void tempRemoveBot()
{
GlobalDie(ID);
}
/// <summary>
/// Completely removes the entity and data of the bot.
/// </summary>
public void removeBot()
{
thread.Stop();
isMoving = false;
Server.Bots.Remove(this);
World.Players.Send(PacketWriter.MakeRemoveEntity(ID));
}
/// <summary>
/// Updates the position and world of the bot for everyone in the world, used to replace bot from the tempRemoveBot() method
/// </summary>
public void updateBotPosition()
{
World.Players.Send(PacketWriter.MakeAddEntity(ID, Name, new Position(Position.X, Position.Y, Position.Z, Position.R, Position.L)));
}
/// <summary>
/// Changes the model of the bot
/// </summary>
public void changeBotModel(String botModel)
{
if (!FunCommands.validEntities.Contains(botModel))
{
return; //something went wrong, model does not exist
}
World.Players.Send(PacketWriter.MakeChangeModel((byte)ID, botModel));
Model = botModel;
}
/// <summary>
/// Changes the skin of the bot
/// </summary>
public void Clone(String targetSkin)
{
PlayerInfo target = PlayerDB.FindPlayerInfoExact(targetSkin);
if (target == null)
{
//something went wrong, player doesn't exist
}
World.Players.Send(PacketWriter.MakeExtAddEntity((byte)ID, targetSkin, targetSkin));
Skin = targetSkin;
}
/// <summary>
/// Epically explodes the bot
/// </summary>
public void explodeBot(Player player)//Prepare for super copy and paste
{
removeBot();
Vector3I vector = new Vector3I(Position.X / 32, Position.Y / 32, Position.Z / 32); //get the position in blockcoords as integers of the bot
//the following code block generates the centers of each explosion hub for the greater explosion
explode(vector, 0, 1);//start the center explosion immediately,last for a second
//all 6 faces of the explosion point
explode(new Vector3I(vector.X + 3, vector.Y, vector.Z), 0.75, 0.5);//start the face explosions at 0.75 seconds, last for a half second
explode(new Vector3I(vector.X - 3, vector.Y, vector.Z), 0.75, 0.5);
explode(new Vector3I(vector.X, vector.Y + 3, vector.Z), 0.75, 0.5);
explode(new Vector3I(vector.X, vector.Y - 3, vector.Z), 0.75, 0.5);
explode(new Vector3I(vector.X, vector.Y, vector.Z + 3), 0.75, 0.5);
explode(new Vector3I(vector.X, vector.Y, vector.Z - 3), 0.75, 0.5);
//all 8 corners of the explosion point
explode(new Vector3I(vector.X + 1, vector.Y + 1, vector.Z + 1), 0.5, 0.5);//start the corner explosions at .5 seconds, last for a half second
explode(new Vector3I(vector.X + 1, vector.Y + 1, vector.Z - 1), 0.5, 0.5);
explode(new Vector3I(vector.X + 1, vector.Y - 1, vector.Z + 1), 0.5, 0.5);
explode(new Vector3I(vector.X + 1, vector.Y - 1, vector.Z - 1), 0.5, 0.5);
explode(new Vector3I(vector.X - 1, vector.Y + 1, vector.Z + 1), 0.5, 0.5);
explode(new Vector3I(vector.X - 1, vector.Y + 1, vector.Z - 1), 0.5, 0.5);
explode(new Vector3I(vector.X - 1, vector.Y - 1, vector.Z + 1), 0.5, 0.5);
explode(new Vector3I(vector.X - 1, vector.Y - 1, vector.Z - 1), 0.5, 0.5);
}
/// <summary>
/// Basic information about the bot
/// </summary>
public override string ToString()
{
return String.Format("{0} on {1} at {2}, Id: {3}", Name, World, Position.ToString(), ID.ToString());
}
#endregion
#region Private Methods
#region movement
//If vector is a fluid block, return true
private bool ValidBlock(Vector3I vector)
{
switch(World.Map.GetBlock(vector))
{
case Block.Air:
case Block.Water:
case Block.StillWater:
case Block.Lava:
case Block.StillLava:
case Block.RedFlower:
case Block.YellowFlower:
case Block.RedMushroom:
case Block.BrownMushroom:
case Block.Plant:
case Block.Fire: //only epic bots can walk through fire
case Block.Snow:
return true;
default:
return false;
}
}
/// <summary>
/// Creates the shortest route between two points, used in Move()
/// </summary>
private void Path(Vector3I targetPosition)
{
//create a grid with all blocks in the 2d plane
byte[,] grid = new byte[World.Map.Width, World.Map.Length];
PathFinder pathFinder;
Vector3I botPos = Position.ToBlockCoords();
//Loop through all XZ coordinates of the map
for (int y = 0; y < World.Map.Length; y++)
{
for (int x = 0; x < World.Map.Width; x++)
{
//Find all valid positions, set as either an available tile or blocked tile
if (ValidBlock(new Vector3I(x, y, botPos.Z)) && ValidBlock(new Vector3I(x, y, botPos.Z - 1)))
{
grid[x, y] = PathFinderHelper.EMPTY_TILE;
}
else
{
grid[x, y] = PathFinderHelper.BLOCKED_TILE;
}
}
}
//set pathFinder to the grid just created, disallow diagonals
pathFinder = new PathFinder(grid);
pathFinder.Diagonals = false;
pathFinder.PunishChangeDirection = false;
Point botInitPoint = new Point(botPos.X, botPos.Y);
Point botFinalPoint = new Point(NewPosition.ToBlockCoords().X, NewPosition.ToBlockCoords().Y);
//Implement A* to determine optimal path between two spots
List<PathFinderNode> path = new PathFinderFast(grid).FindPath(botInitPoint, botFinalPoint);
if (path == null)
{
//There is no path, stop moving
beganMoving = false;
isMoving = false;
return;
}
//Convert node to block positions
foreach(PathFinderNode node in path)
{
PositionList.Add(new Vector3I(node.X, node.Y, botPos.Z));
}
//A* returns points in the opposite order needed, reverse in order to get proper order
PositionList.Reverse();
}
/// <summary>
/// Intended to act like gravity and pull bot down
/// </summary>
private void Drop()
{
//generate vector at block coord under the feet of the bot
Vector3I pos = Position.ToBlockCoords();
Vector3I under1 = new Vector3I(pos.X, pos.Y, pos.Z - 1);
Vector3I under2 = new Vector3I(pos.X, pos.Y, pos.Z - 2);
if (ValidBlock(under1) && ValidBlock(under2))
{
teleportBot(under1.ToPlayerCoords());
}
}
/// <summary>
/// Attempts to move bot between two lines, will use a straight line if possible
/// </summary>
private void Move()
{
if (!isMoving)
{
return;
}
//Create vector list of positions
if (!beganMoving)
{
beganMoving = true;
//create an IEnumerable list of all blocks that will be in the path between blocks
IEnumerable<Vector3I> positions = fCraft.Drawing.LineDrawOperation.LineEnumerator(Position.ToBlockCoords(), NewPosition.ToBlockCoords());
//edit IEnumarable into List
foreach(Vector3I v in positions)
{
PositionList.Add(v);
}
bool clear = true;
//Make sure that each block in the line is a fluid
foreach (Vector3I vect in PositionList)
{
Vector3I underVect = new Vector3I(vect.X, vect.Y, vect.Z - 1);
if (!ValidBlock(vect) || !ValidBlock(underVect))
{
clear = false;
}
}
//if list contains blocks that aren't air blocks, bot cannot make a direct line to target, need a path
if (!clear)
{
PositionList.Clear();
Path(NewPosition.ToBlockCoords());
}
}
//once the posList is empty, we have reached the final point
if (PositionList.Count() == 0 || Position == NewPosition)
{
isMoving = false;
beganMoving = false;
return;
}
//determine distance from the target player to the target bot
double groundDistance = Math.Sqrt(Math.Pow((NewPosition.X - OldPosition.X),2) + Math.Pow((NewPosition.Y - OldPosition.Y),2));
int xDisplacement = NewPosition.X - Position.X;
int yDisplacement = NewPosition.Y - Position.Y;
int zDisplacement = NewPosition.Z - Position.Z;
//use arctan to find appropriate angles (doesn't work yet)
double rAngle = Math.Atan((double)zDisplacement / groundDistance);//pitch
double lAngle = Math.Atan((double)xDisplacement / yDisplacement);//yaw
//create a new position with the next pos list in the posList, then remove that pos
Position targetPosition = new Position
{
X = (short)(PositionList.First().X * 32 + 16),
Y = (short)(PositionList.First().Y * 32 + 16),
Z = (short)(PositionList.First().Z * 32 + 16),
R = (byte)(rAngle),
L = (byte)(lAngle)
};
//Remove the position the bot just went to in the list, teleport to that created position, change position to created position
PositionList.Remove(PositionList.First());
Position = targetPosition;
teleportBot(targetPosition);
}
#endregion
/// <summary>
/// Emulates a small explosion at a specific location
/// </summary>
private void explode(Vector3I center, double delay, double length)
{
Scheduler.NewTask(t => updateBlock(Block.Lava, center, true, length)).RunManual(TimeSpan.FromSeconds(delay));
Random rand1 = new Random((int)DateTime.Now.Ticks);
Random rand2 = new Random((int)DateTime.Now.Ticks + 1);
Random rand3 = new Random((int)DateTime.Now.Ticks + 2);
Random rand4 = new Random((int)DateTime.Now.Ticks + 3);
Random rand5 = new Random((int)DateTime.Now.Ticks + 4);
Random rand6 = new Random((int)DateTime.Now.Ticks + 5);
//The code block generates a lava block from 0 to 3 block spaces, randomly away from the center block
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X, center.Y, center.Z), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X + rand1.Next(0, 3), center.Y, center.Z), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X - rand2.Next(0, 3), center.Y, center.Z), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X, center.Y + rand3.Next(0, 3), center.Z), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X, center.Y - rand4.Next(0, 3), center.Z), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X, center.Y, center.Z + rand5.Next(0, 3)), true, length)).RunManual(TimeSpan.FromSeconds(delay));
Scheduler.NewTask(t => updateBlock(Block.Lava, new Vector3I(center.X, center.Y, center.Z - rand6.Next(0, 3)), true, length)).RunManual(TimeSpan.FromSeconds(delay));
}
/// <summary>
/// Updates a specific block for a given time
/// </summary>
private void updateBlock(Block blockType, Vector3I blockPosition, bool replaceWithAir, double time)//I left this class rather generic incase i used it for anything else
{
BlockUpdate update = new BlockUpdate(null, blockPosition, blockType);
foreach (Player p in World.Players)
{
p.World.Map.QueueUpdate(update);
}
if (replaceWithAir)
{
Scheduler.NewTask(t => updateBlock(Block.Air, blockPosition, false, time)).RunManual(TimeSpan.FromSeconds(time));//place a block, replace it with air once 'time' is up
}
}
#endregion
}
}