iuno just screwing around tbh
Some checks failed
Build Dusk / build-linux (push) Failing after 1m5s
Build Dusk / build-psp (push) Failing after 1m24s

This commit is contained in:
2025-12-24 10:44:53 +10:00
parent aed202ebf9
commit f39b2060a8
15 changed files with 72 additions and 32 deletions

View File

@@ -4,4 +4,5 @@
# https://opensource.org/licenses/MIT
add_asset(SCRIPT test.lua)
add_asset(SCRIPT test2.lua)
add_subdirectory(scene)

5
assets/script/init.lua Normal file
View File

@@ -0,0 +1,5 @@
print('Init')
-- Load map scene
setScene('map')
setMap()

View File

@@ -0,0 +1,4 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT

View File

@@ -1,5 +0,0 @@
include('script/test2')
player = entityAdd(ENTITY_TYPE_PLAYER, 3, 6, 1)
print("Player entity ID: " .. player)

View File

@@ -1 +0,0 @@
print("This is test2.lua")

View File

@@ -8,7 +8,7 @@ ENTITY_DIR_WEST = 1
ENTITY_DIR_EAST = 2
ENTITY_DIR_NORTH = 3
ENTITY_COUNT = 256
ENTITY_COUNT = 128
ENTITY_TYPE_NULL = 0
ENTITY_TYPE_PLAYER = 1

View File

@@ -18,8 +18,6 @@
#include "script/scriptmanager.h"
#include "debug/debug.h"
#include "script/scriptcontext.h"
engine_t ENGINE;
errorret_t engineInit(const int32_t argc, const char_t **argv) {
@@ -41,10 +39,9 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(sceneManagerInit());
// Run the initial script.
scriptcontext_t testCtx;
errorChain(scriptContextInit(&testCtx));
errorChain(scriptContextExecFile(&testCtx, "script/test.dsf"));
scriptContextDispose(&testCtx);
errorChain(scriptContextInit(&ENGINE.mainScriptContext));
errorChain(scriptContextExecFile(&ENGINE.mainScriptContext, "script/test.dsf"));
scriptContextDispose(&ENGINE.mainScriptContext);
errorOk();
}

View File

@@ -8,11 +8,13 @@
#pragma once
#include "display/display.h"// Important to be included first.
#include "error/error.h"
#include "script/scriptcontext.h"
typedef struct {
bool_t running;
int32_t argc;
const char_t **argv;
scriptcontext_t mainScriptContext;
} engine_t;
extern engine_t ENGINE;

View File

@@ -16,8 +16,7 @@ map_t MAP;
errorret_t mapInit() {
memoryZero(&MAP, sizeof(map_t));
// Init the default chunks. In future I'll probably make this based on where
// the player spawns in to save an initial mapSet.
// Init the first chunks.
chunkindex_t index = 0;
for(chunkunit_t z = 0; z < MAP_CHUNK_DEPTH; z++) {
for(chunkunit_t y = 0; y < MAP_CHUNK_HEIGHT; y++) {

View File

@@ -19,12 +19,6 @@ errorret_t sceneManagerInit(void) {
sceneManagerRegisterScene(&SCENE_TEST);
sceneManagerRegisterScene(&SCENE_MAP);
// Initial scene
scene_t *initial = sceneManagerGetSceneByName("map");
sceneManagerSetScene(initial);
if(initial->init) errorChain(initial->init(&SCENE_MANAGER.sceneData));
initial->flags |= SCENE_FLAG_INITIALIZED;
errorOk();
}

View File

@@ -9,7 +9,7 @@
#include "scene.h"
#include "scenedata.h"
#define SCENE_MANAGER_SCENE_COUNT_MAX 32
#define SCENE_MANAGER_SCENE_COUNT_MAX 16
typedef struct {
scene_t *current;

View File

@@ -9,8 +9,6 @@
#include "script/scriptcontext.h"
#include "assert/assert.h"
void scriptFuncCamera(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
#include "scene/scenemanager.h"
#include "debug/debug.h"
#include "assert/assert.h"
#include "error/error.h"
int32_t scriptFuncSetScene(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isstring(L, 1), "First argument must be a string");
const char_t *sceneName = lua_tostring(L, 1);
scene_t *scene = sceneManagerGetSceneByName(sceneName);
assertNotNull(scene, "Scene with given name does not exist");
sceneManagerSetScene(scene);
if(scene->init) {
errorret_t err = scene->init(&SCENE_MANAGER.sceneData);
assertTrue(err.code == ERROR_OK, "Scene initialization failed");
}
scene->flags |= SCENE_FLAG_INITIALIZED;
return 0;
}
void scriptFuncScene(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "setScene", scriptFuncSetScene);
}

View File

@@ -34,19 +34,19 @@ int32_t scriptFuncPrint(lua_State *L) {
int32_t scriptFuncInclude(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isstring(L, 1), "Expected string filename");
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
if (!ctx) return luaL_error(L, "Lua extraspace ctx not set");
assertNotNull(ctx, "Script context cannot be NULL");
const char_t *filename = luaL_checkstring(L, 1);
if(filename == NULL || filename[0] == '\0') {
luaL_error(L, "Filename cannot be NULL or empty");
return 0;
}
assertNotNull(filename, "Filename cannot be NULL");
assertStrLenMin(filename, 1, "Filename cannot be empty");
//
// Copy out filename to mutable buffer
char_t buffer[1024];
stringCopy(buffer, filename, 1024);
// Ensure it has .dsf extension
size_t len = strlen(buffer);
if(len < 4 || strcmp(&buffer[len - 4], ".dsf") != 0) {
@@ -58,6 +58,7 @@ int32_t scriptFuncInclude(lua_State *L) {
stringCopy(&buffer[len], ".dsf", 5);
}
// Execute the script file
errorret_t err = scriptContextExecFile(
ctx,
buffer

View File

@@ -14,6 +14,7 @@
#include "script/func/scriptfunccamera.h"
#include "script/func/scriptfuncentity.h"
#include "script/func/scriptfuncsystem.h"
#include "script/func/scriptfuncscene.h"
errorret_t scriptContextInit(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
@@ -30,9 +31,14 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
// Store context in Lua extraspace
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
// Register variables
// scriptContextExec(context, "PLATFORM = 'DESKTOP'");
// Register functions
scriptFuncSystem(context);
scriptFuncEntity(context);
scriptFuncCamera(context);
scriptFuncScene(context);
errorOk();
}