OSDN Git Service

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