OSDN Git Service

Assorted fixes
[automap/automap.git] / Automap / Designators / DefaultDesignators.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Text;
5 using System.Text.RegularExpressions;
6
7 using HarmonyLib;
8
9 using Vintagestory.API.Client;
10 using Vintagestory.API.Common;
11 using Vintagestory.API.Common.Entities;
12 using Vintagestory.API.Config;
13 using Vintagestory.API.MathTools;
14 using Vintagestory.GameContent;
15
16 namespace Automap
17 {
18         public static class DefaultDesignators
19         {
20                 #region Defaults
21
22                 public static BlockDesignator Roads =
23                          new BlockDesignator(
24                                 new AssetLocation("game", "stonepath"),
25                                 Color.Yellow,
26                                 EnumBlockMaterial.Gravel
27                         );
28
29                 public static BlockDesignator GroundSigns =
30                          new BlockDesignator(
31                                 new AssetLocation("game", "sign-ground"),
32                                 Color.Teal,
33                                 EnumBlockMaterial.Wood,
34                                 DecodeSign
35                         );
36
37                 public static BlockDesignator WallSigns =
38                          new BlockDesignator(
39                                 new AssetLocation("game", "sign-wall"),
40                                 Color.Teal,
41                                 EnumBlockMaterial.Wood,
42                                 DecodeSign
43                         );
44
45                 public static BlockDesignator PostSigns =
46                          new BlockDesignator(
47                                 new AssetLocation("game", "signpost"),
48                                 Color.Teal,
49                                 EnumBlockMaterial.Wood,
50                                 DecodePostSign
51                         );
52
53                 public static BlockDesignator Translocators =
54                          new BlockDesignator(
55                                 new AssetLocation("game", "statictranslocator-normal"),
56                                 Color.SteelBlue,
57                                 EnumBlockMaterial.Metal,
58                                 DecodeTranslocator
59                         );
60
61                 public static BlockDesignator Teleporters =
62                          new BlockDesignator(
63                                 new AssetLocation("game", "teleporterbase"),
64                                 Color.SeaGreen,
65                                 EnumBlockMaterial.Wood,
66                                 DecodeTeleport
67                         );
68
69                 public static EntityDesignator Traders =
70                          new EntityDesignator(
71                                 new AssetLocation("game", "humanoid-trader"),
72                                 Color.LightGoldenrodYellow,
73                                 EnumEntityState.Active,
74                                 KeepTrackOfMerchant
75                         );
76
77                 public static BlockDesignator Wildbeehives =
78                          new BlockDesignator(
79                                 new AssetLocation("game", "wildbeehive"),
80                                 Color.Honeydew,
81                                 EnumBlockMaterial.Other,
82                                 NoteWildbeehive,
83                                 false
84                         );
85
86                 public static BlockDesignator PineResinLeaks =
87                          new BlockDesignator(
88                                 new AssetLocation("game", "log-resin"),
89                                 Color.DarkOrange,
90                                 EnumBlockMaterial.Wood,
91                                 NotePineResinLeak,
92                                 false
93                         );
94
95
96
97                 /// <summary>
98                 /// Not just blocks, but block-entities as well!
99                 /// </summary>
100                 /// <returns>The block designators.</returns>
101                 public static List<BlockDesignator> DefaultBlockDesignators
102                 {
103                         get
104                         {
105                                 return new List<BlockDesignator>{
106                                         DefaultDesignators.Roads,
107                                         DefaultDesignators.GroundSigns,
108                                         DefaultDesignators.WallSigns,
109                                         DefaultDesignators.PostSigns,
110                                         DefaultDesignators.Translocators,
111                                         DefaultDesignators.Teleporters,
112                         DefaultDesignators.Wildbeehives,
113                         DefaultDesignators.PineResinLeaks,
114                                         };
115                         }
116                 }
117
118                 public static List<EntityDesignator> DefaultEntityDesignators
119                 {
120                         get
121                         {
122                                 return new List<EntityDesignator>{
123                                                 DefaultDesignators.Traders,
124                                 };
125                         }
126                 }
127
128                 internal static Encoding SaferUnicodeEncoding = Encoding.GetEncoding(Encoding.UTF8.WebName,
129                 new EncoderReplacementFallback(@" "),
130                 new DecoderReplacementFallback(@" "));
131
132                 #endregion
133
134
135                 #region Designators
136
137                 internal static void DecodeSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
138                 {
139                 #if DEBUG
140                 clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
141                 #endif
142                 //sign Text into a POI field...
143                 BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
144
145                 if (signEntity != null && !String.IsNullOrEmpty(signEntity.text)) {
146
147                 var textTemp = SaferUnicodeEncoding.GetBytes(signEntity.text);
148
149                 poi.AddReplace(
150                                         new PointOfInterest {
151                                                 Name = "Sign",
152                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
153                                                 Location = posn.Copy( ),
154                                                 Notes = SaferUnicodeEncoding.GetString(textTemp).Normalize(),
155                                                 Timestamp = DateTime.UtcNow,
156                                         }
157                                         );
158                 }
159                 }
160
161
162                 internal static void DecodePostSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
163                 {
164 #if DEBUG
165                 clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
166 #endif
167                 //sign post Text into a POI field...
168                 BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
169
170                 if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0) {
171
172                 var textTemp = SaferUnicodeEncoding.GetBytes(string.Join(",", signEntity.textByCardinalDirection));
173
174                 poi.AddReplace(
175                                         new PointOfInterest {
176                                                 Name = "Signpost",
177                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
178                                                 Location = posn.Copy( ),
179                                                 Notes = SaferUnicodeEncoding.GetString(textTemp).Normalize(),
180                                                 Timestamp = DateTime.UtcNow,
181                                         }
182                                         );
183                 }
184                 }
185
186                 internal static void KeepTrackOfMerchant(ICoreClientAPI clientAPI, EntitiesOfInterest poi, BlockPos posn, Entity entity)
187                 {
188                 //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
189
190                 var traderJoe = entity as EntityTrader;
191                 var traderName = entity.GetBehavior<EntityBehaviorNameTag>( )?.DisplayName;
192                 string code;
193                 // this makes me ill
194                 switch (entity.Code.Path) {
195                 case "humanoid-trader-artisan":
196                         code = "{0} the artisan";
197                         break;
198                 case "humanoid-trader-treasurehunter":
199                         code = "{0} the treasure hunter";
200                         break;
201                 case "humanoid-trader-buildmaterials":
202                         code = "{0} the building materials trader";
203                         break;
204                 case "humanoid-trader-clothing":
205                         code = "{0} the clothing merchant";
206                         break;
207                 case "humanoid-trader-commodities":
208                         code = "{0} the commodities merchant";
209                         break;
210                 case "humanoid-trader-foods":
211                         code = "{0} the foods supplier";
212                         break;
213                 case "humanoid-trader-furniture":
214                         code = "{0} the furniture trader";
215                         break;
216                 case "humanoid-trader-luxuries":
217                         code = "{0} the luxuries merchant";
218                         break;
219                 case "humanoid-trader-survivalgoods":
220                         code = "{0} the survival goods supplier";
221                         break;
222                 default:
223                         code = "";
224                         break;
225                 }
226                 var message = string.Format(code, traderName);
227                 if (traderJoe.TradeProps != null) {
228                 message += $" - Gears: {traderJoe.TradeProps.Money}, ";
229                 }
230                 poi.AddReplace(new EntityOfInterest {
231                         Name = "Trader",
232                         PrettyLocation = posn.PrettyCoords(clientAPI),
233                         Location = posn.Copy( ),
234                         Notes = message,
235                         Timestamp = DateTime.UtcNow,
236                         EntityId = entity.EntityId
237                 });
238                 }
239
240                 internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
241                 {
242 #if DEBUG
243                 clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
244 #endif
245                 //Where to? and from!
246
247                 BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
248
249                 if (te != null) {
250                 //FIXME: Delayed rescan ?
251                 StringBuilder textTarget = new StringBuilder( );
252                 //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget);
253                 textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
254                 textTarget.Append(te.Activated ? "Online, " : "Offline, ");
255                 textTarget.Append(" Target: [ ");
256                 textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
257                 textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
258                 poi.AddReplace(
259                                         new PointOfInterest {
260                                                 Name = "Translocator",
261                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
262                                                 Location = posn.Copy( ),
263                                                 Notes = textTarget.ToString( ),
264                                                 Timestamp = DateTime.UtcNow,
265                                                 Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy( )) : null
266                                         }
267                                         );
268                 }
269                 }
270
271
272                 internal static void DecodeTeleport(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
273                 {
274                 #if DEBUG
275                 clientAPI.Logger.VerboseDebug("Teleport Designator Invoked!");
276                 #endif
277                 
278
279                 BlockEntityTeleporter tele = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityTeleporter;
280
281                 if (tele != null) 
282                         {
283                         //TeleporterManager teleManager = clientAPI.ModLoader.GetModSystem<TeleporterManager>();
284                         TeleporterLocation location = AccessTools.FieldRefAccess<BlockEntityTeleporter, TeleporterLocation>(tele, @"tpLocation");//TeleporterLocation tpLocation;
285
286                         if (location != null) 
287                         {
288                         StringBuilder textTarget = new StringBuilder( );
289                         textTarget.Append(" Target: [ ");
290                         textTarget.Append($" '{location.SourceName}' @ {location.SourcePos?.PrettyCoords(clientAPI)} -> '{location.TargetName}' @ {location.TargetPos?.PrettyCoords(clientAPI)}  ");//Or ABS coords?            
291                         textTarget.Append(" ]");
292                         poi.AddReplace(
293                                                 new PointOfInterest {
294                                                         Name = "Teleport",
295                                                         PrettyLocation = posn.PrettyCoords(clientAPI),
296                                                         Location = posn.Copy( ),
297                                                         Notes = textTarget.ToString( ),
298                                                         Timestamp = DateTime.UtcNow,
299                                                         Destination = location.TargetPos != null ? new BlockPosJson(location.TargetPos.Copy( )) : null
300                                                 }
301                                                 );
302                         }
303                 }
304                 }
305
306                 internal static void NoteWildbeehive(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
307                 {
308                 #if DEBUG
309                 clientAPI.Logger.VerboseDebug("Wild bee hive Designator Invoked!");
310                 #endif
311
312                 BlockEntityBeehive bees = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityBeehive;
313
314                 if (bees != null) {                             
315                 EnumHivePopSize hiveSize = AccessTools.FieldRefAccess<BlockEntityBeehive, EnumHivePopSize>(bees, @"hivePopSize");
316
317                 StringBuilder textTarget = new StringBuilder( );
318                 textTarget.AppendLine($" Population: {(hiveSize != null? hiveSize.ToString() : "?")} ");                
319                 
320                 poi.AddReplace(
321                                         new PointOfInterest {
322                                                 Name = "Wildbeehive",
323                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
324                                                 Location = posn.Copy( ),
325                                                 Notes = textTarget.ToString( ),
326                                                 Timestamp = DateTime.UtcNow,
327                                         }
328                                         );              
329                 }
330                 }
331
332                 internal static void NotePineResinLeak(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
333                 {
334                 #if DEBUG
335                 clientAPI.Logger.VerboseDebug("Resin leaking Pine tree Designator Invoked!");
336                 #endif
337                 //Note:Due to how Block-Entities are handled...only Harvested resin is tracked....
338                 poi.AddReplace(
339                                         new PointOfInterest {
340                                                 Name = "PineResin",
341                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
342                                                 Location = posn.Copy( ),
343                                                 Notes = String.Empty,
344                                                 Timestamp = DateTime.UtcNow,
345                                         }
346                                         );
347                 }
348
349                 #endregion
350         }
351 }
352