OSDN Git Service

c8e25c1971a5ce4ebd9f0b70505fa56a56c92a72
[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 != 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                 #if DEBUG
189                 clientAPI.Logger.VerboseDebug("TRADER Designator Invoked!");
190                 #endif
191                 //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
192
193                 var traderJoe = entity as EntityTrader;
194                 if (traderJoe == null) return;
195                 
196                 var traderName = entity.GetBehavior<EntityBehaviorNameTag>( )?.DisplayName ?? @"?";
197                 string code;
198                 // this makes me ill
199                 switch (entity.Code.Path) {
200                 case "humanoid-trader-artisan":
201                         code = "{0} the artisan";
202                         break;
203                 case "humanoid-trader-treasurehunter":
204                         code = "{0} the treasure hunter";
205                         break;
206                 case "humanoid-trader-buildmaterials":
207                         code = "{0} the building materials trader";
208                         break;
209                 case "humanoid-trader-clothing":
210                         code = "{0} the clothing merchant";
211                         break;
212                 case "humanoid-trader-commodities":
213                         code = "{0} the commodities merchant";
214                         break;
215                 case "humanoid-trader-foods":
216                         code = "{0} the foods supplier";
217                         break;
218                 case "humanoid-trader-furniture":
219                         code = "{0} the furniture trader";
220                         break;
221                 case "humanoid-trader-luxuries":
222                         code = "{0} the luxuries merchant";
223                         break;
224                 case "humanoid-trader-survivalgoods":
225                         code = "{0} the survival goods supplier";
226                         break;
227                 default:
228                         code = "";
229                         break;
230                 }
231                 var message = string.Format(code, traderName);
232                 if (traderJoe.TradeProps != null) {
233                 message += $" - Gears: {traderJoe.TradeProps.Money}, ";
234                 }
235                 poi.AddReplace(new EntityOfInterest {
236                         Name = "Trader",
237                         PrettyLocation = posn.PrettyCoords(clientAPI),
238                         Location = posn.Copy( ),
239                         Notes = message,
240                         Timestamp = DateTime.UtcNow,
241                         EntityId = entity.EntityId
242                 });
243                 }
244
245                 internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
246                 {
247                 #if DEBUG
248                 clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
249                 #endif
250                 //Where to? and from!
251
252                 BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
253
254                 if (te != null) {
255                 //FIXME: Delayed rescan ?
256                 StringBuilder textTarget = new StringBuilder( );
257                 
258                 textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
259                 textTarget.Append(te.Activated ? "Online, " : "Offline, ");//Property hardcoded TRUE ?!
260                 textTarget.Append(" Target: [ ");
261                 textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
262                 textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
263                 poi.AddReplace(
264                                         new PointOfInterest {
265                                                 Name = "Translocator",
266                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
267                                                 Location = posn.Copy( ),
268                                                 Notes = textTarget.ToString( ),
269                                                 Timestamp = DateTime.UtcNow,
270                                                 Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy( )) : null
271                                         }
272                                         );
273                 }
274                 }
275
276
277                 internal static void DecodeTeleport(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
278                 {
279                 #if DEBUG
280                 clientAPI.Logger.VerboseDebug("Teleport Designator Invoked!");
281                 #endif
282                 
283
284                 BlockEntityTeleporter tele = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityTeleporter;
285
286                 if (tele != null) 
287                         {
288                         //TeleporterManager teleManager = clientAPI.ModLoader.GetModSystem<TeleporterManager>();
289                         TeleporterLocation location = AccessTools.FieldRefAccess<BlockEntityTeleporter, TeleporterLocation>(tele, @"tpLocation");//TeleporterLocation tpLocation;
290
291                         if (location != null) 
292                         {
293                         StringBuilder textTarget = new StringBuilder( );
294                         textTarget.Append(" Target: [ ");
295                         textTarget.Append($" '{location.SourceName}' @ {location.SourcePos?.PrettyCoords(clientAPI)} -> '{location.TargetName}' @ {location.TargetPos?.PrettyCoords(clientAPI)}  ");//Or ABS coords?            
296                         textTarget.Append(" ]");
297                         poi.AddReplace(
298                                                 new PointOfInterest {
299                                                         Name = "Teleport",
300                                                         PrettyLocation = posn.PrettyCoords(clientAPI),
301                                                         Location = posn.Copy( ),
302                                                         Notes = textTarget.ToString( ),
303                                                         Timestamp = DateTime.UtcNow,
304                                                         Destination = location.TargetPos != null ? new BlockPosJson(location.TargetPos.Copy( )) : null
305                                                 }
306                                                 );
307                         }
308                 }
309                 }
310
311                 internal static void NoteWildbeehive(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
312                 {
313                 #if DEBUG
314                 clientAPI.Logger.VerboseDebug("Wild bee hive Designator Invoked!");
315                 #endif
316
317                 BlockEntityBeehive bees = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityBeehive;
318
319                 if (bees != null) {                             
320                 EnumHivePopSize hiveSize = AccessTools.FieldRefAccess<BlockEntityBeehive, EnumHivePopSize>(bees, @"hivePopSize");
321
322                 StringBuilder textTarget = new StringBuilder( );
323                 textTarget.AppendLine($" Population: {(hiveSize != null? hiveSize.ToString() : "?")} ");                
324                 
325                 poi.AddReplace(
326                                         new PointOfInterest {
327                                                 Name = "Wildbeehive",
328                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
329                                                 Location = posn.Copy( ),
330                                                 Notes = textTarget.ToString( ),
331                                                 Timestamp = DateTime.UtcNow,
332                                         }
333                                         );              
334                 }
335                 }
336
337                 internal static void NotePineResinLeak(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
338                 {
339                 #if DEBUG
340                 clientAPI.Logger.VerboseDebug("Resin leaking Pine tree Designator Invoked!");
341                 #endif
342                 //Note:Due to how Block-Entities are handled...only Harvested resin is tracked....
343                 poi.AddReplace(
344                                         new PointOfInterest {
345                                                 Name = "PineResin",
346                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
347                                                 Location = posn.Copy( ),
348                                                 Notes = String.Empty,
349                                                 Timestamp = DateTime.UtcNow,
350                                         }
351                                         );
352                 }
353
354                 #endregion
355         }
356 }
357