|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Terraria; |
| 4 | + |
| 5 | +namespace TerrariaApi.Server; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Cons"tile"ation provider is a tile adapter that will function similar to heaptile to reduce memory, but instead of using math to determine offsets, a TileData structure is used instead which already knows the layout. |
| 9 | +/// </summary> |
| 10 | +internal class ConstileationProvider : ModFramework.ICollection<ITile> |
| 11 | +{ |
| 12 | + private TileData[] data = null; |
| 13 | + |
| 14 | + public int Width => Main.maxTilesX + 1; |
| 15 | + public int Height => Main.maxTilesY + 1; |
| 16 | + |
| 17 | + public ITile this[int x, int y] |
| 18 | + { |
| 19 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 20 | + get |
| 21 | + { |
| 22 | + data ??= new TileData[Width * Height]; |
| 23 | + return new TileReference(ref data[Main.maxTilesY * x + y]); |
| 24 | + } |
| 25 | + |
| 26 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 27 | + set => new TileReference(ref data[Main.maxTilesY * x + y]).CopyFrom(value); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// <summary> |
| 32 | +/// Maps tile data to memory in an organised format |
| 33 | +/// </summary> |
| 34 | +/// <remarks>When updating this, refer to <see cref="ITile"/> properties, and adjust the Size accordingly. Size is likely not required, but it make it clear how many bytes we expect to consume</remarks> |
| 35 | +[StructLayout(LayoutKind.Sequential, Size = 14, Pack = 1)] |
| 36 | +internal struct TileData |
| 37 | +{ |
| 38 | + public ushort type; |
| 39 | + public ushort wall; |
| 40 | + public byte liquid; |
| 41 | + public ushort sTileHeader; |
| 42 | + public byte bTileHeader; |
| 43 | + public byte bTileHeader2; |
| 44 | + public byte bTileHeader3; |
| 45 | + public short frameX; |
| 46 | + public short frameY; |
| 47 | +} |
| 48 | + |
| 49 | +/// <summary> |
| 50 | +/// This class is intended to be issued back and forth between Terraria, while referring to a preallocated memory mapping of the tile data which avoids duplicating data and storing offsets. |
| 51 | +/// </summary> |
| 52 | +internal unsafe sealed class TileReference : Tile |
| 53 | +{ |
| 54 | + public override ushort type |
| 55 | + { |
| 56 | + get => _tile->type; |
| 57 | + set => _tile->type = value; |
| 58 | + } |
| 59 | + |
| 60 | + public override ushort wall |
| 61 | + { |
| 62 | + get => _tile->wall; |
| 63 | + set => _tile->wall = value; |
| 64 | + } |
| 65 | + |
| 66 | + public override byte liquid |
| 67 | + { |
| 68 | + get => _tile->liquid; |
| 69 | + set => _tile->liquid = value; |
| 70 | + } |
| 71 | + |
| 72 | + public override ushort sTileHeader |
| 73 | + { |
| 74 | + get => _tile->sTileHeader; |
| 75 | + set => _tile->sTileHeader = value; |
| 76 | + } |
| 77 | + |
| 78 | + public override byte bTileHeader |
| 79 | + { |
| 80 | + get => _tile->bTileHeader; |
| 81 | + set => _tile->bTileHeader = value; |
| 82 | + } |
| 83 | + |
| 84 | + public override byte bTileHeader2 |
| 85 | + { |
| 86 | + get => _tile->bTileHeader2; |
| 87 | + set => _tile->bTileHeader2 = value; |
| 88 | + } |
| 89 | + |
| 90 | + public override byte bTileHeader3 |
| 91 | + { |
| 92 | + get => _tile->bTileHeader3; |
| 93 | + set => _tile->bTileHeader3 = value; |
| 94 | + } |
| 95 | + |
| 96 | + public override short frameX |
| 97 | + { |
| 98 | + get => _tile->frameX; |
| 99 | + set => _tile->frameX = value; |
| 100 | + } |
| 101 | + |
| 102 | + public override short frameY |
| 103 | + { |
| 104 | + get => _tile->frameY; |
| 105 | + set => _tile->frameY = value; |
| 106 | + } |
| 107 | + |
| 108 | + public override void Initialise() |
| 109 | + { |
| 110 | + // this is called in ctor. we dont want to run the default code here. |
| 111 | + } |
| 112 | + |
| 113 | + readonly TileData* _tile; |
| 114 | + |
| 115 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 116 | + public TileReference(ref TileData tile) |
| 117 | + { |
| 118 | + _tile = (TileData*)Unsafe.AsPointer(ref tile); |
| 119 | + } |
| 120 | +} |
0 commit comments