diff --git a/MCGalaxy/Bots/BotsFile.cs b/MCGalaxy/Bots/BotsFile.cs index b345995ab..c753b9846 100644 --- a/MCGalaxy/Bots/BotsFile.cs +++ b/MCGalaxy/Bots/BotsFile.cs @@ -109,17 +109,7 @@ namespace MCGalaxy.Bots { } } - public static void RemoveLevelBots(string level) { - lock (locker) { - for (int i = 0; i < SavedBots.Count; i++) { - BotProperties props = SavedBots[i]; - if (level != props.Level) continue; - SavedBots.RemoveAt(i); i--; - } - Save(); - } - } - + /// Deletes all bots which are located on the given map. public static void DeleteBots(string level) { lock (locker) { int removed = 0; @@ -129,23 +119,43 @@ namespace MCGalaxy.Bots { SavedBots.RemoveAt(i); removed++; i--; - } + } if (removed > 0) Save(); } } + /// Moves all bots located on the given source map to the destination map. public static void MoveBots(string srcLevel, string dstLevel) { lock (locker) { int moved = 0; for (int i = 0; i < SavedBots.Count; i++) { BotProperties props = SavedBots[i]; if (!props.Level.CaselessEq(srcLevel)) continue; - props.Level = dstLevel; moved++; + + props.Level = dstLevel; + moved++; } if (moved > 0) Save(); } } + /// Copies all bots located on the given source map to the destination map. + public static void CopyBots(string srcLevel, string dstLevel) { + lock (locker) { + int copied = 0, count = SavedBots.Count; + for (int i = 0; i < SavedBots.Count; i++) { + BotProperties props = SavedBots[i]; + if (!props.Level.CaselessEq(srcLevel)) continue; + + BotProperties copy = props.Copy(); + copy.Level = dstLevel; + SavedBots.Add(copy); + copied++; + } + if (copied > 0) Save(); + } + } + public static void UpdateBot(PlayerBot bot) { lock (locker) DoUpdateBot(bot, true); } @@ -195,5 +205,19 @@ namespace MCGalaxy.Bots { X = bot.pos[0]; Y = bot.pos[1]; Z = bot.pos[2]; RotX = bot.rot[0]; RotY = bot.rot[1]; } + + public BotProperties Copy() { + BotProperties copy = new BotProperties(); + copy.DisplayName = DisplayName; copy.Name = Name; + copy.Level = Level; copy.Skin = Skin; + copy.Model = Model; copy.Color = Color; + + copy.AI = AI; copy.Kill = Kill; + copy.Hunt = Hunt; copy.CurInstruction = CurInstruction; + + copy.X = X; copy.Y = Y; copy.Z = Z; + copy.RotX = RotX; copy.RotY = RotY; + return copy; + } } } diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index 701436a2e..59de4b03c 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -93,15 +93,15 @@ namespace MCGalaxy { public static void UnloadFromLevel(Level lvl) { BotsFile.UnloadBots(lvl); - RemoveAll(lvl, false); + RemoveLoadedBots(lvl, false); } public static void RemoveAllFromLevel(Level lvl) { - RemoveAll(lvl, true); - BotsFile.RemoveLevelBots(lvl.name); + RemoveLoadedBots(lvl, true); + BotsFile.DeleteBots(lvl.name); } - static void RemoveAll(Level lvl, bool save) { + static void RemoveLoadedBots(Level lvl, bool save) { PlayerBot[] bots = Bots.Items; for (int i = 0; i < bots.Length; i++) { PlayerBot bot = bots[i]; diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index 11f1721c8..3fdc4834a 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -31,21 +31,22 @@ namespace MCGalaxy { public static void Rename(string src, string dst) { File.Move(LevelInfo.MapPath(src), LevelInfo.MapPath(dst)); - SafeMove(LevelInfo.MapPath(src) + ".backup", - LevelInfo.MapPath(dst) + ".backup"); - SafeMove("levels/level properties/" + src + ".properties", - "levels/level properties/" + dst + ".properties"); - SafeMove("levels/level properties/" + src, - "levels/level properties/" + dst + ".properties"); - SafeMove("blockdefs/lvl_" + src + ".json", - "blockdefs/lvl_" + dst + ".json"); - SafeMove("blockprops/lvl_" + src + ".txt", - "blockprops/lvl_" + dst + ".txt"); + MoveIfExists(LevelInfo.MapPath(src) + ".backup", + LevelInfo.MapPath(dst) + ".backup"); + MoveIfExists("levels/level properties/" + src + ".properties", + "levels/level properties/" + dst + ".properties"); + MoveIfExists("levels/level properties/" + src, + "levels/level properties/" + dst + ".properties"); + MoveIfExists("blockdefs/lvl_" + src + ".json", + "blockdefs/lvl_" + dst + ".json"); + MoveIfExists("blockprops/lvl_" + src + ".txt", + "blockprops/lvl_" + dst + ".txt"); try { MoveBackups(src, dst); } catch { } + BotsFile.MoveBots(src, dst); RenameDatabaseTables(src, dst); BlockDBFile.MoveBackingFile(src, dst); @@ -75,7 +76,7 @@ namespace MCGalaxy { } } - static void SafeMove(string src, string dst) { + static void MoveIfExists(string src, string dst) { if (!File.Exists(src)) return; try { File.Move(src, dst); @@ -127,10 +128,10 @@ namespace MCGalaxy { File.Move(LevelInfo.MapPath(name), LevelInfo.DeletedPath(name)); } - SafeDelete("levels/level properties/" + name); - SafeDelete("levels/level properties/" + name + ".properties"); - SafeDelete("blockdefs/lvl_" + name + ".json"); - SafeDelete("blockprops/lvl_" + name + ".txt"); + DeleteIfExists("levels/level properties/" + name); + DeleteIfExists("levels/level properties/" + name + ".properties"); + DeleteIfExists("blockdefs/lvl_" + name + ".json"); + DeleteIfExists("blockprops/lvl_" + name + ".txt"); BotsFile.DeleteBots(name); DeleteDatabaseTables(name); @@ -155,7 +156,7 @@ namespace MCGalaxy { } } - static void SafeDelete(string src) { + static void DeleteIfExists(string src) { if (!File.Exists(src)) return; try { File.Delete(src); @@ -208,14 +209,16 @@ namespace MCGalaxy { public static void CopyLevel(string src, string dst) { File.Copy(LevelInfo.MapPath(src), LevelInfo.MapPath(dst)); - SafeCopy("levels/level properties/" + src, - "levels/level properties/" + dst + ".properties"); - SafeCopy("levels/level properties/" + src + ".properties", - "levels/level properties/" + dst + ".properties"); - SafeCopy("blockdefs/lvl_" + src + ".json", - "blockdefs/lvl_" + dst + ".json"); - SafeCopy("blockprops/lvl_" + src + ".txt", - "blockprops/lvl_" + dst + ".txt"); + CopyIfExists("levels/level properties/" + src, + "levels/level properties/" + dst + ".properties"); + CopyIfExists("levels/level properties/" + src + ".properties", + "levels/level properties/" + dst + ".properties"); + CopyIfExists("blockdefs/lvl_" + src + ".json", + "blockdefs/lvl_" + dst + ".json"); + CopyIfExists("blockprops/lvl_" + src + ".txt", + "blockprops/lvl_" + dst + ".txt"); + + BotsFile.CopyBots(src, dst); CopyDatabaseTables(src, dst); } @@ -244,7 +247,7 @@ namespace MCGalaxy { } } - static void SafeCopy(string src, string dst) { + static void CopyIfExists(string src, string dst) { if (!File.Exists(src)) return; try { File.Copy(src, dst, true);