Skip to content

Commit 295db79

Browse files
authored
Nicer map handling in lua (#164)
map no longer needs to be a global variable.
1 parent c05b0f5 commit 295db79

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

data/mapgen.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ end
165165
-- END FUNCTIONS
166166

167167
-- BEGIN SCRIPT
168-
map = create_map(CURRENT_LEVEL) -- 'map' needs to be global
168+
local map = create_map(CURRENT_LEVEL)
169169
room_builder.load_textures(map)
170170
local map_matrix = generate_path()
171171

@@ -188,4 +188,7 @@ end
188188
if lockedDoorsAdded then
189189
add_keybearers(map)
190190
end
191+
192+
-- Finalize by returning the map
193+
return map
191194
-- END SCRIPT

data/maproombuilder.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ local floorDecor = {}
3535
local blockingFloorDecor = {}
3636
local lightDecor = {}
3737

38-
local function load_decor_textures()
38+
local function load_decor_textures(map)
3939
local td0 = add_texture(map, "Objects/Decor0.png")
4040
local td1 = add_texture(map, "Objects/Decor1.png")
4141

@@ -107,7 +107,7 @@ local function load_decor_textures()
107107
lightDecor.candle2 = { td0, td1, 32, 8 * 16, true, true }
108108
end
109109

110-
local function load_special_tiles()
110+
local function load_special_tiles(map)
111111
local tt = add_texture(map, "Objects/Tile.png")
112112
special.level_exit = { tt, -1, 16, 16, false, true, true }
113113
end
@@ -591,8 +591,8 @@ function module.load_textures(map)
591591
wall.vertical = { t_wall, -1, xo + 0, yo + 16, true }
592592
wall.horizontal = { t_wall, -1, xo + 16, yo + 0, true }
593593

594-
load_decor_textures()
595-
load_special_tiles()
594+
load_decor_textures(map)
595+
load_special_tiles(map)
596596
end
597597

598598
return module

data/menumapgen.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local monster_gen = require("monstergen")
33
local trap_gen = require("trapgen")
44
local chest_gen = require("chestgen")
55

6-
map = create_map(CURRENT_LEVEL) -- 'map' needs to be global
6+
local map = create_map(CURRENT_LEVEL)
77

88
room_builder.load_textures(map)
99

@@ -18,3 +18,5 @@ room_builder.load_room(map, room)
1818
monster_gen.load_monsters(map, room.monsters)
1919
trap_gen.load_traps(map, room.traps)
2020
chest_gen.load_chests(map, room.chests)
21+
22+
return map

src/map_lua.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ luaL_checkmap(lua_State *L, int index)
7474
Map *map;
7575

7676
if (!lua_islightuserdata(L, index))
77-
fatal("in luaL_checkmap(), pointer lost in lua script");
77+
fatal("pointer lost in lua script");
7878

7979
map = lua_touserdata(L, index);
8080
if (map == NULL)
81-
fatal("in luaL_checkmap(), bad map pointer");
81+
fatal("bad map pointer");
8282

8383
return map;
8484
}
@@ -91,11 +91,11 @@ luaL_checksdlrenderer(lua_State *L)
9191
lua_pushlightuserdata(L, &RendererKey);
9292
lua_gettable(L, LUA_REGISTRYINDEX);
9393
if (!lua_islightuserdata(L, -1))
94-
fatal("in luaL_checksdlrenderer(), pointer lost in lua script");
94+
fatal("pointer lost in lua script");
9595

9696
renderer = lua_touserdata(L, -1);
9797
if (renderer == NULL)
98-
fatal("in luaL_checksdlrenderer(), bad SDL_Renderer pointer");
98+
fatal("bad SDL_Renderer pointer");
9999

100100
return renderer;
101101
}
@@ -107,7 +107,7 @@ luaL_checkplayer(lua_State *L)
107107
lua_pushlightuserdata(L, &PlayerKey);
108108
lua_gettable(L, LUA_REGISTRYINDEX);
109109
if (!lua_islightuserdata(L, -1))
110-
fatal("in luaL_checkplayer(), pointer lost in lua script");
110+
fatal("pointer lost in lua script");
111111

112112
player = lua_touserdata(L, -1);
113113
return player;
@@ -709,8 +709,7 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, Player *pl
709709
fatal("Failed to run script: %s\n", lua_tostring(L, -1));
710710
}
711711

712-
lua_getglobal(L, "map");
713-
Map *map = lua_touserdata(L, 1);
712+
Map *map = luaL_checkmap(L, -1);
714713

715714
lua_close(L);
716715

0 commit comments

Comments
 (0)