OSDN Git Service

4bdcaab21c84def30b1687bad367ccd6313550a7
[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 using HarmonyLib;
7 using Vintagestory.API.Client;
8 using Vintagestory.API.Common;
9 using Vintagestory.API.Common.Entities;
10 using Vintagestory.API.Config;
11 using Vintagestory.API.MathTools;
12 using Vintagestory.GameContent;
13
14 namespace Automap
15 {
16         public static class DefaultDesignators
17         {
18                 #region Defaults
19
20                 public static BlockDesignator Roads =
21                          new BlockDesignator(
22                                 new AssetLocation("game", "stonepath"),
23                                 Color.Yellow,
24                                 EnumBlockMaterial.Gravel
25                         );
26
27                 public static BlockDesignator GroundSigns =
28                          new BlockDesignator(
29                                 new AssetLocation("game", "sign-ground"),
30                                 Color.Teal,
31                                 EnumBlockMaterial.Wood,
32                                 DecodeSign
33                         );
34
35                 public static BlockDesignator WallSigns =
36                          new BlockDesignator(
37                                 new AssetLocation("game", "sign-wall"),
38                                 Color.Teal,
39                                 EnumBlockMaterial.Wood,
40                                 DecodeSign
41                         );
42
43                 public static BlockDesignator PostSigns =
44                          new BlockDesignator(
45                                 new AssetLocation("game", "signpost"),
46                                 Color.Teal,
47                                 EnumBlockMaterial.Wood,
48                                 DecodePostSign
49                         );
50
51                 public static BlockDesignator Translocators =
52                          new BlockDesignator(
53                                 new AssetLocation("game", "statictranslocator-normal"),
54                                 Color.SteelBlue,
55                                 EnumBlockMaterial.Metal,
56                                 DecodeTranslocator
57                         );
58
59                 public static BlockDesignator Teleporters =
60                          new BlockDesignator(
61                                 new AssetLocation("game", "teleporterbase"),
62                                 Color.SeaGreen,
63                                 EnumBlockMaterial.Wood,
64                                 DecodeTeleport
65                         );
66
67                 public static EntityDesignator Traders =
68                          new EntityDesignator(
69                                 new AssetLocation("game", "humanoid-trader"),
70                                 Color.LightGoldenrodYellow,
71                                 EnumEntityState.Active,
72                                 KeepTrackOfMerchant
73                         );
74
75                 /// <summary>
76                 /// Not just blocks, but block-entities as well!
77                 /// </summary>
78                 /// <returns>The block designators.</returns>
79                 public static List<BlockDesignator> DefaultBlockDesignators
80                 {
81                         get
82                         {
83                                 return new List<BlockDesignator>{
84                                         DefaultDesignators.Roads,
85                                         DefaultDesignators.GroundSigns,
86                                         DefaultDesignators.WallSigns,
87                                         DefaultDesignators.PostSigns,
88                                         DefaultDesignators.Translocators,
89                                         DefaultDesignators.Teleporters,
90                                         };
91                         }
92                 }
93
94                 public static List<EntityDesignator> DefaultEntityDesignators
95                 {
96                         get
97                         {
98                                 return new List<EntityDesignator>{
99                                                 DefaultDesignators.Traders,
100                                 };
101                         }
102                 }
103
104                 #endregion
105
106
107                 #region Designators
108
109                 internal static void DecodeSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
110                 {
111 #if DEBUG
112                 clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
113 #endif
114                 //sign Text into a POI field...
115                 BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
116
117                 if (signEntity != null && !String.IsNullOrEmpty(signEntity.text)) {
118
119                 poi.AddReplace(
120                                         new PointOfInterest {
121                                                 Name = "Sign",
122                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
123                                                 Location = posn.Copy( ),
124                                                 Notes = signEntity.text,
125                                                 Timestamp = DateTime.UtcNow,
126                                         }
127                                         );
128                 }
129                 }
130
131
132                 internal static void DecodePostSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
133                 {
134 #if DEBUG
135                 clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
136 #endif
137                 //sign post Text into a POI field...
138                 BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
139
140                 if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0) {
141
142                 poi.AddReplace(
143                                         new PointOfInterest {
144                                                 Name = "Signpost",
145                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
146                                                 Location = posn.Copy( ),
147                                                 Notes = string.Join(",", signEntity.textByCardinalDirection),
148                                                 Timestamp = DateTime.UtcNow,
149                                         }
150                                         );
151                 }
152                 }
153
154                 internal static void KeepTrackOfMerchant(ICoreClientAPI clientAPI, EntitiesOfInterest poi, BlockPos posn, Entity entity)
155                 {
156                 //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
157
158                 var traderJoe = entity as EntityTrader;
159                 var traderName = entity.GetBehavior<EntityBehaviorNameTag>( )?.DisplayName;
160                 string code;
161                 // this makes me ill
162                 switch (entity.Code.Path) {
163                 case "humanoid-trader-artisan":
164                         code = "{0} the artisan";
165                         break;
166                 case "humanoid-trader-treasurehunter":
167                         code = "{0} the treasure hunter";
168                         break;
169                 case "humanoid-trader-buildmaterials":
170                         code = "{0} the building materials trader";
171                         break;
172                 case "humanoid-trader-clothing":
173                         code = "{0} the clothing merchant";
174                         break;
175                 case "humanoid-trader-commodities":
176                         code = "{0} the commodities merchant";
177                         break;
178                 case "humanoid-trader-foods":
179                         code = "{0} the foods supplier";
180                         break;
181                 case "humanoid-trader-furniture":
182                         code = "{0} the furniture trader";
183                         break;
184                 case "humanoid-trader-luxuries":
185                         code = "{0} the luxuries merchant";
186                         break;
187                 case "humanoid-trader-survivalgoods":
188                         code = "{0} the survival goods supplier";
189                         break;
190                 default:
191                         code = "";
192                         break;
193                 }
194                 var message = string.Format(code, traderName);
195                 if (traderJoe.TradeProps != null) {
196                 message += $" - Gears: {traderJoe.TradeProps.Money}, ";
197                 }
198                 poi.AddReplace(new EntityOfInterest {
199                         Name = "Trader",
200                         PrettyLocation = posn.PrettyCoords(clientAPI),
201                         Location = posn.Copy( ),
202                         Notes = message,
203                         Timestamp = DateTime.UtcNow,
204                         EntityId = entity.EntityId
205                 });
206                 }
207
208                 internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
209                 {
210 #if DEBUG
211                 clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
212 #endif
213                 //Where to? and from!
214
215                 BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
216
217                 if (te != null) {
218                 //FIXME: Delayed rescan ?
219                 StringBuilder textTarget = new StringBuilder( );
220                 //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget);
221                 textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
222                 textTarget.Append(te.Activated ? "Online, " : "Offline, ");
223                 textTarget.Append(" Target: [ ");
224                 textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
225                 textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
226                 poi.AddReplace(
227                                         new PointOfInterest {
228                                                 Name = "Translocator",
229                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
230                                                 Location = posn.Copy( ),
231                                                 Notes = textTarget.ToString( ),
232                                                 Timestamp = DateTime.UtcNow,
233                                                 Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy( )) : null
234                                         }
235                                         );
236                 }
237                 }
238
239
240                 internal static void DecodeTeleport(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
241                 {
242                 #if DEBUG
243                 clientAPI.Logger.VerboseDebug("Teleport Designator Invoked!");
244                 #endif
245                 
246
247                 BlockEntityTeleporter tele = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityTeleporter;
248
249                 if (tele != null) 
250                         {
251                         //TeleporterManager teleManager = clientAPI.ModLoader.GetModSystem<TeleporterManager>();
252                         TeleporterLocation location = AccessTools.FieldRefAccess<BlockEntityTeleporter, TeleporterLocation>(tele, @"tpLocation");//TeleporterLocation tpLocation;
253
254                         if (location != null) 
255                         {
256                         StringBuilder textTarget = new StringBuilder( );
257                         textTarget.Append(" Target: [ ");
258                         textTarget.Append($" '{location.SourceName}' @ {location.SourcePos?.PrettyCoords(clientAPI)} -> '{location.TargetName}' @ {location.TargetPos?.PrettyCoords(clientAPI)}  ");//Or ABS coords?            
259                         textTarget.Append(" ]");
260                         poi.AddReplace(
261                                                 new PointOfInterest {
262                                                         Name = "Teleport",
263                                                         PrettyLocation = posn.PrettyCoords(clientAPI),
264                                                         Location = posn.Copy( ),
265                                                         Notes = textTarget.ToString( ),
266                                                         Timestamp = DateTime.UtcNow,
267                                                         Destination = location.TargetPos != null ? new BlockPosJson(location.TargetPos.Copy( )) : null
268                                                 }
269                                                 );
270                         }
271                 }
272                 }
273
274                 #endregion
275         }
276 }
277