Skip to content

Commit

Permalink
Walkable surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Feb 20, 2024
1 parent b11317c commit c715aed
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 97 deletions.
16 changes: 11 additions & 5 deletions CentrED/HuesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,23 @@ public enum HueMode
{
NONE = 0,
HUED = 1,
PARTIAL = 2
PARTIAL = 2,
RGB = 255
}

public Vector3 GetHueVector(StaticTile tile, float alpha = 1)
public Vector4 GetHueVector(StaticTile tile, float alpha = 1)
{
return GetHueVector(tile.Id, tile.Hue, alpha);
}

public Vector3 GetHueVector(ushort id, ushort hue, float alpha = 1)
public Vector4 GetHueVector(ushort id, ushort hue, float alpha = 1)
{
var partial = TileDataLoader.Instance.StaticData[id].IsPartialHue;
var translucent = TileDataLoader.Instance.StaticData[id].IsTranslucent;
return GetHueVector(hue, partial, translucent ? 0.6f : alpha);
}

public Vector3 GetHueVector(ushort hue, bool partial, float alpha = 1)
public Vector4 GetHueVector(ushort hue, bool partial, float alpha = 1)
{
HueMode mode;

Expand All @@ -90,6 +91,11 @@ public Vector3 GetHueVector(ushort hue, bool partial, float alpha = 1)
mode = HueMode.NONE;
}

return new Vector3(hue, (int)mode, alpha);
return new Vector4(hue, 0, alpha, (int)mode);
}

public Vector4 GetRGBVector(Color color)
{
return new Vector4(color.ToVector3(), (int)HueMode.RGB);
}
}
4 changes: 2 additions & 2 deletions CentrED/Map/LandObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void UpdateId(ushort newId)
isStretched |= CalculateNormals(out var normals);
for (int i = 0; i < 4; i++)
{
Vertices[i].HueVec = normals[i];
Vertices[i].Normal = normals[i];
}
}
var useTexMap = isTexMapValid && (Config.Instance.PreferTexMaps || isStretched);
Expand Down Expand Up @@ -110,7 +110,7 @@ public void UpdateId(ushort newId)
}
for (int i = 0; i < 4; i++)
{
Vertices[i].TextureCoordinate = texCoords[i];
Vertices[i].Texture = texCoords[i];
}

}
Expand Down
101 changes: 86 additions & 15 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using CentrED.Client;
using CentrED.Network;
using CentrED.Renderer;
Expand Down Expand Up @@ -54,6 +53,7 @@ public Tool ActiveTool
public bool ShowNoDraw = false;
public int VirtualLayerZ;
public bool UseVirtualLayer = false;
public bool WalkableSurfaces = false;

public readonly Camera Camera = new();

Expand Down Expand Up @@ -721,12 +721,71 @@ public bool IsTileVisible(ushort id)
return true;
}

private static Vector4 NonWalkableHue = HuesManager.Instance.GetRGBVector(new Color(50, 0, 0));
private static Vector4 WalkableHue = HuesManager.Instance.GetRGBVector(new Color(0, 50, 0));

public bool IsWalkable(LandObject lo)
{
//TODO: Save value for later to avoid calculation, recalculate whole cell on operations
if (lo.Walkable.HasValue)
return lo.Walkable.Value;

var landTile = lo.LandTile;
bool walkable = !TileDataLoader.Instance.LandData[landTile.Id].IsImpassable;
if (walkable)
{
var staticObjects = StaticTiles[landTile.X, landTile.Y];
if (staticObjects != null)
{
foreach (var so in staticObjects)
{
var staticTile = so.StaticTile;
var staticTileData = TileDataLoader.Instance.StaticData[staticTile.Id];
var ok = staticTile.Z + staticTileData.Height <= landTile.Z || landTile.Z + 16 <= staticTile.Z;
if (!ok && !staticTileData.IsSurface && staticTileData.IsImpassable)
{
return false;
}
}
}
}
return true;
}

public bool IsWalkable(StaticObject so)
{
if (so.Walkable.HasValue)
return so.Walkable.Value;

var tile = so.StaticTile;
var thisTileData = TileDataLoader.Instance.StaticData[tile.Id];
bool walkable = !thisTileData.IsImpassable;
if (walkable)
{
var staticObjects = StaticTiles[tile.X, tile.Y];
if (staticObjects != null)
{
foreach (var so2 in staticObjects)
{
var staticTile = so2.StaticTile;
var staticTileData = TileDataLoader.Instance.StaticData[staticTile.Id];
var ok = staticTile.Z + staticTileData.Height <= tile.Z || tile.Z + 16 <= staticTile.Z;
if (!ok && !staticTileData.IsSurface && staticTileData.IsImpassable)
{
return false;
}
}
}
}
return true;
}

