|
| 1 | +#include "pre_inc.h" |
| 2 | + |
| 3 | +#include <lua.h> |
| 4 | +#include <lauxlib.h> |
| 5 | +#include <lualib.h> |
| 6 | + |
| 7 | +#include "globals.h" |
| 8 | +#include "game_legacy.h" // game.map_tiles_x/y, game.pool, game.texture_id |
| 9 | +#include "config_campaigns.h" // is_map_pack, campaign, LevelInformation, get_level_info |
| 10 | +#include "config.h" // LvKind_IsSingle, LvKind_IsMulti, etc. |
| 11 | +#include "config_creature.h" // creature_desc, CREATURE_TYPES_MAX |
| 12 | +#include "engine_textures.h" // load_texture_map_file |
| 13 | +#include "lvl_filesdk1.h" // get_loaded_level_number, get_level_fgroup |
| 14 | +#include "lvl_script_lib.h" // texture_pack_desc |
| 15 | + |
| 16 | +#include "lua_base.h" |
| 17 | +#include "lua_params.h" // luaL_checkNamedCommand |
| 18 | + |
| 19 | +#include "post_inc.h" |
| 20 | + |
| 21 | +/**********************************************/ |
| 22 | + |
| 23 | +static int map_tostring(lua_State *L) { |
| 24 | + lua_pushfstring(L, "Map(%dx%d)", game.map_tiles_x, game.map_tiles_y); |
| 25 | + return 1; |
| 26 | +} |
| 27 | + |
| 28 | +//read creature pool from a map |
| 29 | +static void push_map_pool(lua_State *L) { |
| 30 | + lua_newtable(L); |
| 31 | + for (int i = 0; i < CREATURE_TYPES_MAX; i++) { |
| 32 | + int n = game.pool.crtr_kind[i]; |
| 33 | + if (n <= 0) continue; |
| 34 | + lua_pushstring(L, get_conf_parameter_text(creature_desc, i)); |
| 35 | + lua_pushinteger(L, n); |
| 36 | + lua_rawset(L, -3); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +//read map fields |
| 42 | +static int map_get_field(lua_State *L){ |
| 43 | + const char *key = luaL_checkstring(L, 2); |
| 44 | + LevelNumber lv_number = get_loaded_level_number(); |
| 45 | + struct LevelInformation *lv_inf = get_level_info(lv_number); |
| 46 | + |
| 47 | + const char *map_name = ""; |
| 48 | + unsigned long ltype = 0; |
| 49 | + if (lv_inf) { |
| 50 | + map_name = lv_inf->name; |
| 51 | + ltype = lv_inf->level_type; |
| 52 | + } |
| 53 | + |
| 54 | + if (strcmp(key, "creature_pool") == 0) |
| 55 | + push_map_pool(L); |
| 56 | + else if (strcmp(key, "map_name") == 0) { |
| 57 | + lua_pushstring(L, map_name); |
| 58 | + } else if (strcmp(key, "map_number") == 0) |
| 59 | + lua_pushinteger(L, lv_number); |
| 60 | + else if (strcmp(key, "campaign") == 0) |
| 61 | + lua_pushstring(L, campaign.name); |
| 62 | + else if (strcmp(key, "map_type") == 0) { |
| 63 | + if (ltype & LvKind_IsMulti) { |
| 64 | + lua_pushstring(L, "Multiplayer"); |
| 65 | + } else if (ltype & LvKind_IsExtra) { |
| 66 | + lua_pushstring(L, "Moon"); |
| 67 | + } else if (ltype & LvKind_IsBonus) { |
| 68 | + lua_pushstring(L, "Bonus"); |
| 69 | + } else if (ltype & LvKind_IsFree) { |
| 70 | + lua_pushstring(L, "MapPack"); |
| 71 | + } else if (ltype & LvKind_IsSingle) { |
| 72 | + lua_pushstring(L, "Campaign"); |
| 73 | + } else { |
| 74 | + lua_pushstring(L, "Unknown"); |
| 75 | + } |
| 76 | + } else if (strcmp(key, "width") == 0) { |
| 77 | + lua_pushinteger(L, game.map_tiles_x); |
| 78 | + } else if (strcmp(key, "height") == 0) { |
| 79 | + lua_pushinteger(L, game.map_tiles_y); |
| 80 | + } else if (strcmp(key, "default_texture") == 0) { |
| 81 | + const char *name = get_conf_parameter_text(texture_pack_desc, game.texture_id); |
| 82 | + if (name[0] != '\0') { |
| 83 | + lua_pushstring(L, name); // "STANDARD", "ANCIENT", ... |
| 84 | + } else { |
| 85 | + lua_pushinteger(L, game.texture_id); // 17, 20, ... (Custom) |
| 86 | + } |
| 87 | + } else { |
| 88 | + return luaL_error(L, "Unknown field '%s' for MAP", key); |
| 89 | + } |
| 90 | + return 1; |
| 91 | +} |
| 92 | + |
| 93 | +//write map fields |
| 94 | +static int map_set_field(lua_State *L) { |
| 95 | + const char *key = luaL_checkstring(L, 2); |
| 96 | + |
| 97 | + if (strcmp(key, "default_texture") == 0) { |
| 98 | + long texture_id = luaL_checkNamedCommand(L, 3, texture_pack_desc); |
| 99 | + game.texture_id = texture_id; |
| 100 | + LevelNumber lvnumb = get_loaded_level_number(); |
| 101 | + load_texture_map_file(texture_id, lvnumb, get_level_fgroup(lvnumb)); |
| 102 | + |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + return luaL_error(L, "MAP field '%s' is read-only", key); |
| 107 | +} |
| 108 | + |
| 109 | +// Metamethod registration table for the MAP metatable. |
| 110 | +static const struct luaL_Reg map_meta[] = { |
| 111 | + {"__index", map_get_field}, |
| 112 | + {"__newindex", map_set_field}, |
| 113 | + {"__tostring", map_tostring}, |
| 114 | + {NULL, NULL} |
| 115 | +}; |
| 116 | + |
| 117 | +// Builds the global MAP singleton and binds its metatable. |
| 118 | +void Map_register(lua_State *L) { |
| 119 | + luaL_newmetatable(L, "Map"); |
| 120 | + luaL_setfuncs(L, map_meta, 0); |
| 121 | + |
| 122 | + lua_pushliteral(L, "__metatable"); |
| 123 | + lua_pushnil(L); |
| 124 | + lua_rawset(L, -3); |
| 125 | + |
| 126 | + lua_pop(L, 1); |
| 127 | + |
| 128 | + lua_newtable(L); |
| 129 | + |
| 130 | + lua_pushstring(L, "Map"); |
| 131 | + lua_setfield(L, -2, "__class"); |
| 132 | + |
| 133 | + luaL_getmetatable(L, "Map"); |
| 134 | + lua_setmetatable(L, -2); |
| 135 | + |
| 136 | + lua_setglobal(L, "Map"); |
| 137 | +} |
0 commit comments