forked from ClassiCube/MCGalaxy-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FootballInstruction.cs
123 lines (99 loc) · 3.39 KB
/
FootballInstruction.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
using System;
using System.IO;
using MCGalaxy;
using MCGalaxy.Bots;
using MCGalaxy.Maths;
namespace PluginFootball
{
public sealed class FootballPlugin : Plugin
{
BotInstruction ins;
public override string name { get { return "FootballInstruction"; } }
public override string MCGalaxy_Version { get { return "1.9.1.4"; } }
public override string creator { get { return ""; } }
public override void Load(bool startup) {
ins = new FootballInstruction();
BotInstruction.Instructions.Add(ins);
}
public override void Unload(bool shutdown) {
BotInstruction.Instructions.Remove(ins);
}
}
sealed class FootballInstruction : BotInstruction
{
public FootballInstruction() { Name = "football"; }
public override bool Execute(PlayerBot bot, InstructionData data) {
int strength = 20;
if (data.Metadata != null) strength = (ushort)data.Metadata;
if (bot.movementSpeed > 0) {
Step(bot);
//Server.s.Log("STEP: " + bot.movementSpeed);
bot.movementSpeed--;
}
GetKicked(bot, strength);
return true;
}
void Step(PlayerBot bot) {
bot.TargetPos = bot.Pos;
bot.movement = true;
Vec3F32 dir = DirUtils.GetDirVector(bot.Rot.RotY, 0);
bot.TargetPos.X = bot.Pos.X + (int)(dir.X * bot.movementSpeed);
bot.TargetPos.Z = bot.Pos.Z + (int)(dir.Z * bot.movementSpeed);
}
void GetKicked(PlayerBot bot, int strength) {
int closestDist = int.MaxValue;
Player[] players = PlayerInfo.Online.Items;
Player closest = null;
foreach (Player p in players) {
if (p.level != bot.level || p.invincible || p.hidden) continue;
int dx = p.Pos.X - bot.Pos.X, dy = p.Pos.Y - bot.Pos.Y, dz = p.Pos.Z - bot.Pos.Z;
dx = Math.Abs(dx); dy = Math.Abs(dy); dz = Math.Abs(dz);
if (dx > 8 || dy > 8 || dz > 8) continue;
int dist = dx + dy + dz;
if (dist > closestDist) continue;
closest = p;
closestDist = dist;
}
if (closest == null) return;
bot.SetYawPitch(closest.Rot.RotY, closest.Rot.HeadX);
bot.movementSpeed = strength;
Step(bot);
}
static bool MoveTowards(PlayerBot bot, Player p) {
int dx = p.Pos.X - bot.Pos.X, dy = p.Pos.Y - bot.Pos.Y, dz = p.Pos.Z - bot.Pos.Z;
bot.TargetPos = p.Pos;
bot.movement = true;
Vec3F32 dir = new Vec3F32(dx, dy, dz);
dir = Vec3F32.Normalise(dir);
Orientation rot = bot.Rot;
DirUtils.GetYawPitch(dir, out rot.RotY, out rot.HeadX);
// If we are very close to a player, switch from trying to look
// at them to just facing the opposite direction to them
if (Math.Abs(dx) < 4 && Math.Abs(dz) < 4) {
rot.RotY = (byte)(p.Rot.RotY + 128);
}
bot.Rot = rot;
return dx <= 8 && dy <= 16 && dz <= 8;
}
public override InstructionData Parse(string[] args) {
InstructionData data = default(InstructionData);
if (args.Length > 1)
data.Metadata = ushort.Parse(args[1]);
return data;
}
public override void Output(Player p, string[] args, TextWriter w) {
if (args.Length > 3) {
w.WriteLine(Name + " " + ushort.Parse(args[3]));
} else {
w.WriteLine(Name);
}
}
public override string[] Help { get { return help; } }
static string[] help = new string[] {
"&T/BotAI add [name] football <strength>",
"&HCauses the bot to get kicked around when players touch it.",
"&H[strength] is how much a power a 'kick' does to the ball",
"&H <strength> defaults to 20.",
};
}
}