Skip to content

Commit

Permalink
implements world.maxx and world.maxy (#1687)
Browse files Browse the repository at this point in the history
  • Loading branch information
hry-gh authored Feb 29, 2024
1 parent 62c384c commit e764074
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Content.Tests/DummyDreamMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public bool TryGetTurfAt(Vector2i pos, int z, out DreamObjectTurf turf) {

public void SetZLevels(int levels) { }

public void SetWorldSize(Vector2i size) { }

public EntityUid GetZLevelEntity(int z) {
return EntityUid.Invalid;
}
Expand Down
31 changes: 30 additions & 1 deletion OpenDreamRuntime/DreamMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ private DreamObjectArea GetOrCreateArea(MapObjectJson prototype) {
return area;
}

public void SetWorldSize(Vector2i size) {
if (size.X < Size.X || size.Y < Size.Y) {
return;
}

DreamObjectArea defaultArea = GetOrCreateArea(_defaultArea);
Vector2i oldSize = Size;

Size = size;
foreach (Level existingLevel in _levels) {
var oldCells = existingLevel.Cells;

existingLevel.Cells = new Cell[size.X, size.Y];
for (int x = 1; x <= size.X; x++) {
for (int y = 1; y <= size.Y; y++) {
if (x <= oldSize.X && y <= oldSize.Y) {
existingLevel.Cells[x - 1, y - 1] = oldCells[x - 1, y - 1];
continue;
}

existingLevel.Cells[x - 1, y - 1] = new Cell(defaultArea);
SetTurf(new Vector2i(x, y), existingLevel.Z, _defaultTurf.ObjectDefinition, new());
}
}
}

}

public void SetZLevels(int levels) {
if (levels > Levels) {
DreamObjectArea defaultArea = GetOrCreateArea(_defaultArea);
Expand Down Expand Up @@ -337,7 +365,7 @@ public interface IDreamMapManager {
public sealed class Level {
public readonly int Z;
public readonly MapGridComponent Grid;
public readonly Cell[,] Cells;
public Cell[,] Cells;
public readonly Dictionary<Vector2i, Tile> QueuedTileUpdates = new();

public Level(int z, MapGridComponent grid, DreamObjectArea area, Vector2i size) {
Expand Down Expand Up @@ -376,5 +404,6 @@ public Cell(DreamObjectArea area) {
public bool TryGetCellAt(Vector2i pos, int z, [NotNullWhen(true)] out Cell? cell);
public bool TryGetTurfAt(Vector2i pos, int z, [NotNullWhen(true)] out DreamObjectTurf? turf);
public void SetZLevels(int levels);
public void SetWorldSize(Vector2i size);
public EntityUid GetZLevelEntity(int z);
}
14 changes: 12 additions & 2 deletions OpenDreamRuntime/Objects/Types/DreamObjectWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ protected override void SetVar(string varName, DreamValue value) {
case "game_state":
case "hub":
case "hub_password":
case "maxx":
case "maxy":
case "mob":
case "name":
case "sleep_offline":
Expand Down Expand Up @@ -271,6 +269,18 @@ protected override void SetVar(string varName, DreamValue value) {
SetLog(value);
break;

case "maxx":
value.TryGetValueAsInteger(out var maxx);

DreamMapManager.SetWorldSize(new Vector2i(maxx, DreamMapManager.Size.Y));
break;

case "maxy":
value.TryGetValueAsInteger(out var maxy);

DreamMapManager.SetWorldSize(new Vector2i(DreamMapManager.Size.X, maxy));
break;

default:
throw new Exception($"Cannot set var \"{varName}\" on world");
}
Expand Down

0 comments on commit e764074

Please sign in to comment.