lua_polyobjlib.c 12.2 KB
Newer Older
1 2 3
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 2020 by Iestyn "Monster Iestyn" Jealous.
4
// Copyright (C) 2020 by Sonic Team Junior.
5 6 7 8 9 10 11 12 13
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file  lua_polyobjlib.c
/// \brief polyobject library for Lua scripting

#include "doomdef.h"
14
#include "fastcmp.h"
15 16 17 18
#include "p_local.h"
#include "p_polyobj.h"
#include "lua_script.h"
#include "lua_libs.h"
19 20 21 22
#include "lua_hud.h" // hud_running errors

#define NOHUD if (hud_running)\
return luaL_error(L, "HUD rendering code should not call this function!");
23

24
enum polyobj_e {
25
	// properties
26
	polyobj_valid = 0,
27 28
	polyobj_id,
	polyobj_parent,
29 30
	polyobj_vertices,
	polyobj_lines,
31
	polyobj_sector,
32 33 34
	polyobj_angle,
	polyobj_damage,
	polyobj_thrust,
35 36
	polyobj_flags,
	polyobj_translucency,
37
	polyobj_triggertag,
38
	// special functions - utility
39 40
	polyobj_pointInside,
	polyobj_mobjTouching,
41 42 43 44
	polyobj_mobjInside,
	// special functions - manipulation
	polyobj_moveXY,
	polyobj_rotate
45 46
};
static const char *const polyobj_opt[] = {
47
	// properties
48
	"valid",
49 50
	"id",
	"parent",
51 52
	"vertices",
	"lines",
53
	"sector",
54 55 56 57
	"angle",
	"damage",
	"thrust",
	"flags",
58 59
	"translucency",
	"triggertag",
60
	// special functions - utility
61 62 63
	"pointInside",
	"mobjTouching",
	"mobjInside",
64 65 66
	// special functions - manipulation
	"moveXY",
	"rotate",
67 68
	NULL};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static const char *const valid_opt[] ={"valid",NULL};

////////////////////////
// polyobj.vertices[] //
////////////////////////

// polyobj.vertices, i -> polyobj.vertices[i]
// polyobj.vertices.valid, for validity checking
//
// see sectorlines_get in lua_maplib.c
//
static int polyobjvertices_get(lua_State *L)
{
	vertex_t ***polyverts = *((vertex_t ****)luaL_checkudata(L, 1, META_POLYOBJVERTICES));
	size_t i;
	size_t numofverts = 0;
	lua_settop(L, 2);
	if (!lua_isnumber(L, 2))
	{
		int field = luaL_checkoption(L, 2, NULL, valid_opt);
		if (!polyverts || !(*polyverts))
		{
			if (field == 0) {
				lua_pushboolean(L, 0);
				return 1;
			}
			return luaL_error(L, "accessed polyobj_t.vertices doesn't exist anymore.");
		} else if (field == 0) {
			lua_pushboolean(L, 1);
			return 1;
		}
	}

102
	numofverts = *(size_t *)FIELDFROM (polyobj_t, polyverts, vertices,/* -> */numVertices);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

	if (!numofverts)
		return luaL_error(L, "no vertices found!");

	i = (size_t)lua_tointeger(L, 2);
	if (i >= numofverts)
		return 0;
	LUA_PushUserdata(L, (*polyverts)[i], META_VERTEX);
	return 1;
}

// #(polyobj.vertices) -> polyobj.numVertices
static int polyobjvertices_num(lua_State *L)
{
	vertex_t ***polyverts = *((vertex_t ****)luaL_checkudata(L, 1, META_POLYOBJVERTICES));
	size_t numofverts = 0;

	if (!polyverts || !(*polyverts))
		return luaL_error(L, "accessed polyobj_t.vertices doesn't exist anymore.");

123
	numofverts = *(size_t *)FIELDFROM (polyobj_t, polyverts, vertices,/* -> */numVertices);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	lua_pushinteger(L, numofverts);
	return 1;
}

/////////////////////
// polyobj.lines[] //
/////////////////////

