2 using System.Collections.Generic;
6 using Vintagestory.API.Client;
7 using Vintagestory.API.Common;
8 using Vintagestory.API.MathTools;
12 public static class Helpers
16 /// Hue, Saturation Value colorspace
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)
24 int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
25 double f = hue / 60 - Math.Floor(hue / 60);
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));
34 return Color.FromArgb(255, v, t, p);
36 return Color.FromArgb(255, q, v, p);
38 return Color.FromArgb(255, p, v, t);
40 return Color.FromArgb(255, p, q, v);
42 return Color.FromArgb(255, t, p, v);
44 return Color.FromArgb(255, v, p, q);
47 public static string PrettyCoords(this BlockPos location, ICoreClientAPI ClientApi)
49 var start = ClientApi.World.DefaultSpawnPosition.AsBlockPos;
51 return string.Format("X{0}, Y{1}, Z{2}", location.X - start.X, location.Y, location.Z - start.Z );
54 public static BlockPos AverageHighestPos(List<BlockPos> positions)
56 int x = 0, y = 0, z = 0, length = positions.Count;
57 foreach (BlockPos pos in positions)
60 y = Math.Max(y, pos.Y);//Mutant Y-axis, take "HIGHEST"
63 return new BlockPos(x/ length, y, z / length);
66 public static BlockPos PickRepresentativePosition(List<BlockPos> positions)
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!
73 //Otherwise...pick one
74 var whichever = positions.Last(poz => poz.Y == averagePos.Y);
82 /// Find a BLOCK partial path match: BlockID
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)
88 Dictionary<int, string> arbBlockIDTable = new Dictionary<int, string>( );
91 if (CoreApi.World.Blocks != null) {
94 CoreApi.World.Logger.VerboseDebug(" World Blocks [Count: {0}]", CoreApi.World.Blocks.Count);
96 //If Brute force won't work; use GROOT FORCE!
97 //var theBlock = ClientApi.World.BlockAccessor.GetBlock(0);
99 if (!material.HasValue) {
100 foreach (Block blk in CoreApi.World.Blocks) {
101 if (blk.IsMissing || blk.Id == 0 || blk.BlockId == 0) {
103 } else if (blk.Code != null && blk.Code.BeginsWith(assetName.Domain, assetName.Path)) {
105 //CoreApi.World.Logger.VerboseDebug("Block: [{0} ({1})] = #{2}", blk.Code.Path, blk.BlockMaterial, blk.BlockId);
108 arbBlockIDTable.Add(blk.BlockId, blk.Code.Path);
112 foreach (Block blk in CoreApi.World.Blocks) {
113 if (blk.IsMissing || blk.Id == 0 || blk.BlockId == 0) {
115 } else if (blk.Code != null && material.Value == blk.BlockMaterial && blk.Code.BeginsWith(assetName.Domain, assetName.Path)) {
117 //CoreApi.World.Logger.VerboseDebug("Block: [{0} ({1})] = #{2}", blk.Code.Path, blk.BlockMaterial, blk.BlockId);
120 arbBlockIDTable.Add(blk.BlockId, blk.Code.Path);
126 CoreApi.World.Logger.VerboseDebug("Block gaps: {0}", emptyCount);
130 return arbBlockIDTable;
136 /// Chunk local index. Not block position!
138 /// <remarks>Clamps to 5 bit ranges automagically</remarks>
139 public static int ChunkBlockIndicie16(int X_index, int Y_index, int Z_index)
141 return ((Y_index & 31) * 32 + (Z_index & 31)) * 32 + (X_index & 31);
145 /// Chunk index converted from block position (in world)
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)
153 return ((blockPos.Y & 31) * 32 + (blockPos.Z & 31)) * 32 + (blockPos.X & 31);