| 1 |
/* |
| 2 |
Copyright (c) 2010 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#ifdef WITH_TRACY |
| 26 |
#include <tracy/Tracy.hpp> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <cstring> |
| 30 |
#include <list> |
| 31 |
#include <map> |
| 32 |
#include <stdexcept> |
| 33 |
#include <string> |
| 34 |
#include <string_view> |
| 35 |
#include <utility> |
| 36 |
#include <vector> |
| 37 |
|
| 38 |
#include "lua.hpp" |
| 39 |
#include "th_gfx.h" |
| 40 |
#include "th_lua.h" |
| 41 |
#include "th_lua_internal.h" |
| 42 |
#include "th_map.h" |
| 43 |
#include "th_pathfind.h" |
| 44 |
|
| 45 |
class lua_persist_reader; |
| 46 |
class lua_persist_writer; |
| 47 |
class render_target; |
| 48 |
class sprite_sheet; |
| 49 |
|
| 50 |
namespace { |
| 51 |
|
| 52 |
constexpr int player_max = 4; |
| 53 |
|
| 54 |
int l_map_new(lua_State* L) { |
| 55 |
luaT_stdnew<level_map>(L, luaT_environindex, true); |
| 56 |
return 1; |
| 57 |
} |
| 58 |
|
| 59 |
int l_map_set_sheet(lua_State* L) { |
| 60 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 61 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 62 |
lua_settop(L, 2); |
| 63 |
|
| 64 |
pMap->set_block_sheet(pSheet); |
| 65 |
luaT_setenvfield(L, 1, "sprites"); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
int l_map_persist(lua_State* L) { |
| 70 |
ZoneScoped; |
| 71 |
|
| 72 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 73 |
luaT_rotate(L, 1, -1); |
| 74 |
pMap->persist((lua_persist_writer*)lua_touserdata(L, 1)); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
int l_map_depersist(lua_State* L) { |
| 79 |
ZoneScoped; |
| 80 |
|
| 81 |
void* map_ud = luaT_testuserdata<level_map>(L); |
| 82 |
lua_settop(L, 2); |
| 83 |
lua_insert(L, 1); |
| 84 |
lua_persist_reader* pReader = (lua_persist_reader*)lua_touserdata(L, 1); |
| 85 |
|
| 86 |
level_map* map = new (map_ud) level_map(); |
| 87 |
map->depersist(pReader); |
| 88 |
luaT_getenvfield(L, 2, "sprites"); |
| 89 |
map->set_block_sheet((sprite_sheet*)lua_touserdata(L, -1)); |
| 90 |
lua_pop(L, 1); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
void l_map_load_obj_cb(void* pL, int iX, int iY, object_type eTHOB, |
| 95 |
uint8_t iFlags) { |
| 96 |
lua_State* L = static_cast<lua_State*>(pL); |
| 97 |
lua_createtable(L, 4, 0); |
| 98 |
|
| 99 |
lua_pushinteger(L, 1 + (lua_Integer)iX); |
| 100 |
lua_rawseti(L, -2, 1); |
| 101 |
lua_pushinteger(L, 1 + (lua_Integer)iY); |
| 102 |
lua_rawseti(L, -2, 2); |
| 103 |
lua_pushinteger(L, (lua_Integer)eTHOB); |
| 104 |
lua_rawseti(L, -2, 3); |
| 105 |
lua_pushinteger(L, (lua_Integer)iFlags); |
| 106 |
lua_rawseti(L, -2, 4); |
| 107 |
|
| 108 |
lua_rawseti(L, 3, static_cast<int>(lua_objlen(L, 3)) + 1); |
| 109 |
} |
| 110 |
|
| 111 |
int l_map_load(lua_State* L) { |
| 112 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 113 |
size_t iDataLen; |
| 114 |
const uint8_t* pData = luaT_checkfile(L, 2, &iDataLen); |
| 115 |
lua_settop(L, 2); |
| 116 |
lua_newtable(L); |
| 117 |
if (pMap->load_from_th_file(pData, iDataLen, l_map_load_obj_cb, (void*)L)) { |
| 118 |
lua_pushboolean(L, 1); |
| 119 |
} else { |
| 120 |
lua_pushboolean(L, 0); |
| 121 |
} |
| 122 |
lua_insert(L, -2); |
| 123 |
return 2; |
| 124 |
} |
| 125 |
|
| 126 |
int l_map_loadblank(lua_State* L) { |
| 127 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 128 |
if (pMap->load_blank()) { |
| 129 |
lua_pushboolean(L, 1); |
| 130 |
} else { |
| 131 |
lua_pushboolean(L, 0); |
| 132 |
} |
| 133 |
lua_newtable(L); |
| 134 |
return 2; |
| 135 |
} |
| 136 |
|
| 137 |
int l_map_save(lua_State* L) { |
| 138 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 139 |
std::string filename(luaL_checkstring(L, 2)); |
| 140 |
pMap->save(filename); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
animation* l_map_updateblueprint_getnextanim(lua_State* L, int& iIndex) { |
| 145 |
animation* pAnim; |
| 146 |
lua_rawgeti(L, 11, iIndex); |
| 147 |
if (lua_type(L, -1) == LUA_TNIL) { |
| 148 |
lua_pop(L, 1); |
| 149 |
pAnim = luaT_new<animation>(L); |
| 150 |
lua_pushvalue(L, luaT_upvalueindex(2)); |
| 151 |
lua_setmetatable(L, -2); |
| 152 |
lua_createtable(L, 0, 2); |
| 153 |
lua_pushvalue(L, 1); |
| 154 |
lua_setfield(L, -2, "map"); |
| 155 |
lua_pushvalue(L, 12); |
| 156 |
lua_setfield(L, -2, "animator"); |
| 157 |
lua_setfenv(L, -2); |
| 158 |
lua_rawseti(L, 11, iIndex); |
| 159 |
} else { |
| 160 |
pAnim = luaT_testuserdata<animation>(L, -1, luaT_upvalueindex(2)); |
| 161 |
lua_pop(L, 1); |
| 162 |
} |
| 163 |
++iIndex; |
| 164 |
return pAnim; |
| 165 |
} |
| 166 |
|
| 167 |
uint16_t l_check_temp(lua_State* L, int iArg) { |
| 168 |
lua_Number n = luaL_checknumber(L, iArg); |
| 169 |
if (n < static_cast<lua_Number>(0) || static_cast<lua_Number>(1) < n) { |
| 170 |
luaL_argerror(L, iArg, "temperature (number in [0,1])"); |
| 171 |
} |
| 172 |
return static_cast<uint16_t>(n * static_cast<lua_Number>(65535)); |
| 173 |
} |
| 174 |
|
| 175 |
int l_map_settemperaturedisplay(lua_State* L) { |
| 176 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 177 |
lua_Integer iTD = luaL_checkinteger(L, 2); |
| 178 |
|
| 179 |
temperature_theme temperatureDisplay; |
| 180 |
switch (iTD) { |
| 181 |
case 1: |
| 182 |
temperatureDisplay = temperature_theme::red; |
| 183 |
break; |
| 184 |
case 2: |
| 185 |
temperatureDisplay = temperature_theme::multi_colour; |
| 186 |
break; |
| 187 |
case 3: |
| 188 |
temperatureDisplay = temperature_theme::yellow_red; |
| 189 |
break; |
| 190 |
default: |
| 191 |
return luaL_argerror(L, 2, "TemperatureDisplay index out of bounds"); |
| 192 |
} |
| 193 |
|
| 194 |
pMap->set_temperature_display(temperatureDisplay); |
| 195 |
|
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
int l_map_updatetemperature(lua_State* L) { |
| 200 |
ZoneScoped; |
| 201 |
|
| 202 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 203 |
uint16_t iAir = l_check_temp(L, 2); |
| 204 |
uint16_t iRadiator = l_check_temp(L, 3); |
| 205 |
pMap->update_temperatures(iAir, iRadiator); |
| 206 |
lua_settop(L, 1); |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
int l_map_gettemperature(lua_State* L) { |
| 211 |
ZoneScoped; |
| 212 |
|
| 213 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 214 |
int iX = static_cast<int>(luaL_checkinteger(L, 2)) - 1; |
| 215 |
int iY = static_cast<int>(luaL_checkinteger(L, 3)) - 1; |
| 216 |
const map_tile* pNode = pMap->get_tile(iX, iY); |
| 217 |
uint16_t iTemp = pMap->get_tile_temperature(pNode); |
| 218 |
lua_pushnumber( |
| 219 |
L, static_cast<lua_Number>(iTemp) / static_cast<lua_Number>(65535)); |
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Is the tile position valid for a new room? |
| 225 |
* @param entire_invalid Entire blueprint is invalid (eg wrong position or too |
| 226 |
* small). |
| 227 |
* @param pNode Tile to examine. |
| 228 |
* @param pMap The world map. |
| 229 |
* @param player_id The player to check for. |
| 230 |
* @return Whether the tile position is valid for a new room. |
| 231 |
*/ |
| 232 |
inline bool is_valid(bool entire_invalid, const map_tile* pNode, |
| 233 |
const level_map* pMap, int player_id) { |
| 234 |
return !entire_invalid && !pNode->flags.room && pNode->flags.buildable && |
| 235 |
(player_id == 0 || pMap->get_tile_owner(pNode) == player_id); |
| 236 |
} |
| 237 |
|
| 238 |
int l_map_updateblueprint(lua_State* L) { |
| 239 |
ZoneScoped; |
| 240 |
|
| 241 |
// NB: This function can be implemented in Lua, but is implemented in C for |
| 242 |
// efficiency. |
| 243 |
const unsigned short iFloorTileGood = 24 + (thdf_alpha_50 << 8); |
| 244 |
const unsigned short iFloorTileGoodCenter = 37 + (thdf_alpha_50 << 8); |
| 245 |
const unsigned short iFloorTileBad = 67 + (thdf_alpha_50 << 8); |
| 246 |
const unsigned int iWallAnimTopCorner = 124; |
| 247 |
const unsigned int iWallAnim = 120; |
| 248 |
|
| 249 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 250 |
int iOldX = static_cast<int>(luaL_checkinteger(L, 2)) - 1; |
| 251 |
int iOldY = static_cast<int>(luaL_checkinteger(L, 3)) - 1; |
| 252 |
int iOldW = static_cast<int>(luaL_checkinteger(L, 4)); |
| 253 |
int iOldH = static_cast<int>(luaL_checkinteger(L, 5)); |
| 254 |
int iNewX = static_cast<int>(luaL_checkinteger(L, 6)) - 1; |
| 255 |
int iNewY = static_cast<int>(luaL_checkinteger(L, 7)) - 1; |
| 256 |
int iNewW = static_cast<int>(luaL_checkinteger(L, 8)); |
| 257 |
int iNewH = static_cast<int>(luaL_checkinteger(L, 9)); |
| 258 |
int player_id = static_cast<int>(luaL_checkinteger(L, 10)); |
| 259 |
|
| 260 |
luaL_checktype(L, 11, LUA_TTABLE); // Animation list |
| 261 |
animation_manager* pAnims = |
| 262 |
luaT_testuserdata<animation_manager>(L, 12, luaT_upvalueindex(1)); |
| 263 |
bool entire_invalid = lua_toboolean(L, 13) != 0; |
| 264 |
bool valid = !entire_invalid; |
| 265 |
|
| 266 |
if (iOldX < 0 || iOldY < 0 || (iOldX + iOldW) > pMap->get_width() || |
| 267 |
(iOldY + iOldH) > pMap->get_height()) |
| 268 |
luaL_argerror(L, 2, "Old rectangle is out of bounds"); |
| 269 |
if (iNewX < 0 || iNewY < 0 || (iNewX + iNewW) >= pMap->get_width() || |
| 270 |
(iNewY + iNewH) >= pMap->get_height()) |
| 271 |
luaL_argerror(L, 6, "New rectangle is out of bounds"); |
| 272 |
|
| 273 |
// Clear blueprint flag from previous selected floor tiles (copying it to |
| 274 |
// the passable flag). |
| 275 |
for (int iY = iOldY; iY < iOldY + iOldH; ++iY) { |
| 276 |
for (int iX = iOldX; iX < iOldX + iOldW; ++iX) { |
| 277 |
map_tile* pNode = pMap->get_tile_unchecked(iX, iY); |
| 278 |
pNode->tile_layers[tile_layer::ui] = 0; |
| 279 |
pNode->flags.passable |= pNode->flags.passable_if_not_for_blueprint; |
| 280 |
pNode->flags.passable_if_not_for_blueprint = false; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Add blueprint flag to new floor tiles. |
| 285 |
for (int iY = iNewY; iY < iNewY + iNewH; ++iY) { |
| 286 |
for (int iX = iNewX; iX < iNewX + iNewW; ++iX) { |
| 287 |
map_tile* pNode = pMap->get_tile_unchecked(iX, iY); |
| 288 |
if (is_valid(entire_invalid, pNode, pMap, player_id)) { |
| 289 |
pNode->tile_layers[tile_layer::ui] = iFloorTileGood; |
| 290 |
} else { |
| 291 |
pNode->tile_layers[tile_layer::ui] = iFloorTileBad; |
| 292 |
valid = false; |
| 293 |
} |
| 294 |
pNode->flags.passable_if_not_for_blueprint = pNode->flags.passable; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
// Set center floor tiles |
| 299 |
if (iNewW >= 2 && iNewH >= 2) { |
| 300 |
int iCenterX = iNewX + (iNewW - 2) / 2; |
| 301 |
int iCenterY = iNewY + (iNewH - 2) / 2; |
| 302 |
|
| 303 |
map_tile* pNode = pMap->get_tile_unchecked(iCenterX, iCenterY); |
| 304 |
if (pNode->tile_layers[tile_layer::ui] == iFloorTileGood) |
| 305 |
pNode->tile_layers[tile_layer::ui] = iFloorTileGoodCenter + 2; |
| 306 |
|
| 307 |
pNode = pMap->get_tile_unchecked(iCenterX + 1, iCenterY); |
| 308 |
if (pNode->tile_layers[tile_layer::ui] == iFloorTileGood) |
| 309 |
pNode->tile_layers[tile_layer::ui] = iFloorTileGoodCenter + 1; |
| 310 |
|
| 311 |
pNode = pMap->get_tile_unchecked(iCenterX, iCenterY + 1); |
| 312 |
if (pNode->tile_layers[tile_layer::ui] == iFloorTileGood) |
| 313 |
pNode->tile_layers[tile_layer::ui] = iFloorTileGoodCenter + 0; |
| 314 |
|
| 315 |
pNode = pMap->get_tile_unchecked(iCenterX + 1, iCenterY + 1); |
| 316 |
if (pNode->tile_layers[tile_layer::ui] == iFloorTileGood) |
| 317 |
pNode->tile_layers[tile_layer::ui] = iFloorTileGoodCenter + 3; |
| 318 |
} |
| 319 |
|
| 320 |
// Set wall animations |
| 321 |
int iNextAnim = 1; |
| 322 |
animation* pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 323 |
map_tile* pNode = pMap->get_tile_unchecked(iNewX, iNewY); |
| 324 |
pAnim->set_animation(pAnims, iWallAnimTopCorner); |
| 325 |
pAnim->set_flags( |
| 326 |
is_valid(entire_invalid, pNode, pMap, player_id) ? 0 : thdf_alt_palette); |
| 327 |
pAnim->attach_to_tile({iNewX, iNewY}, pNode, 0); |
| 328 |
|
| 329 |
for (int iX = iNewX; iX < iNewX + iNewW; ++iX) { |
| 330 |
if (iX != iNewX) { |
| 331 |
pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 332 |
pNode = pMap->get_tile_unchecked(iX, iNewY); |
| 333 |
pAnim->set_animation(pAnims, iWallAnim); |
| 334 |
pAnim->set_flags(is_valid(entire_invalid, pNode, pMap, player_id) |
| 335 |
? 0 |
| 336 |
: thdf_alt_palette); |
| 337 |
pAnim->attach_to_tile({iX, iNewY}, pNode, 0); |
| 338 |
pAnim->set_pixel_offset(0, 0); |
| 339 |
} |
| 340 |
pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 341 |
pNode = pMap->get_tile_unchecked(iX, iNewY + iNewH - 1); |
| 342 |
pAnim->set_animation(pAnims, iWallAnim); |
| 343 |
pAnim->set_flags(is_valid(entire_invalid, pNode, pMap, player_id) |
| 344 |
? 0 |
| 345 |
: thdf_alt_palette); |
| 346 |
pNode = pMap->get_tile_unchecked(iX, iNewY + iNewH); |
| 347 |
pAnim->attach_to_tile({iX, iNewY + iNewH}, pNode, 0); |
| 348 |
pAnim->set_pixel_offset(0, -1); |
| 349 |
} |
| 350 |
for (int iY = iNewY; iY < iNewY + iNewH; ++iY) { |
| 351 |
if (iY != iNewY) { |
| 352 |
pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 353 |
pNode = pMap->get_tile_unchecked(iNewX, iY); |
| 354 |
pAnim->set_animation(pAnims, iWallAnim); |
| 355 |
pAnim->set_flags(thdf_flip_horizontal | |
| 356 |
(is_valid(entire_invalid, pNode, pMap, player_id) |
| 357 |
? 0 |
| 358 |
: thdf_alt_palette)); |
| 359 |
pAnim->attach_to_tile({iNewX, iY}, pNode, 0); |
| 360 |
pAnim->set_pixel_offset(2, 0); |
| 361 |
} |
| 362 |
pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 363 |
pNode = pMap->get_tile_unchecked(iNewX + iNewW - 1, iY); |
| 364 |
pAnim->set_animation(pAnims, iWallAnim); |
| 365 |
pAnim->set_flags(thdf_flip_horizontal | |
| 366 |
(is_valid(entire_invalid, pNode, pMap, player_id) |
| 367 |
? 0 |
| 368 |
: thdf_alt_palette)); |
| 369 |
pNode = pMap->get_tile_unchecked(iNewX + iNewW, iY); |
| 370 |
pAnim->attach_to_tile({iNewX + iNewW, iY}, pNode, 0); |
| 371 |
pAnim->set_pixel_offset(2, -1); |
| 372 |
} |
| 373 |
|
| 374 |
// Clear away extra animations |
| 375 |
int iAnimCount = (int)lua_objlen(L, 11); |
| 376 |
if (iAnimCount >= iNextAnim) { |
| 377 |
for (int i = iNextAnim; i <= iAnimCount; ++i) { |
| 378 |
pAnim = l_map_updateblueprint_getnextanim(L, iNextAnim); |
| 379 |
pAnim->remove_from_tile(); |
| 380 |
lua_pushnil(L); |
| 381 |
lua_rawseti(L, 11, i); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
lua_pushboolean(L, valid ? 1 : 0); |
| 386 |
return 1; |
| 387 |
} |
| 388 |
|
| 389 |
int l_map_getsize(lua_State* L) { |
| 390 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 391 |
lua_pushinteger(L, pMap->get_width()); |
| 392 |
lua_pushinteger(L, pMap->get_height()); |
| 393 |
return 2; |
| 394 |
} |
| 395 |
|
| 396 |
int l_map_get_player_count(lua_State* L) { |
| 397 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 398 |
lua_pushinteger(L, pMap->get_player_count()); |
| 399 |
return 1; |
| 400 |
} |
| 401 |
|
| 402 |
int l_map_set_player_count(lua_State* L) { |
| 403 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 404 |
int count = static_cast<int>(luaL_checkinteger(L, 2)); |
| 405 |
|
| 406 |
try { |
| 407 |
pMap->set_player_count(count); |
| 408 |
} catch (std::out_of_range&) { |
| 409 |
return luaL_error(L, "Player count out of range %d", count); |
| 410 |
} |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
int l_map_get_player_camera(lua_State* L) { |
| 415 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 416 |
int iX, iY; |
| 417 |
int iPlayer = static_cast<int>(luaL_optinteger(L, 2, 1)); |
| 418 |
bool bGood = pMap->get_player_camera_tile(iPlayer - 1, &iX, &iY); |
| 419 |
if (!bGood) { |
| 420 |
return luaL_error(L, "Player index out of range: %d", iPlayer); |
| 421 |
} |
| 422 |
lua_pushinteger(L, iX + 1); |
| 423 |
lua_pushinteger(L, iY + 1); |
| 424 |
return 2; |
| 425 |
} |
| 426 |
|
| 427 |
int l_map_set_player_camera(lua_State* L) { |
| 428 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 429 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - 1); |
| 430 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); |
| 431 |
int iPlayer = static_cast<int>(luaL_optinteger(L, 4, 1)); |
| 432 |
|
| 433 |
if (iPlayer < 1 || iPlayer > player_max) { |
| 434 |
return luaL_error(L, "Player index out of range: %i", iPlayer); |
| 435 |
} |
| 436 |
|
| 437 |
pMap->set_player_camera_tile(iPlayer - 1, iX, iY); |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
int l_map_get_player_heliport(lua_State* L) { |
| 442 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 443 |
int iX, iY; |
| 444 |
int iPlayer = static_cast<int>(luaL_optinteger(L, 2, 1)); |
| 445 |
bool bGood = pMap->get_player_heliport_tile(iPlayer - 1, &iX, &iY); |
| 446 |
if (!bGood) { |
| 447 |
return luaL_error(L, "Player index out of range: %d", iPlayer); |
| 448 |
} |
| 449 |
lua_pushinteger(L, iX + 1); |
| 450 |
lua_pushinteger(L, iY + 1); |
| 451 |
return 2; |
| 452 |
} |
| 453 |
|
| 454 |
int l_map_set_player_heliport(lua_State* L) { |
| 455 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 456 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - 1); |
| 457 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); |
| 458 |
int iPlayer = static_cast<int>(luaL_optinteger(L, 4, 1)); |
| 459 |
|
| 460 |
if (iPlayer < 1 || iPlayer > player_max) { |
| 461 |
return luaL_error(L, "Player index out of range: %i", iPlayer); |
| 462 |
} |
| 463 |
|
| 464 |
pMap->set_player_heliport_tile(iPlayer - 1, iX, iY); |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
int l_map_getcell(lua_State* L) { |
| 469 |
ZoneScoped; |
| 470 |
|
| 471 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 472 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 473 |
1); // Lua arrays start at 1 - pretend |
| 474 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 475 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 476 |
if (pNode == nullptr) { |
| 477 |
return luaL_argerror(L, 2, |
| 478 |
lua_pushfstring(L, |
| 479 |
"Map coordinates out " |
| 480 |
"of bounds (%d, %d)", |
| 481 |
iX + 1, iY + 1)); |
| 482 |
} |
| 483 |
if (lua_isnoneornil(L, 4)) { |
| 484 |
lua_pushinteger(L, pNode->tile_layers[tile_layer::ground]); |
| 485 |
lua_pushinteger(L, pNode->tile_layers[tile_layer::north_wall]); |
| 486 |
lua_pushinteger(L, pNode->tile_layers[tile_layer::west_wall]); |
| 487 |
lua_pushinteger(L, pNode->tile_layers[tile_layer::ui]); |
| 488 |
return 4; |
| 489 |
} else { |
| 490 |
lua_Integer iLayer = luaL_checkinteger(L, 4) - 1; |
| 491 |
if (iLayer < tile_layer::ground || iLayer >= tile_layer::num_tile_layers) |
| 492 |
return luaL_argerror(L, 4, "Tile layer index is out of bounds (1-4)"); |
| 493 |
lua_pushinteger(L, pNode->tile_layers[iLayer]); |
| 494 |
return 1; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
/** Recognized tile flags by Lua. */ |
| 499 |
const std::map<std::string, map_tile_flags::key> lua_tile_flag_map{ |
| 500 |
{"passable", map_tile_flags::key::passable_mask}, |
| 501 |
{"hospital", map_tile_flags::key::hospital_mask}, |
| 502 |
{"buildable", map_tile_flags::key::buildable_mask}, |
| 503 |
{"room", map_tile_flags::key::room_mask}, |
| 504 |
{"doorWest", map_tile_flags::key::door_west_mask}, |
| 505 |
{"doorNorth", map_tile_flags::key::door_north_mask}, |
| 506 |
{"tallWest", map_tile_flags::key::tall_west_mask}, |
| 507 |
{"tallNorth", map_tile_flags::key::tall_north_mask}, |
| 508 |
{"travelNorth", map_tile_flags::key::can_travel_n_mask}, |
| 509 |
{"travelEast", map_tile_flags::key::can_travel_e_mask}, |
| 510 |
{"travelSouth", map_tile_flags::key::can_travel_s_mask}, |
| 511 |
{"travelWest", map_tile_flags::key::can_travel_w_mask}, |
| 512 |
{"doNotIdle", map_tile_flags::key::do_not_idle_mask}, |
| 513 |
{"buildableNorth", map_tile_flags::key::buildable_n_mask}, |
| 514 |
{"buildableEast", map_tile_flags::key::buildable_e_mask}, |
| 515 |
{"buildableSouth", map_tile_flags::key::buildable_s_mask}, |
| 516 |
{"buildableWest", map_tile_flags::key::buildable_w_mask}, |
| 517 |
}; |
| 518 |
|
| 519 |
/** |
| 520 |
* Add the current value of the \a flag in the \a tile to the output. |
| 521 |
* @param L Lua context. |
| 522 |
* @param tile Tile to inspect. |
| 523 |
* @param flag Flag of the tile to check (and report). |
| 524 |
* @param name Name of the flag in Lua code. |
| 525 |
*/ |
| 526 |
void add_cellflag(lua_State* L, const map_tile* tile, |
| 527 |
const map_tile_flags::key flag, const std::string_view name) { |
| 528 |
lua_pushlstring(L, name.data(), name.size()); |
| 529 |
lua_pushboolean(L, tile->flags[flag] ? 1 : 0); |
| 530 |
lua_settable(L, 4); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Add the current value of a tile field to the output. |
| 535 |
* @param L Lua context. |
| 536 |
* @param value Value of the tile field to add. |
| 537 |
* @param name Name of the field in Lua code. |
| 538 |
*/ |
| 539 |
void add_cellint(lua_State* L, const int value, const std::string_view name) { |
| 540 |
lua_pushlstring(L, name.data(), name.size()); |
| 541 |
lua_pushinteger(L, value); |
| 542 |
lua_settable(L, 4); |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Get the room id at the given position |
| 547 |
* @param L Lua context. |
| 548 |
* [1] - The world map object |
| 549 |
* [2] - The X coordinate |
| 550 |
* [3] - The Y coordinate |
| 551 |
* @return Number of results of the call. The roomId is pushed onto L. |
| 552 |
*/ |
| 553 |
int l_map_get_room_id(lua_State* L) { |
| 554 |
ZoneScoped; |
| 555 |
|
| 556 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 557 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 558 |
1); // Lua arrays start at 1 - pretend |
| 559 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 560 |
|
| 561 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 562 |
if (pNode == nullptr) { |
| 563 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 564 |
} |
| 565 |
|
| 566 |
lua_pushinteger(L, pNode->iRoomId); |
| 567 |
return 1; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Get the value of all cell flags at a position. |
| 572 |
* @param L Lua context. |
| 573 |
* @return Number of results of the call. |
| 574 |
*/ |
| 575 |
int l_map_getcellflags(lua_State* L) { |
| 576 |
ZoneScoped; |
| 577 |
|
| 578 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 579 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 580 |
1); // Lua arrays start at 1 - pretend |
| 581 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 582 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 583 |
if (pNode == nullptr) { |
| 584 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 585 |
} |
| 586 |
if (lua_type(L, 4) != LUA_TTABLE) { |
| 587 |
lua_settop(L, 3); |
| 588 |
lua_createtable(L, 0, 1); |
| 589 |
} else { |
| 590 |
lua_settop(L, 4); |
| 591 |
} |
| 592 |
|
| 593 |
// Fill Lua table with the flags and numbers of the tile. |
| 594 |
for (const auto& val : lua_tile_flag_map) { |
| 595 |
add_cellflag(L, pNode, val.second, val.first); |
| 596 |
} |
| 597 |
add_cellint(L, pNode->iRoomId, "roomId"); |
| 598 |
add_cellint(L, pNode->iParcelId, "parcelId"); |
| 599 |
add_cellint(L, pMap->get_tile_owner(pNode), "owner"); |
| 600 |
add_cellint(L, |
| 601 |
static_cast<int>(pNode->objects.empty() ? object_type::no_object |
| 602 |
: pNode->objects.front()), |
| 603 |
"thob"); |
| 604 |
return 1; |
| 605 |
} |
| 606 |
|
| 607 |
int l_map_getcellraw(lua_State* L) { |
| 608 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 609 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 610 |
1); // Lua arrays start at 1 - pretend |
| 611 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 612 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 613 |
if (pNode == nullptr) { |
| 614 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 615 |
} |
| 616 |
lua_pushlstring(L, reinterpret_cast<const char*>(pNode->raw), |
| 617 |
map_tile::raw_length); |
| 618 |
return 1; |
| 619 |
} |
| 620 |
|
| 621 |
/* because all the thobs are not retrieved when the map is loaded in c |
| 622 |
lua objects use the afterLoad function to be registered after a load, |
| 623 |
if the object list would not be cleared it would result in duplication |
| 624 |
of thobs in the object list. */ |
| 625 |
int l_map_erase_thobs(lua_State* L) { |
| 626 |
ZoneScoped; |
| 627 |
|
| 628 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 629 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 630 |
1); // Lua arrays start at 1 - pretend |
| 631 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 632 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 633 |
if (pNode == nullptr) { |
| 634 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 635 |
} |
| 636 |
pNode->objects.clear(); |
| 637 |
return 1; |
| 638 |
} |
| 639 |
|
| 640 |
int l_map_remove_cell_thob(lua_State* L) { |
| 641 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 642 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 643 |
1); // Lua arrays start at 1 - pretend |
| 644 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 645 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 646 |
if (pNode == nullptr) { |
| 647 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 648 |
} |
| 649 |
auto thob = static_cast<object_type>(luaL_checkinteger(L, 4)); |
| 650 |
for (auto iter = pNode->objects.begin(); iter != pNode->objects.end(); |
| 651 |
iter++) { |
| 652 |
if (*iter == thob) { |
| 653 |
pNode->objects.erase(iter); |
| 654 |
break; |
| 655 |
} |
| 656 |
} |
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
int l_map_setcellflags(lua_State* L) { |
| 661 |
ZoneScoped; |
| 662 |
|
| 663 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 664 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 665 |
1); // Lua arrays start at 1 - pretend |
| 666 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 667 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 668 |
if (pNode == nullptr) { |
| 669 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 670 |
} |
| 671 |
luaL_checktype(L, 4, LUA_TTABLE); |
| 672 |
lua_settop(L, 4); |
| 673 |
|
| 674 |
lua_pushnil(L); |
| 675 |
|
| 676 |
while (lua_next(L, 4)) { |
| 677 |
if (lua_type(L, 5) == LUA_TSTRING) { |
| 678 |
const char* field = lua_tostring(L, 5); |
| 679 |
|
| 680 |
auto iter = lua_tile_flag_map.find(field); |
| 681 |
if (iter != lua_tile_flag_map.end()) { |
| 682 |
if (lua_toboolean(L, 6) == 0) { |
| 683 |
pNode->flags[(*iter).second] = false; |
| 684 |
} else { |
| 685 |
pNode->flags[(*iter).second] = true; |
| 686 |
} |
| 687 |
} else if (std::strcmp(field, "thob") == 0) { |
| 688 |
auto thob = static_cast<object_type>(lua_tointeger(L, 6)); |
| 689 |
pNode->objects.push_back(thob); |
| 690 |
} else if (std::strcmp(field, "parcelId") == 0) { |
| 691 |
pNode->iParcelId = static_cast<uint16_t>(lua_tointeger(L, 6)); |
| 692 |
} else if (std::strcmp(field, "roomId") == 0) { |
| 693 |
pNode->iRoomId = static_cast<uint16_t>(lua_tointeger(L, 6)); |
| 694 |
} else { |
| 695 |
luaL_error(L, "Invalid flag \'%s\'", field); |
| 696 |
} |
| 697 |
} |
| 698 |
lua_settop(L, 5); |
| 699 |
} |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
int l_map_setwallflags(lua_State* L) { |
| 704 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 705 |
pMap->set_all_wall_draw_flags((uint8_t)luaL_checkinteger(L, 2)); |
| 706 |
lua_settop(L, 1); |
| 707 |
return 1; |
| 708 |
} |
| 709 |
|
| 710 |
int l_map_setcell(lua_State* L) { |
| 711 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 712 |
int iX = static_cast<int>(luaL_checkinteger(L, 2) - |
| 713 |
1); // Lua arrays start at 1 - pretend |
| 714 |
int iY = static_cast<int>(luaL_checkinteger(L, 3) - 1); // the map does too. |
| 715 |
map_tile* pNode = pMap->get_tile(iX, iY); |
| 716 |
if (pNode == nullptr) { |
| 717 |
return luaL_argerror(L, 2, "Map coordinates out of bounds"); |
| 718 |
} |
| 719 |
if (lua_gettop(L) >= 7) { |
| 720 |
pNode->tile_layers[tile_layer::ground] = |
| 721 |
static_cast<uint16_t>(luaL_checkinteger(L, 4)); |
| 722 |
pNode->tile_layers[tile_layer::north_wall] = |
| 723 |
static_cast<uint16_t>(luaL_checkinteger(L, 5)); |
| 724 |
pNode->tile_layers[tile_layer::west_wall] = |
| 725 |
static_cast<uint16_t>(luaL_checkinteger(L, 6)); |
| 726 |
pNode->tile_layers[tile_layer::ui] = |
| 727 |
static_cast<uint16_t>(luaL_checkinteger(L, 7)); |
| 728 |
} else { |
| 729 |
lua_Integer layer = luaL_checkinteger(L, 4) - 1; |
| 730 |
if (layer < tile_layer::ground || layer >= tile_layer::num_tile_layers) |
| 731 |
return luaL_argerror(L, 4, "Tile layer index is out of bounds (1-4)"); |
| 732 |
uint16_t blk = static_cast<uint16_t>(luaL_checkinteger(L, 5)); |
| 733 |
pNode->tile_layers[layer] = blk; |
| 734 |
} |
| 735 |
|
| 736 |
lua_settop(L, 1); |
| 737 |
return 1; |
| 738 |
} |
| 739 |
|
| 740 |
int l_map_updateshadows(lua_State* L) { |
| 741 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 742 |
pMap->update_shadows(); |
| 743 |
lua_settop(L, 1); |
| 744 |
return 1; |
| 745 |
} |
| 746 |
|
| 747 |
int l_map_updatepathfinding(lua_State* L) { |
| 748 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 749 |
pMap->update_pathfinding(); |
| 750 |
lua_settop(L, 1); |
| 751 |
return 1; |
| 752 |
} |
| 753 |
|
| 754 |
int l_map_mark_room(lua_State* L) { |
| 755 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 756 |
int iX_ = static_cast<int>(luaL_checkinteger(L, 2) - 1); |
| 757 |
int iY_ = static_cast<int>(luaL_checkinteger(L, 3) - 1); |
| 758 |
int iW = static_cast<int>(luaL_checkinteger(L, 4)); |
| 759 |
int iH = static_cast<int>(luaL_checkinteger(L, 5)); |
| 760 |
uint16_t iTile = static_cast<uint16_t>(luaL_checkinteger(L, 6)); |
| 761 |
uint16_t iRoomId = static_cast<uint16_t>(luaL_optinteger(L, 7, 0)); |
| 762 |
|
| 763 |
if (iX_ < 0 || iY_ < 0 || (iX_ + iW) > pMap->get_width() || |
| 764 |
(iY_ + iH) > pMap->get_height()) { |
| 765 |
luaL_argerror(L, 2, "Rectangle is out of bounds"); |
| 766 |
} |
| 767 |
|
| 768 |
for (int iY = iY_; iY < iY_ + iH; ++iY) { |
| 769 |
for (int iX = iX_; iX < iX_ + iW; ++iX) { |
| 770 |
map_tile* pNode = pMap->get_tile_unchecked(iX, iY); |
| 771 |
pNode->tile_layers[tile_layer::ground] = iTile; |
| 772 |
pNode->tile_layers[tile_layer::ui] = 0; |
| 773 |
pNode->flags.room = true; |
| 774 |
pNode->flags.passable |= pNode->flags.passable_if_not_for_blueprint; |
| 775 |
pNode->flags.passable_if_not_for_blueprint = false; |
| 776 |
pNode->iRoomId = iRoomId; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
pMap->update_pathfinding(); |
| 781 |
pMap->update_shadows(); |
| 782 |
lua_settop(L, 1); |
| 783 |
return 1; |
| 784 |
} |
| 785 |
|
| 786 |
int l_map_unmark_room(lua_State* L) { |
| 787 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 788 |
int iX_ = static_cast<int>(luaL_checkinteger(L, 2) - 1); |
| 789 |
int iY_ = static_cast<int>(luaL_checkinteger(L, 3) - 1); |
| 790 |
int iW = static_cast<int>(luaL_checkinteger(L, 4)); |
| 791 |
int iH = static_cast<int>(luaL_checkinteger(L, 5)); |
| 792 |
|
| 793 |
if (iX_ < 0 || iY_ < 0 || (iX_ + iW) > pMap->get_width() || |
| 794 |
(iY_ + iH) > pMap->get_height()) { |
| 795 |
luaL_argerror(L, 2, "Rectangle is out of bounds"); |
| 796 |
} |
| 797 |
|
| 798 |
for (int iY = iY_; iY < iY_ + iH; ++iY) { |
| 799 |
for (int iX = iX_; iX < iX_ + iW; ++iX) { |
| 800 |
map_tile* pNode = pMap->get_tile_unchecked(iX, iY); |
| 801 |
pNode->tile_layers[tile_layer::ground] = |
| 802 |
pMap->get_original_tile_unchecked(iX, iY) |
| 803 |
->tile_layers[tile_layer::ground]; |
| 804 |
pNode->flags.room = false; |
| 805 |
pNode->iRoomId = 0; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
pMap->update_pathfinding(); |
| 810 |
pMap->update_shadows(); |
| 811 |
|
| 812 |
lua_settop(L, 1); |
| 813 |
return 1; |
| 814 |
} |
| 815 |
|
| 816 |
int l_map_draw(lua_State* L) { |
| 817 |
ZoneScoped; |
| 818 |
|
| 819 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 820 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 821 |
|
| 822 |
pMap->draw(pCanvas, static_cast<int>(luaL_checkinteger(L, 3)), |
| 823 |
static_cast<int>(luaL_checkinteger(L, 4)), |
| 824 |
static_cast<int>(luaL_checkinteger(L, 5)), |
| 825 |
static_cast<int>(luaL_checkinteger(L, 6)), |
| 826 |
static_cast<int>(luaL_optinteger(L, 7, 0)), |
| 827 |
static_cast<int>(luaL_optinteger(L, 8, 0))); |
| 828 |
|
| 829 |
lua_settop(L, 1); |
| 830 |
return 1; |
| 831 |
} |
| 832 |
|
| 833 |
int l_map_hittest(lua_State* L) { |
| 834 |
ZoneScoped; |
| 835 |
|
| 836 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 837 |
drawable* pObject = pMap->hit_test(static_cast<int>(luaL_checkinteger(L, 2)), |
| 838 |
static_cast<int>(luaL_checkinteger(L, 3))); |
| 839 |
if (pObject == nullptr) { |
| 840 |
return 0; |
| 841 |
} |
| 842 |
lua_rawgeti(L, luaT_upvalueindex(1), 1); |
| 843 |
lua_pushlightuserdata(L, pObject); |
| 844 |
lua_gettable(L, -2); |
| 845 |
return 1; |
| 846 |
} |
| 847 |
|
| 848 |
int l_map_get_parcel_tilecount(lua_State* L) { |
| 849 |
ZoneScoped; |
| 850 |
|
| 851 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 852 |
int iParcel = static_cast<int>(luaL_checkinteger(L, 2)); |
| 853 |
lua_Integer iCount = pMap->get_parcel_tile_count(iParcel); |
| 854 |
lua_pushinteger(L, iCount); |
| 855 |
return 1; |
| 856 |
} |
| 857 |
|
| 858 |
int l_map_get_parcel_count(lua_State* L) { |
| 859 |
ZoneScoped; |
| 860 |
|
| 861 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 862 |
lua_pushinteger(L, pMap->get_parcel_count()); |
| 863 |
return 1; |
| 864 |
} |
| 865 |
|
| 866 |
int l_map_set_parcel_owner(lua_State* L) { |
| 867 |
ZoneScoped; |
| 868 |
|
| 869 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 870 |
int parcelId = static_cast<int>(luaL_checkinteger(L, 2)); |
| 871 |
int player = static_cast<int>(luaL_checkinteger(L, 3)); |
| 872 |
if (lua_type(L, 4) != LUA_TTABLE) { |
| 873 |
lua_settop(L, 3); |
| 874 |
lua_newtable(L); |
| 875 |
} else { |
| 876 |
lua_settop(L, 4); |
| 877 |
} |
| 878 |
std::vector<std::pair<int, int>> vSplitTiles = |
| 879 |
pMap->set_parcel_owner(parcelId, player); |
| 880 |
for (std::vector<std::pair<int, int>>::size_type i = 0; |
| 881 |
i != vSplitTiles.size(); i++) { |
| 882 |
lua_pushinteger(L, i + 1); |
| 883 |
lua_createtable(L, 0, 2); |
| 884 |
lua_pushinteger(L, 1); |
| 885 |
lua_pushinteger(L, vSplitTiles[i].first + 1); |
| 886 |
lua_settable(L, 6); |
| 887 |
lua_pushinteger(L, 2); |
| 888 |
lua_pushinteger(L, vSplitTiles[i].second + 1); |
| 889 |
lua_settable(L, 6); |
| 890 |
lua_settable(L, 4); |
| 891 |
} |
| 892 |
return 1; |
| 893 |
} |
| 894 |
|
| 895 |
int l_map_get_parcel_owner(lua_State* L) { |
| 896 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 897 |
lua_pushinteger( |
| 898 |
L, pMap->get_parcel_owner(static_cast<int>(luaL_checkinteger(L, 2)))); |
| 899 |
return 1; |
| 900 |
} |
| 901 |
|
| 902 |
int l_map_is_parcel_purchasable(lua_State* L) { |
| 903 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 904 |
lua_pushboolean( |
| 905 |
L, pMap->is_parcel_purchasable(static_cast<int>(luaL_checkinteger(L, 2)), |
| 906 |
static_cast<int>(luaL_checkinteger(L, 3))) |
| 907 |
? 1 |
| 908 |
: 0); |
| 909 |
return 1; |
| 910 |
} |
| 911 |
|
| 912 |
/* Compute the fraction of corridor tiles with litter, of the parcels owned by |
| 913 |
* the given player. */ |
| 914 |
int l_map_get_litter_fraction(lua_State* L) { |
| 915 |
ZoneScoped; |
| 916 |
|
| 917 |
level_map* pMap = luaT_testuserdata<level_map>(L); |
| 918 |
int owner = static_cast<int>(luaL_checkinteger(L, 2)); |
| 919 |
if (owner == 0) { |
| 920 |
lua_pushnumber(L, 0.0); // Outside has no litter. |
| 921 |
return 1; |
| 922 |
} |
| 923 |
|
| 924 |
double tile_count = 0; |
| 925 |
double litter_count = 0; |
| 926 |
for (int x = 0; x < pMap->get_width(); x++) { |
| 927 |
for (int y = 0; y < pMap->get_height(); y++) { |
| 928 |
const map_tile* pNode = pMap->get_tile_unchecked(x, y); |
| 929 |
if (pNode->iParcelId == 0 || |
| 930 |
owner != pMap->get_parcel_owner(pNode->iParcelId) || |
| 931 |
pNode->iRoomId != 0) { |
| 932 |
continue; |
| 933 |
} |
| 934 |
|
| 935 |
tile_count++; |
| 936 |
for (auto iter = pNode->objects.begin(); iter != pNode->objects.end(); |
| 937 |
iter++) { |
| 938 |
if (*iter == object_type::litter) { |
| 939 |
litter_count++; |
| 940 |
break; |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
double fraction = (tile_count == 0) ? 0.0 : litter_count / tile_count; |
| 947 |
lua_pushnumber(L, fraction); |
| 948 |
return 1; |
| 949 |
} |
| 950 |
|
| 951 |
int l_path_new(lua_State* L) { |
| 952 |
luaT_stdnew<pathfinder>(L, luaT_environindex, true); |
| 953 |
return 1; |
| 954 |
} |
| 955 |
|
| 956 |
int l_path_set_map(lua_State* L) { |
| 957 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 958 |
level_map* pMap = luaT_testuserdata<level_map>(L, 2); |
| 959 |
lua_settop(L, 2); |
| 960 |
|
| 961 |
pPathfinder->set_default_map(pMap); |
| 962 |
luaT_setenvfield(L, 1, "map"); |
| 963 |
return 1; |
| 964 |
} |
| 965 |
|
| 966 |
int l_path_persist(lua_State* L) { |
| 967 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 968 |
pPathfinder->persist((lua_persist_writer*)lua_touserdata(L, 2)); |
| 969 |
return 0; |
| 970 |
} |
| 971 |
|
| 972 |
int l_path_depersist(lua_State* L) { |
| 973 |
void* pathfinder_ud = luaT_testuserdata<pathfinder>(L); |
| 974 |
lua_settop(L, 2); |
| 975 |
lua_insert(L, 1); |
| 976 |
lua_persist_reader* pReader = (lua_persist_reader*)lua_touserdata(L, 1); |
| 977 |
|
| 978 |
pathfinder* pf = new (pathfinder_ud) pathfinder(); |
| 979 |
pf->depersist(pReader); |
| 980 |
luaT_getenvfield(L, 2, "map"); |
| 981 |
pf->set_default_map(static_cast<level_map*>(lua_touserdata(L, -1))); |
| 982 |
return 0; |
| 983 |
} |
| 984 |
|
| 985 |
int l_path_is_reachable_from_hospital(lua_State* L) { |
| 986 |
ZoneScoped; |
| 987 |
|
| 988 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 989 |
|
| 990 |
bool found = pPathfinder->find_path_to_hospital( |
| 991 |
nullptr, static_cast<int>(luaL_checkinteger(L, 2) - 1), |
| 992 |
static_cast<int>(luaL_checkinteger(L, 3) - 1)); |
| 993 |
if (found) { |
| 994 |
lua_pushboolean(L, 1); |
| 995 |
int iX, iY; |
| 996 |
pPathfinder->get_path_end(&iX, &iY); |
| 997 |
lua_pushinteger(L, iX + 1); |
| 998 |
lua_pushinteger(L, iY + 1); |
| 999 |
return 3; |
| 1000 |
} else { |
| 1001 |
lua_pushboolean(L, 0); |
| 1002 |
return 1; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
int l_path_distance(lua_State* L) { |
| 1007 |
ZoneScoped; |
| 1008 |
|
| 1009 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 1010 |
|
| 1011 |
bool found = pPathfinder->find_path( |
| 1012 |
nullptr, static_cast<int>(luaL_checkinteger(L, 2)) - 1, |
| 1013 |
static_cast<int>(luaL_checkinteger(L, 3)) - 1, |
| 1014 |
static_cast<int>(luaL_checkinteger(L, 4)) - 1, |
| 1015 |
static_cast<int>(luaL_checkinteger(L, 5)) - 1); |
| 1016 |
if (found) { |
| 1017 |
lua_pushinteger(L, pPathfinder->get_path_length()); |
| 1018 |
} else { |
| 1019 |
lua_pushboolean(L, 0); |
| 1020 |
} |
| 1021 |
return 1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
int l_path_path(lua_State* L) { |
| 1025 |
ZoneScoped; |
| 1026 |
|
| 1027 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 1028 |
pPathfinder->find_path(nullptr, static_cast<int>(luaL_checkinteger(L, 2)) - 1, |
| 1029 |
static_cast<int>(luaL_checkinteger(L, 3)) - 1, |
| 1030 |
static_cast<int>(luaL_checkinteger(L, 4)) - 1, |
| 1031 |
static_cast<int>(luaL_checkinteger(L, 5)) - 1); |
| 1032 |
pPathfinder->push_result(L); |
| 1033 |
return 2; |
| 1034 |
} |
| 1035 |
|
| 1036 |
int l_path_idle(lua_State* L) { |
| 1037 |
ZoneScoped; |
| 1038 |
|
| 1039 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 1040 |
|
| 1041 |
bool found = pPathfinder->find_idle_tile( |
| 1042 |
nullptr, // Default map. |
| 1043 |
static_cast<int>(luaL_checkinteger(L, 2)) - 1, // X coordinate queue. |
| 1044 |
static_cast<int>(luaL_checkinteger(L, 3)) - 1, // Y coordinate queue. |
| 1045 |
static_cast<int>( |
| 1046 |
luaL_optinteger(L, 4, 0)), // Take Nth candidate tile, used for |
| 1047 |
// queueing and randomizing idling tiles. |
| 1048 |
static_cast<int>(luaL_optinteger(L, 5, 0))); // Optional indoor parcel. |
| 1049 |
if (!found) { |
| 1050 |
return 0; |
| 1051 |
} |
| 1052 |
int iX, iY; |
| 1053 |
pPathfinder->get_path_end(&iX, &iY); |
| 1054 |
lua_pushinteger(L, iX + 1); |
| 1055 |
lua_pushinteger(L, iY + 1); |
| 1056 |
return 2; |
| 1057 |
} |
| 1058 |
|
| 1059 |
int l_path_visit(lua_State* L) { |
| 1060 |
ZoneScoped; |
| 1061 |
|
| 1062 |
pathfinder* pPathfinder = luaT_testuserdata<pathfinder>(L); |
| 1063 |
luaL_checktype(L, 6, LUA_TFUNCTION); |
| 1064 |
|
| 1065 |
bool found = pPathfinder->visit_objects( |
| 1066 |
nullptr, static_cast<int>(luaL_checkinteger(L, 2)) - 1, |
| 1067 |
static_cast<int>(luaL_checkinteger(L, 3)) - 1, |
| 1068 |
static_cast<object_type>(luaL_checkinteger(L, 4)), |
| 1069 |
static_cast<int>(luaL_checkinteger(L, 5)), L, 6, |
| 1070 |
luaL_checkinteger(L, 4) == 0 ? true : false); |
| 1071 |
|
| 1072 |
lua_pushboolean(L, found); |
| 1073 |
return 1; |
| 1074 |
} |
| 1075 |
|
| 1076 |
} // namespace |
| 1077 |
|
| 1078 |
void lua_register_map(const lua_register_state* pState) { |
| 1079 |
// Map |
| 1080 |
{ |
| 1081 |
lua_class_binding<level_map> lcb(pState, "map", l_map_new, |
| 1082 |
lua_metatable::map); |
| 1083 |
lcb.add_metamethod(l_map_persist, "persist", lua_metatable::anim); |
| 1084 |
lcb.add_metamethod(l_map_depersist, "depersist", lua_metatable::anim); |
| 1085 |
lcb.add_function(l_map_load, "load"); |
| 1086 |
lcb.add_function(l_map_loadblank, "loadBlank"); |
| 1087 |
lcb.add_function(l_map_save, "save"); |
| 1088 |
lcb.add_function(l_map_getsize, "size"); |
| 1089 |
lcb.add_function(l_map_get_player_count, "getPlayerCount"); |
| 1090 |
lcb.add_function(l_map_set_player_count, "setPlayerCount"); |
| 1091 |
lcb.add_function(l_map_get_player_camera, "getCameraTile"); |
| 1092 |
lcb.add_function(l_map_set_player_camera, "setCameraTile"); |
| 1093 |
lcb.add_function(l_map_get_player_heliport, "getHeliportTile"); |
| 1094 |
lcb.add_function(l_map_set_player_heliport, "setHeliportTile"); |
| 1095 |
lcb.add_function(l_map_getcell, "getCell"); |
| 1096 |
lcb.add_function(l_map_gettemperature, "getCellTemperature"); |
| 1097 |
lcb.add_function(l_map_get_room_id, "getRoomId"); |
| 1098 |
lcb.add_function(l_map_getcellflags, "getCellFlags"); |
| 1099 |
lcb.add_function(l_map_setcellflags, "setCellFlags"); |
| 1100 |
lcb.add_function(l_map_getcellraw, "getCellRaw"); |
| 1101 |
lcb.add_function(l_map_setcell, "setCell"); |
| 1102 |
lcb.add_function(l_map_setwallflags, "setWallDrawFlags"); |
| 1103 |
lcb.add_function(l_map_settemperaturedisplay, "setTemperatureDisplay"); |
| 1104 |
lcb.add_function(l_map_updatetemperature, "updateTemperatures"); |
| 1105 |
lcb.add_function(l_map_updateblueprint, "updateRoomBlueprint", |
| 1106 |
lua_metatable::anims, lua_metatable::anim); |
| 1107 |
lcb.add_function(l_map_updateshadows, "updateShadows"); |
| 1108 |
lcb.add_function(l_map_updatepathfinding, "updatePathfinding"); |
| 1109 |
lcb.add_function(l_map_mark_room, "markRoom"); |
| 1110 |
lcb.add_function(l_map_unmark_room, "unmarkRoom"); |
| 1111 |
lcb.add_function(l_map_set_sheet, "setSheet", lua_metatable::sheet); |
| 1112 |
lcb.add_function(l_map_draw, "draw", lua_metatable::surface); |
| 1113 |
lcb.add_function(l_map_hittest, "hitTestObjects", lua_metatable::anim); |
| 1114 |
lcb.add_function(l_map_get_parcel_tilecount, "getParcelTileCount"); |
| 1115 |
lcb.add_function(l_map_get_parcel_count, "getPlotCount"); |
| 1116 |
lcb.add_function(l_map_set_parcel_owner, "setPlotOwner"); |
| 1117 |
lcb.add_function(l_map_get_parcel_owner, "getPlotOwner"); |
| 1118 |
lcb.add_function(l_map_is_parcel_purchasable, "isParcelPurchasable"); |
| 1119 |
lcb.add_function(l_map_erase_thobs, "eraseObjectTypes"); |
| 1120 |
lcb.add_function(l_map_remove_cell_thob, "removeObjectType"); |
| 1121 |
lcb.add_function(l_map_get_litter_fraction, "getLitterFraction"); |
| 1122 |
} |
| 1123 |
|
| 1124 |
// Pathfinder |
| 1125 |
{ |
| 1126 |
lua_class_binding<pathfinder> lcb(pState, "pathfinder", l_path_new, |
| 1127 |
lua_metatable::pathfinder); |
| 1128 |
lcb.add_metamethod(l_path_persist, "persist"); |
| 1129 |
lcb.add_metamethod(l_path_depersist, "depersist"); |
| 1130 |
lcb.add_function(l_path_distance, "findDistance"); |
| 1131 |
lcb.add_function(l_path_is_reachable_from_hospital, |
| 1132 |
"isReachableFromHospital"); |
| 1133 |
lcb.add_function(l_path_path, "findPath"); |
| 1134 |
lcb.add_function(l_path_idle, "findIdleTile"); |
| 1135 |
lcb.add_function(l_path_visit, "findObject"); |
| 1136 |
lcb.add_function(l_path_set_map, "setMap", lua_metatable::map); |
| 1137 |
} |
| 1138 |
} |