// polyobj.lines, i -> polyobj.lines[i]
// polyobj.lines.valid, for validity checking
//
// see sectorlines_get in lua_maplib.c
//
static int polyobjlines_get(lua_State *L)
{
	line_t ***polylines = *((line_t ****)luaL_checkudata(L, 1, META_POLYOBJLINES));
	size_t i;
	size_t numoflines = 0;
	lua_settop(L, 2);
	if (!lua_isnumber(L, 2))
	{
		int field = luaL_checkoption(L, 2, NULL, valid_opt);
		if (!polylines || !(*polylines))
		{
			if (field == 0) {
				lua_pushboolean(L, 0);
				return 1;
			}
			return luaL_error(L, "accessed polyobj_t.lines doesn't exist anymore.");
		} else if (field == 0) {
			lua_pushboolean(L, 1);
			return 1;
		}
	}

159
	numoflines = *(size_t *)FIELDFROM (polyobj_t, polylines, lines,/* -> */numLines);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	if (!numoflines)
		return luaL_error(L, "no lines found!");

	i = (size_t)lua_tointeger(L, 2);
	if (i >= numoflines)
		return 0;
	LUA_PushUserdata(L, (*polylines)[i], META_LINE);
	return 1;
}

// #(polyobj.lines) -> polyobj.numLines
static int polyobjlines_num(lua_State *L)
{
	line_t ***polylines = *((line_t ****)luaL_checkudata(L, 1, META_POLYOBJLINES));
	size_t numoflines = 0;

	if (!polylines || !(*polylines))
		return luaL_error(L, "accessed polyobj_t.lines doesn't exist anymore.");

180
	numoflines = *(size_t *)FIELDFROM (polyobj_t, polylines, lines,/* -> */numLines);
181 182 183 184 185 186 187 188
	lua_pushinteger(L, numoflines);
	return 1;
}

/////////////////////////////////
// polyobj_t function wrappers //
/////////////////////////////////

189
// special functions - utility
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
static int lib_polyobj_PointInside(lua_State *L)
{
	polyobj_t *po = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	fixed_t x = luaL_checkfixed(L, 2);
	fixed_t y = luaL_checkfixed(L, 3);
	INLEVEL
	if (!po)
		return LUA_ErrInvalid(L, "polyobj_t");
	lua_pushboolean(L, P_PointInsidePolyobj(po, x, y));
	return 1;
}

static int lib_polyobj_MobjTouching(lua_State *L)
{
	polyobj_t *po = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	mobj_t *mo = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ));
	INLEVEL
	if (!po)
		return LUA_ErrInvalid(L, "polyobj_t");
	if (!mo)
		return LUA_ErrInvalid(L, "mobj_t");
	lua_pushboolean(L, P_MobjTouchingPolyobj(po, mo));
	return 1;
}

static int lib_polyobj_MobjInside(lua_State *L)
{
	polyobj_t *po = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	mobj_t *mo = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ));
	INLEVEL
	if (!po)
		return LUA_ErrInvalid(L, "polyobj_t");
	if (!mo)
		return LUA_ErrInvalid(L, "mobj_t");
	lua_pushboolean(L, P_MobjInsidePolyobj(po, mo));
	return 1;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
// special functions - manipulation
static int lib_polyobj_moveXY(lua_State *L)
{
	polyobj_t *po = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	fixed_t x = luaL_checkfixed(L, 2);
	fixed_t y = luaL_checkfixed(L, 3);
	boolean checkmobjs = lua_opttrueboolean(L, 4);
	NOHUD
	INLEVEL
	if (!po)
		return LUA_ErrInvalid(L, "polyobj_t");
	lua_pushboolean(L, Polyobj_moveXY(po, x, y, checkmobjs));
	return 1;
}

static int lib_polyobj_rotate(lua_State *L)
{
	polyobj_t *po = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	angle_t delta = luaL_checkangle(L, 2);
	UINT8 turnthings = (UINT8)luaL_optinteger(L, 3, 0); // don't turn anything by default? (could change this if not desired)
	boolean checkmobjs = lua_opttrueboolean(L, 4);
	NOHUD
	INLEVEL
	if (!po)
		return LUA_ErrInvalid(L, "polyobj_t");
	lua_pushboolean(L, Polyobj_rotate(po, delta, turnthings, checkmobjs));
	return 1;
}

