2013-02-01 13:05:00 +00:00
|
|
|
-- Bound-checking functions for engine and game "things".
|
2013-02-01 13:04:56 +00:00
|
|
|
|
|
|
|
local ffiC = require("ffi").C
|
2013-06-13 17:11:09 +00:00
|
|
|
local type = type
|
2013-03-24 18:53:14 +00:00
|
|
|
local error = error
|
|
|
|
|
2013-02-01 13:04:56 +00:00
|
|
|
local bcheck = {}
|
|
|
|
|
|
|
|
--== ENGINE ==--
|
|
|
|
|
|
|
|
function bcheck.sector_idx(sectnum)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (sectnum >= 0 and sectnum < ffiC.numsectors)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("invalid sector number "..sectnum, 3)
|
2013-02-01 13:05:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.wall_idx(wallnum)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (wallnum >= 0 and wallnum < ffiC.numwalls)) then
|
2013-02-01 13:05:00 +00:00
|
|
|
error("invalid wall number "..wallnum, 3)
|
2013-02-01 13:04:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Provide another function that also checks whether the sprite exists in
|
|
|
|
-- the game world (statnum != MAXSTATUS).
|
|
|
|
function bcheck.sprite_idx(spritenum)
|
2013-07-19 12:49:02 +00:00
|
|
|
-- if (not (spritenum >= 0 and spritenum < ffiC.MAXSPRITES)) then
|
|
|
|
if (not (spritenum >= 0 and spritenum < ffiC.MAXSPRITES)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("invalid sprite number "..spritenum, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.tile_idx(tilenum)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (tilenum >= 0 and tilenum < ffiC.MAXTILES)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("invalid tile number "..tilenum, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-12-31 11:51:55 +00:00
|
|
|
--== HELPERS ==--
|
|
|
|
|
|
|
|
function bcheck.number(val, errlev)
|
|
|
|
if (type(val)~="number" or val~=val) then
|
|
|
|
error("invalid argument: must be a non-NaN number", errlev or 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.type(val, typestr, errlev)
|
|
|
|
if (type(val)~=typestr) then
|
|
|
|
error("invalid argument: must be a "..typestr, errlev or 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-01 13:04:56 +00:00
|
|
|
--== GAME ==--
|
2013-12-31 11:51:55 +00:00
|
|
|
if (ffiC.LUNATIC_CLIENT == ffiC.LUNATIC_CLIENT_MAPSTER32) then
|
|
|
|
return bcheck
|
|
|
|
end
|
|
|
|
|
|
|
|
local con_lang = require("con_lang")
|
2013-02-01 13:04:56 +00:00
|
|
|
|
|
|
|
function bcheck.player_idx(snum)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (snum >= 0 and snum < ffiC.playerswhenstarted)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("invalid player number "..snum, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.sound_idx(sndidx)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (sndidx >= 0 and sndidx < con_lang.MAXSOUNDS)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("invalid sound number "..sndidx, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.weapon_idx(weap)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (weap >= 0 and weap < ffiC.MAX_WEAPONS)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("Invalid weapon ID "..weap, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.inventory_idx(inv)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (inv >= 0 and inv < ffiC.GET_MAX)) then
|
2013-02-01 13:04:56 +00:00
|
|
|
error("Invalid inventory ID "..inv, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-16 18:53:24 +00:00
|
|
|
function bcheck.volume_idx(volume)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (volume >= 0 and volume < con_lang.MAXVOLUMES)) then
|
2013-06-01 20:09:42 +00:00
|
|
|
error("invalid volume number "..volume, 3)
|
2013-02-16 18:53:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bcheck.level_idx(level)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (level >= 0 and level < con_lang.MAXLEVELS)) then
|
2013-06-01 20:09:42 +00:00
|
|
|
error("invalid level number "..level, 3)
|
2013-02-16 18:53:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-25 12:57:44 +00:00
|
|
|
function bcheck.linear_map_idx(idx)
|
|
|
|
if (not (idx >= 0 and idx <= con_lang.MAXLEVELS * con_lang.MAXVOLUMES)) then
|
|
|
|
error("invalid linear map index "..idx, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 16:05:22 +00:00
|
|
|
function bcheck.quote_idx(qnum, onlyidx)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (qnum >= 0 and qnum < con_lang.MAXQUOTES)) then
|
2013-02-18 16:07:32 +00:00
|
|
|
error("invalid quote number "..qnum, 3)
|
|
|
|
end
|
|
|
|
|
2013-02-25 15:31:13 +00:00
|
|
|
local cstr = ffiC.ScriptQuotes[qnum]
|
2013-02-24 16:05:22 +00:00
|
|
|
if (onlyidx) then
|
2013-02-25 15:31:13 +00:00
|
|
|
return cstr
|
2013-02-24 16:05:22 +00:00
|
|
|
end
|
|
|
|
|
2013-02-18 16:07:42 +00:00
|
|
|
if (cstr == nil) then
|
2013-02-18 16:07:32 +00:00
|
|
|
error("null quote "..qnum, 3)
|
|
|
|
end
|
2013-02-18 16:07:42 +00:00
|
|
|
|
|
|
|
return cstr
|
2013-02-18 16:07:32 +00:00
|
|
|
end
|
|
|
|
|
2015-02-22 19:31:11 +00:00
|
|
|
function bcheck.top_level(funcname, errlev)
|
2013-03-03 16:06:37 +00:00
|
|
|
if (ffiC.g_elCallDepth > 0) then
|
2013-06-30 20:38:39 +00:00
|
|
|
error("Invalid use of "..funcname..": must be called from top level", errlev or 3)
|
2013-03-03 16:06:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 13:04:56 +00:00
|
|
|
return bcheck
|