Skip to content

Commit

Permalink
Add support for zero or multiple compiled-in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Oct 15, 2023
1 parent 2698cab commit c738d2a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 142 deletions.
6 changes: 3 additions & 3 deletions DMCompiler/DMStandard/Types/World.dm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
var/tick_usage
var/loop_checks = 0 as opendream_unimplemented

var/maxx = 0
var/maxy = 0
var/maxz = 0
var/maxx = null
var/maxy = null
var/maxz = null
var/icon_size = 32
var/view = 5
var/movement_mode = LEGACY_MOVEMENT_MODE as opendream_unimplemented
Expand Down
4 changes: 3 additions & 1 deletion OpenDream.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestGame", "TestGame", "{72
ProjectSection(SolutionItems) = preProject
TestGame\code.dm = TestGame\code.dm
TestGame\environment.dme = TestGame\environment.dme
TestGame\map.dmm = TestGame\map.dmm
TestGame\renderer_tests.dm = TestGame\renderer_tests.dm
TestGame\map_z1.dmm = TestGame\map_z1.dmm
TestGame\map_z2.dmm = TestGame\map_z2.dmm
TestGame\map_z3.dmm = TestGame\map_z3.dmm
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "icons", "icons", "{4F1F4C68-4117-4ECC-8174-D399C1D6C409}"
Expand Down
10 changes: 2 additions & 8 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void StartWorld() {

// Call New() on all /area and /turf that exist, each with waitfor=FALSE separately. If <global init> created any /area, call New a SECOND TIME
// new() up /objs and /mobs from compiled-in maps [order: (1,1) then (2,1) then (1,2) then (2,2)]
_dreamMapManager.InitializeAtoms(_compiledJson.Maps![0]);
_dreamMapManager.InitializeAtoms(_compiledJson.Maps);

// Call world.New()
WorldInstance.SpawnProc("New");
Expand Down Expand Up @@ -115,11 +115,6 @@ public bool LoadJson(string? jsonPath) {
_sawmill.Error("Compiler opcode version does not match the runtime version!");
}

if (json.Maps == null || json.Maps.Count == 0) throw new ArgumentException("No maps were given");
if (json.Maps.Count > 1) {
_sawmill.Warning("Loading more than one map is not implemented, skipping additional maps");
}

_compiledJson = json;
var rootPath = Path.GetDirectoryName(jsonPath)!;
var resources = _compiledJson.Resources ?? Array.Empty<string>();
Expand Down Expand Up @@ -149,8 +144,7 @@ public bool LoadJson(string? jsonPath) {

Globals[GlobalNames.IndexOf("world")] = new DreamValue(WorldInstance);

// Load turfs and areas of compiled-in maps, recursively calling <init>, but suppressing all New
_dreamMapManager.LoadAreasAndTurfs(_compiledJson.Maps[0]);
_dreamMapManager.LoadMaps(_compiledJson.Maps);

_statusHost.SetMagicAczProvider(new DreamMagicAczProvider(
_dependencyCollection, rootPath, resources
Expand Down
52 changes: 41 additions & 11 deletions OpenDreamRuntime/DreamMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace OpenDreamRuntime {
public sealed class DreamMapManager : IDreamMapManager {
[Dependency] private readonly DreamManager _dreamManager = default!;
[Dependency] private readonly AtomManager _atomManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly DreamObjectTree _objectTree = default!;
Expand Down Expand Up @@ -81,16 +82,41 @@ public void UpdateTiles() {
}
}

public void LoadAreasAndTurfs(DreamMapJson map) {
Size = new Vector2i(map.MaxX, map.MaxY);
SetZLevels(map.MaxZ);
public void LoadMaps(List<DreamMapJson>? maps) {
var world = _dreamManager.WorldInstance;
var maxX = (int)world.ObjectDefinition.Variables["maxx"].UnsafeGetValueAsFloat();
var maxY = (int)world.ObjectDefinition.Variables["maxy"].UnsafeGetValueAsFloat();
var maxZ = (int)world.ObjectDefinition.Variables["maxz"].UnsafeGetValueAsFloat();

if (maps != null) {
foreach (var map in maps) {
maxX = Math.Max(maxX, map.MaxX);
maxY = Math.Max(maxY, map.MaxY);
maxZ = Math.Max(maxZ, map.MaxZ);
}
}

foreach (MapBlockJson block in map.Blocks) {
LoadMapAreasAndTurfs(block, map.CellDefinitions);
if (maxX == 0 || maxY == 0 || maxZ == 0) {
_sawmill.Error("Zero-size maps are not supported; map dimensions were set to a minimum of 1");
maxX = Math.Max(maxX, 1);
maxY = Math.Max(maxY, 1);
maxZ = Math.Max(maxZ, 1);
}

Size = new Vector2i(maxX, maxY);
SetZLevels(maxZ);

if (maps != null) {
// Load turfs and areas of compiled-in maps, recursively calling <init>, but suppressing all New
foreach (var map in maps) {
foreach (MapBlockJson block in map.Blocks) {
LoadMapAreasAndTurfs(block, map.CellDefinitions);
}
}
}
}

public void InitializeAtoms(DreamMapJson map) {
public void InitializeAtoms(List<DreamMapJson>? maps) {
// Call New() on all /area in this particular order, each with waitfor=FALSE
var seenAreas = new HashSet<DreamObject>();
for (var z = 1; z <= Levels; ++z) {
Expand Down Expand Up @@ -121,9 +147,13 @@ public void InitializeAtoms(DreamMapJson map) {
}
}

// new() up /objs and /mobs from compiled-in maps
foreach (MapBlockJson block in map.Blocks) {
LoadMapObjectsAndMobs(block, map.CellDefinitions);
if (maps != null) {
// new() up /objs and /mobs from compiled-in maps
foreach (var map in maps) {
foreach (MapBlockJson block in map.Blocks) {
LoadMapObjectsAndMobs(block, map.CellDefinitions);
}
}
}
}

Expand Down Expand Up @@ -344,8 +374,8 @@ public Cell(DreamObjectArea area) {
public int Levels { get; }

public void Initialize();
public void LoadAreasAndTurfs(DreamMapJson map);
public void InitializeAtoms(DreamMapJson map);
public void LoadMaps(List<DreamMapJson>? maps);
public void InitializeAtoms(List<DreamMapJson>? maps);
public void UpdateTiles();

public void SetTurf(DreamObjectTurf turf, DreamObjectDefinition type, DreamProcArguments creationArguments);
Expand Down
10 changes: 5 additions & 5 deletions OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,11 +1763,6 @@ public static ProcStatus GetStep(DMProcState state) {
}

var dir = d.MustGetValueAsInteger();
if (dir >= 16) { // Anything greater than (NORTH | SOUTH | EAST | WEST) is not valid. < 0 is fine though!
state.Push(DreamValue.Null);
return ProcStatus.Continue;
}

var locPos = state.Proc.AtomManager.GetAtomPosition(loc);

if ((dir & (int) AtomDirection.North) != 0)
Expand All @@ -1780,6 +1775,11 @@ public static ProcStatus GetStep(DMProcState state) {
if ((dir & (int) AtomDirection.West) != 0) // A dir of EAST | WEST will cancel out
locPos.X -= 1;

if ((dir & (int) AtomDirection.Up) != 0)
locPos.Z += 1;
if ((dir & (int) AtomDirection.Down) != 0) // A dir of UP | DOWN will cancel out
locPos.Z -= 1;

state.Proc.DreamMapManager.TryGetTurfAt((locPos.X, locPos.Y), locPos.Z, out var turf);
state.Push(new DreamValue(turf));
return ProcStatus.Continue;
Expand Down
3 changes: 3 additions & 0 deletions OpenDreamShared/Dream/AtomDirection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public enum AtomDirection : byte {
East = 4,
West = 8,

Up = 16,
Down = 32,

Northeast = North | East,
Southeast = South | East,
Southwest = South | West,
Expand Down
4 changes: 3 additions & 1 deletion TestGame/environment.dme
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
// BEGIN_INCLUDE
#include "code.dm"
#include "renderer_tests.dm"
#include "map.dmm"
#include "map_z1.dmm"
#include "map_z2.dmm"
#include "map_z3.dmm"
// END_INCLUDE
113 changes: 0 additions & 113 deletions TestGame/map.dmm

This file was deleted.

15 changes: 15 additions & 0 deletions TestGame/map_z2.dmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"a" = (/turf,/area)
"b" = (/turf/blue,/area)

(1,1,2) = {"
baaaaaaaab
abaaaaaaba
aabaaaabaa
aaabaabaaa
aaaabbaaaa
aaaabbaaaa
aaabaabaaa
aabaaaabaa
abaaaaaaba
baaaaaaaab
"}
15 changes: 15 additions & 0 deletions TestGame/map_z3.dmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"a" = (/turf,/area)
"b" = (/turf/blue,/area)

(1,1,3) = {"
aaaabbaaaa
aaabaabaaa
aabaaaabaa
abaaaaaaba
baaaaaaaab
baaaaaaaab
abaaaaaaba
aabaaaabaa
aaabaabaaa
aaaabbaaaa
"}

0 comments on commit c738d2a

Please sign in to comment.