private bool WithinZRange(short z)
{
return z >= MinZ && z <= MaxZ;
}

private void DrawStatic(StaticObject so, Vector3 hueOverride = default)
private void DrawStatic(StaticObject so, Vector4 hueOverride = default)
{
var tile = so.StaticTile;
if (!CanDrawStatic(tile.Id))
Expand All @@ -741,13 +800,14 @@ private void DrawStatic(StaticObject so, Vector3 hueOverride = default)
_mapRenderer.DrawMapObject(so, hueOverride);
}

private void DrawLand(LandObject lo, Vector3 hueOverride = default)
private void DrawLand(LandObject lo, Vector4 hueOverride = default)
{
if (!CanDrawLand(lo.Tile.Id))
var landTile = lo.LandTile;
if (!CanDrawLand(landTile.Id))
return;
if (lo.Tile.Id > TileDataLoader.Instance.LandData.Length)
if (landTile.Id > TileDataLoader.Instance.LandData.Length)
return;
if (!WithinZRange(lo.Tile.Z))
if (!WithinZRange(landTile.Z))
return;

_mapRenderer.DrawMapObject(lo, hueOverride);
Expand Down Expand Up @@ -824,9 +884,7 @@ private void DrawSelectionBuffer()
var landTile = LandTiles[x, y];
if (landTile != null)
{
var i = landTile.ObjectId;
var color = new Color(i & 0xFF, (i >> 8) & 0xFF, (i >> 16) & 0xFF);
DrawLand(landTile, color.ToVector3());
DrawLand(landTile, landTile.ObjectIdColor);
}

var tiles = StaticTiles[x, y];
Expand All @@ -835,9 +893,7 @@ private void DrawSelectionBuffer()
{
if (tile.Visible)
{
var i = tile.ObjectId;
var color = new Color(i & 0xFF, (i >> 8) & 0xFF, (i >> 16) & 0xFF);
DrawStatic(tile, color.ToVector3());
DrawStatic(tile, tile.ObjectIdColor);
}
}
}
Expand Down Expand Up @@ -869,7 +925,15 @@ private void DrawLand()
{
var tile = LandTiles[x, y];
if (tile != null && tile.Visible)
DrawLand(tile);
{
var hueOverride = Vector4.Zero;
if (WalkableSurfaces && !TileDataLoader.Instance.LandData[tile.LandTile.Id].IsWet)
{
hueOverride = IsWalkable(tile) ? WalkableHue : NonWalkableHue;

}
DrawLand(tile, hueOverride);
}
}
}
foreach (var tile in GhostLandTiles.Values)
Expand Down Expand Up @@ -905,7 +969,14 @@ private void DrawStatics()
foreach (var tile in tiles)
{
if (tile.Visible)
DrawStatic(tile);
{
var hueOverride = Vector4.Zero;
if (WalkableSurfaces && TileDataLoader.Instance.StaticData[tile.Tile.Id].IsSurface)
{
hueOverride = IsWalkable(tile) ? WalkableHue : NonWalkableHue;
}
DrawStatic(tile, hueOverride);
}
}
}
}
Expand Down Expand Up @@ -933,7 +1004,7 @@ public void DrawVirtualLayer()
null
);
VirtualLayer.Z = (sbyte)VirtualLayerZ;
_mapRenderer.DrawMapObject(VirtualLayer, Vector3.Zero);
_mapRenderer.DrawMapObject(VirtualLayer, Vector4.Zero);
_mapRenderer.End();
}

Expand Down
3 changes: 3 additions & 0 deletions CentrED/Map/MapObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public abstract class MapObject
public MapObject()
{
ObjectId = GetNextId();
ObjectIdColor = new Color(ObjectId & 0xFF, (ObjectId >> 8) & 0xFF, (ObjectId >> 16) & 0xFF).ToVector4();
}

public static int GetNextId()
Expand All @@ -26,6 +27,8 @@ public static int GetNextId()
}

public readonly int ObjectId;
public readonly Vector4 ObjectIdColor;

