-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnpc.h
531 lines (470 loc) · 14.5 KB
/
npc.h
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
#ifndef _NPC_H
#define _NPC_H
#include "action_management/action_structs.h"
#include "game-state/game_state_common.h"
#include "game-state/item.h"
#include "game-state/player.h"
#include "playerclass/class_structs.h"
#include "playerclass/class.h"
#include "npc/dialogue.h"
#include "npc/npc_battle.h"
#include "npc/npc_move.h"
#include "npc/npc_quests.h"
#include "cli/util.h"
// NPC STRUCTURE DEFINITION ---------------------------------------------------
/* A non-playable character in game */
typedef struct npc {
/* hh is used for hashtable, as provided in uthash.h */
/* Second hash handle is for storing npcs in specific rooms */
UT_hash_handle hh, hh_room;
/* NPC identifier */
char *npc_id;
/* short description of the NPC, <51 chars */
char *short_desc;
/* long description of the NPC, <301 chars */
char *long_desc;
/* pointer to existing convo struct; changed depending on whether
npc has activated quest or task convo */
convo_t *active_dialogue;
/* pointer to an existing convo struct; for normal dialogue */
convo_t *standard_dialogue;
/* pointer to inventory hashtable */
item_hash_t *inventory;
/* pointer to an existing class struct */
class_t *class;
/* pointer to an existing npc_move struct */
npc_mov_t *movement;
/* enum indicating hostility level of the npc */
hostility_t hostility_level;
/* either NULL or a pointer to an existing npc_battle struct */
npc_battle_t *npc_battle;
/* linked list of all possible actions the player can initiate with the npc */
list_action_t *npc_actions;
/* pointer to a quest with dialogue */
npc_quest_list_t *quests;
/* pointer to a task with dialogue */
npc_task_list_t *tasks;
/* pointer to game_action hashtable */
game_action_hash_t *actions;
} npc_t;
/* Agent: a struct of things that you can perform actions upon
* - item: an item
* - npc: an NPC
*/
typedef struct agent
{
item_t *item;
npc_t *npc;
} agent_t;
enum agent_tag {ITEMS, NPCS};
// ACTION STRUCTURE DEFINITION + BASIC FUNCTIONS ------------------------------
typedef struct game_action_effect{
enum agent_tag agent_tag;
agent_t *agent;
attribute_t *attribute_to_modify;
attribute_value_t *new_value;
struct game_action_effect *next; //mandatory for utlist macros
} game_action_effect_t;
/* This typedef is to distinguish between game_action_effect_t
* pointers which are used to point to the game_action_effect_t structs
* in the traditional sense, and those which are used to enable UTLIST functionality
* on the game_action_effect_t structs as specified in src/common/include */
typedef struct game_action_effect action_effect_list_t;
typedef struct game_action {
UT_hash_handle hh;
char* action_name;
condition_list_t *conditions; //must be initialized to NULL
action_effect_list_t *effects; //must be initialized to NULL
char* success_str;
char* fail_str;
} game_action_t;
/* This typedef is to distinguish between npc_t pointers which are
* used to point to the npc_t structs in the traditional sense,
* and those which are used to hash npc_t structs with the
* UTHASH macros as specified in src/common/include */
typedef struct npc npc_hash_t;
// STRUCT FUNCTIONS -----------------------------------------------------------
/*
* Initializes an npc with the given parameters.
*
* Parameters:
* npc: an npc; must point to already allocated memory
* npc_id: unique string ID of npc
* short_desc: description of npc <51 chars
* long_desc: description of npc <301 chars
* standard_dialogue: a pointer to an existing convo_t struct defining the
npc's conversations (non quest/task-related)
(see /include/npc/dialogue.h)
* inventory: a pointer to an existing item_hash_t struct defining the npc's
* inventory (see /include/game-state/item.h)
* class: a pointer to an existing class_t struct defining the npc's class
(see /include/playerclass/class_structs.h)
* hostility_level: an enum indicating the npc's hostility level
(see /include/npc/npc_battle.h)
*
* Returns:
* SUCCESS on success, FAILURE if an error occurs
*/
int npc_init(npc_t *npc, char *npc_id, char *short_desc, char *long_desc,
class_t *class, npc_mov_t *movement, hostility_t hostility_level);
/*
* Allocates a new npc in the heap.
*
* Parameters:
* npc_id: unique string ID of npc
* short_desc: description of npc <51 chars
* long_desc: description of npc <301 chars
* standard_dialogue: a pointer to an existing convo_t struct defining the
* npc's conversations (non-quest/task-related)
* (see /include/npc/dialogue.h)
* inventory: a pointer to an existing item_hash_t struct defining the npc's
* inventory (see /include/game-state/item.h)
* class: a pointer to an existing class_t struct defining the npc's class
(see /include/playerclass/class_structs.h)
* hostility_level: an enum indicating the npc's hostility level
(see /include/npc/npc_battle.h)
*
* Returns:
* pointer to allocated npc
*/
npc_t *npc_new(char *npc_id, char *short_desc, char *long_desc,
class_t *class, npc_mov_t *movement, hostility_t hostility_level);
/*
* Frees resources associated with an npc.
*
* Parameters:
* npc: the npc to be freed
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurs
*/
int npc_free(npc_t *npc);
// "CHECK" FUNCTIONS ----------------------------------------------------------
/*
* Checks if an npc's npc_battle struct is set correctly.
*
* Parameters:
* npc: the npc
*
* Returns:
* true if:
* - the npc's hostility_level is HOSTILE and its npc_battle is not NULL
* - the npc's hostility_level is FRIENDLY and its npc_battle is NULL
* - note: since CONDITIONAL FRIENDLY npcs can fight under certain
* circumstances, its npc_batle could be not NULL
* and false otherwise
*/
bool check_npc_battle(npc_t *npc);
/*
* Checks if an item is in the NPC's inventory.
*
* Parameters:
* npc: the npc
* item_id: the item's ID
*
* Returns:
* true if the item is in the NPC's inventory, false otherwise
*/
bool item_in_npc_inventory(npc_t *npc, char *item_id);
/* Checks if an indefinite npc needs moved based on how long it's been in its
* current room
*
* Parameters:
* - npc: The NPC struct
*
* Returns:
* - true if the npc does need to be moved
* - false if the npc does not need to be moved,
* or the npc has a definite movement path
*/
bool check_if_npc_indefinite_needs_moved(npc_t *npc);
// "GET" FUNCTIONS ------------------------------------------------------------
/*
* Gets a quest with a specific id
*
* Parameters:
* - npc: the npc
* - quest_id: the quest's id
*
* Returns:
* - the quest if present, else NULL
*/
npc_quest_t *get_npc_quest(npc_t *npc, char *quest_id);
/*
* Gets a task with a specific id
*
* Parameters:
* - npc: the npc
* - task_id: the task's's id
*
* Returns:
* - the task if present, else NULL
*/
npc_task_t *get_npc_task(npc_t *npc, char *task_id);
/*
* Gets short description of npc.
*
* Parameters:
* npc: the npc
*
* Returns:
* short description string, NULL if npc is NULL
*/
char *get_sdesc_npc(npc_t *npc);
/*
* Gets long description of npc.
*
* Parameters:
* npc: the npc
*
* Returns:
* long description string, NULL if npc is NULL
*/
char *get_ldesc_npc(npc_t *npc);
/* Function to return an item in an NPC's inventory given the item_id
*
* Parameters:
* npc: the npc
* item_id: the item_id of the item that you want to be returned
*
* Returns:
* a pointer to the item that matches the input item_id, or NULL if nonexistent
*/
item_t *get_item_from_npc(npc_t *npc, char *item_id);
/*
* Function to get a hashtable (uthash) of all items in the npc's inventory.
*
* Parameters:
* npc: the npc
*
* Returns:
* hashtable of items, the inventory
*/
item_hash_t *get_npc_inv_hash(npc_t *npc);
/*
* Function to get a linked list (utlist) of all items in the npc's inventory.
*
* Parameters:
* npc: the npc
*
* Returns:
* linked list of pointers to items (the head element)
*/
item_list_t *get_npc_inv_list(npc_t *npc);
/*
* Function to get an npc's npc_battle struct if it isn't NULL.
*
* Parameters:
* npc: the npc
*
* Returns:
* a pointer to the npc's npc_battle struct or NULL
*/
npc_battle_t *get_npc_battle(npc_t *npc);
/*
* Returns the max_hp level of an npc.
*
* Parameters:
* npc: the npc
*
* Returns:
* the npc's max_hp or -1 if its npc_battle field is NULL
*/
int get_npc_max_hp(npc_t *npc);
/*
* Returns the hp level of an npc.
*
* Parameters:
* npc: the npc
*
* Returns:
* the npc's hp or -1 if its npc_battle field is NULL
*/
int get_npc_hp(npc_t *npc);
/*
* Function to get an npc's npc_mov struct or NULL
*
* Parameters:
* npc: the npc
*
* Returns:
* a pointer to the npc's npc_mov struct or NULL
*/
npc_mov_t *get_npc_mov(npc_t *npc);
// "SET" FUNCTIONS ------------------------------------------------------------
/*
* Adds the given item to the given npc.
*
* Parameters:
* npc: the npc to receive the item
* item: the item
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int add_item_to_npc(npc_t *npc, item_t *item);
/*
* Removes the given item from the given npc.
* Note that the memory associated with this item is not freed
*
* Parameters:
* npc: the npc from whom to remove the item
* item: the item
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int remove_item_from_npc(npc_t *npc, item_t *item);
/* Removes/Deletes all items in an NPCs inventory
*
* Parameters:
* npc: the npc whose inventory we're deleting
*
* Returns:
* SUCCESS upon completion
*/
int delete_all_items_from_npc(npc_t *npc);
/* Deletes and frees all items in an NPC's inventory
*
* Parameters:
* npc: the npc whose inventory we're deleting and freeing
*
* Returns:
* SUCCESS upon completion
*/
int free_all_items_from_npc(npc_t *npc);
/*
* Adds the given convo to the given npc's standard_dialogue
*
* Parameters:
* npc: the npc to receive the convo
* c: the convo
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int add_convo_to_npc(npc_t *npc, convo_t *c);
/*
* Adds an npc_battle struct to a given npc.
*
* Parameters:
* npc: the npc to receive the npc_battle struct
* stats: a pointer to an existing stat_t struct defining the npc's battle
stats (see /include/battle/battle_structs.h)
* moves: a pointer to an existing move_t struct defining the npc's battle
moves (see /include/battle/battle_structs.h)
* ai: the npc's difficulty level (see /include/battle/battle_common.h)
* hostility_level: the npc's hostility level
* class_type: a pointer to an existing class_t struct defining the npc's class
(see /include/playerclass/class_structs.h)
* items: An inventory of items that can be used in battle
* armor: a pointer to the armor an npc has
* accessory: a pointer to the accessory an npc has
* weapon: a pointer to the weapon an npc has
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int add_battle_to_npc(npc_t *npc, stat_t *stats, move_t *moves,
difficulty_t ai, hostility_t hostility_level,
class_t *class_type, battle_item_t *items,
battle_equipment_t *armor,
battle_equipment_t *accessory, battle_equipment_t *weapon);
/*
* Changes the hp level of the npc.
*
* Parameters:
* npc: the npc
* change: the positive or negative change to be made to the hp level
*
* The change has a minimum value of 0
* The change has a maximum value of the npc's max_hp level
*
* Returns:
* the npc's new hp level
*/
int change_npc_hp(npc_t *npc, int change);
/* Moves an npc to the next room in their movement path
*
* Parameters:
* npc: The NPC struct
*
* Returns:
* FAILURE: if a move does not occur,
* SUCCESS: if successful move to the next room, with the only exception
* being if an indefinite NPC has more than one room in its path, and then
* reaches the end of its path, in which case its path direction is flipped,
* and its time_ray for the current step in its path is reset, but it does not
* technically change rooms.
*
*/
int move_npc(npc_t *npc);
// HASH TABLE FUNCTIONS ---------------------------------------------------
/*
* Deletes and frees the elements of a hashtable of NPCs.
* Implemented with macros provided by uthash.h
*
* Parameters:
* npcs: a hashtable of npcs to be deleted
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int delete_all_npcs(npc_hash_t *npcs);
/* Sets the npc's active dialogue to the proper dialogue
* - This handles quest interaction, since NPCs can have different
* dialogue when giving quests or completing tasks
*
* Parameters:
* - qctx: A quest context containing a player and a hash of all quests in the game
* - npc: An npc
*
* Returns:
* - SUCCESS on success, FAILURE if an error occurs
*/
int set_proper_dialogue(quest_ctx_t *qctx, npc_t *npc);
// Conversion FUNCTIONS ---------------------------------------------------
/*
* converts hostility_level of an NPC to HOSTILE
*
* Parameters:
* npc: the NPC whose hostility_level becomes HOSTILE
* (must point to already allocated memory)
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int make_npc_hostile(npc_t *npc);
/*
* converts hostility_level of an NPC to CONDITIONAL_FRIENDLY
*
* Parameters:
* npc: the NPC whose hostility_level becomes CONDITIONAL_FRIENDLY
* (must point to already allocated memory)
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int make_npc_cond_friendly(npc_t *npc);
/*
* changes an NPC's hostility_level based on player's
* tone at a conversation edge
* ex. NEGATIVE tone makes NPC HOSTILE
*
* NOTE FOR FUTURE IMPLEMENTATION:
* when this function is called to make NPC HOSTILE,
* it should call add_battle_to_npc() as well to
* initialize the npc_battle_t struct in npc_t
* and allow NPC to participate in battle
*
* Parameters:
* npc: the NPC whose hostility_level changes
* (must point to already allocated memory)
* edge: the edge containing the tone_t struct that determines
* an NPC's hostility_level
* (must point to already allocated memory)
*
* Returns:
* SUCCESS if successful, FAILURE if an error occurred.
*/
int change_npc_hostility(npc_t *npc, edge_t *edge);
#endif /* _NPC_H */