OSDN Git Service

W.I.P. #3 This Coordinate system...tricky!
[automap/automap.git] / Automap / Helpers.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Linq;
5
6 using Vintagestory.API.Client;
7 using Vintagestory.API.Common;
8 using Vintagestory.API.MathTools;
9
10 namespace Automap
11 {
12         public static class Helpers
13         {
14                 
15                 /// <summary>
16                 /// Hue, Saturation Value colorspace
17                 /// </summary>
18                 /// <returns>The color equiv.</returns>
19                 /// <param name="hue">0 - 360 for hue.</param>
20                 /// <param name="saturation"> 0 - 1 for saturation or value..</param>
21                 /// <param name="value"> 0 - 1 for saturation or value..</param>
22                 public static Color FromHSV(double hue, double saturation, double value)
23                 {
24                         int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
25                         double f = hue / 60 - Math.Floor(hue / 60);
26
27                         value = value * 255;
28                         int v = Convert.ToInt32(value);
29                         int p = Convert.ToInt32(value * (1 - saturation));
30                         int q = Convert.ToInt32(value * (1 - f * saturation));
31                         int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
32
33                         if (hi == 0)
34                                 return Color.FromArgb(255, v, t, p);
35                         else if (hi == 1)
36                                 return Color.FromArgb(255, q, v, p);
37                         else if (hi == 2)
38                                 return Color.FromArgb(255, p, v, t);
39                         else if (hi == 3)
40                                 return Color.FromArgb(255, p, q, v);
41                         else if (hi == 4)
42                                 return Color.FromArgb(255, t, p, v);
43                         else
44                                 return Color.FromArgb(255, v, p, q);
45                 }
46
47                 public static string PrettyCoords(this BlockPos location, ICoreClientAPI ClientApi)
48                 {
49                         var start = ClientApi.World.DefaultSpawnPosition.AsBlockPos;
50
51                         return string.Format("X{0}, Y{1}, Z{2}", location.X - start.X, location.Y, location.Z - start.Z );
52                 }
53
54                 public static BlockPos AverageHighestPos(List<BlockPos> positions)
55                 {
56                         int x = 0, y = 0, z = 0, length = positions.Count;
57                         foreach (BlockPos pos in positions)
58                         {
59                                 x += pos.X;
60                                 y = Math.Max(y, pos.Y);//Mutant Y-axis, take "HIGHEST"
61                                 z += pos.Z;
62                         }
63                         return new BlockPos(x/ length, y, z / length);
64                 }
65
66                 public static BlockPos PickRepresentativePosition(List<BlockPos> positions)
67                 {
68                         var averagePos = AverageHighestPos( positions );
69                         if ( positions.Any( pos => pos.X == averagePos.X && pos.Y == averagePos.Y && pos.Z == averagePos.Z ) ) {
70                                 return averagePos;//lucky ~ center was it!
71                         }
72
73                         //Otherwise...pick one
74                         var whichever = positions.Last(poz => poz.Y == averagePos.Y);
75
76                         return whichever;
77                 }
78
79
80
81                 /// <summary>
82                 /// Find a BLOCK partial path match: BlockID
83                 /// </summary>
84                 /// <returns>Matching finds</returns>
85                 /// <param name="assetName">Asset name.</param>
86                 public static Dictionary<int, string> ArbitrarytBlockIdHunter(this ICoreAPI CoreApi ,AssetLocation assetName, EnumBlockMaterial? material = null)
87                 {
88                         Dictionary<int, string> arbBlockIDTable = new Dictionary<int, string>( );
89                         uint emptyCount = 0;
90
91                         if (CoreApi.World.Blocks != null) {
92
93                                 #if DEBUG
94                                 CoreApi.World.Logger.VerboseDebug(" World Blocks [Count: {0}]", CoreApi.World.Blocks.Count);
95                                 #endif
96                                 //If Brute force won't work; use GROOT FORCE!
97                                 //var theBlock = ClientApi.World.BlockAccessor.GetBlock(0);
98
99                                 if (!material.HasValue) {
100                                         foreach (Block blk in CoreApi.World.Blocks) {
101                                                 if (blk.IsMissing || blk.Id == 0 || blk.BlockId == 0) {
102                                                         emptyCount++;
103                                                 } else if (blk.Code != null && blk.Code.BeginsWith(assetName.Domain, assetName.Path)) {
104                                                         #if DEBUG
105                                                         //CoreApi.World.Logger.VerboseDebug("Block: [{0} ({1})] =  #{2}", blk.Code.Path, blk.BlockMaterial, blk.BlockId);
106                                                         #endif
107
108                                                         arbBlockIDTable.Add(blk.BlockId, blk.Code.Path);
109                                                 }
110                                         }
111                                 } else {
112                                         foreach (Block blk in CoreApi.World.Blocks) {
113                                                 if (blk.IsMissing || blk.Id == 0 || blk.BlockId == 0) {
114                                                         emptyCount++;
115                                                 } else if (blk.Code != null && material.Value == blk.BlockMaterial && blk.Code.BeginsWith(assetName.Domain, assetName.Path)) {
116                                                         #if DEBUG
117                                                         //CoreApi.World.Logger.VerboseDebug("Block: [{0} ({1})] =  #{2}", blk.Code.Path, blk.BlockMaterial, blk.BlockId);
118                                                         #endif
119
120                                                         arbBlockIDTable.Add(blk.BlockId, blk.Code.Path);
121                                                 }
122                                         }
123                                 }
124
125                                 #if DEBUG
126                                 CoreApi.World.Logger.VerboseDebug("Block gaps: {0}", emptyCount);
127                                 #endif
128                         }
129
130                         return arbBlockIDTable;
131                 }
132
133
134
135                 /// <summary>
136                 /// Chunk local index. Not block position!
137                 /// </summary>
138                 /// <remarks>Clamps to 5 bit ranges automagically</remarks>
139                 public static int ChunkBlockIndicie16(int X_index, int Y_index, int Z_index)
140                 {                       
141                         return ((Y_index & 31) * 32 + (Z_index & 31)) * 32 + (X_index & 31);
142                 }
143
144                 /// <summary>
145                 /// Chunk index converted from block position (in world)
146                 /// </summary>
147                 /// <returns>The block indicie.</returns>
148                 /// <param name="blockPos">Block position.</param>
149                 /// <remarks>Clamps to 5 bit ranges automagically</remarks>
150                 public static int ChunkBlockIndicie16(BlockPos blockPos)
151                 {
152                         //Chunk masked
153                         return ((blockPos.Y & 31) * 32 + (blockPos.Z & 31)) * 32 + (blockPos.X & 31);
154                 }
155         }
156 }
157