-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamecodemanager.c
More file actions
336 lines (293 loc) · 9.95 KB
/
gamecodemanager.c
File metadata and controls
336 lines (293 loc) · 9.95 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
//global includes
#include <GL/glew.h> //may be unneeded
#include <GL/gl.h>
//local includes
#include "globaldefs.h"
#include "matrixlib.h"
#include "texturemanager.h"
#include "vbomanager.h"
#include "modelmanager.h"
#include "shadermanager.h"
#include "physics.h"
#include "entitymanager.h"
#include "worldmanager.h"
#include "viewportmanager.h"
#include "lightmanager.h"
#include "gamecodemanager.h"
#include "mathlib.h"
#include "console.h"
#include "cvarmanager.h"
#include "filemanager.h"
#include "stringlib.h"
#include "gamecodeincludes.h"
#include <dlfcn.h> //todo move to sys
int gamecodeOK;
int gamecode_tGameTime = 0;
gcallheader_t *gc = 0;
static void * game_lib;
ecallheader_t ec;
void sys_unloadGameAPI(void){ // todo move to sys
if(game_lib)dlclose(game_lib);
game_lib = 0;
}
gcallheader_t * sys_getGameAPI(ecallheader_t *ec){ //todo move to sys
if(game_lib){ console_printf("Error: game DLL still open, cant reload\n"); return FALSE;}
//todo search for it, cvar it, etc
game_lib = dlopen("./gamecode.so", RTLD_NOW);
if(!game_lib){ console_printf("Error opening gamecode.so, file not found\n"); return FALSE;}
void *(*dllSetupCallbacks) (void*);
dllSetupCallbacks = (void *) dlsym (game_lib, "setupGameCodeCallbacks");
if(!dllSetupCallbacks){
console_printf("Error opening gamecode.so\n");
sys_unloadGameAPI();
return FALSE;
}
return dllSetupCallbacks(ec);
}
int setupGameCodeCallbacks(void){
ec.console_printf = console_printf;
ec.console_nprintf = console_nprintf;
ec.cvar_register = cvar_register;
ec.cvar_unregister = cvar_unregister;
ec.cvar_nameset = cvar_nameset;
ec.cvar_pset = cvar_pset;
ec.cvar_idset = cvar_idset;
ec.cvar_returnById = cvar_returnById;
ec.cvar_findByNameRPOINT = cvar_findByNameRPOINT;
ec.cvar_findByNameRINT = cvar_findByNameRINT;
ec.entity_findByNameRPOINT = entity_findByNameRPOINT;
ec.entity_findByNameRINT = entity_findByNameRINT;
ec.entity_findAllByNameRPOINT = entity_findAllByNameRPOINT;
ec.entity_findAllByNameRINT = entity_findAllByNameRINT;
ec.entity_returnById = entity_returnById;
ec.entity_addRPOINT = entity_addRPOINT;
ec.entity_addRINT = entity_addRINT;
ec.entity_delete = entity_delete;
ec.file_loadString = file_loadString;
ec.file_loadStringNoLength = file_loadStringNoLength;
ec.light_addRINT = light_addRINT;
ec.light_addRPOINT = light_addRPOINT;
ec.shader_createAndAddRINT = shader_createAndAddRINT;
ec.string_toVec = string_toVec;
ec.texture_createAndAddGroupRINT = texture_createAndAddGroupRINT;
ec.model_createAndAddRINT = model_createAndAddRINT;
gc = sys_getGameAPI(&ec);
if(!gc){
console_printf("Error: could not load game code\n");
return FALSE;
}
//todo implement a new api checking formula, maybe ranges of compatible
if(gc->apiver != GAMECODEINCLUDEVERSION){
console_printf("Error: Gamecode version is %i, engine version is %i, not compatible\n", gc->apiver, GAMECODEINCLUDEVERSION);
return FALSE;
}
if(!gc->initgame){
console_printf("Error: Gamecode does not have an initgame function\n");
return FALSE;
}
//todo
return TRUE;
}
extern int calcEntAttachMat(entity_t *e);
extern int recalcEntBBox(entity_t *e);
int initGameCodeSystem(void){
entity_init();
#ifdef ODE_COMPILE
physics_init();
#endif
if(!entity_ok){
gamecodeOK = FALSE;
return FALSE;
}
if(!setupGameCodeCallbacks()){
gamecodeOK = FALSE;
return FALSE; //todo something
}
loadWorld("world");
loadWorld("world2");
gc->initgame();
entity_pruneList();
gamecodeOK = TRUE;
return TRUE; // todo error check
}
int recalcEntBBox(entity_t * e){
model_t * m = model_returnById(e->modelid);
if(!m) return FALSE;
int i;
e->bbox[0] = -3.4028e+38;
e->bbox[1] = 3.4028e+38;
e->bbox[2] = -3.4028e+38;
e->bbox[3] = 3.4028e+38;
e->bbox[4] = -3.4028e+38;
e->bbox[5] = 3.4028e+38;
for(i = 0; i < 8; i++){
int oneplace = i*3;
Matrix4x4_Transformsimdu(&e->mat, &m->bboxp[oneplace], &e->bboxp[oneplace]);
if(e->bboxp[oneplace] > e->bbox[0]) e->bbox[0] = e->bboxp[oneplace];
else if(e->bboxp[oneplace] < e->bbox[1]) e->bbox[1] = e->bboxp[oneplace];
if(e->bboxp[oneplace+1] > e->bbox[2]) e->bbox[2] = e->bboxp[oneplace+1];
else if(e->bboxp[oneplace+1] < e->bbox[3]) e->bbox[3] = e->bboxp[oneplace+1];
if(e->bboxp[oneplace+2] > e->bbox[4]) e->bbox[4] = e->bboxp[oneplace+2];
else if(e->bboxp[oneplace+2] < e->bbox[5]) e->bbox[5] = e->bboxp[oneplace+2];
}
e->needsbboxupdate = FALSE;
if(!e->leaf) addEntityToWorld(e);
else moveEntInWorld(e);
return TRUE;
}
int calcEntAttachMat(entity_t * e){ //return value is weather e->mat got changed
if(!e->type) return FALSE;
if(e->attachmentid){
entity_t * attacher = entity_returnById(e->attachmentid);
if(!attacher){
e->attachmentid = 0;
}
else if (calcEntAttachMat(attacher)){ //dat recursion
matrix4x4_t tempmat;
// Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale);
// Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale/attacher->scale);
Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0]/attacher->scale, e->pos[1]/attacher->scale, e->pos[2]/attacher->scale, e->angle[0], e->angle[1], e->angle[2], e->scale/attacher->scale);
Matrix4x4_Concatsimdu(&e->mat, &attacher->mat, &tempmat);
e->needsmatupdate = 2;
return TRUE;
}
//todo figure this out...
else if (e->needsmatupdate & 1){
// else if (TRUE){
e->needsmatupdate = 2;
if(attacher){
matrix4x4_t tempmat;
// Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale);
// Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale/attacher->scale);
Matrix4x4_CreateFromQuakeEntity(&tempmat, e->pos[0]/attacher->scale, e->pos[1]/attacher->scale, e->pos[2]/attacher->scale, e->angle[0], e->angle[1], e->angle[2], e->scale/attacher->scale);
Matrix4x4_Concatsimdu(&e->mat, &attacher->mat, &tempmat);
return TRUE;
} else {
Matrix4x4_CreateFromQuakeEntity(&e->mat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale);
return TRUE;
}
}
else if (e->needsmatupdate) return TRUE;
//else implied
return FALSE;
} else if (e->needsmatupdate & 1) {
Matrix4x4_CreateFromQuakeEntity(&e->mat, e->pos[0], e->pos[1], e->pos[2], e->angle[0], e->angle[1], e->angle[2], e->scale);
e->needsmatupdate = 2;
return TRUE;
} else if(e->needsmatupdate) return TRUE;
return FALSE;
}
void entityCollideBBoxL(entity_t * e, worldleaf_t *l){
int * elist = l->entlist;
int max = l->entityarraylasttaken+1;
int i;
for(i = 0; i < max; i++){
int cei = elist[i];
if(!cei) continue;
entity_t * ce = entity_returnById(cei);
//check if they collide
if(testBBoxInBBox(e->bbox, ce->bbox)){
//run collide function
e->touch(e, cei);
// e->touch(e, ce);
}
}
worldleaf_t ** children = l->children;
int j;
for(j = 0; j < 4; j++){
if(children[j] && (children[j]->includes & WORLDTREEENTITY) && testBBoxInBBox(e->bbox, children[j]->bbox))
entityCollideBBoxL(e, children[j]);
}
}
//same as entityCollideBBoxL, but it checks to make sure it doesnt collide against itself
void entityCollideBBoxSL(entity_t * e, worldleaf_t *l){
int myid = e->myid;
int * elist = l->entlist;
int max = l->entityarraylasttaken+1;
int i;
for(i = 0; i < max; i++){
int cei = elist[i];
if(!cei || cei == myid) continue;
entity_t * ce = entity_returnById(cei);
//check if they collide
if(testBBoxInBBox(e->bbox, ce->bbox)){
//run collide function
e->touch(e, cei);
// e->touch(e, ce);
}
}
worldleaf_t ** children = l->children;
int j;
for(j = 0; j < 4; j++){
if(children[j] && (children[j]->includes & WORLDTREEENTITY) && testBBoxInBBox(e->bbox, children[j]->bbox))
entityCollideBBoxL(e, children[j]);
}
}
void entityCollideBBox(entity_t *e){
//caller of this function should make sure e has a ontouch func defined
worldleaf_t * l = e->leaf;
worldleaf_t * upleaf;
//entity will always collide with all its parent leafs, so test them all
for(upleaf = l->parent; upleaf; upleaf = upleaf->parent){
if(!(upleaf->myincludes & WORLDTREEENTITY))continue; // may be unnecisary
int * elist = upleaf->entlist;
int max = upleaf->entityarraylasttaken+1;
int i;
for(i = 0; i < max; i++){
int cei = elist[i];
if(!cei) continue;
entity_t * ce = entity_returnById(cei);
//check if they collide
if(testBBoxInBBox(e->bbox, ce->bbox)){
//run collide function
e->touch(e, cei);
// e->touch(e, ce);
}
}
}
//now check children leafs
entityCollideBBoxSL(e, l);
}
void gameCodeTick(void){ //todo maybe change to float in seconds
gamecode_tGameTime+=GCTIMESTEP;
int i;
//ent phys
for(i = 0; i <= entity_arraylasttaken; i++){
entity_t * e = &entity_list[i];
if(!e->type) continue;
if(e->phys.movetype == ODEDYNAMIC){
#ifdef ODE_COMPILE
if(physics_getEntD(e)) e->needsmatupdate = TRUE;
#endif
} else {
if(e->vel[0] || e->vel[1] || e->vel[2]){
e->pos[0] += e->vel[0] * GCTIMESTEPSECONDS;
e->pos[1] += e->vel[1] * GCTIMESTEPSECONDS;
e->pos[2] += e->vel[2] * GCTIMESTEPSECONDS;
e->needsmatupdate = TRUE;
}
if(e->anglevel[0] || e->anglevel[1] || e->anglevel[2]){
e->angle[0] += e->anglevel[0] * GCTIMESTEPSECONDS;
e->angle[1] += e->anglevel[1] * GCTIMESTEPSECONDS;
e->angle[2] += e->anglevel[2] * GCTIMESTEPSECONDS;
e->needsmatupdate = TRUE;
}
}
}
for(i = 0; i <= entity_arraylasttaken; i++){
entity_t *e = &entity_list[i];
if(!e->type) continue;
calcEntAttachMat(e);
if(e->needsmatupdate || e->needsbboxupdate) recalcEntBBox(e);
}
//todo maybe convert to an entity "carry" system instead of a light attach system
lightLoop();
for(i = 0; i <= entity_arraylasttaken; i++){// make sure they dont update again
entity_list[i].needsmatupdate = FALSE;
entity_list[i].needsbboxupdate = FALSE;
}
// if(e->think && e->nextthink <= gamecode_tGameTime){
// e->think();
// }
//ent gamecode
}