mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 04:32:50 -04:00
Refactor bots code to use separate Instruction class.
This commit is contained in:
parent
b15e598250
commit
3a0c852ab6
@ -24,7 +24,10 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
public static Dictionary<string, Func<PlayerBot, bool>> Defined =
|
||||
new Dictionary<string, Func<PlayerBot, bool>>{
|
||||
{ "walk", DoWalk }, { "teleport", DoTeleport },
|
||||
{ "walk", DoWalk }, { "teleport", DoTeleport }, { "wait", DoWait },
|
||||
{ "nod", DoNod }, { "spin", DoSpin }, { "speed", DoSpeed },
|
||||
{ "jump", DoJump }, { "reset", DoReset }, { "remove", DoRemove },
|
||||
{ "linkscript", DoLinkscript },
|
||||
};
|
||||
|
||||
static bool DoWalk(PlayerBot bot) {
|
||||
@ -113,7 +116,7 @@ namespace MCGalaxy.Bots {
|
||||
static bool DoSpeed(PlayerBot bot) {
|
||||
bot.movementSpeed = (int)Math.Round(24m / 100m * bot.Waypoints[bot.cur].seconds);
|
||||
if (bot.movementSpeed == 0) bot.movementSpeed = 1;
|
||||
bot.NextInstruction(); return true;
|
||||
bot.NextInstruction(); return false;
|
||||
}
|
||||
|
||||
static bool DoJump(PlayerBot bot) {
|
||||
@ -127,7 +130,28 @@ namespace MCGalaxy.Bots {
|
||||
case 5: bot.pos[1] -= 24; bot.jumping = false; bot.currentjump = 0; bot.jumpTimer.Stop(); break;
|
||||
}
|
||||
};
|
||||
|
||||
bot.jumpTimer.Start();
|
||||
bot.NextInstruction();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool DoReset(PlayerBot bot) {
|
||||
bot.cur = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DoRemove(PlayerBot bot) {
|
||||
PlayerBot.Remove(bot);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DoLinkscript(PlayerBot bot) {
|
||||
if (File.Exists("bots/" + bot.Waypoints[bot.cur].newscript)) {
|
||||
string args = bot.name + " " + bot.Waypoints[bot.cur].newscript;
|
||||
Command.all.Find("botset").Use(null, args);
|
||||
return true;
|
||||
}
|
||||
bot.NextInstruction(); return true;
|
||||
}
|
||||
}
|
||||
|
@ -84,51 +84,20 @@ namespace MCGalaxy {
|
||||
if (Waypoints.Count == 0) {
|
||||
if (hunt) DoHunt();
|
||||
} else {
|
||||
bool skip = false;
|
||||
|
||||
retry:
|
||||
switch (Waypoints[cur].type) {
|
||||
case "walk":
|
||||
if (!DoWalk(ref skip)) goto retry;
|
||||
break;
|
||||
case "teleport":
|
||||
DoTeleport();
|
||||
return;
|
||||
case "wait":
|
||||
if (!DoWait(ref skip)) goto retry;
|
||||
return;
|
||||
case "nod":
|
||||
if (!DoNod(ref skip)) goto retry;
|
||||
return;
|
||||
case "spin":
|
||||
if (!DoSpin(ref skip)) goto retry;
|
||||
return;
|
||||
case "speed":
|
||||
if (!DoSpeed(ref skip)) goto retry;
|
||||
return;
|
||||
case "reset":
|
||||
cur = 0;
|
||||
return;
|
||||
case "remove":
|
||||
PlayerBot.Remove(this);
|
||||
return;
|
||||
case "linkscript":
|
||||
if (File.Exists("bots/" + Waypoints[cur].newscript)) {
|
||||
Command.all.Find("botset").Use(null, name + " " + Waypoints[cur].newscript);
|
||||
return;
|
||||
}
|
||||
|
||||
cur++;
|
||||
bool doNextInstruction = !DoInstruction();
|
||||
if (cur == Waypoints.Count) cur = 0;
|
||||
if (!skip) { skip = true; goto retry; }
|
||||
return;
|
||||
case "jump":
|
||||
if (!DoJump(ref skip)) goto retry;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!doNextInstruction) return;
|
||||
DoInstruction();
|
||||
if (cur == Waypoints.Count) cur = 0;
|
||||
}
|
||||
AdvanceRotation();
|
||||
}
|
||||
|
||||
bool DoInstruction() {
|
||||
Func<PlayerBot, bool> instruction;
|
||||
if (!Instructions.Defined.TryGetValue(Waypoints[cur].type, out instruction))
|
||||
return false;
|
||||
return instruction(this);
|
||||
}
|
||||
|
||||
public void AdvanceRotation() {
|
||||
@ -144,124 +113,7 @@ namespace MCGalaxy {
|
||||
|
||||
public void NextInstruction() {
|
||||
cur++;
|
||||
if (cur == Waypoints.Count)
|
||||
cur = 0;
|
||||
}
|
||||
|
||||
bool DoWalk(ref bool skip) {
|
||||
foundPos[0] = Waypoints[cur].x;
|
||||
foundPos[1] = Waypoints[cur].y;
|
||||
foundPos[2] = Waypoints[cur].z;
|
||||
movement = true;
|
||||
|
||||
if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[cur].x / 32)) {
|
||||
if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[cur].z / 32)) {
|
||||
rot[0] = Waypoints[cur].rotx;
|
||||
rot[1] = Waypoints[cur].roty;
|
||||
movement = false;
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DoTeleport() {
|
||||
pos[0] = Waypoints[cur].x;
|
||||
pos[1] = Waypoints[cur].y;
|
||||
pos[2] = Waypoints[cur].z;
|
||||
rot[0] = Waypoints[cur].rotx;
|
||||
rot[1] = Waypoints[cur].roty;
|
||||
NextInstruction();
|
||||
}
|
||||
|
||||
bool DoWait(ref bool skip) {
|
||||
if (countdown != 0) {
|
||||
countdown--;
|
||||
if (countdown == 0) {
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
}
|
||||
} else {
|
||||
countdown = Waypoints[cur].seconds;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DoNod(ref bool skip) {
|
||||
if (countdown != 0) {
|
||||
countdown--;
|
||||
|
||||
if (nodUp) {
|
||||
if (rot[1] > 32 && rot[1] < 128) nodUp = !nodUp;
|
||||
else
|
||||
{
|
||||
if (rot[1] + (byte)Waypoints[cur].rotspeed > 255) rot[1] = 0;
|
||||
else rot[1] += (byte)Waypoints[cur].rotspeed;
|
||||
}
|
||||
} else {
|
||||
if (rot[1] > 128 && rot[1] < 224) nodUp = !nodUp;
|
||||
else
|
||||
{
|
||||
if (rot[1] - (byte)Waypoints[cur].rotspeed < 0) rot[1] = 255;
|
||||
else rot[1] -= (byte)Waypoints[cur].rotspeed;
|
||||
}
|
||||
}
|
||||
|
||||
if (countdown == 0) {
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
}
|
||||
} else {
|
||||
countdown = Waypoints[cur].seconds;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DoSpin(ref bool skip) {
|
||||
if (countdown != 0) {
|
||||
countdown--;
|
||||
|
||||
if (rot[0] + (byte)Waypoints[cur].rotspeed > 255) rot[0] = 0;
|
||||
else if (rot[0] + (byte)Waypoints[cur].rotspeed < 0) rot[0] = 255;
|
||||
else rot[0] += (byte)Waypoints[cur].rotspeed;
|
||||
|
||||
if (countdown == 0) {
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
}
|
||||
} else {
|
||||
countdown = Waypoints[cur].seconds;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DoSpeed(ref bool skip) {
|
||||
movementSpeed = (int)Math.Round(24m / 100m * Waypoints[cur].seconds);
|
||||
if (movementSpeed == 0) movementSpeed = 1;
|
||||
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DoJump(ref bool skip) {
|
||||
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();
|
||||
NextInstruction();
|
||||
if (!skip) { skip = true; return false; }
|
||||
return true;
|
||||
if (cur == Waypoints.Count) cur = 0;
|
||||
}
|
||||
|
||||
void MoveTimerFunc(object sender, ElapsedEventArgs e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user