102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
interpreter.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include <math.h>
11#include <stdarg.h>
12#include <string.h>
13
14// Original pcdogs.exe: 0x40f910
15// Resolves entity-local, level, global, and timer references and returns their current value through the selected storage pointer. Reference 0xfc returns zero without setting that pointer; other special references may reuse its incoming value.
16int32_t Script_ResolveVariableRef(struct Entity *entity, int32_t **value_storage_inout, int32_t ref_id)
17{
18 // Actor-local variables: 9 vars at actor+0x24, indexed 0x00-0x08
19 if (ref_id < SCRIPT_LOCAL_VARIABLE_COUNT)
20 {
21 *value_storage_inout = (&entity->local_vars[ref_id]);
22
23 return entity->local_vars[ref_id];
24 }
25
26 // Level-global variables: 241 vars at g_currentLevel+0x18, indexed 0x09-0xF9
27 if (ref_id >= (int32_t)SCRIPT_VALUE_INPUT_BUTTON_P0)
28 {
29 // Variable index 0xFC: Null constant, always returns 0
30 if (ref_id == SCRIPT_VALUE_ZERO)
31 return 0;
32
33 if (ref_id == SCRIPT_VALUE_TIMER)
34 // Variable index 0xFD: Script timer/tick counter at actor+0x13C
35 *value_storage_inout = (&entity->script_timer);
36
37 return **value_storage_inout;
38 }
39
40 struct Level_Data *current_level = pkg_resource_current_level_data;
41 int32_t level_variable_count = (uint16_t)(current_level->sound_or_var_count);
42
43 if (ref_id - SCRIPT_LOCAL_VARIABLE_COUNT >= level_variable_count)
44 {
45 *value_storage_inout = (int32_t *)&Script_GlobalVariables[ref_id - SCRIPT_LOCAL_VARIABLE_COUNT];
46
48 }
49
50 int32_t *level_variable_storage = &current_level->var_list[ref_id - SCRIPT_LOCAL_VARIABLE_COUNT];
51
52 *value_storage_inout = (level_variable_storage);
53
54 return *level_variable_storage;
55}
56
57// Original pcdogs.exe: 0x40f980
58// Reads a level or global progress variable through the byte-sized reference identifier formed by adding nine to the index.
59int32_t Script_GetVariableById(uint32_t progress_var_slot)
60{
61 int32_t *value_storage = (int32_t *)(uintptr_t)progress_var_slot;
62
63 return Script_ResolveVariableRef(NULL, &value_storage,
64 (uint8_t)(progress_var_slot + SCRIPT_LOCAL_VARIABLE_COUNT));
65}
66
67// Original pcdogs.exe: 0x40f9a0
68// Resolves the byte-sized progress reference formed by adding nine to the index and writes the supplied value to its storage.
69void Script_SetVariableById(uint32_t progress_var_slot, int32_t progress_value)
70{
71 int32_t *resolved_value = (int32_t *)(uintptr_t)progress_var_slot;
72
73 Script_ResolveVariableRef(NULL, &resolved_value, (uint32_t)(uint8_t)(progress_var_slot + SCRIPT_LOCAL_VARIABLE_COUNT));
74 *resolved_value = progress_value;
75}
76
77// Original pcdogs.exe: 0x4404b0
78// Runs an entity's behavior bytecode from its header-selected entry until a handler clears the cursor, adjusting the script timer around execution. Opcode dispatch has no bounds or null-entry check, and a runtime actor is distinct from the entity that owns the script.
80{
81 g_scriptCurrentEntity = entity; // Set global executing actor context for all script commands
82
83 if (!entity)
84 return;
85
86 // Actor+0x48 (position_z) is reused as script bytecode pointer for scripted actors
87 uint8_t *script_base_ptr = entity->script_base_ptr;
88
89 if (!script_base_ptr)
90 return;
91
92 // Read 4-byte big-endian offset to script instruction pointer (IP)
93 uint8_t *next_opcode_cursor;
94 int32_t entry_offset = (uint32_t)(*(uint8_t *)script_base_ptr) << 24;
95
96 // Read 4-byte big-endian offset to initial instruction pointer
97 entry_offset |= (uint32_t)(script_base_ptr[2]) << 8;
98 entry_offset |= (uint32_t)(script_base_ptr[1]) << 16;
99 entry_offset |= script_base_ptr[3];
100 // Main bytecode execution loop: read opcode byte and dispatch
102 next_opcode_cursor = &script_base_ptr[entry_offset];
103
104 // Dispatch via g_scriptCommandTable[opcode] function pointer
105 uint32_t execution_tick = Timer_GetElapsedTickCountThunk();
106 uint8_t *opcode_cursor = next_opcode_cursor;
107
108 entity->script_timer = execution_tick - entity->script_timer;
109
110 do
111 {
112 int32_t opcode_index = 0;
113
114 // Script execution loop: read command byte (opcode) and dispatch
115 next_opcode_cursor = &opcode_cursor[1];
116 opcode_index = (uint8_t)(*(uint8_t *)opcode_cursor);
117 // Dispatch to command handler via g_scriptCommandTable[opcode]
118 Script_ActorOpcodeDispatchTable[opcode_index](entity, &next_opcode_cursor);
119 opcode_cursor = next_opcode_cursor;
120 } while (opcode_cursor);
121
122 entity->script_timer = execution_tick - entity->script_timer;
123}
void Script_SetVariableById(uint32_t progress_var_slot, int32_t progress_value)
Definition interpreter.c:69
int32_t Script_GetVariableById(uint32_t progress_var_slot)
Definition interpreter.c:59
int32_t Script_ResolveVariableRef(struct Entity *entity, int32_t **value_storage_inout, int32_t ref_id)
Definition interpreter.c:16
void Script_ExecuteBehaviorScript(struct Entity *entity)
Definition interpreter.c:79
#define pkg_resource_current_level_data
Definition globals.h:117
uint32_t Timer_GetElapsedTickCountThunk()
Definition timer.c:9
@ SCRIPT_VALUE_ZERO
Definition constants.h:23
@ SCRIPT_VALUE_TIMER
Definition constants.h:24
#define SCRIPT_LOCAL_VARIABLE_COUNT
Definition constants.h:16
@ SCRIPT_VALUE_INPUT_BUTTON_P0
Definition enums.h:338
#define Script_ActorOpcodeDispatchTable
Definition globals.h:20
#define g_scriptEntityIndex
Definition globals.h:35
#define g_scriptCurrentEntity
Definition globals.h:32
#define Script_GlobalVariables
Definition globals.h:39
Definition types.h:22
int32_t script_timer
Definition types.h:109
uint8_t behavior_index
Definition types.h:110
uint8_t * script_base_ptr
Definition types.h:45
int32_t local_vars[0x9]
Definition types.h:44
int32_t * var_list
Definition types.h:49
int16_t sound_or_var_count
Definition types.h:47