diff options
Diffstat (limited to 'kon')
| -rw-r--r-- | kon/konscript.c | 248 | ||||
| -rw-r--r-- | kon/konscript.h | 134 |
2 files changed, 382 insertions, 0 deletions
diff --git a/kon/konscript.c b/kon/konscript.c new file mode 100644 index 0000000..dc706bf --- /dev/null +++ b/kon/konscript.c @@ -0,0 +1,248 @@ +/* SPDX-License-Identifier: MIT */ +/* + * konscript.c + * + * Copyright (c) 2026 KonAki + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "konscript.h" + +#include <stddef.h> + +/*** variables ***/ + +int kon_scriptSwitch(const kon_script_t *vm, int id) +{ + return id >= 0 && id < vm->switchCount && vm->switches && vm->switches[id] ? 1 : 0; +} + +int32_t kon_scriptVar(const kon_script_t *vm, int id) +{ + return id >= 0 && id < vm->varCount && vm->vars ? vm->vars[id] : 0; +} + +void kon_scriptSetSwitch(kon_script_t *vm, int id, int on) +{ + if (id >= 0 && id < vm->switchCount && vm->switches) vm->switches[id] = on ? 1 : 0; +} + +void kon_scriptSetVar(kon_script_t *vm, int id, int32_t value) +{ + if (id >= 0 && id < vm->varCount && vm->vars) vm->vars[id] = value; +} + +/* done in 64 bits and clamped so a script can never overflow or divide by 0 */ +static int32_t arithmetic(int32_t x, int how, int32_t y) +{ + int64_t r; + + switch (how) { + case KON_ADD: r = (int64_t)x + y; break; + case KON_SUB: r = (int64_t)x - y; break; + case KON_MUL: r = (int64_t)x * y; break; + case KON_DIV: r = y ? (int64_t)x / y : x; break; + case KON_MOD: r = y ? (int64_t)x % y : x; break; + default: r = y; break; + } + if (r > INT32_MAX) r = INT32_MAX; + if (r < INT32_MIN) r = INT32_MIN; + return (int32_t)r; +} + +static int32_t operand(const kon_script_t *vm, int n) +{ + if (n >= KON_VARIABLE && n < KON_VARIABLE + 0x20000000) return kon_scriptVar(vm, n - KON_VARIABLE); + return n; +} + +static int compare(int32_t x, int how, int32_t y) +{ + switch (how) { + case KON_IS_EQ: return x == y; + case KON_IS_NE: return x != y; + case KON_IS_GE: return x >= y; + case KON_IS_LE: return x <= y; + case KON_IS_GT: return x > y; + case KON_IS_LT: return x < y; + } + return 0; +} + +/*** script ***/ + +void kon_scriptInit(kon_script_t *vm, const kon_cmd_t *cmds, int count, uint8_t *switches, int switchCount, int32_t *vars, int varCount) +{ + kon_script_t zero = {0}; + + *vm = zero; + vm->cmds = cmds; + vm->count = cmds && count > 0 ? count : 0; + vm->switches = switches; + vm->switchCount = switches && switchCount > 0 ? switchCount : 0; + vm->vars = vars; + vm->varCount = vars && varCount > 0 ? varCount : 0; + vm->status = KON_SCRIPT_DONE; +} + +void kon_scriptStart(kon_script_t *vm, int where) +{ + vm->pc = where; + vm->depth = 0; + vm->optionCount = 0; + vm->wait = 0.0f; + vm->status = where >= 0 && where < vm->count ? KON_SCRIPT_RUNNING : KON_SCRIPT_ERROR; +} + +static int menuOptions(kon_script_t *vm, const kon_cmd_t *menu) +{ + int i, n = menu->a; + + vm->optionCount = 0; + if (n > KON_SCRIPT_MAX_OPTIONS) n = KON_SCRIPT_MAX_OPTIONS; + for (i = 0; i < n; i++) { + int at = vm->pc + 1 + i; + + if (at >= vm->count || vm->cmds[at].op != KON_OP_OPTION) return 0; + if (vm->cmds[at].b >= 0 && !kon_scriptSwitch(vm, vm->cmds[at].b)) continue; + vm->options[vm->optionCount++] = at; + } + return 1; +} + +kon_scriptStatus_t kon_scriptRun(kon_script_t *vm) +{ + int steps = 0; + + while (vm->status == KON_SCRIPT_RUNNING) { + const kon_cmd_t *c; + int next; + + if (vm->pc < 0 || vm->pc >= vm->count || ++steps > 100000) { + vm->status = KON_SCRIPT_ERROR; + break; + } + c = &vm->cmds[vm->pc]; + next = vm->pc + 1; + vm->cmd = vm->pc; + + switch (c->op) { + case KON_OP_END: + vm->status = KON_SCRIPT_DONE; + break; + case KON_OP_SAY: + vm->status = KON_SCRIPT_SAY; + break; + case KON_OP_MENU: + if (!menuOptions(vm, c)) { + vm->status = KON_SCRIPT_ERROR; + break; + } + if (vm->optionCount == 0) { + next = vm->pc + 1 + (c->a > 0 ? c->a : 0); + break; + } + vm->status = KON_SCRIPT_MENU; + break; + case KON_OP_OPTION: + break; + case KON_OP_JUMP: + next = c->a; + break; + case KON_OP_SWITCH: + kon_scriptSetSwitch(vm, c->a, c->b == 2 ? !kon_scriptSwitch(vm, c->a) : c->b); + break; + case KON_OP_VAR: + kon_scriptSetVar(vm, c->a, arithmetic(kon_scriptVar(vm, c->a), c->b, operand(vm, c->c))); + break; + case KON_OP_IFSWITCH: + if (kon_scriptSwitch(vm, c->a) != (c->b ? 1 : 0)) next = c->c; + break; + case KON_OP_IFVAR: + if (!compare(kon_scriptVar(vm, c->a), c->b, operand(vm, c->c))) next = c->d; + break; + case KON_OP_WAIT: + if (c->a > 0) { + vm->wait = (float)c->a * 0.001f; + vm->status = KON_SCRIPT_WAIT; + } + break; + case KON_OP_HOST: + if (vm->host && vm->host(vm, c, vm->user)) vm->status = KON_SCRIPT_HOST; + break; + case KON_OP_CALL: + if (vm->depth >= KON_SCRIPT_MAX_CALLS) { + vm->status = KON_SCRIPT_ERROR; + break; + } + vm->stack[vm->depth++] = next; + next = c->a; + break; + case KON_OP_RETURN: + if (vm->depth == 0) vm->status = KON_SCRIPT_DONE; + else next = vm->stack[--vm->depth]; + break; + default: + vm->status = KON_SCRIPT_ERROR; + break; + } + vm->pc = next; + } + return vm->status; +} + +void kon_scriptUpdate(kon_script_t *vm, float dt) +{ + if (vm->status != KON_SCRIPT_WAIT || !(dt > 0.0f)) return; + vm->wait -= dt; + if (vm->wait <= 0.0f) { + vm->wait = 0.0f; + vm->status = KON_SCRIPT_RUNNING; + } +} + +void kon_scriptResume(kon_script_t *vm) +{ + if (vm->status == KON_SCRIPT_SAY || vm->status == KON_SCRIPT_HOST) vm->status = KON_SCRIPT_RUNNING; +} + +const kon_cmd_t *kon_scriptCurrent(const kon_script_t *vm) +{ + return vm->status == KON_SCRIPT_SAY || vm->status == KON_SCRIPT_HOST ? &vm->cmds[vm->cmd] : NULL; +} + +int kon_scriptOptionCount(const kon_script_t *vm) +{ + return vm->status == KON_SCRIPT_MENU ? vm->optionCount : 0; +} + +const char *kon_scriptOption(const kon_script_t *vm, int i) +{ + if (vm->status != KON_SCRIPT_MENU || i < 0 || i >= vm->optionCount) return NULL; + return vm->cmds[vm->options[i]].text; +} + +void kon_scriptChoose(kon_script_t *vm, int i) +{ + if (vm->status != KON_SCRIPT_MENU || i < 0 || i >= vm->optionCount) return; + vm->pc = vm->cmds[vm->options[i]].a; + vm->optionCount = 0; + vm->status = KON_SCRIPT_RUNNING; +} diff --git a/kon/konscript.h b/kon/konscript.h new file mode 100644 index 0000000..8b5a024 --- /dev/null +++ b/kon/konscript.h @@ -0,0 +1,134 @@ +/* SPDX-License-Identifier: MIT */ +/* + * konscript.h + * + * Copyright (c) 2026 KonAki + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef KONSCRIPT_H +#define KONSCRIPT_H + +#include <stddef.h> +#include <stdint.h> + +/*** definition ***/ + +/* a script is a flat list of commands and jumps go to a place in the list. no nesting, so a converter can write one +(or write it as C) without a parser, and a person can write one by hand. the program does the talking and the +showing: say, menus and anything else the script cannot know about are handed over and the script waits */ + +typedef enum { + KON_OP_END, + KON_OP_SAY, /* a: who (your own number, -1 for nobody), text */ + KON_OP_MENU, /* a: how many OPTION commands follow */ + KON_OP_OPTION, /* a: where to go, b: switch that must be on to show it (-1 always), text */ + KON_OP_JUMP, /* a: where */ + KON_OP_SWITCH, /* a: switch, b: 0 off, 1 on, 2 flip */ + KON_OP_VAR, /* a: variable, b: KON_SET.., c: number, or KON_VARIABLE + a variable */ + KON_OP_IFSWITCH, /* a: switch, b: wanted 0 or 1, c: where to go if it is not */ + KON_OP_IFVAR, /* a: variable, b: KON_IS.., c: number or KON_VARIABLE + a variable, d: where to go if not */ + KON_OP_WAIT, /* a: milliseconds */ + KON_OP_HOST, /* a: your own command number, b, c, d, text: yours to use */ + KON_OP_CALL, /* a: where, comes back at the RETURN */ + KON_OP_RETURN +} kon_op_t; + +enum { KON_SET, KON_ADD, KON_SUB, KON_MUL, KON_DIV, KON_MOD }; +enum { KON_IS_EQ, KON_IS_NE, KON_IS_GE, KON_IS_LE, KON_IS_GT, KON_IS_LT }; + +/* add a variable number to this to use that variable where a command wants a number */ +#define KON_VARIABLE 0x40000000 + +typedef struct kon_cmd { + int op, a, b, c, d; + const char *text; +} kon_cmd_t; + +/* so a script written by hand or by a converter reads as a list */ +#define KON_END() {KON_OP_END, 0, 0, 0, 0, NULL} +#define KON_SAY(who, text) {KON_OP_SAY, who, 0, 0, 0, text} +#define KON_MENU(count) {KON_OP_MENU, count, 0, 0, 0, NULL} +#define KON_OPTION(text, where, needs) {KON_OP_OPTION, where, needs, 0, 0, text} +#define KON_JUMP(where) {KON_OP_JUMP, where, 0, 0, 0, NULL} +#define KON_SWITCH(id, how) {KON_OP_SWITCH, id, how, 0, 0, NULL} +#define KON_VAR(id, how, n) {KON_OP_VAR, id, how, n, 0, NULL} +#define KON_IFSWITCH(id, want, elseWhere) {KON_OP_IFSWITCH, id, want, elseWhere, 0, NULL} +#define KON_IFVAR(id, how, n, elseWhere) {KON_OP_IFVAR, id, how, n, elseWhere, NULL} +#define KON_WAIT(ms) {KON_OP_WAIT, ms, 0, 0, 0, NULL} +#define KON_HOST(id, b, c, d, text) {KON_OP_HOST, id, b, c, d, text} +#define KON_CALL(where) {KON_OP_CALL, where, 0, 0, 0, NULL} +#define KON_RETURN() {KON_OP_RETURN, 0, 0, 0, 0, NULL} + +typedef enum { + KON_SCRIPT_DONE, + KON_SCRIPT_RUNNING, /* started, kon_scriptRun has not stopped it yet */ + KON_SCRIPT_SAY, /* show kon_scriptText, then kon_scriptResume */ + KON_SCRIPT_MENU, /* show the options, then kon_scriptChoose */ + KON_SCRIPT_WAIT, /* kon_scriptUpdate counts it down */ + KON_SCRIPT_HOST, /* the host function wanted to wait, kon_scriptResume when it is over */ + KON_SCRIPT_ERROR /* bad jump, calls too deep, or going round and round without stopping */ +} kon_scriptStatus_t; + +#define KON_SCRIPT_MAX_OPTIONS 16 +#define KON_SCRIPT_MAX_CALLS 8 + +struct kon_script; +typedef int (*kon_hostFn)(struct kon_script *vm, const kon_cmd_t *cmd, void *user); + +typedef struct kon_script { + const kon_cmd_t *cmds; + int count; + uint8_t *switches; + int switchCount; + int32_t *vars; + int varCount; + kon_hostFn host; /* returns nonzero to make the script wait for kon_scriptResume */ + void *user; + kon_scriptStatus_t status; + int pc; + int cmd; /* the command that is being waited on */ + float wait; + int stack[KON_SCRIPT_MAX_CALLS], depth; + int options[KON_SCRIPT_MAX_OPTIONS], optionCount; +} kon_script_t; + +/*** script ***/ + +/* the switches and variables are yours, they start however you left them, and a number past the end reads as 0 and is not written */ +void kon_scriptInit(kon_script_t *vm, const kon_cmd_t *cmds, int count, uint8_t *switches, int switchCount, int32_t *vars, int varCount); +void kon_scriptStart(kon_script_t *vm, int where); + +/* goes until it has to wait for something and says what, call it each frame (it does nothing while waiting) */ +kon_scriptStatus_t kon_scriptRun(kon_script_t *vm); +void kon_scriptUpdate(kon_script_t *vm, float dt); +void kon_scriptResume(kon_script_t *vm); + +const kon_cmd_t *kon_scriptCurrent(const kon_script_t *vm); +int kon_scriptOptionCount(const kon_script_t *vm); +const char *kon_scriptOption(const kon_script_t *vm, int i); +void kon_scriptChoose(kon_script_t *vm, int i); + +int kon_scriptSwitch(const kon_script_t *vm, int id); +int32_t kon_scriptVar(const kon_script_t *vm, int id); +void kon_scriptSetSwitch(kon_script_t *vm, int id, int on); +void kon_scriptSetVar(kon_script_t *vm, int id, int32_t value); + +#endif /* KONSCRIPT_H */ |