257 258 259 260
///////////////
// polyobj_t //
///////////////

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static int polyobj_get(lua_State *L)
{
	polyobj_t *polyobj = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	enum polyobj_e field = luaL_checkoption(L, 2, NULL, polyobj_opt);

	if (!polyobj) {
		if (field == polyobj_valid) {
			lua_pushboolean(L, false);
			return 1;
		}
		return LUA_ErrInvalid(L, "polyobj_t");
	}

	switch (field)
	{
276
	// properties
277 278 279
	case polyobj_valid:
		lua_pushboolean(L, true);
		break;
280 281 282 283 284 285
	case polyobj_id:
		lua_pushinteger(L, polyobj->id);
		break;
	case polyobj_parent:
		lua_pushinteger(L, polyobj->parent);
		break;
286 287 288 289 290 291
	case polyobj_vertices: // vertices
		LUA_PushUserdata(L, &polyobj->vertices, META_POLYOBJVERTICES); // push the address of the "vertices" member in the struct, to allow our hacks to work
		break;
	case polyobj_lines: // lines
		LUA_PushUserdata(L, &polyobj->lines, META_POLYOBJLINES); // push the address of the "lines" member in the struct, to allow our hacks to work
		break;
292 293 294
	case polyobj_sector: // shortcut that exists only in Lua!
		LUA_PushUserdata(L, polyobj->lines[0]->backsector, META_SECTOR);
		break;
295 296 297 298 299 300 301 302 303 304 305 306
	case polyobj_angle:
		lua_pushangle(L, polyobj->angle);
		break;
	case polyobj_damage:
		lua_pushinteger(L, polyobj->damage);
		break;
	case polyobj_thrust:
		lua_pushfixed(L, polyobj->thrust);
		break;
	case polyobj_flags:
		lua_pushinteger(L, polyobj->flags);
		break;
307 308 309 310 311 312
	case polyobj_translucency:
		lua_pushinteger(L, polyobj->translucency);
		break;
	case polyobj_triggertag:
		lua_pushinteger(L, polyobj->triggertag);
		break;
313
	// special functions - utility
314 315 316 317 318 319 320 321 322
	case polyobj_pointInside:
		lua_pushcfunction(L, lib_polyobj_PointInside);
		break;
	case polyobj_mobjTouching:
		lua_pushcfunction(L, lib_polyobj_MobjTouching);
		break;
	case polyobj_mobjInside:
		lua_pushcfunction(L, lib_polyobj_MobjInside);
		break;
323 324 325 326 327 328 329
	// special functions - manipulation
	case polyobj_moveXY:
		lua_pushcfunction(L, lib_polyobj_moveXY);
		break;
	case polyobj_rotate:
		lua_pushcfunction(L, lib_polyobj_rotate);
		break;
330 331
	}
	return 1;
332
}
333 334 335

static int polyobj_set(lua_State *L)
{
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	polyobj_t *polyobj = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	enum polyobj_e field = luaL_checkoption(L, 2, NULL, polyobj_opt);

	if (!polyobj)
		return LUA_ErrInvalid(L, "polyobj_t");

	if (hud_running)
		return luaL_error(L, "Do not alter polyobj_t in HUD rendering code!");

	switch (field)
	{
	default:
		return luaL_error(L, LUA_QL("polyobj_t") " field " LUA_QS " cannot be modified.", polyobj_opt[field]);
	case polyobj_angle:
		return luaL_error(L, LUA_QL("polyobj_t") " field " LUA_QS " should not be set directly. Use the function " LUA_QL("polyobj:rotate(angle)") " instead.", polyobj_opt[field]);
	case polyobj_parent:
		polyobj->parent = luaL_checkinteger(L, 3);
		break;
	case polyobj_flags:
		polyobj->flags = luaL_checkinteger(L, 3);
		break;
	case polyobj_translucency:
		polyobj->translucency = luaL_checkinteger(L, 3);
		break;
	}

	return 0;
363 364
}

365 366 367 368 369 370 371 372 373
static int polyobj_num(lua_State *L)
{
	polyobj_t *polyobj = *((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ));
	if (!polyobj)
		return luaL_error(L, "accessed polyobj_t doesn't exist anymore.");
	lua_pushinteger(L, polyobj-PolyObjects);
	return 1;
}

