109 lines
2.7 KiB
C
109 lines
2.7 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "modulestoryflag.h"
|
|
#include "assert/assert.h"
|
|
#include "story/storyflag.h"
|
|
|
|
void moduleStoryFlag(scriptcontext_t *context) {
|
|
assertNotNull(context, "Script context cannot be NULL");
|
|
|
|
lua_register(context->luaState, "storyFlagGet", moduleStoryFlagGet);
|
|
lua_register(context->luaState, "storyFlagSet", moduleStoryFlagSet);
|
|
lua_register(
|
|
context->luaState, "storyFlagIncrement", moduleStoryFlagIncrement
|
|
);
|
|
lua_register(
|
|
context->luaState, "storyFlagDecrement", moduleStoryFlagDecrement
|
|
);
|
|
}
|
|
|
|
int moduleStoryFlagGet(lua_State *L) {
|
|
assertNotNull(L, "Lua state cannot be NULL");
|
|
|
|
// Require story flag ID argument
|
|
if(!lua_isnumber(L, 1)) {
|
|
luaL_error(L, "Expected flag ID.");
|
|
return 0;
|
|
}
|
|
|
|
storyflag_t flag = (storyflag_t)lua_tonumber(L, 1);
|
|
if(flag <= STORY_FLAG_NULL || flag >= STORY_FLAG_COUNT) {
|
|
luaL_error(L, "Invalid flag ID %d", flag);
|
|
return 0;
|
|
}
|
|
|
|
storyflagvalue_t value = storyFlagGet(flag);
|
|
lua_pushnumber(L, value);
|
|
return 1;
|
|
}
|
|
|
|
int moduleStoryFlagSet(lua_State *L) {
|
|
assertNotNull(L, "Lua state cannot be NULL");
|
|
|
|
// Require story flag ID argument
|
|
if(!lua_isnumber(L, 1)) {
|
|
luaL_error(L, "Expected flag ID.");
|
|
return 0;
|
|
}
|
|
|
|
// Require story flag value argument
|
|
if(!lua_isnumber(L, 2)) {
|
|
luaL_error(L, "Expected flag value.");
|
|
return 0;
|
|
}
|
|
|
|
storyflag_t flag = (storyflag_t)lua_tonumber(L, 1);
|
|
if(flag <= STORY_FLAG_NULL || flag >= STORY_FLAG_COUNT) {
|
|
luaL_error(L, "Invalid flag ID %d", flag);
|
|
return 0;
|
|
}
|
|
|
|
storyflagvalue_t value = (storyflagvalue_t)lua_tonumber(L, 2);
|
|
storyFlagSet(flag, value);
|
|
return 0;
|
|
}
|
|
|
|
int moduleStoryFlagIncrement(lua_State *L) {
|
|
assertNotNull(L, "Lua state cannot be NULL");
|
|
|
|
// Require story flag ID argument
|
|
if(!lua_isnumber(L, 1)) {
|
|
luaL_error(L, "Expected flag ID.");
|
|
return 0;
|
|
}
|
|
|
|
storyflag_t flag = (storyflag_t)lua_tonumber(L, 1);
|
|
if(flag <= STORY_FLAG_NULL || flag >= STORY_FLAG_COUNT) {
|
|
luaL_error(L, "Invalid flag ID %d", flag);
|
|
return 0;
|
|
}
|
|
|
|
storyflagvalue_t value = storyFlagGet(flag);
|
|
storyFlagSet(flag, value + 1);
|
|
return 0;
|
|
}
|
|
|
|
int moduleStoryFlagDecrement(lua_State *L) {
|
|
assertNotNull(L, "Lua state cannot be NULL");
|
|
|
|
// Require story flag ID argument
|
|
if(!lua_isnumber(L, 1)) {
|
|
luaL_error(L, "Expected flag ID.");
|
|
return 0;
|
|
}
|
|
|
|
storyflag_t flag = (storyflag_t)lua_tonumber(L, 1);
|
|
if(flag <= STORY_FLAG_NULL || flag >= STORY_FLAG_COUNT) {
|
|
luaL_error(L, "Invalid flag ID %d", flag);
|
|
return 0;
|
|
}
|
|
|
|
storyflagvalue_t value = storyFlagGet(flag);
|
|
storyFlagSet(flag, value - 1);
|
|
return 0;
|
|
} |