From db294ce5cfb91d3ec7ccfa38607cafa0685d2231 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 18 Apr 2018 08:12:41 +1000 Subject: [PATCH] Add /bot copy --- MCGalaxy/Bots/BotsFile.cs | 6 +- MCGalaxy/Bots/PlayerBot.cs | 4 +- MCGalaxy/Commands/Bots/CmdBot.cs | 108 ++++++++++++-------- MCGalaxy/Commands/Information/CmdMapInfo.cs | 4 +- MCGalaxy/CorePlugin/MiscHandlers.cs | 2 +- MCGalaxy/Entity/Entities.cs | 3 +- MCGalaxy/Entity/Entity.cs | 9 ++ MCGalaxy/Levels/Level.cs | 2 +- MCGalaxy/Player/Player.Login.cs | 5 +- 9 files changed, 87 insertions(+), 56 deletions(-) diff --git a/MCGalaxy/Bots/BotsFile.cs b/MCGalaxy/Bots/BotsFile.cs index 421f3327f..0c6f4c276 100644 --- a/MCGalaxy/Bots/BotsFile.cs +++ b/MCGalaxy/Bots/BotsFile.cs @@ -44,7 +44,7 @@ namespace MCGalaxy.Bots { PlayerBot bot = new PlayerBot(props.Name, lvl); props.ApplyTo(bot); - bot.ModelBB = AABB.ModelAABB(bot, lvl); + bot.SetModel(bot.Model, lvl); LoadAi(props, bot); PlayerBot.Add(bot, false); } @@ -72,7 +72,7 @@ namespace MCGalaxy.Bots { } } - static void LoadAi(BotProperties props, PlayerBot bot) { + internal static void LoadAi(BotProperties props, PlayerBot bot) { if (String.IsNullOrEmpty(props.AI)) return; try { ScriptFile.Parse(null, bot, "bots/" + props.AI); @@ -128,7 +128,7 @@ namespace MCGalaxy.Bots { } public void ApplyTo(PlayerBot bot) { - bot.Pos = new Position(X, Y, Z); + bot.SetInitialPos(new Position(X, Y, Z)); bot.SetYawPitch(RotX, RotY); Orientation rot = bot.Rot; rot.RotX = BodyX; rot.RotZ = BodyZ; diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index 3abbdda2c..860998c33 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -48,9 +48,9 @@ namespace MCGalaxy { public PlayerBot(string n, Level lvl) { name = n; DisplayName = n; SkinName = n; - color = "&1"; - ModelBB = AABB.ModelAABB(this, lvl); + color = "&1"; level = lvl; + SetModel(Model, lvl); hasExtPositions = true; BotsScheduler.Activate(); } diff --git a/MCGalaxy/Commands/Bots/CmdBot.cs b/MCGalaxy/Commands/Bots/CmdBot.cs index 8f3dacb73..447738a85 100644 --- a/MCGalaxy/Commands/Bots/CmdBot.cs +++ b/MCGalaxy/Commands/Bots/CmdBot.cs @@ -35,39 +35,42 @@ namespace MCGalaxy.Commands.Bots { if (args.Length < 2) { Help(p); return; } if (!Formatter.ValidName(p, args[1], "bot")) return; + string bot = args[1], value = args.Length > 2 ? args[2] : null; if (args[0].CaselessEq("add")) { - AddBot(p, args[1]); + AddBot(p, bot); } else if (args[0].CaselessEq("remove")) { - RemoveBot(p, args[1]); + RemoveBot(p, bot); } else if (args[0].CaselessEq("text")) { - string text = args.Length > 2 ? args[2] : null; - SetBotText(p, args[1], text); + SetBotText(p, bot, value); } else if (args[0].CaselessEq("deathmsg") || args[0].CaselessEq("deathmessage")) { - string text = args.Length > 2 ? args[2] : null; - SetDeathMessage(p, args[1], text); + SetDeathMessage(p, bot, value); } else if (args[0].CaselessEq("rename")) { - string newName = args.Length > 2 ? args[2] : null; - RenameBot(p, args[1], newName); - } else { + RenameBot(p, bot, value); + } else if (args[0].CaselessEq("copy")) { + CopyBot(p, bot, value); + } else { Help(p); } } void AddBot(Player p, string botName) { if (!LevelInfo.ValidateAction(p, p.level.name, "add bots to this level")) return; - - if (BotExists(p.level, botName, null)) { + PlayerBot bot = new PlayerBot(botName, p.level); + TryAddBot(p, bot); + } + + void TryAddBot(Player p, PlayerBot bot) { + if (BotExists(p.level, bot.name, null)) { Player.Message(p, "A bot with that name already exists."); return; } if (p.level.Bots.Count >= ServerConfig.MaxBotsPerLevel) { Player.Message(p, "Reached maximum number of bots allowed on this map."); return; } - PlayerBot bot = new PlayerBot(botName, p.level); - bot.Pos = p.Pos; bot.lastPos = bot.Pos; + bot.SetInitialPos(p.Pos); bot.SetYawPitch(p.Rot.RotY, 0); - Player.Message(p, "You added the bot " + bot.ColoredName ); + Player.Message(p, "You added the bot " + bot.ColoredName); PlayerBot.Add(bot); } @@ -95,9 +98,41 @@ namespace MCGalaxy.Commands.Bots { } } + void SetBotText(Player p, string botName, string text) { + PlayerBot bot = Matcher.FindBots(p, botName); + if (bot == null) return; + if (!LevelInfo.ValidateAction(p, p.level.name, "set bot text of that bot")) return; + + if (text == null) { + Player.Message(p, "Removed text shown when bot {0} %Sclicked on", bot.ColoredName); + bot.ClickedOnText = null; + } else { + if (!MessageBlock.Validate(p, text, false)) return; + Player.Message(p, "Set text shown when bot {0} %Sis clicked on to {1}", bot.ColoredName, text); + bot.ClickedOnText = text; + } + BotsFile.Save(bot.level); + } + + void SetDeathMessage(Player p, string botName, string text) { + PlayerBot bot = Matcher.FindBots(p, botName); + if (bot == null) return; + if (!LevelInfo.ValidateAction(p, p.level.name, "set kill message of that bot")) return; + + if (text == null) { + Player.Message(p, "Reset shown when bot {0} %Skills someone", bot.ColoredName); + bot.DeathMessage = null; + } else { + if (!MessageBlock.Validate(p, text, false)) return; + Player.Message(p, "Set message shown when bot {0} %Skills someone to {1}", bot.ColoredName, text); + bot.DeathMessage = text; + } + BotsFile.Save(bot.level); + } + void RenameBot(Player p, string botName, string newName) { if (!LevelInfo.ValidateAction(p, p.level.name, "rename bots on this level")) return; - if (newName == null) { Player.Message(p, "New name of bot required."); return; } + if (newName == null) { Player.Message(p, "New name of bot required."); return; } if (!Formatter.ValidName(p, newName, "bot")) return; PlayerBot bot = Matcher.FindBots(p, botName); @@ -117,36 +152,24 @@ namespace MCGalaxy.Commands.Bots { BotsFile.Save(bot.level); } - void SetBotText(Player p, string botName, string text) { + void CopyBot(Player p, string botName, string newName) { + if (!LevelInfo.ValidateAction(p, p.level.name, "copy bots on this level")) return; + if (newName == null) { Player.Message(p, "Name of new bot required."); return; } + if (!Formatter.ValidName(p, newName, "bot")) return; + PlayerBot bot = Matcher.FindBots(p, botName); if (bot == null) return; - if (!LevelInfo.ValidateAction(p, p.level.name, "set bot text of that bot")) return; - if (text == null) { - Player.Message(p, "Removed text shown when bot {0} %Sclicked on", bot.ColoredName); - bot.ClickedOnText = null; - } else { - if (!MessageBlock.Validate(p, text, false)) return; - Player.Message(p, "Set text shown when bot {0} %Sis clicked on to {1}", bot.ColoredName, text); - bot.ClickedOnText = text; - } - BotsFile.Save(bot.level); - } - - void SetDeathMessage(Player p, string botName, string text) { - PlayerBot bot = Matcher.FindBots(p, botName); - if (bot == null) return; - if (!LevelInfo.ValidateAction(p, p.level.name, "set kill message of that bot")) return; - - if (text == null) { - Player.Message(p, "Reset shown when bot {0} %Skills someone", bot.ColoredName); - bot.DeathMessage = null; - } else { - if (!MessageBlock.Validate(p, text, false)) return; - Player.Message(p, "Set message shown when bot {0} %Skills someone to {1}", bot.ColoredName, text); - bot.DeathMessage = text; - } - BotsFile.Save(bot.level); + PlayerBot clone = new PlayerBot(newName, p.level); + BotProperties props = new BotProperties(); + props.FromBot(bot); + props.ApplyTo(clone); + + clone.SetModel(clone.Model, p.level); + BotsFile.LoadAi(props, clone); + // Preserve custom name tag + if (bot.DisplayName == bot.name) clone.DisplayName = newName; + TryAddBot(p, clone); } public override void Help(Player p) { @@ -160,6 +183,7 @@ namespace MCGalaxy.Commands.Bots { Player.Message(p, "%HSets the message shown when this bot kills a player"); Player.Message(p, "%T/Bot rename [name] [new name] %H- Renames a bot"); Player.Message(p, "%H Note: To only change name tag of a bot, use %T/Nick bot"); + Player.Message(p, "%T/Bot copy [name] [new name] %H- Clones an existing bot"); } } } diff --git a/MCGalaxy/Commands/Information/CmdMapInfo.cs b/MCGalaxy/Commands/Information/CmdMapInfo.cs index 49613b869..37b5c0b99 100644 --- a/MCGalaxy/Commands/Information/CmdMapInfo.cs +++ b/MCGalaxy/Commands/Information/CmdMapInfo.cs @@ -107,7 +107,7 @@ namespace MCGalaxy.Commands.Info { string realmOwner = cfg.RealmOwner; if (String.IsNullOrEmpty(cfg.RealmOwner)) { - realmOwner = GetRealmMapOwner(data.Name); + realmOwner = DefaultRealmOwner(data.Name); } if (String.IsNullOrEmpty(realmOwner)) return; @@ -135,7 +135,7 @@ namespace MCGalaxy.Commands.Info { cfg.Likes, cfg.Dislikes); } - static string GetRealmMapOwner(string map) { + static string DefaultRealmOwner(string map) { bool plus = ServerConfig.ClassicubeAccountPlus; // Early out when accounts have + and map doesn't. if (plus && map.IndexOf('+') == -1) return null; diff --git a/MCGalaxy/CorePlugin/MiscHandlers.cs b/MCGalaxy/CorePlugin/MiscHandlers.cs index 9c0df54a7..0503303bc 100644 --- a/MCGalaxy/CorePlugin/MiscHandlers.cs +++ b/MCGalaxy/CorePlugin/MiscHandlers.cs @@ -44,7 +44,7 @@ namespace MCGalaxy.Core { p.prevMsg = ""; p.showMBs = false; p.showPortals = false; - p.ModelBB = AABB.ModelAABB(p, level); // in case had been using a level-only custom block for their model + p.SetModel(p.Model, level); // in case had been using a level-only custom block for their model if (!Hacks.CanUseHacks(p, level) && p.isFlying) { Player.Message(p, "You cannot use /fly on this map."); diff --git a/MCGalaxy/Entity/Entities.cs b/MCGalaxy/Entity/Entities.cs index 66f521c2e..84fc44c30 100644 --- a/MCGalaxy/Entity/Entities.cs +++ b/MCGalaxy/Entity/Entities.cs @@ -182,9 +182,8 @@ namespace MCGalaxy { public static void UpdateModel(Entity entity, string model) { Player[] players = PlayerInfo.Online.Items; - entity.Model = model; Level lvl = entity.Level; - entity.ModelBB = AABB.ModelAABB(entity, lvl); + entity.SetModel(model, lvl); foreach (Player pl in players) { if (pl.level != lvl || !pl.Supports(CpeExt.ChangeModel)) continue; diff --git a/MCGalaxy/Entity/Entity.cs b/MCGalaxy/Entity/Entity.cs index 8c8543d3b..352f9fe62 100644 --- a/MCGalaxy/Entity/Entity.cs +++ b/MCGalaxy/Entity/Entity.cs @@ -46,6 +46,15 @@ namespace MCGalaxy { set { Interlocked.Exchange(ref _pos, value.Pack()); OnSetPos(); } } + public void SetInitialPos(Position pos) { + Pos = pos; lastPos = pos; + } + + public void SetModel(string model, Level lvl) { + Model = model; + ModelBB = AABB.ModelAABB(this, lvl); + } + public void SetYawPitch(byte yaw, byte pitch) { Orientation rot = Rot; rot.RotY = yaw; rot.HeadX = pitch; diff --git a/MCGalaxy/Levels/Level.cs b/MCGalaxy/Levels/Level.cs index 72ac22b8a..3c4653b01 100644 --- a/MCGalaxy/Levels/Level.cs +++ b/MCGalaxy/Levels/Level.cs @@ -242,7 +242,7 @@ namespace MCGalaxy { if (!Directory.Exists("levels/prev")) Directory.CreateDirectory("levels/prev"); if (Changed || !File.Exists(path) || force || (physicschanged && clearPhysics)) { - lock (saveLock) SaveCore(path); + lock (saveLock) SaveCore(path); if (clearPhysics) ClearPhysics(); } else { Logger.Log(LogType.SystemActivity, "Skipping level save for " + name + "."); diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index 5c64ed252..086b01041 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -211,9 +211,8 @@ namespace MCGalaxy { byte.TryParse(bits[0], out rot.RotX); byte.TryParse(bits[1], out rot.RotZ); Rot = rot; - } - - ModelBB = AABB.ModelAABB(this, level); + } + SetModel(Model, level); } void GetPlayerStats() {