This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathgameflow.cpp
More file actions
372 lines (299 loc) · 10.8 KB
/
gameflow.cpp
File metadata and controls
372 lines (299 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "core/lua.h"
#include "core/gl_text.h"
#include "core/console.h"
#include "script/script.h"
#include "gui/gui.h"
#include "audio/audio.h"
#include "engine.h"
#include "gameflow.h"
#include "game.h"
#include "world.h"
#include <assert.h>
#include <string.h>
typedef struct gameflow_action_s
{
int16_t opcode;
uint16_t operand;
struct gameflow_action_s *next;
struct gameflow_action_s *prev;
} gameflow_action_t;
struct gameflow_s
{
int m_currentGameID;
int m_currentLevelID;
int m_nextGameID;
int m_nextLevelID;
char m_currentLevelName[LEVEL_NAME_MAX_LEN];
char m_currentLevelPath[MAX_ENGINE_PATH];
char m_secretsTriggerMap[GF_MAX_SECRETS];
gameflow_action_t *head;
gameflow_action_t *tail;
gameflow_action_t *vacate;
} global_gameflow;
typedef struct level_info_s
{
int num_levels = 0;
char name[LEVEL_NAME_MAX_LEN];
char path[MAX_ENGINE_PATH];
char pic[MAX_ENGINE_PATH];
}level_info_t, *level_info_p;
bool Gameflow_GetLevelInfo(level_info_p info, int game_id, int level_id);
bool Gameflow_GetFMVPath(level_info_p info, int fmv_id);
bool Gameflow_SetGameInternal(int game_id, int level_id);
void Gameflow_Init()
{
global_gameflow.m_nextGameID = -1;
global_gameflow.m_nextLevelID = -1;
memset(global_gameflow.m_currentLevelName, 0, sizeof(global_gameflow.m_currentLevelName));
memset(global_gameflow.m_currentLevelPath, 0, sizeof(global_gameflow.m_currentLevelPath));
memset(global_gameflow.m_secretsTriggerMap, 0, sizeof(global_gameflow.m_secretsTriggerMap));
global_gameflow.head = (gameflow_action_t*)malloc(sizeof(gameflow_action_t));
global_gameflow.head->opcode = GF_FREE_LIST;
global_gameflow.head->operand = 0;
global_gameflow.head->next = NULL;
global_gameflow.head->prev = NULL;
global_gameflow.tail = global_gameflow.vacate = global_gameflow.head;
}
void Gameflow_Destroy()
{
global_gameflow.vacate = NULL;
global_gameflow.tail = NULL;
while(global_gameflow.head)
{
gameflow_action_t *next = global_gameflow.head->next;
free(global_gameflow.head);
global_gameflow.head = next;
}
}
bool Gameflow_Send(int opcode, int operand)
{
if(global_gameflow.vacate)
{
global_gameflow.vacate->opcode = opcode;
global_gameflow.vacate->operand = operand;
if(!global_gameflow.vacate->next)
{
global_gameflow.vacate->next = (gameflow_action_t*)malloc(sizeof(gameflow_action_t));
global_gameflow.vacate->next->opcode = GF_FREE_LIST;
global_gameflow.vacate->next->operand = 0;
global_gameflow.vacate->next->next = NULL;
global_gameflow.vacate->next->prev = global_gameflow.vacate->next;
}
global_gameflow.vacate = global_gameflow.vacate->next;
return true;
}
return false;
}
void Gameflow_ProcessCommands()
{
level_info_t info;
while(!Engine_IsVideoPlayed() && (global_gameflow.head->opcode != GF_FREE_LIST))
{
gameflow_action_t *processed = global_gameflow.head;
int16_t opcode = processed->opcode;
uint16_t operand = processed->operand;
processed->opcode = GF_FREE_LIST;
if(processed->next)
{
global_gameflow.head = processed->next;
global_gameflow.head->prev = NULL;
processed->next = NULL;
processed->prev = global_gameflow.tail;
global_gameflow.tail->next = processed;
global_gameflow.tail = processed;
}
switch(opcode)
{
case GF_OP_LEVELCOMPLETE:
if(World_GetPlayer())
{
luaL_dostring(engine_lua, "saved_inventory = getItems(player);");
}
if(Gameflow_SetGameInternal(global_gameflow.m_currentGameID, global_gameflow.m_currentLevelID + 1) && World_GetPlayer())
{
luaL_dostring(engine_lua, "if(saved_inventory ~= nil) then\n"
"removeAllItems(player);\n"
"for k, v in pairs(saved_inventory) do\n"
"addItem(player, k, v);\n"
"end;\n"
"saved_inventory = nil;\n"
"end;");
}
break;
case GF_OP_SETTRACK:
Audio_StreamPlay(operand);
break;
case GF_OP_STARTFMV:
if(Gameflow_GetFMVPath(&info, operand))
{
Engine_PlayVideo(info.path);
}
break;
case GF_NOENTRY:
continue;
default:
//Con_Printf("Unimplemented gameflow opcode: %i", global_gameflow.m_actions[i].m_opcode);
break;
}; // end switch(gameflow_manager.Operand)
}
if(global_gameflow.m_nextGameID >= 0)
{
Gameflow_SetGameInternal(global_gameflow.m_nextGameID, global_gameflow.m_nextLevelID);
global_gameflow.m_nextGameID = -1;
global_gameflow.m_nextLevelID = -1;
}
}
bool Gameflow_SetMap(const char* filePath, int game_id, int level_id)
{
level_info_t info;
if(Gameflow_GetLevelInfo(&info, game_id, level_id))
{
level_id = (level_id <= info.num_levels) ? (level_id) : (1);
if(!Gui_LoadScreenAssignPic(info.pic))
{
Gui_LoadScreenAssignPic("resource/graphics/legal");
}
}
strncpy(global_gameflow.m_currentLevelPath, filePath, MAX_ENGINE_PATH);
global_gameflow.m_currentGameID = game_id;
global_gameflow.m_currentLevelID = level_id;
return Engine_LoadMap(filePath);
}
bool Gameflow_SetGame(int game_id, int level_id)
{
global_gameflow.m_nextGameID = game_id;
global_gameflow.m_nextLevelID = level_id;
return true;
}
bool Gameflow_GetLevelInfo(level_info_p info, int game_id, int level_id)
{
int top = lua_gettop(engine_lua);
lua_getglobal(engine_lua, "gameflow_params");
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_rawgeti(engine_lua, -1, game_id);
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_getfield(engine_lua, -1, "title");
strncpy(info->pic, lua_tostring(engine_lua, -1), MAX_ENGINE_PATH);
lua_pop(engine_lua, 1);
lua_getfield(engine_lua, -1, "numlevels");
info->num_levels = lua_tointeger(engine_lua, -1);
lua_pop(engine_lua, 1);
lua_getfield(engine_lua, -1, "levels");
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
level_id = (level_id <= info->num_levels) ? (level_id) : (1);
lua_rawgeti(engine_lua, -1, level_id);
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_getfield(engine_lua, -1, "name");
strncpy(info->name, lua_tostring(engine_lua, -1), LEVEL_NAME_MAX_LEN);
lua_pop(engine_lua, 1);
lua_getfield(engine_lua, -1, "filepath");
strncpy(info->path, lua_tostring(engine_lua, -1), MAX_ENGINE_PATH);
lua_pop(engine_lua, 1);
lua_getfield(engine_lua, -1, "picpath");
strncpy(info->pic, lua_tostring(engine_lua, -1), MAX_ENGINE_PATH);
lua_pop(engine_lua, 1);
lua_pop(engine_lua, 1); // level_id
lua_pop(engine_lua, 1); // levels
lua_pop(engine_lua, 1); // game_id
lua_settop(engine_lua, top);
return true;
}
bool Gameflow_GetFMVPath(level_info_p info, int fmv_id)
{
int top = lua_gettop(engine_lua);
lua_getglobal(engine_lua, "gameflow_params");
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_rawgeti(engine_lua, -1, global_gameflow.m_currentGameID);
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_getfield(engine_lua, -1, "fmv");
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_rawgeti(engine_lua, -1, fmv_id);
if(!lua_istable(engine_lua, -1))
{
lua_settop(engine_lua, top);
return false;
}
lua_getfield(engine_lua, -1, "name");
strncpy(info->name, lua_tostring(engine_lua, -1), LEVEL_NAME_MAX_LEN);
lua_pop(engine_lua, 1);
lua_getfield(engine_lua, -1, "filepath");
strncpy(info->path, lua_tostring(engine_lua, -1), MAX_ENGINE_PATH);
lua_pop(engine_lua, 1);
lua_pop(engine_lua, 1); // fmv_id
lua_pop(engine_lua, 1); // fmv
lua_pop(engine_lua, 1); // game_id
lua_settop(engine_lua, top);
return true;
}
bool Gameflow_SetGameInternal(int game_id, int level_id)
{
level_info_t info;
if(Gameflow_GetLevelInfo(&info, game_id, level_id))
{
level_id = (level_id <= info.num_levels) ? (level_id) : (1);
if(!Gui_LoadScreenAssignPic(info.pic))
{
Gui_LoadScreenAssignPic("resource/graphics/legal");
}
global_gameflow.m_currentGameID = game_id;
global_gameflow.m_currentLevelID = level_id;
strncpy(global_gameflow.m_currentLevelName, info.name, LEVEL_NAME_MAX_LEN);
strncpy(global_gameflow.m_currentLevelPath, info.path, MAX_ENGINE_PATH);
return Engine_LoadMap(info.path);
}
return false;
}
const char *Gameflow_GetCurrentLevelPathLocal()
{
return global_gameflow.m_currentLevelPath + strlen(Engine_GetBasePath());
}
uint8_t Gameflow_GetCurrentGameID()
{
return global_gameflow.m_currentGameID;
}
uint8_t Gameflow_GetCurrentLevelID()
{
return global_gameflow.m_currentLevelID;
}
void Gameflow_ResetSecrets()
{
memset(global_gameflow.m_secretsTriggerMap, 0, GF_MAX_SECRETS * sizeof(*global_gameflow.m_secretsTriggerMap));
}
void Gameflow_SetSecretStateAtIndex(int index, int value)
{
assert((index >= 0) && index <= (GF_MAX_SECRETS));
global_gameflow.m_secretsTriggerMap[index] = (char)value; ///@FIXME should not cast.
}
int Gameflow_GetSecretStateAtIndex(int index)
{
assert((index >= 0) && index <= (GF_MAX_SECRETS));
return global_gameflow.m_secretsTriggerMap[index];
}