mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-24 05:03:34 -04:00
Cleanup /botai a bit.
This commit is contained in:
parent
ef7a6d31f1
commit
89f08dad70
@ -22,7 +22,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to nod spin around for a certain interval. </summary>
|
||||
public class SpinInstruction : BotInstruction {
|
||||
public override string Name { get { return "Spin"; } }
|
||||
public override string Name { get { return "spin"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
Metadata meta = (Metadata)data.Metadata;
|
||||
@ -52,7 +52,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to nod down up and down for a certain interval. </summary>
|
||||
public sealed class NodInstruction : SpinInstruction {
|
||||
public override string Name { get { return "Nod"; } }
|
||||
public override string Name { get { return "nod"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
Metadata meta = (Metadata)data.Metadata;
|
||||
|
@ -21,7 +21,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to move towards the closest human, within a defined search radius. </summary>
|
||||
public sealed class HuntInstruction : BotInstruction {
|
||||
public override string Name { get { return "Hunt"; } }
|
||||
public override string Name { get { return "hunt"; } }
|
||||
|
||||
const int searchRadius = 75;
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
@ -68,7 +68,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to kill nearby humans. </summary>
|
||||
public sealed class KillInstruction : BotInstruction {
|
||||
public override string Name { get { return "Kill"; } }
|
||||
public override string Name { get { return "kill"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
Player[] players = PlayerInfo.Online.Items;
|
||||
|
@ -32,11 +32,16 @@ namespace MCGalaxy.Bots {
|
||||
/// next instruction in the same tick. </returns>
|
||||
public abstract bool Execute(PlayerBot bot, InstructionData data);
|
||||
|
||||
/// <summary> Parses the given line which contains the metadata for this instruction. </summary>
|
||||
/// <summary> Parses the given arguments which contains the data for this instruction. </summary>
|
||||
public virtual InstructionData Parse(string[] args) {
|
||||
return default(InstructionData);
|
||||
}
|
||||
|
||||
/// <summary> Writes the data for this instruction to the given AI file. </summary>
|
||||
public virtual void Output(Player p, string[] args, StreamWriter w) {
|
||||
w.WriteLine(Name);
|
||||
}
|
||||
|
||||
/// <summary> All instructions that bots can execute. </summary>
|
||||
public static List<BotInstruction> Instructions = new List<BotInstruction>() {
|
||||
new NodInstruction(), new SpinInstruction(), new HuntInstruction(), new KillInstruction(),
|
||||
|
@ -21,7 +21,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to instantly teleport to a position. </summary>
|
||||
public class TeleportInstruction : BotInstruction {
|
||||
public override string Name { get { return "Teleport"; } }
|
||||
public override string Name { get { return "teleport"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
Coords coords = (Coords)data.Metadata;
|
||||
@ -52,7 +52,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to gradually move to to a position. </summary>
|
||||
public sealed class WalkInstruction : TeleportInstruction {
|
||||
public override string Name { get { return "Walk"; } }
|
||||
public override string Name { get { return "walk"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
Coords target = (Coords)data.Metadata;
|
||||
@ -73,7 +73,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to begin jumping. </summary>
|
||||
public sealed class JumpInstruction : BotInstruction {
|
||||
public override string Name { get { return "Jump"; } }
|
||||
public override string Name { get { return "jump"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
bot.jumping = true;
|
||||
@ -83,7 +83,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to change how fast it moves. </summary>
|
||||
public sealed class SpeedInstruction : BotInstruction {
|
||||
public override string Name { get { return "Speed"; } }
|
||||
public override string Name { get { return "speed"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
bot.movementSpeed = (int)Math.Round(3m * (short)data.Metadata / 100m);
|
||||
|
@ -22,7 +22,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to reset to the and execute first instruction. </summary>
|
||||
public sealed class ResetInstruction : BotInstruction {
|
||||
public override string Name { get { return "Reset"; } }
|
||||
public override string Name { get { return "reset"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
bot.cur = 0; return false;
|
||||
@ -31,7 +31,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to be removed from the world. </summary>
|
||||
public sealed class RemoveInstruction : BotInstruction {
|
||||
public override string Name { get { return "Remove"; } }
|
||||
public override string Name { get { return "remove"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
PlayerBot.Remove(bot); return true;
|
||||
@ -40,7 +40,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to switch to a different AI. </summary>
|
||||
public sealed class LinkScriptInstruction : BotInstruction {
|
||||
public override string Name { get { return "LinkScript"; } }
|
||||
public override string Name { get { return "linkscript"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
string script = (string)data.Metadata;
|
||||
@ -60,7 +60,7 @@ namespace MCGalaxy.Bots {
|
||||
|
||||
/// <summary> Causes the bot to wait/do nothing for a certain interval. </summary>
|
||||
public sealed class WaitInstruction : BotInstruction {
|
||||
public override string Name { get { return "Wait"; } }
|
||||
public override string Name { get { return "wait"; } }
|
||||
|
||||
public override bool Execute(PlayerBot bot, InstructionData data) {
|
||||
if (bot.countdown == 0) {
|
||||
|
@ -59,39 +59,45 @@ namespace MCGalaxy.Commands
|
||||
}
|
||||
|
||||
void HandleDelete(Player p, string ai, string[] args) {
|
||||
if (!Directory.Exists("bots/deleted")) Directory.CreateDirectory("bots/deleted");
|
||||
|
||||
int currentTry = 0;
|
||||
if (!Directory.Exists("bots/deleted"))
|
||||
Directory.CreateDirectory("bots/deleted");
|
||||
if (!File.Exists("bots/" + ai)) {
|
||||
Player.Message(p, "Could not find specified AI."); return;
|
||||
}
|
||||
|
||||
retry:
|
||||
try {
|
||||
if (args.Length == 2) {
|
||||
if (currentTry == 0)
|
||||
File.Move("bots/" + ai, "bots/deleted/" + ai);
|
||||
else
|
||||
File.Move("bots/" + ai, "bots/deleted/" + ai + currentTry);
|
||||
} else {
|
||||
if (args[2].ToLower() == "last") {
|
||||
string[] Lines = File.ReadAllLines("bots/" + ai);
|
||||
string[] outLines = new string[Lines.Length - 1];
|
||||
for (int i = 0; i < Lines.Length - 1; i++) {
|
||||
outLines[i] = Lines[i];
|
||||
}
|
||||
|
||||
File.WriteAllLines("bots/" + ai, outLines);
|
||||
Player.Message(p, "Deleted the last instruction from " + ai);
|
||||
return;
|
||||
for (int attempt = 0; attempt < 10; attempt++) {
|
||||
try {
|
||||
if (args.Length == 2) {
|
||||
DeleteAI(p, ai); return;
|
||||
} else if (args[2].ToLower() == "last") {
|
||||
DeleteLast(p, ai); return;
|
||||
} else {
|
||||
Help(p); return;
|
||||
}
|
||||
} catch (IOException) {
|
||||
}
|
||||
}
|
||||
catch (IOException) { currentTry++; goto retry; }
|
||||
}
|
||||
|
||||
static void DeleteAI(Player p, string ai, int attempt) {
|
||||
if (attempt == 0) {
|
||||
File.Move("bots/" + ai, "bots/deleted/" + ai);
|
||||
} else {
|
||||
File.Move("bots/" + ai, "bots/deleted/" + ai + attempt);
|
||||
}
|
||||
Player.Message(p, "Deleted &b" + ai);
|
||||
}
|
||||
|
||||
static void DeleteLast(Player p, string ai) {
|
||||
string[] Lines = File.ReadAllLines("bots/" + ai);
|
||||
string[] outLines = new string[Lines.Length - 1];
|
||||
for (int i = 0; i < Lines.Length - 1; i++) {
|
||||
outLines[i] = Lines[i];
|
||||
}
|
||||
|
||||
File.WriteAllLines("bots/" + ai, outLines);
|
||||
Player.Message(p, "Deleted the last instruction from " + ai);
|
||||
}
|
||||
|
||||
void HandleAdd(Player p, string ai, string action = "", string extra = "10", string more = "2") {
|
||||
string[] allLines;
|
||||
@ -127,8 +133,8 @@ namespace MCGalaxy.Commands
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
Player.Message(p, "Invalid parameter");
|
||||
} catch {
|
||||
Player.Message(p, "Invalid parameter");
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,8 +155,8 @@ namespace MCGalaxy.Commands
|
||||
Player.Message(p, "%T/botai info [name] %H- prints list of instructions that AI has");
|
||||
Player.Message(p, "%T/botai add [name] [instruction] <args>");
|
||||
|
||||
var instructions = Instructions.Defined.Keys;
|
||||
Player.Message(p, "%HInstructions: %S{0}, reverse", instructions.Join());
|
||||
Player.Message(p, "%HInstructions: %S{0}, reverse",
|
||||
BotInstruction.Instructions.Join(ins => ins.Name.ToLower()));
|
||||
Player.Message(p, "%HTo see extended help, type %T/help botai [instruction]");
|
||||
Player.Message(p, "%Hwait, nod, spin %S- optional arg specifies '0.1 seconds'");
|
||||
Player.Message(p, "%Hnod, spin %S- optional second arg specifies 'speed'");
|
||||
|
Loading…
x
Reference in New Issue
Block a user