374 375 376 377
///////////////////
// PolyObjects[] //
///////////////////

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
static int lib_iteratePolyObjects(lua_State *L)
{
	INT32 i = -1;
	if (lua_gettop(L) < 2)
	{
		//return luaL_error(L, "Don't call PolyObjects.iterate() directly, use it as 'for polyobj in PolyObjects.iterate do <block> end'.");
		lua_pushcfunction(L, lib_iteratePolyObjects);
		return 1;
	}
	lua_settop(L, 2);
	lua_remove(L, 1); // state is unused.
	if (!lua_isnil(L, 1))
		i = (INT32)(*((polyobj_t **)luaL_checkudata(L, 1, META_POLYOBJ)) - PolyObjects);
	for (i++; i < numPolyObjects; i++)
	{
		LUA_PushUserdata(L, &PolyObjects[i], META_POLYOBJ);
		return 1;
	}
	return 0;
}

399 400 401 402 403 404 405 406 407 408 409
static int lib_PolyObject_getfornum(lua_State *L)
{
	INT32 id = (INT32)luaL_checkinteger(L, 1);

	if (!numPolyObjects)
		return 0; // if there's no PolyObjects then bail out here

	LUA_PushUserdata(L, Polyobj_GetForNum(id), META_POLYOBJ);
	return 1;
}

410 411 412 413 414 415 416 417 418 419
static int lib_getPolyObject(lua_State *L)
{
	const char *field;
	INT32 i;

	// find PolyObject by number
	if (lua_type(L, 2) == LUA_TNUMBER)
	{
		i = luaL_checkinteger(L, 2);
		if (i < 0 || i >= numPolyObjects)
420
			return luaL_error(L, "polyobjects[] index %d out of range (0 - %d)", i, numPolyObjects-1);
421 422 423 424 425 426 427 428 429 430 431
		LUA_PushUserdata(L, &PolyObjects[i], META_POLYOBJ);
		return 1;
	}

	field = luaL_checkstring(L, 2);
	// special function iterate
	if (fastcmp(field,"iterate"))
	{
		lua_pushcfunction(L, lib_iteratePolyObjects);
		return 1;
	}
432 433 434 435 436 437
	// find PolyObject by ID
	else if (fastcmp(field,"GetForNum")) // name could probably be better
	{
		lua_pushcfunction(L, lib_PolyObject_getfornum);
		return 1;
	}
438 439 440 441 442 443 444 445 446
	return 0;
}

static int lib_numPolyObjects(lua_State *L)
{
	lua_pushinteger(L, numPolyObjects);
	return 1;
}

447 448
int LUA_PolyObjLib(lua_State *L)
{
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	luaL_newmetatable(L, META_POLYOBJVERTICES);
		lua_pushcfunction(L, polyobjvertices_get);
		lua_setfield(L, -2, "__index");

		lua_pushcfunction(L, polyobjvertices_num);
		lua_setfield(L, -2, "__len");
	lua_pop(L, 1);

	luaL_newmetatable(L, META_POLYOBJLINES);
		lua_pushcfunction(L, polyobjlines_get);
		lua_setfield(L, -2, "__index");

		lua_pushcfunction(L, polyobjlines_num);
		lua_setfield(L, -2, "__len");
	lua_pop(L, 1);

465 466 467 468 469 470 471
	luaL_newmetatable(L, META_POLYOBJ);
		lua_pushcfunction(L, polyobj_get);
		lua_setfield(L, -2, "__index");

		lua_pushcfunction(L, polyobj_set);
		lua_setfield(L, -2, "__newindex");

472 473
		lua_pushcfunction(L, polyobj_num);
		lua_setfield(L, -2, "__len");
474 475 476 477 478 479 480 481 482 483
	lua_pop(L,1);

	lua_newuserdata(L, 0);
		lua_createtable(L, 0, 2);
			lua_pushcfunction(L, lib_getPolyObject);
			lua_setfield(L, -2, "__index");

			lua_pushcfunction(L, lib_numPolyObjects);
			lua_setfield(L, -2, "__len");
		lua_setmetatable(L, -2);
484
	lua_setglobal(L, "polyobjects");
485 486
	return 0;
}