OSDN Git Service

Snapshots crash fix
[automap/automap.git] / Automap / Designators / DefaultDesignators.cs
index ff4ad14..652b5c1 100644 (file)
@@ -4,6 +4,8 @@ using System.Drawing;
 using System.Text;
 using System.Text.RegularExpressions;
 
+using HarmonyLib;
+
 using Vintagestory.API.Client;
 using Vintagestory.API.Common;
 using Vintagestory.API.Common.Entities;
@@ -14,7 +16,9 @@ using Vintagestory.GameContent;
 namespace Automap
 {
        public static class DefaultDesignators
-       {               
+       {
+               #region Defaults
+
                public static BlockDesignator Roads =
                         new BlockDesignator(
                                new AssetLocation("game", "stonepath"),
@@ -54,6 +58,14 @@ namespace Automap
                                DecodeTranslocator
                        );
 
+               public static BlockDesignator Teleporters =
+                        new BlockDesignator(
+                               new AssetLocation("game", "teleporterbase"),
+                               Color.SeaGreen,
+                               EnumBlockMaterial.Wood,
+                               DecodeTeleport
+                       );
+
                public static EntityDesignator Traders =
                         new EntityDesignator(
                                new AssetLocation("game", "humanoid-trader"),
@@ -62,99 +74,167 @@ namespace Automap
                                KeepTrackOfMerchant
                        );
 
+               public static BlockDesignator Wildbeehives =
+                        new BlockDesignator(
+                               new AssetLocation("game", "wildbeehive"),
+                               Color.Honeydew,
+                               EnumBlockMaterial.Other,
+                               NoteWildbeehive,
+                               false
+                       );
+
+               public static BlockDesignator PineResinLeaks =
+                        new BlockDesignator(
+                               new AssetLocation("game", "log-resin"),
+                               Color.DarkOrange,
+                               EnumBlockMaterial.Wood,
+                               NotePineResinLeak,
+                               false
+                       );
+
+
+
                /// <summary>
                /// Not just blocks, but block-entities as well!
                /// </summary>
                /// <returns>The block designators.</returns>
-               public static List<BlockDesignator> DefaultBlockDesignators()
+               public static List<BlockDesignator> DefaultBlockDesignators
                {
-                       return new List<BlockDesignator>{
-                               DefaultDesignators.Roads,
-                               DefaultDesignators.GroundSigns,
-                               DefaultDesignators.WallSigns,
-                               DefaultDesignators.PostSigns,
-                               DefaultDesignators.Translocators,
-                               };
+                       get
+                       {
+                               return new List<BlockDesignator>{
+                                       DefaultDesignators.Roads,
+                                       DefaultDesignators.GroundSigns,
+                                       DefaultDesignators.WallSigns,
+                                       DefaultDesignators.PostSigns,
+                                       DefaultDesignators.Translocators,
+                                       DefaultDesignators.Teleporters,
+                       DefaultDesignators.Wildbeehives,
+                       DefaultDesignators.PineResinLeaks,
+                                       };
+                       }
                }
 
-               public static List<EntityDesignator> DefaultEntityDesignators()
+               public static List<EntityDesignator> DefaultEntityDesignators
                {
-                       return new List<EntityDesignator>{
-                               DefaultDesignators.Traders,
-               };
+                       get
+                       {
+                               return new List<EntityDesignator>{
+                                               DefaultDesignators.Traders,
+                               };
+                       }
                }
 
+               internal static Encoding SaferUnicodeEncoding = Encoding.GetEncoding(Encoding.UTF8.WebName,
+               new EncoderReplacementFallback(@" "),
+               new DecoderReplacementFallback(@" "));
+
+               #endregion
+
+
                #region Designators
 
                internal static void DecodeSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
                {
-#if DEBUG
-                       clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
-#endif
-                       //sign Text into a POI field...
-                       BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
+               #if DEBUG
+               clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
+               #endif
+               //sign Text into a POI field...
+               BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
 
-                       if (signEntity != null && !String.IsNullOrEmpty(signEntity.text))
-                       {
+               if (signEntity != null && !String.IsNullOrEmpty(signEntity.text)) {
 
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Sign",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = signEntity.text,
-                                                               Timestamp = DateTime.UtcNow,
-                                                       }
-                                                       );
-                       }
+               var textTemp = SaferUnicodeEncoding.GetBytes(signEntity.text);
+
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Sign",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = SaferUnicodeEncoding.GetString(textTemp).Normalize(),
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );
+               }
                }
 
 
                internal static void DecodePostSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
                {
 #if DEBUG
-                       clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
+               clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
 #endif
-                       //sign post Text into a POI field...
-                       BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
+               //sign post Text into a POI field...
+               BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
 
-                       if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0)
-                       {
+               if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0) {
 
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Signpost",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = string.Join(",", signEntity.textByCardinalDirection),
-                                                               Timestamp = DateTime.UtcNow,
-                                                       }
-                                                       );
-                       }
+               var textTemp = SaferUnicodeEncoding.GetBytes(string.Join(",", signEntity.textByCardinalDirection));
+
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Signpost",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = SaferUnicodeEncoding.GetString(textTemp).Normalize(),
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );
+               }
                }
 
                internal static void KeepTrackOfMerchant(ICoreClientAPI clientAPI, EntitiesOfInterest poi, BlockPos posn, Entity entity)
                {
-                       //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
+               //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
 
-                       var traderJoe = entity as EntityTrader;
-                       var traderName = entity.GetBehavior<EntityBehaviorNameTag>()?.DisplayName;
-                       var message = Lang.Get("tradingwindow-" + entity.Code.Path, traderName);
-                       if (traderJoe.TradeProps != null)
-                       {
-                               message += $" - Gears: {traderJoe.TradeProps.Money}, ";
-                       }
-                       poi.AddReplace(new EntityOfInterest
-                       {
-                               Name = "Trader",
-                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                               Location = posn.Copy(),
-                               Notes = message,
-                               Timestamp = DateTime.UtcNow,
-                               EntityId = entity.EntityId
-                       });
+               var traderJoe = entity as EntityTrader;
+               var traderName = entity.GetBehavior<EntityBehaviorNameTag>( )?.DisplayName;
+               string code;
+               // this makes me ill
+               switch (entity.Code.Path) {
+               case "humanoid-trader-artisan":
+                       code = "{0} the artisan";
+                       break;
+               case "humanoid-trader-treasurehunter":
+                       code = "{0} the treasure hunter";
+                       break;
+               case "humanoid-trader-buildmaterials":
+                       code = "{0} the building materials trader";
+                       break;
+               case "humanoid-trader-clothing":
+                       code = "{0} the clothing merchant";
+                       break;
+               case "humanoid-trader-commodities":
+                       code = "{0} the commodities merchant";
+                       break;
+               case "humanoid-trader-foods":
+                       code = "{0} the foods supplier";
+                       break;
+               case "humanoid-trader-furniture":
+                       code = "{0} the furniture trader";
+                       break;
+               case "humanoid-trader-luxuries":
+                       code = "{0} the luxuries merchant";
+                       break;
+               case "humanoid-trader-survivalgoods":
+                       code = "{0} the survival goods supplier";
+                       break;
+               default:
+                       code = "";
+                       break;
+               }
+               var message = string.Format(code, traderName);
+               if (traderJoe.TradeProps != null) {
+               message += $" - Gears: {traderJoe.TradeProps.Money}, ";
+               }
+               poi.AddReplace(new EntityOfInterest {
+                       Name = "Trader",
+                       PrettyLocation = posn.PrettyCoords(clientAPI),
+                       Location = posn.Copy( ),
+                       Notes = message,
+                       Timestamp = DateTime.UtcNow,
+                       EntityId = entity.EntityId
+               });
                }
 
                internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
