Skip to content

Commit 2a3378c

Browse files
committed
Work on cutscene engine
1 parent 921627b commit 2a3378c

8 files changed

+248
-0
lines changed

assets/dialog/mentor/intro.script

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
global player_name: string[8]
3+
global intro_played: bool
4+
5+
if intro_played then
6+
exit
7+
end
8+
9+
intro_played = true
10+
11+
say "Good morining {player_name}! Today is the day you start your training. Meet me in the training area. Be quick!"
12+
13+
walk_to mentor, exit

scripting_brainstorm.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# features
2+
the ablity to embed variables into text
3+
global variables/local variables
4+
simple computations
5+
conditional branching/loops
6+
show dialog
7+
run arbitrary cutscene steps
8+
animation
9+
10+
## the ablity to embed variables into text
11+
12+
```
13+
dialog "A text block with a {variable} embedded"
14+
```
15+
16+
## global variables
17+
18+
```
19+
global variable: bool
20+
global variable: i32
21+
```
22+
23+
## local varaibles
24+
25+
```
26+
local variable: bool
27+
```
28+
29+
## setting variables
30+
31+
```
32+
variable = expression
33+
```
34+
35+
# simple computations
36+
37+
operators and, or, not, +, -, *, /, ==, !=, >, <, <=, >=
38+
39+
lisp? (and a b (or c d))
40+
infix? a and b and (c or d)
41+
42+
# conditional
43+
44+
```
45+
if condition then
46+
actions
47+
end
48+
```
49+
50+
# dialog
51+
52+
```
53+
say "A message of text"
54+
dialog "A message of text"
55+
56+
dialog """
57+
This is a how dialog that has mulitple lines will look
58+
59+
This should show up after pressing a
60+
"""
61+
62+
```
63+
64+
# cutscene steps
65+
66+
steps will look like function calls with comma separated values
67+
68+
```
69+
look_at player, active_npc
70+
run_animation intro
71+
walk_to active_npc, exit
72+
73+
```

src/cutscene/evaluation_context.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "evaluation_context.h"
2+
3+
#include <assert.h>
4+
5+
void evaluation_context_init(struct evaluation_context* context) {
6+
context->current_stack = 0;
7+
}
8+
9+
void evaluation_context_push(struct evaluation_context* context, int value) {
10+
assert(context->current_stack < MAX_STACK_SIZE);
11+
context->stack[context->current_stack] = value;
12+
++context->current_stack;
13+
}
14+
15+
int evaluation_context_pop(struct evaluation_context* context) {
16+
assert(context->current_stack > 0);
17+
--context->current_stack;
18+
return context->stack[context->current_stack];
19+
}
20+
21+
int evaluation_context_load(void* data, enum data_type data_type, int word_offset) {
22+
switch (data_type) {
23+
case DATA_TYPE_NULL:
24+
return 0;
25+
case DATA_TYPE_S8:
26+
return ((int8_t*)data)[word_offset];
27+
case DATA_TYPE_S16:
28+
return ((int16_t*)data)[word_offset];
29+
case DATA_TYPE_S32:
30+
case DATA_TYPE_F32:
31+
return ((int32_t*)data)[word_offset];
32+
case DATA_TYPE_BOOL: {
33+
uint32_t word = ((uint32_t*)data)[word_offset >> 5];
34+
return ((uint32_t)0x80000000 >> (word_offset & 0x1F)) != 0;
35+
}
36+
default:
37+
return 0;
38+
}
39+
}

