/
enemy.c
616 lines (503 loc) · 17.8 KB
/
enemy.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* Open Surge Engine
* enemy.c - baddies
* Copyright (C) 2008-2010 Alexandre Martins <alemartf(at)gmail(dot)com>
* http://opensnc.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <math.h>
#include "../core/global.h"
#include "../core/util.h"
#include "../core/audio.h"
#include "../core/timer.h"
#include "../core/input.h"
#include "../core/logfile.h"
#include "../core/stringutil.h"
#include "../core/osspec.h"
#include "../core/nanocalcext.h"
#include "../core/video.h"
#include "../core/hashtable.h"
#include "../core/nanoparser/nanoparser.h"
#include "../scenes/level.h"
#include "actor.h"
#include "player.h"
#include "enemy.h"
#include "object_vm.h"
#include "object_compiler.h"
#include "item.h"
#include "brick.h"
/* private stuff */
#define MAX_OBJECTS 10240
#define MAX_CATEGORIES 10240
#define ROOT_CATEGORY category_table.category[0] /* all objects belong to the root category */
typedef parsetree_program_t objectcode_t;
HASHTABLE_GENERATE_CODE(objectcode_t);
typedef struct object_children_t object_children_t;
struct object_children_t {
char *name;
enemy_t *data;
object_children_t *next;
};
static object_children_t* object_children_new();
static object_children_t* object_children_delete(object_children_t* list);
static object_children_t* object_children_add(object_children_t* list, const char *name, enemy_t *data);
static object_children_t* object_children_remove(object_children_t* list, enemy_t *data);
static object_t* object_children_find(object_children_t* list, const char *name);
static void object_children_visitall(object_children_t* list, void *any_data, void (*fun)(enemy_t*,void*));
typedef struct { const char* name[MAX_OBJECTS]; int length; } object_name_data_t;
typedef struct { const char* category[MAX_CATEGORIES]; int length; } object_category_data_t;
static int object_name_table_cmp(const void *a, const void *b);
static int object_category_table_cmp(const void *a, const void *b);
static enemy_t* create_from_script(const char *object_name);
static int fill_object_names(const parsetree_statement_t *stmt, void *object_name_data);
static int fill_object_categories(const parsetree_statement_t *stmt, void *object_category_data);
static int fill_lookup_table(const parsetree_statement_t *stmt, void *lookup_table);
static int prepare_to_fill_object_categories(const parsetree_statement_t *stmt, void *object_category_data);
static int dirfill(const char *filename, void *param); /* file system callback */
static int is_hidden_object(const char *name);
static int category_exists(const char *category);
static parsetree_program_t *objects;
static object_name_data_t name_table;
static object_category_data_t category_table;
static hashtable_objectcode_t* lookup_table;
/* ------ public class methods ---------- */
/*
* objects_init()
* Initializes this module
*/
void objects_init()
{
const char *path = "objects/*.obj";
logfile_message("Loading objects scripts...");
objects = NULL;
/* reading the parse tree */
foreach_resource(path, dirfill, (void*)(&objects), TRUE);
/* creating the name table */
name_table.length = 0;
nanoparser_traverse_program_ex(objects, (void*)(&name_table), fill_object_names);
qsort(name_table.name, name_table.length, sizeof(name_table.name[0]), object_name_table_cmp);
/* creating the category table */
ROOT_CATEGORY = "*"; /* name of the root category */
category_table.length = 1; /* the length of the table is at least one, as it contains the root category */
nanoparser_traverse_program_ex(objects, (void*)(&category_table), prepare_to_fill_object_categories);
qsort(category_table.category, category_table.length, sizeof(category_table.category[0]), object_category_table_cmp);
/* creating a lookup table to find objects fast */
lookup_table = hashtable_objectcode_t_create(NULL);
nanoparser_traverse_program_ex(objects, (void*)lookup_table, fill_lookup_table);
/* done! */
logfile_message("All objects have been loaded!");
}
/*
* objects_release()
* Releases this module
*/
void objects_release()
{
lookup_table = hashtable_objectcode_t_destroy(lookup_table);
name_table.length = category_table.length = 0;
objects = nanoparser_deconstruct_tree(objects);
}
/*
* objects_get_list_of_names()
* Returns an array v[0..n-1] of available object names
*/
const char** objects_get_list_of_names(int *n)
{
*n = name_table.length;
return name_table.name;
}
/*
* objects_get_list_of_categories()
* Returns an array v[0..n-1] of available object categories
*/
const char** objects_get_list_of_categories(int *n)
{
*n = category_table.length;
return category_table.category;
}
/* ------ public instance methods ------- */
/*
* enemy_create()
* Creates a new enemy
*/
enemy_t *enemy_create(const char *name)
{
return create_from_script(name);
}
/*
* enemy_destroy()
* Destroys an enemy
*/
enemy_t *enemy_destroy(enemy_t *enemy)
{
object_children_t *it;
/* tell my children I died */
for(it=enemy->children; it; it=it->next)
it->data->parent = NULL;
/* destroy my children list */
object_children_delete(enemy->children);
/* tell my parent I died */
if(enemy->parent != NULL)
enemy_remove_child(enemy->parent, enemy);
/* destroy my virtual machine */
objectvm_destroy(enemy->vm);
/* destroy my categories */
if(enemy->category != NULL)
free(enemy->category);
/* destroy me */
actor_destroy(enemy->actor);
free(enemy->name);
free(enemy);
/* success */
return NULL;
}
/*
* enemy_update()
* Runs every cycle of the game to update an enemy
*/
void enemy_update(enemy_t *enemy, player_t **team, int team_size, brick_list_t *brick_list, item_list_t *item_list, enemy_list_t *object_list)
{
objectmachine_t *machine = *(objectvm_get_reference_to_current_state(enemy->vm));
if(enemy->state == ES_DEAD) return;
nanocalcext_set_target_object((object_t*)enemy, brick_list, item_list, object_list);
machine->update(machine, team, team_size, brick_list, item_list, object_list);
}
/*
* enemy_render()
* Renders an enemy
*/
void enemy_render(enemy_t *enemy, v2d_t camera_position)
{
objectmachine_t *machine = *(objectvm_get_reference_to_current_state(enemy->vm));
if(enemy->state == ES_DEAD) return;
if(!enemy->hide_unless_in_editor_mode || (enemy->hide_unless_in_editor_mode && level_editmode())) {
if(!enemy->detach_from_camera || (enemy->detach_from_camera && level_editmode()))
machine->render(machine, camera_position);
else
machine->render(machine, v2d_new(VIDEO_SCREEN_W/2, VIDEO_SCREEN_H/2));
}
}
/*
* enemy_get_parent()
* Finds the parent of this object
*/
enemy_t *enemy_get_parent(enemy_t *enemy)
{
return enemy->parent;
}
/*
* enemy_get_child()
* Finds a child of this object
*/
enemy_t *enemy_get_child(enemy_t *enemy, const char *child_name)
{
return object_children_find(enemy->children, child_name);
}
/*
* enemy_add_child()
* Adds a child to this object
*/
void enemy_add_child(enemy_t *enemy, const char *child_name, enemy_t *child)
{
enemy->children = object_children_add(enemy->children, child_name, child);
child->parent = enemy;
child->created_from_editor = FALSE;
}
/*
* enemy_remove_child()
* Removes a child from this object (the child is not deleted, though)
*/
void enemy_remove_child(enemy_t *enemy, enemy_t *child)
{
enemy->children = object_children_remove(enemy->children, child);
}
/*
* enemy_visit_children()
* Calls fun for each of the children of the object. any_data is anything you want.
*/
void enemy_visit_children(enemy_t *enemy, void *any_data, void (*fun)(enemy_t*,void*))
{
object_children_visitall(enemy->children, any_data, fun);
}
/*
* enemy_get_observed_player()
* returns the observed player
*/
player_t *enemy_get_observed_player(enemy_t *enemy)
{
return enemy->observed_player != NULL ? enemy->observed_player : level_player();
}
/*
* enemy_observe_player()
* observes a new player
*/
void enemy_observe_player(enemy_t *enemy, player_t *player)
{
enemy->observed_player = player;
}
/*
* enemy_observe_current_player()
* observes the current player
*/
void enemy_observe_current_player(enemy_t *enemy)
{
enemy->observed_player = level_player();
}
/*
* enemy_observe_active_player()
* observes the active player
*/
void enemy_observe_active_player(enemy_t *enemy)
{
enemy->observed_player = NULL;
}
/*
* enemy_belongs_to_category()
* checks if a given object belongs to a category
*/
int enemy_belongs_to_category(enemy_t *enemy, const char *category)
{
if(str_icmp(category, ROOT_CATEGORY) != 0) {
int i;
for(i=0; i<enemy->category_count; i++) {
if(str_icmp(enemy->category[i], category) == 0)
return TRUE;
}
return FALSE;
}
else
return TRUE;
}
/* ----------- private functions ----------- */
enemy_t* create_from_script(const char *object_name)
{
enemy_t* e = mallocx(sizeof *e);
objectcode_t *object_code;
/* setup the object */
e->name = str_dup(object_name);
e->annotation = "";
e->category = NULL;
e->category_count = 0;
e->state = ES_IDLE;
e->zindex = 0.5f;
e->actor = actor_create();
e->actor->input = input_create_computer();
actor_change_animation(e->actor, sprite_get_animation("SD_QUESTIONMARK", 0));
e->preserve = TRUE;
e->obstacle = FALSE;
e->obstacle_angle = 0;
e->always_active = FALSE;
e->hide_unless_in_editor_mode = FALSE;
e->detach_from_camera = FALSE;
e->vm = objectvm_create(e);
e->created_from_editor = TRUE;
e->parent = NULL;
e->children = object_children_new();
e->observed_player = NULL;
e->attached_to_player = FALSE;
e->attached_to_player_offset = v2d_new(0,0);
/* Let's compile the object */
object_code = hashtable_objectcode_t_find(lookup_table, object_name);
if(object_code != NULL)
objectcompiler_compile(e, object_code);
else
fatal_error("Can't spawn object '%s': it does not exist!", object_name);
/* success! */
return e;
}
int is_hidden_object(const char *name)
{
return name[0] == '.';
}
int category_exists(const char *category)
{
int i;
for(i=0; i<category_table.length; i++) {
if(str_icmp(category_table.category[i], category) == 0)
return TRUE;
}
return FALSE;
}
int fill_object_names(const parsetree_statement_t* stmt, void *object_name_data)
{
object_name_data_t *x = (object_name_data_t*)object_name_data;
const char *id = nanoparser_get_identifier(stmt);
const parsetree_parameter_t *param_list = nanoparser_get_parameter_list(stmt);
if(str_icmp(id, "object") == 0) {
const parsetree_parameter_t *p1 = nanoparser_get_nth_parameter(param_list, 1);
nanoparser_expect_string(p1, "Object script error: object name is expected");
if(x->length < MAX_OBJECTS) {
const char *name = nanoparser_get_string(p1);
if(!is_hidden_object(name))
(x->name)[ (x->length)++ ] = name;
}
else
fatal_error("Object script error: can't have more than %d objects", MAX_OBJECTS);
}
else
fatal_error("Object script error: unknown keyword '%s'\nin \"%s\" near line %d", id, nanoparser_get_file(stmt), nanoparser_get_line_number(stmt));
return 0;
}
int prepare_to_fill_object_categories(const parsetree_statement_t* stmt, void *object_category_data)
{
const char *id = nanoparser_get_identifier(stmt);
const parsetree_parameter_t *param_list = nanoparser_get_parameter_list(stmt);
if(str_icmp(id, "object") == 0) {
const parsetree_parameter_t *p1 = nanoparser_get_nth_parameter(param_list, 1);
const parsetree_parameter_t *p2 = nanoparser_get_nth_parameter(param_list, 2);
const char *name;
const parsetree_program_t *code;
nanoparser_expect_string(p1, "Object script error: object code is expected");
name = nanoparser_get_string(p1);
nanoparser_expect_program(p2, "Object script error: object code is expected");
code = nanoparser_get_program(p2);
if(!is_hidden_object(name))
nanoparser_traverse_program_ex(code, object_category_data, fill_object_categories);
}
return 0;
}
int fill_object_categories(const parsetree_statement_t* stmt, void *object_category_data)
{
object_category_data_t *x = (object_category_data_t*)object_category_data;
const char *id = nanoparser_get_identifier(stmt);
const parsetree_parameter_t *param_list = nanoparser_get_parameter_list(stmt);
if(str_icmp(id, "category") == 0) {
int n = nanoparser_get_number_of_parameters(param_list);
if(n > 0) {
if(x->length + n < MAX_CATEGORIES) {
int i;
for(i=1; i<=n; i++) {
const parsetree_parameter_t *param = nanoparser_get_nth_parameter(param_list, i);
const char *category;
nanoparser_expect_string(param, "Object script error: object category is expected");
category = nanoparser_get_string(param);
if(!category_exists(category))
(x->category)[ (x->length)++ ] = category;
}
}
else
fatal_error("Object script error: can't have more than %d categories\nin \"%s\" near line %d", MAX_CATEGORIES, nanoparser_get_file(stmt), nanoparser_get_line_number(stmt));
}
else
fatal_error("Object script error: empty 'category' field\nin \"%s\" near line %d", nanoparser_get_file(stmt), nanoparser_get_line_number(stmt));
}
return 0;
}
int fill_lookup_table(const parsetree_statement_t *stmt, void *lookup_table)
{
hashtable_objectcode_t* table = (hashtable_objectcode_t*)lookup_table;
const char *id = nanoparser_get_identifier(stmt);
const parsetree_parameter_t *param_list = nanoparser_get_parameter_list(stmt);
if(str_icmp(id, "object") == 0) {
const char *object_name;
const parsetree_program_t *object_code;
const parsetree_parameter_t *p1 = nanoparser_get_nth_parameter(param_list, 1);
const parsetree_parameter_t *p2 = nanoparser_get_nth_parameter(param_list, 2);
nanoparser_expect_string(p1, "Object script error: object name is expected (first parameter)");
nanoparser_expect_program(p2, "Object script error: object code is expected (second parameter)");
object_name = nanoparser_get_string(p1);
object_code = nanoparser_get_program(p2);
if(hashtable_objectcode_t_find(table, object_name) == NULL)
hashtable_objectcode_t_add(table, object_name, (objectcode_t*)object_code);
else
fatal_error("Object script error: duplicate definition of the object \"%s\"\nin \"%s\" near line %d", object_name, nanoparser_get_file(stmt), nanoparser_get_line_number(stmt));
}
return 0;
}
int object_name_table_cmp(const void *a, const void *b)
{
const char *i = *((const char**)a);
const char *j = *((const char**)b);
return str_icmp(i, j);
}
int object_category_table_cmp(const void *a, const void *b)
{
const char *i = *((const char**)a);
const char *j = *((const char**)b);
return str_icmp(i, j);
}
int dirfill(const char *filename, void *param)
{
parsetree_program_t** p = (parsetree_program_t**)param;
*p = nanoparser_append_program(*p, nanoparser_construct_tree(filename));
return 0;
}
/* ------------------------------------- */
object_children_t* object_children_new()
{
return NULL;
}
object_children_t* object_children_delete(object_children_t* list)
{
if(list != NULL) {
object_children_delete(list->next);
free(list->name);
free(list);
}
return NULL;
}
object_children_t* object_children_add(object_children_t* list, const char *name, enemy_t *data)
{
object_children_t *x = mallocx(sizeof *x);
x->name = str_dup(name);
x->data = data;
x->next = list;
return x;
}
object_t* object_children_find(object_children_t* list, const char *name)
{
object_children_t *it = list;
while(it != NULL) {
if(strcmp(it->name, name) == 0)
return it->data;
else
it = it->next;
}
return NULL;
}
object_children_t* object_children_remove(object_children_t* list, enemy_t *data)
{
object_children_t *it, *next;
if(list != NULL) {
if(list->data == data) {
next = list->next;
free(list->name);
free(list);
return next;
}
else {
it = list;
while(it->next != NULL && it->next->data != data)
it = it->next;
if(it->next != NULL) {
next = it->next->next;
free(it->next->name);
free(it->next);
it->next = next;
}
return list;
}
}
else
return NULL;
}
void object_children_visitall(object_children_t* list, void *any_data, void (*fun)(enemy_t*,void*))
{
object_children_t *it = list;
for(; it; it = it->next)
fun(it->data, any_data);
}