Skip to content

Commit

Permalink
handles shrinking maxx/y (#1798)
Browse files Browse the repository at this point in the history
Co-authored-by: wixoa <wixoag@gmail.com>
  • Loading branch information
harryob and wixoaGit committed Jun 22, 2024
1 parent caec1ef commit 94ea105
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions Content.IntegrationTests/DMProject/Tests/range.dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//Tests that /proc/range() is iterating along the correct, wonky path
/proc/test_range()
world.maxx = world.maxy = 5
//Test that it goes in the right order
var/list/correctCoordinates = list(
list(3,3),
Expand Down
57 changes: 42 additions & 15 deletions OpenDreamRuntime/DreamMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,54 @@ public sealed class DreamMapManager : IDreamMapManager {
}

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

var newX = Math.Max(oldSize.X, size.X);
var newY = Math.Max(oldSize.Y, size.Y);

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

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

if(Size.X > oldSize.X || Size.Y > oldSize.Y) {
foreach (Level existingLevel in _levels) {
var oldCells = existingLevel.Cells;

existingLevel.Cells = new Cell[Size.X, Size.Y];
for (var x = 1; x <= Size.X; x++) {
for (var 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 = 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());
}
}
}
}

existingLevel.Cells[x - 1, y - 1] = new Cell(defaultArea);
SetTurf(new Vector2i(x, y), existingLevel.Z, _defaultTurf.ObjectDefinition, new());
if (Size.X > size.X || Size.Y > size.Y) {
Size = size;

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

existingLevel.Cells = new Cell[size.X, size.Y];
for (var x = 1; x <= oldSize.X; x++) {
for (var y = 1; y <= oldSize.Y; y++) {
if (x > size.X || y > size.Y) {
var deleteCell = oldCells[x - 1, y - 1];
deleteCell.Turf?.Delete();
_mapSystem.SetTile(existingLevel.Grid, new Vector2i(x, y), Tile.Empty);
foreach (var movableToDelete in deleteCell.Movables) {
movableToDelete.Delete();
}
} else {
existingLevel.Cells[x - 1, y - 1] = oldCells[x - 1, y - 1];
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions TestGame/code.dm
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@
world.ODHotReloadInterface()
src << "done hot reload of interface!"

verb/manipulate_world_size()
var/x = input("New World X?", "World Size", 0) as num|null
var/y = input("New World Y?", "World Size", 0) as num|null

if(!x || !y)
return

world.maxx = x
world.maxy = y

/mob/Stat()
if (statpanel("Status"))
stat("tick_usage", world.tick_usage)
Expand Down

0 comments on commit 94ea105

Please sign in to comment.