src/cutscene/evaluation_context.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef __CUTSCENE_EVALUATION_CONTEXT_H__
2+
#define __CUTSCENE_EVALUATION_CONTEXT_H__
3+
4+
#define MAX_STACK_SIZE 128
5+
6+
#include <stdint.h>
7+
8+
enum data_type {
9+
DATA_TYPE_NULL,
10+
DATA_TYPE_S8,
11+
DATA_TYPE_S16,
12+
DATA_TYPE_S32,
13+
14+
DATA_TYPE_BOOL,
15+
16+
DATA_TYPE_F32,
17+
};
18+
19+
struct evaluation_context {
20+
int stack[MAX_STACK_SIZE];
21+
uint16_t current_stack;
22+
void* local_varaibles;
23+
void* global_variables;
24+
};
25+
26+
void evaluation_context_init(struct evaluation_context* context);
27+
28+
void evaluation_context_push(struct evaluation_context* context, int value);
29+
int evaluation_context_pop(struct evaluation_context* context);
30+
31+
int evaluation_context_load(void* data, enum data_type data_type, int word_offset);
32+
33+
#endif

src/cutscene/expression.c

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "expression.h"

src/cutscene/expression.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef __CUTSCENE_EXPRESSION_H__
2+
#define __CUTSCENE_EXPRESSION_H__
3+
4+
#include <stdint.h>
5+
6+
enum expression_type {
7+
EXPRESSION_TYPE_END,
8+
EXPRESSION_TYPE_LOAD_LOCAL,
9+
EXPRESSION_TYPE_LOAD_GLOBAL,
10+
EXPRESSION_TYPE_LOAD_LITERAL,
11+
EXPRESSION_TYPE_AND,
12+
EXPRESSION_TYPE_OR,
13+
EXPRESSION_TYPE_NOT,
14+
EXPRESSION_TYPE_ADD,
15+
};
16+
17+
union expression_data {
18+
struct {
19+
uint16_t data_type;
20+
uint16_t word_offset;
21+
} load_variable;
22+
int literal;
23+
};
24+
25+
struct expression {
26+
uint8_t* steps;
27+
union expression_data* step_data;
28+
};
29+
30+
#endif

src/cutscene/expression_evaluate.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "expression_evaluate.h"
2+
3+
void expression_evaluate(struct evaluation_context* context, struct expression* expression) {
4+
uint8_t* current = expression->steps;
5+
union expression_data* data = expression->step_data;
6+
7+
while (*current != EXPRESSION_TYPE_END) {
8+
switch (*current) {
9+
case EXPRESSION_TYPE_LOAD_LOCAL:
10+
evaluation_context_push(context, evaluation_context_load(context->local_varaibles, data->load_variable.data_type, data->load_variable.word_offset));
11+
++data;
12+
break;
13+
case EXPRESSION_TYPE_LOAD_GLOBAL:
14+
evaluation_context_push(context, evaluation_context_load(context->global_variables, data->load_variable.data_type, data->load_variable.word_offset));
15+
++data;
16+
break;
17+
case EXPRESSION_TYPE_LOAD_LITERAL:
18+
evaluation_context_push(context, data->literal);
19+
++data;
20+
break;
21+
case EXPRESSION_TYPE_AND:
22+
evaluation_context_push(
23+
context,
24+
evaluation_context_pop(context) && evaluation_context_pop(context)
25+
);
26+
break;
27+
case EXPRESSION_TYPE_OR:
28+
evaluation_context_push(
29+
context,
30+
evaluation_context_pop(context) || evaluation_context_pop(context)
31+
);
32+
break;
33+
case EXPRESSION_TYPE_NOT:
34+
evaluation_context_push(
35+
context,
36+
!evaluation_context_pop(context)
37+
);
38+
break;
39+
case EXPRESSION_TYPE_ADD:
40+
evaluation_context_push(
41+
context,
42+
evaluation_context_pop(context) + evaluation_context_pop(context)
43+
);
44+
break;
45+
}
46+
47+
++current;
48+
}
49+
}

src/cutscene/expression_evaluate.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __CUTSCENE_EXPRESSION_EVALUATE_H__
2+
#define __CUTSCENE_EXPRESSION_EVALUATE_H__
3+
4+
#include "expression.h"
5+
#include "evaluation_context.h"
6+
7+
// the evaluation result is left on the top of th context stack
8+
void expression_evaluate(struct evaluation_context* context, struct expression* expression);
9+
10+
#endif

0 commit comments

Comments
 (0)