public bool Visible = true;
public Texture2D Texture;
public Rectangle TextureBounds;
Expand Down
22 changes: 13 additions & 9 deletions CentrED/Map/StaticObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public StaticObject(StaticTile tile)
UpdateId(Tile.Id);
UpdatePos(tile.X, tile.Y, tile.Z);
UpdateHue(tile.Hue);
for (int i = 0; i < 4; i++)
{
Vertices[i].Normal = Vector3.Zero;
}
}

public void UpdateId(ushort newId)
Expand All @@ -28,10 +32,10 @@ public void UpdateId(ushort newId)
var texWidth = TextureBounds.Width / (float)Texture.Width - onePixel;
var texHeight = TextureBounds.Height / (float)Texture.Height - onePixel;

Vertices[0].TextureCoordinate = new Vector3(texX, texY, 0f);
Vertices[1].TextureCoordinate = new Vector3(texX + texWidth, texY, 0f);
Vertices[2].TextureCoordinate = new Vector3(texX, texY + texHeight, 0f);
Vertices[3].TextureCoordinate = new Vector3(texX + texWidth, texY + texHeight, 0f);
Vertices[0].Texture = new Vector3(texX, texY, 0f);
Vertices[1].Texture = new Vector3(texX + texWidth, texY, 0f);
Vertices[2].Texture = new Vector3(texX, texY + texHeight, 0f);
Vertices[3].Texture = new Vector3(texX + texWidth, texY + texHeight, 0f);
UpdateDepthOffset();
}

Expand All @@ -40,7 +44,7 @@ public void UpdateDepthOffset()
var depthOffset = StaticTile.CellIndex * 0.0001f;
for (int i = 0; i < 4; i++)
{
Vertices[i].TextureCoordinate.Z = depthOffset;
Vertices[i].Texture.Z = depthOffset;
}
}

Expand All @@ -63,7 +67,7 @@ public void UpdateHue(ushort newHue)
var hueVec = HuesManager.Instance.GetHueVector(Tile.Id, newHue);
for (int i = 0; i < 4; i++)
{
Vertices[i].HueVec = hueVec;
Vertices[i].Hue = hueVec;
}
}

Expand All @@ -84,19 +88,19 @@ public int GhostHue
_ghostHue = value;
for (var index = 0; index < Vertices.Length; index++)
{
Vertices[index].HueVec = HuesManager.Instance.GetHueVector(Tile.Id, (ushort)_ghostHue, Vertices[index].HueVec.Z);
Vertices[index].Hue = HuesManager.Instance.GetHueVector(Tile.Id, (ushort)_ghostHue, Vertices[index].Hue.Z);
}
}
}

public float Alpha
{
get => Vertices[0].HueVec.Z;
get => Vertices[0].Hue.Z;
set
{
for (var index = 0; index < Vertices.Length; index++)
{
Vertices[index].HueVec.Z = value;
Vertices[index].Hue.Z = value;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions CentrED/Map/TileObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public abstract class TileObject : MapObject
public const float TILE_SIZE = 31.11f;
public const float TILE_Z_SCALE = 4.0f;
public BaseTile Tile;
public bool? Walkable;

public virtual void Reset()
{
Expand Down
8 changes: 4 additions & 4 deletions CentrED/Map/VirtualLayerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private VirtualLayerObject()
{
for (int i = 0; i < 4; i++)
{
Vertices[i] = new MapVertex(Vector3.Zero,Vector3.Zero, Vector3.Zero);
Vertices[i] = new MapVertex(Vector3.Zero,Vector3.Zero, Vector4.Zero, Vector3.Zero);
}
}

Expand Down Expand Up @@ -55,13 +55,13 @@ public sbyte Z
}
}

public Vector3 Color
public Vector4 Color
{
set
{
for (var i = 0; i < Vertices.Length; i++)
{
Vertices[i].HueVec = value;
Vertices[i].Hue = value;
}
}
}
Expand All @@ -72,7 +72,7 @@ public float Alpha
{
for (var i = 0; i < Vertices.Length; i++)
{
Vertices[i].TextureCoordinate.X = value;
Vertices[i].Texture.X = value;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CentrED/Map/VirtualLayerTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public VirtualLayerTile(ushort x = 0, ushort y = 0, sbyte z = 0)
Tile = new LandTile(0, x, y, z);
for (int i = 0; i < 4; i++)
{
Vertices[i] = new MapVertex(Vector3.Zero,Vector3.Zero, Vector3.Zero);
Vertices[i] = new MapVertex(Vector3.Zero,Vector3.Zero, Vector4.Zero, Vector3.Zero);
}
}

Expand Down
Loading

0 comments on commit c715aed

Please sign in to comment.