Skip to content

Commit

Permalink
Moved the detection code
Browse files Browse the repository at this point in the history
Added in a cache system to stop the game from lagging when spawning
in a new entity that uses the color system that may eventually move to
the fox core to make recoloring easier
  • Loading branch information
NimbusFox committed Oct 4, 2018
1 parent b0095a0 commit 7e520f0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
37 changes: 11 additions & 26 deletions Entities/Bot/BotEntityPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,23 @@ public override void RenderUpdate(Timestep timestep, Entity entity, AvatarContro
}

if (logic.UpdateColors) {
WorldEditHook.Instance.CreateCache();

var botTile = Helpers.MakeTile(_botTileCode);
var matrix = botTile.Configuration.Icon as MatrixDrawable;
var bot = botTile.Configuration.Icon.GetPrivateFieldValue<CompactVertexDrawable>("_drawable");

var list = bot.CompileToVoxelVertexArray();

for (var i = 0; i < list.Length; i++) {
var colSelected = false;
foreach (var color in logic.ColorReplace) {
if (colSelected) {
break;
}
for (var r = -logic.BotComponent.PaletteTolerance; r <= logic.BotComponent.PaletteTolerance; r++) {
if (colSelected) {
break;
}
for (var g = -logic.BotComponent.PaletteTolerance; g <= logic.BotComponent.PaletteTolerance; g++) {
if (colSelected) {
break;
}
for (var b = -logic.BotComponent.PaletteTolerance; b <= logic.BotComponent.PaletteTolerance; b++) {
if (colSelected) {
break;
}
var col = list[i].Color;
var curCol = new Color(color.Key.R + r, color.Key.G + g, color.Key.B + b);

if (col.R == curCol.R && col.G == curCol.G && col.B == curCol.B) {
list[i].Color = new Color(color.Value.R + r, color.Value.G + g, color.Value.B + b, col.A);
colSelected = true;
}
}
if (WorldEditHook.Cache.ContainsKey(_botTileCode)) {
foreach (var cache in WorldEditHook.Cache[_botTileCode]) {
foreach (var color in logic.ColorReplace) {
if (color.Key.R + cache.Value.r == list[cache.Key].Color.R &&
color.Key.G + cache.Value.g == list[cache.Key].Color.G &&
color.Key.B + cache.Value.b == list[cache.Key].Color.B) {
list[cache.Key].Color = new Color(color.Value.R + cache.Value.r,
color.Value.G + cache.Value.g, color.Value.B + cache.Value.b,
list[cache.Key].Color.A);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Items/Wands/Selection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public override void Control(Entity entity, EntityUniverseFacade facade, Control

protected void CreateSelectionEntity(Entity entity, EntityUniverseFacade facade, Vector3I location) {
if (entity.Logic is PlayerEntityLogic logic) {
//_selectionEntity = BorderEntityBuilder.Spawn(location, facade, logic.Uid());
BotEntityBuilder.Spawn(location, facade, logic.DisplayName(), logic.Uid());
_selectionEntity = BorderEntityBuilder.Spawn(location, facade, logic.Uid());
}
}

Expand Down
2 changes: 1 addition & 1 deletion Staxel/Bots/WorldEditBot.tile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"FF000000",
"FFFFFFFF"
],
"paletteTolerance": 6
"paletteTolerance": 20
}
}
78 changes: 76 additions & 2 deletions WorldEditHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using NimbusFox.FoxCore;
using NimbusFox.FoxCore.Classes;
using NimbusFox.WorldEdit.Components;
using Plukit.Base;
using Staxel;
using Staxel.Draw;
using Staxel.Items;
using Staxel.Logic;
using Staxel.Player;
Expand All @@ -14,6 +19,10 @@ namespace NimbusFox.WorldEdit {
internal class WorldEditHook : IFoxModHookV3 {
internal static WorldEditHook Instance { get; private set; }

public static Dictionary<string, Dictionary<int, (int r, int g, int b)>> Cache = new Dictionary<string, Dictionary<int, (int r, int g, int b)>>();

private static bool _cleanCache = true;

public Fox_Core FxCore { get; }

public WorldEditHook() {
Expand All @@ -24,10 +33,16 @@ public WorldEditHook() {
public void Dispose() { }
public void GameContextInitializeInit() { }
public void GameContextInitializeBefore() { }
public void GameContextInitializeAfter() { }

public void GameContextInitializeAfter() {
}
public void GameContextDeinitialize() { }
public void GameContextReloadBefore() { }
public void GameContextReloadAfter() { }

public void GameContextReloadAfter() {
_cleanCache = true;
CreateCache();
}
public void UniverseUpdateBefore(Universe universe, Timestep step) { }
public void UniverseUpdateAfter() { }
public bool CanPlaceTile(Entity entity, Vector3I location, Tile tile, TileAccessFlags accessFlags) {
Expand Down Expand Up @@ -68,5 +83,64 @@ public void OnPlayerSaveAfter(PlayerEntityLogic logic, out Blob saveBlob) {

public void OnPlayerConnect(Entity entity) { }
public void OnPlayerDisconnect(Entity entity) { }

public void CreateCache() {
if (_cleanCache) {
Cache = new Dictionary<string, Dictionary<int, (int r, int g, int b)>>();

foreach (var tile in GameContext.TileDatabase.AllMaterials()
.Where(x => x.Components.Contains<BotComponent>())) {
var data = new Dictionary<int, (int r, int g, int b)>();

var component = tile.Components.Get<BotComponent>();

var toLook = component.Palettes.Keys;

if (tile.Icon == null) {
return;
}

var bot = tile.Icon.GetPrivateFieldValue<CompactVertexDrawable>("_drawable");

var list = bot.CompileToVoxelVertexArray();

for (var i = 0; i < list.Length; i++) {
var colSelected = false;
var col = list[i].Color;
foreach (var color in toLook) {
if (colSelected) {
break;
}
for (var r = -component.PaletteTolerance; r <= component.PaletteTolerance; r++) {
if (colSelected) {
break;
}
for (var g = -component.PaletteTolerance; g <= component.PaletteTolerance; g++) {
if (colSelected) {
break;
}
for (var b = -component.PaletteTolerance; b <= component.PaletteTolerance; b++) {
if (colSelected) {
break;
}

var curCol = new Color(color.R + r, color.G + g, color.B + b);

if (col.R == curCol.R && col.G == curCol.G && col.B == curCol.B) {
data.Add(i, (r, g, b));
colSelected = true;
}
}
}
}
}
}

Cache.Add(tile.Code, data);
}

_cleanCache = false;
}
}
}
}

0 comments on commit 7e520f0

Please sign in to comment.