@@ -162,33 +242,109 @@ namespace Automap
                #if DEBUG
                clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
                #endif
-                       //Where to? and from!
+               //Where to? and from!
+
+               BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
+
+               if (te != null) {
+               //FIXME: Delayed rescan ?
+               StringBuilder textTarget = new StringBuilder( );
+               
+               textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
+               textTarget.Append(te.Activated ? "Online, " : "Offline, ");//Property hardcoded TRUE ?!
+               textTarget.Append(" Target: [ ");
+               textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
+               textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Translocator",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = textTarget.ToString( ),
+                                               Timestamp = DateTime.UtcNow,
+                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy( )) : null
+                                       }
+                                       );
+               }
+               }
+
+
+               internal static void DecodeTeleport(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
+               {
+               #if DEBUG
+               clientAPI.Logger.VerboseDebug("Teleport Designator Invoked!");
+               #endif
+               
 
-                       BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
+               BlockEntityTeleporter tele = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityTeleporter;
 
-                       if (te != null)
+               if (tele != null) 
                        {
-                               //FIXME: Delayed rescan ?
-                               StringBuilder textTarget = new StringBuilder();
-                               //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget);
-                               textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
-                               textTarget.Append(te.Activated ? "Online, " : "Offline, ");
-                               textTarget.Append(" Target: [ ");
-                               textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
-                               textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
-                               poi.AddReplace(
-                                                       new PointOfInterest
-                                                       {
-                                                               Name = "Translocator",
-                                                               PrettyLocation = posn.PrettyCoords(clientAPI),
-                                                               Location = posn.Copy(),
-                                                               Notes = textTarget.ToString(),
-                                                               Timestamp = DateTime.UtcNow,
-                                                               Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy()) : null
-                                                       }
-                                                       );
+                       //TeleporterManager teleManager = clientAPI.ModLoader.GetModSystem<TeleporterManager>();
+                       TeleporterLocation location = AccessTools.FieldRefAccess<BlockEntityTeleporter, TeleporterLocation>(tele, @"tpLocation");//TeleporterLocation tpLocation;
+
+                       if (location != null) 
+                       {
+                       StringBuilder textTarget = new StringBuilder( );
+                       textTarget.Append(" Target: [ ");
+                       textTarget.Append($" '{location.SourceName}' @ {location.SourcePos?.PrettyCoords(clientAPI)} -> '{location.TargetName}' @ {location.TargetPos?.PrettyCoords(clientAPI)}  ");//Or ABS coords?            
+                       textTarget.Append(" ]");
+                       poi.AddReplace(
+                                               new PointOfInterest {
+                                                       Name = "Teleport",
+                                                       PrettyLocation = posn.PrettyCoords(clientAPI),
+                                                       Location = posn.Copy( ),
+                                                       Notes = textTarget.ToString( ),
+                                                       Timestamp = DateTime.UtcNow,
+                                                       Destination = location.TargetPos != null ? new BlockPosJson(location.TargetPos.Copy( )) : null
+                                               }
+                                               );
                        }
                }
+               }
+
+               internal static void NoteWildbeehive(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
+               {
+               #if DEBUG
+               clientAPI.Logger.VerboseDebug("Wild bee hive Designator Invoked!");
+               #endif
+
+               BlockEntityBeehive bees = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityBeehive;
+
+               if (bees != null) {                             
+               EnumHivePopSize hiveSize = AccessTools.FieldRefAccess<BlockEntityBeehive, EnumHivePopSize>(bees, @"hivePopSize");
+
+               StringBuilder textTarget = new StringBuilder( );
+               textTarget.AppendLine($" Population: {(hiveSize != null? hiveSize.ToString() : "?")} ");                
+               
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "Wildbeehive",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = textTarget.ToString( ),
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );              
+               }
+               }
+
+               internal static void NotePineResinLeak(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
+               {
+               #if DEBUG
+               clientAPI.Logger.VerboseDebug("Resin leaking Pine tree Designator Invoked!");
+               #endif
+               //Note:Due to how Block-Entities are handled...only Harvested resin is tracked....
+               poi.AddReplace(
+                                       new PointOfInterest {
+                                               Name = "PineResin",
+                                               PrettyLocation = posn.PrettyCoords(clientAPI),
+                                               Location = posn.Copy( ),
+                                               Notes = String.Empty,
+                                               Timestamp = DateTime.UtcNow,
+                                       }
+                                       );
+               }
 
                #endregion
        }