OSDN Git Service

W.I.P. Fixes for several issues - plus better Shard processing
[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 Vintagestory.API.Client;
8 using Vintagestory.API.Common;
9 using Vintagestory.API.Common.Entities;
10 using Vintagestory.API.MathTools;
11 using Vintagestory.GameContent;
12
13 namespace Automap
14 {
15         public static class DefaultDesignators
16         {
17                 private static Regex stripControlChars = new Regex(@"\s", RegexOptions.Multiline);
18
19                 public static BlockDesignator Roads =
20                          new BlockDesignator(
21                                 new AssetLocation("game", "stonepath"),
22                                 Color.Yellow,
23                                 EnumBlockMaterial.Gravel
24                         );
25
26                 public static BlockDesignator GroundSigns =
27                          new BlockDesignator(
28                                 new AssetLocation("game", "sign-ground"),
29                                 Color.Teal,
30                                 EnumBlockMaterial.Wood,
31                                 DecodeSign
32                         );
33
34                 public static BlockDesignator WallSigns =
35                          new BlockDesignator(
36                                 new AssetLocation("game", "sign-wall"),
37                                 Color.Teal,
38                                 EnumBlockMaterial.Wood,
39                                 DecodeSign
40                         );
41
42                 public static BlockDesignator PostSigns =
43                          new BlockDesignator(
44                                 new AssetLocation("game", "signpost"),
45                                 Color.Teal,
46                                 EnumBlockMaterial.Wood,
47                                 DecodePostSign
48                         );
49
50                 public static BlockDesignator Translocators =
51                          new BlockDesignator(
52                                 new AssetLocation("game", "statictranslocator-normal"),
53                                 Color.SteelBlue,
54                                 EnumBlockMaterial.Metal,
55                                 DecodeTranslocator
56                         );
57
58                 public static EntityDesignator Traders =
59                          new EntityDesignator(
60                                 new AssetLocation("game", "humanoid-trader"),
61                                 Color.LightGoldenrodYellow,
62                                 EnumEntityState.Active,
63                                 KeepTrackOfMerchant
64                         );
65
66                 /// <summary>
67                 /// Not just blocks, but block-entities as well!
68                 /// </summary>
69                 /// <returns>The block designators.</returns>
70                 public static List<BlockDesignator> DefaultBlockDesignators()
71                 {
72                         return new List<BlockDesignator>{
73                                 DefaultDesignators.Roads,
74                                 DefaultDesignators.GroundSigns,
75                                 DefaultDesignators.WallSigns,
76                                 DefaultDesignators.PostSigns,
77                                 DefaultDesignators.Translocators,
78                                 };
79                 }
80
81                 public static List<EntityDesignator> DefaultEntityDesignators()
82                 {
83                         return new List<EntityDesignator>{
84                                 DefaultDesignators.Traders,
85                 };
86                 }
87
88                 #region Designators
89
90                 internal static void DecodeSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
91                 {
92 #if DEBUG
93                         clientAPI.Logger.VerboseDebug("Sign Designator Invoked!");
94 #endif
95                         //sign Text into a POI field...
96                         BlockEntitySign signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySign;
97
98                         if (signEntity != null && !String.IsNullOrEmpty(signEntity.text))
99                         {
100
101                                 poi.AddReplace(
102                                                         new PointOfInterest
103                                                         {
104                                                                 Name = "Sign",
105                                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
106                                                                 Location = posn.Copy(),
107                                                                 Notes = stripControlChars.Replace(signEntity.text, " "),
108                                                                 Timestamp = DateTime.UtcNow,
109                                                         }
110                                                         );
111
112                         }
113
114                 }
115
116
117                 internal static void DecodePostSign(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
118                 {
119 #if DEBUG
120                         clientAPI.Logger.VerboseDebug("Post-sign Designator Invoked!");
121 #endif
122                         //sign post Text into a POI field...
123                         BlockEntitySignPost signEntity = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntitySignPost;
124
125                         if (signEntity != null && signEntity.textByCardinalDirection?.Length > 0)
126                         {
127
128                                 poi.AddReplace(
129                                                         new PointOfInterest
130                                                         {
131                                                                 Name = "Signpost",
132                                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
133                                                                 Location = posn.Copy(),
134                                                                 Notes = stripControlChars.Replace(string.Join(",", signEntity.textByCardinalDirection), " "),
135                                                                 Timestamp = DateTime.UtcNow,
136                                                         }
137                                                         );
138
139                         }
140                 }
141
142                 internal static void KeepTrackOfMerchant(ICoreClientAPI clientAPI, EntitiesOfInterest poi, BlockPos posn, Entity entity)
143                 {
144                         //clientAPI.Logger.VerboseDebug("Trader: {0} @ {1}", entity.GetName(), posn);
145
146                         var traderJoe = entity as EntityTrader;
147                         var message = $"{entity.GetName()} Alive: {traderJoe.Alive}";
148                         if (traderJoe.TradeProps != null)
149                         {
150                                 message += $" - Gears: {traderJoe.TradeProps.Money}, ";
151                         }
152                         poi.AddReplace(new EntityOfInterest
153                         {
154                                 Name = "Trader",
155                                 PrettyLocation = posn.PrettyCoords(clientAPI),
156                                 Location = posn.Copy(),
157                                 Notes = message,
158                                 Timestamp = DateTime.UtcNow,
159                                 EntityId = entity.EntityId
160                         });
161                 }
162
163                 internal static void DecodeTranslocator(ICoreClientAPI clientAPI, PointsOfInterest poi, BlockPos posn, Block block)
164                 {
165                 #if DEBUG
166                 clientAPI.Logger.VerboseDebug("TRANSLOCATOR Designator Invoked!");
167                 #endif
168                         //Where to? and from!
169
170                         BlockEntityStaticTranslocator te = clientAPI.World.BlockAccessor.GetBlockEntity(posn) as BlockEntityStaticTranslocator;
171
172                         if (te != null)
173                         {
174                                 //FIXME: Delayed rescan ?
175                                 StringBuilder textTarget = new StringBuilder();
176                                 //translocatorEntity.GetBlockInfo(clientAPI.World.Player, textTarget);
177                                 textTarget.Append(te.FullyRepaired ? "Functional, " : "Broken, ");
178                                 textTarget.Append(te.Activated ? "Online, " : "Offline, ");
179                                 textTarget.Append(" Target: [ ");
180                                 textTarget.Append(te.TargetLocation != null ? "Set ]" : "Invalid ]");//Or ABS coords?           
181                                 textTarget.AppendFormat(", Range ({0} ~ {1})", te.MinTeleporterRangeInBlocks, te.MaxTeleporterRangeInBlocks);
182                                 poi.AddReplace(
183                                                         new PointOfInterest
184                                                         {
185                                                                 Name = "Translocator",
186                                                                 PrettyLocation = posn.PrettyCoords(clientAPI),
187                                                                 Location = posn.Copy(),
188                                                                 Notes = textTarget.ToString(),
189                                                                 Timestamp = DateTime.UtcNow,
190                                                                 Destination = te.TargetLocation != null ? new BlockPosJson(te.TargetLocation.Copy()) : null
191                                                         }
192                                                         );
193                         }
194                 }
195
196                 #endregion
197         }
198 }
199