Getting shaders working with lua.

This commit is contained in:
2026-03-28 21:50:59 -05:00
parent cbb68a399d
commit dbb7e9f53c
49 changed files with 305 additions and 114 deletions

View File

@@ -0,0 +1,9 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirectories
add_subdirectory(item)
add_subdirectory(map)
add_subdirectory(story)

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
moduleitem.c
)

View File

@@ -0,0 +1,262 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "moduleitem.h"
#include "item/inventory.h"
#include "item/backpack.h"
#include "assert/assert.h"
void moduleItem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Set item information
scriptContextExec(context, ITEM_SCRIPT);
// Bind BACKPACK const pointer
lua_pushlightuserdata(context->luaState, &BACKPACK);
lua_setglobal(context->luaState, "BACKPACK");
// Bind Methods
lua_register(
context->luaState, "inventoryItemExists", moduleInventoryItemExists
);
lua_register(context->luaState, "inventoryAdd", moduleInventoryAdd);
lua_register(context->luaState, "inventorySet", moduleInventorySet);
lua_register(context->luaState, "inventoryRemove", moduleInventoryRemove);
lua_register(context->luaState, "inventoryGetCount", moduleInventoryGetCount);
lua_register(context->luaState, "inventoryIsFull", moduleInventoryIsFull);
lua_register(context->luaState, "inventoryItemFull", moduleInventoryItemFull);
lua_register(context->luaState, "inventorySort", moduleInventorySort);
}
int moduleInventoryItemExists(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Expect inventory pointer and item ID
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryItemExists: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventoryItemExists: Expected item ID as second argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
// Error if item is ITEM_ID_NULL
if(item == ITEM_ID_NULL) {
luaL_error(L, "inventoryItemExists: Item ID cannot be ITEM_ID_NULL");
return 0;
}
bool_t hasItem = inventoryItemExists(inventory, item);
lua_pushboolean(L, hasItem);
return 1;
}
int moduleInventorySet(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer, item ID and quantity (uint8_t)
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventorySet: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventorySet: Expected item ID as second argument");
return 0;
}
if(!lua_isnumber(L, 3)) {
luaL_error(L, "inventorySet: Expected quantity as third argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
uint8_t quantity = (uint8_t)lua_tonumber(L, 3);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
inventorySet(inventory, item, quantity);
return 0;
}
int moduleInventoryAdd(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer, item ID and quantity (uint8_t)
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryAdd: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventoryAdd: Expected item ID as second argument");
return 0;
}
if(!lua_isnumber(L, 3)) {
luaL_error(L, "inventoryAdd: Expected quantity as third argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
uint8_t quantity = (uint8_t)lua_tonumber(L, 3);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
inventoryAdd(inventory, item, quantity);
return 0;
}
int moduleInventoryRemove(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer and item ID
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryRemove: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventoryRemove: Expected item ID as second argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
// if there is a third argument (quantity), then we are actually doing a
// partial removal.
if(lua_gettop(L) >= 3) {
if(!lua_isnumber(L, 3)) {
luaL_error(L, "inventoryRemove: Expected quantity as third argument");
return 0;
}
uint8_t amount = (uint8_t)lua_tonumber(L, 3);
uint8_t currentQuantity = inventoryGetCount(inventory, item);
if(amount >= currentQuantity) {
// Remove entire stack
inventoryRemove(inventory, item);
return 0;
}
// Set new quantity
inventorySet(inventory, item, currentQuantity - amount);
return 0;
}
inventoryRemove(inventory, item);
return 0;
}
int moduleInventoryGetCount(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer and item ID
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryGetCount: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventoryGetCount: Expected item ID as second argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
uint8_t count = inventoryGetCount(inventory, item);
lua_pushnumber(L, count);
return 1;
}
int moduleInventoryIsFull(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryIsFull: Expected inventory pointer as first argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
bool_t isFull = inventoryIsFull(inventory);
lua_pushboolean(L, isFull);
return 1;
}
int moduleInventoryItemFull(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer and item ID
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventoryItemFull: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventoryItemFull: Expected item ID as second argument");
return 0;
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
itemid_t item = (itemid_t)lua_tonumber(L, 2);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
bool_t isFull = inventoryItemFull(inventory, item);
lua_pushboolean(L, isFull);
return 1;
}
int moduleInventorySort(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires inventory pointer, sort type and reverse flag
if(!lua_islightuserdata(L, 1)) {
luaL_error(L, "inventorySort: Expected inventory pointer as first argument");
return 0;
}
if(!lua_isnumber(L, 2)) {
luaL_error(L, "inventorySort: Expected sort type as second argument");
return 0;
}
// Optional, reverse
bool_t reverse = false;
if(lua_gettop(L) >= 3) {
if(!lua_isboolean(L, 3)) {
luaL_error(L, "inventorySort: Expected reverse flag as third argument");
return 0;
}
reverse = (bool_t)lua_toboolean(L, 3);
}
inventory_t *inventory = (inventory_t *)lua_touserdata(L, 1);
inventorysort_t sortBy = (inventorysort_t)lua_tonumber(L, 2);
assertNotNull(inventory, "Inventory pointer cannot be NULL.");
inventorySort(inventory, sortBy, reverse);
return 0;
}

View File

@@ -0,0 +1,80 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register item functions to the given script context.
*
* @param context The script context to register item functions to.
*/
void moduleItem(scriptcontext_t *context);
/**
* Script binding for checking if an item exists in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryItemExists(lua_State *L);
/**
* Script binding for adding an item to an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventorySet(lua_State *L);
/**
* Script binding for setting the quantity of an item in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryAdd(lua_State *L);
/**
* Script binding for removing an item from an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryRemove(lua_State *L);
/**
* Script binding for getting the count of an item in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryGetCount(lua_State *L);
/**
* Script binding for checking if an inventory is full.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryIsFull(lua_State *L);
/**
* Script binding for checking if an item stack in an inventory is full.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryItemFull(lua_State *L);
/**
* Script binding for sorting an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventorySort(lua_State *L);

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
modulemap.c
)

View File

@@ -0,0 +1,60 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "modulemap.h"
#include "assert/assert.h"
#include "map/map.h"
void moduleMap(scriptcontext_t *ctx) {
assertNotNull(ctx, "Script context cannot be NULL");
// Register map functions
lua_register(ctx->luaState, "mapIsLoaded", moduleMapIsLoaded);
lua_register(ctx->luaState, "mapGetTile", moduleMapGetTile);
lua_register(ctx->luaState, "mapRender", moduleMapRender);
lua_register(ctx->luaState, "mapLoad", moduleMapLoad);
}
int moduleMapIsLoaded(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
lua_pushboolean(L, mapIsLoaded());
return 1;
}
int moduleMapGetTile(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
return 1;
}
int moduleMapRender(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
mapRender();
return 0;
}
int moduleMapLoad(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
if(!lua_isstring(L, 1)) {
luaL_error(L, "Expected string as first argument (file path).");
return 0;
}
const char_t *path = lua_tostring(L, 1);
// Optional position.
chunkpos_t position = {0, 0, 0};
errorret_t err = mapLoad(path, position);
if(err.code != ERROR_OK) {
errorCatch(errorPrint(err));
luaL_error(L, "Failed to load map!");
return 0;
}
return 0;
}

View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register map functions to the given script context.
*
* @param context The script context to register map functions to.
*/
void moduleMap(scriptcontext_t *context);
/**
* Script function to check if a map is loaded.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleMapIsLoaded(lua_State *L);
/**
* Script function to get the tile at a given world position.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleMapGetTile(lua_State *L);
/**
* Script function to render the map.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleMapRender(lua_State *L);
/**
* Script function to load a map from a given file path.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleMapLoad(lua_State *L);

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
modulestoryflag.c
)

View File

@@ -0,0 +1,109 @@
/**
* 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;
}

View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register story flag module functions to the given script context.
*
* @param context The script context to register story flag module functions to.
*/
void moduleStoryFlag(scriptcontext_t *context);
/**
* Script binding for getting the value of a story flag.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleStoryFlagGet(lua_State *L);
/**
* Script binding for setting the value of a story flag.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleStoryFlagSet(lua_State *L);
/**
* Script binding for incrementing a story flag.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleStoryFlagIncrement(lua_State *L);
/**
* Script binding for decrementing a story flag.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleStoryFlagDecrement(lua_State *L);