Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a63893d
including npcs in spec room struct and functions
May 24, 2020
ed85e23
made edits to have npc_t struct considered
May 25, 2020
072a177
lets see
Jun 1, 2020
b812938
saving sample_npc.c
Jun 1, 2020
c40ae97
Merge remote-tracking branch 'origin/dev' into openworld/samples-npcs
dwahme Jun 1, 2020
ee9e84d
slight edits in autogeneration to include npcs
Jun 1, 2020
f95c171
added npcs in test, need to add room descriptions
Jun 3, 2020
c3a6f76
fixed errors in test_structs.c
Jun 5, 2020
045f5f4
Fixed errors in test_struct
Jun 5, 2020
909ffce
pushing
Jun 6, 2020
ef7c123
Merge branch 'dev' of https://github.com/uchicago-cs/chiventure into …
Jun 6, 2020
2c9cd61
fixed merge issues by simply deletin all and replacing it with Bryans…
Jun 6, 2020
b3e1486
look at autogenerate.c for my thought/confusion process
Jun 6, 2020
02bf49b
revamping npc structure and attempting integration into autogenerate
Jun 6, 2020
45cc244
successfully integrated default_npc into autogenerate. need to write …
Jun 6, 2020
abc268a
fixed merge conflict
Jun 6, 2020
3a15044
resolved merge conflicts
Jun 6, 2020
4d83aff
still need to fix test_autogenerate errors
Jun 7, 2020
a724869
Added tests and made default npcs hash-based
Jun 7, 2020
ac34586
Merge pull request #766 from uchicago-cs/openworld/autogenerate-bugs
carolinaecalderon Jun 7, 2020
1c82bd1
Fixed final tests
Jun 7, 2020
3efc5e7
Merge branch 'openworld/samples-npcs' of https://github.com/uchicago-…
Jun 7, 2020
b6786c5
Editing comments
navila-luna Jun 7, 2020
20ef151
rm since default_npcs is the updated duplicate
navila-luna Jun 7, 2020
daf6b6f
deleting the duplicate of samples-npcs file
Jun 7, 2020
baa8963
Merge branch 'openworld/samples-npcs' of https://github.com/uchicago-…
Jun 7, 2020
9854cfa
made some changes, will come back to more space comments
Jun 7, 2020
26027fd
went through all suggestions
Jun 7, 2020
4523676
minor changes
Jun 7, 2020
bbfd365
will fix comment issues in sample npc
Jun 7, 2020
2d742d3
changes to spaces
navila-luna Jun 7, 2020
5204f90
added on to comment tests and fixed spelling issue in .c file
Jun 7, 2020
d82a55d
fixed merge issue
Jun 7, 2020
6610d3f
made changes in npc functions, need one more fix
Jun 7, 2020
4519c71
I think everythings done!
Jun 8, 2020
c5159b2
Merge branch 'dev' into openworld/samples-npcs
navila-luna Jun 8, 2020
6f47f0f
went back for tabs issues
navila-luna Jun 8, 2020
c0bcacb
went back for tabs issues
navila-luna Jun 8, 2020
b46c5ce
went through for any spacing issues not seen before
navila-luna Jun 8, 2020
fb4dd03
fixed overlooked tabs
navila-luna Jun 8, 2020
768e26a
fixed overlooked tab issue
navila-luna Jun 8, 2020
2048461
fixed overlooks spacing issues
navila-luna Jun 8, 2020
4d43dc4
fixed tab issues
navila-luna Jun 8, 2020
9f4dea9
I really hope spacing issues are resolved
Jun 8, 2020
7865488
fixed merges and spaces thru astyles
Jun 8, 2020
d812be2
Merge branch 'dev' into openworld/samples-npcs
navila-luna Jun 8, 2020
204008d
Merge branch 'dev' into openworld/samples-npcs
navila-luna Jun 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion include/openworld/autogenerate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include "game-state/game.h"
#include "gen_structs.h"
#include "default_rooms.h"
#include "default_npcs.h"

#define MAX_RAND_ITEMS (6)
#define MAX_NPCS (3)

/*
* path_exists_in_dir
Expand Down Expand Up @@ -160,6 +162,33 @@ item_hash_t *random_items(roomspec_t *room);
*/
int random_item_lookup(item_hash_t **dst, item_hash_t *src, int num_iters);

#endif /* INCLUDE_AUTOGENERATE_H */
/*
* random_npcs
* randomly selects a number of npcs in a room where the max NPCs in a room is 3
* Assumes that npcs are not automatically assigned to rooms
* Includes a NPC room description and then randomly generates
* a certain amount of generic, friendly, and hostile per room.
*
* parameters:
* - a roomspe_t that icnludes the type npc_t struct
* returns: type npc_t and a room npc description
*/
npc_t *random_npcs(roomspec_t *room);

/*
* random_npc_lookup
* Iterates through num_iters times to and copies the source item at most 3 times
* Helper function for random_npcs
*
* parameters:
* - item_hash_t *dst is where you want to store the npc you just found
* - item_hash_t *src is where you're looking up the npc
* - num_iters is how many times you'll iterate through src to settle on an npc
*
* returns:
* - Failure if the NPC is NULL or if the num_iters is 0
* - Success if NPC is not NULL and there's at least 1 num_iters.
*/
int random_npc_lookup(npc_t **dst, npc_t *src, int num_iters);

#endif /* INCLUDE_AUTOGENERATE_H */
87 changes: 87 additions & 0 deletions include/openworld/default_npcs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Team RPG-Openworld
*
* Default NPC header file
*
* default_npc.h: This file: contains all of the necessary npc structs and functions
* that are necessary to move npcs in a autogenerated room
*
* See chiventure/src/openworld/default_npc.c source code to see implementation.
*/
#ifndef _DEFAULT_NPCS_H
#define _DEFAULT_NPCS_H

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "game-state/game_state_common.h"

// Assigns the max_name_lenght an NPC can have
#define MAX_NAME_LEN (50)

/*
* npc_classification
* Classification of the type of NPC characters to be found
* within the rooms. Where friendly means NPC doesn't engage in fights,
* Hostile means engages in fights, and generic means can engage in fights
*/

typedef enum npc_classification {
NPC_GENERIC = 0,
NPC_HOSTILE = 1,
NPC_FRIENDLY = 2
} npc_classification_t;


/* npc_t struct
* This struct will carry the necessary info for creating a NPC in a room
* This struct contains:
* - int level: player level in game
* - char *npc_name: strings a NPC type and name, ex: wizard type Hostile
* - npc_classification_t classification: enum for the three types of NPCs
* - item_hash_t *item inventory: item inventory for the NPCs
*/
typedef struct npc
{
char *npc_name;
int level;
item_hash_t *inventory;
npc_classification_t classification;
struct npc *next;
struct npc *prev;
UT_hash_handle hh;
} npc_t;

/* get_hostile_npcs produces mock NPCs of type Hostile.
* Will recieve a linked list of hostile npcs and includes the items that
* each NPC carries with them makes 3 npcs that are hostile
* Input:
* - none
* Output:
* - a hash list of hostile npcs with their own items inventory
*/
npc_t *get_hostile_npcs();


/* get_friendly_npcs produces mock NPCs of type Friendly
* makes 3 npcs that are friendly and includes the items that each NPC carries with them.
* Input:
* - none
* Output:
* - Returns them as a hash list
*/
npc_t *get_friendly_npcs();



/* get_generic_npcs produces mock NPCs of type Generic
* Generic: NPCs that you can choose to be friendly or hostile with
* Function makes 3 npcs that are generic and includes the items that each NPC carries with them
* Input:
* - none
* Output:
* - puts the NPCs in as a hash list
*/
npc_t *get_generic_npcs();


#endif
9 changes: 6 additions & 3 deletions include/openworld/gen_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stdlib.h>
#include "game-state/game.h"
#include "game-state/game_state_common.h"
#include "default_npcs.h"

#ifndef GEN_STRUCTS_H
#define GEN_STRUCTS_H
Expand All @@ -30,6 +31,7 @@
* - char *long_desc: long description for room
* - int num_built: how many rooms of this type have been already built. An identifier.
* - item_hash_t *items: hash table of items in room
* - npc_t *npcs: doubly linked list of the npcs
* - UT_hash_handle hh: hash handle for room spec
*/
typedef struct roomspec {
Expand All @@ -38,6 +40,7 @@ typedef struct roomspec {
char *long_desc;
int num_built;
item_hash_t *items;
npc_t *npcs;
UT_hash_handle hh;
} roomspec_t;

Expand Down Expand Up @@ -135,7 +138,7 @@ int gencontext_free(gencontext_t *context);
* SUCCESS - for SUCCESS
* FAILURE - if failed to initialize
*/
int init_roomspec(roomspec_t *spec, char *room_name, char *short_desc, char *long_desc, item_hash_t *items);
int init_roomspec(roomspec_t *spec, char *room_name, char *short_desc, char *long_desc, item_hash_t *items, npc_t *npcs);

/* roomspec_new
* Creates a new roomspec_t* based off the given parameters.
Expand All @@ -150,7 +153,7 @@ int init_roomspec(roomspec_t *spec, char *room_name, char *short_desc, char *lon
* roomspec_t *roomspecnew - the new roomspec
* NULL - if fails to create a new roomspec.
*/
roomspec_t* roomspec_new(char *room_name, char *short_desc, char *long_desc, item_hash_t *items);
roomspec_t* roomspec_new(char *room_name, char *short_desc, char *long_desc, item_hash_t *items, npc_t *npcs);

/* roomspec_free
* Frees a gencontext_t* and returns whether or not it was succesful.
Expand Down Expand Up @@ -218,4 +221,4 @@ int speclist_free(speclist_t *list);
*/
int speclist_free_all(speclist_t *list);

#endif
#endif
1 change: 1 addition & 0 deletions src/openworld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(openworld
src/autogenerate.c
src/default_rooms.c
src/default_items.c
src/default_npcs.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all tabs- convert to spaces

src/gen_structs.c)

target_include_directories(openworld PRIVATE src/)
Expand Down
78 changes: 75 additions & 3 deletions src/openworld/src/autogenerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "openworld/autogenerate.h"
#include "openworld/gen_structs.h"
#include "openworld/default_rooms.h"
#include "openworld/default_npcs.h"

/* See autogenerate.h */
bool path_exists_in_dir(room_t *r, char *direction)
Expand Down Expand Up @@ -51,7 +52,7 @@ room_t* roomspec_to_room(roomspec_t *roomspec)
room_t *res = room_new(buff, roomspec->short_desc, roomspec->long_desc);
// instead of taking all the items, just take a few of them
res->items = random_items(roomspec);

//res->npcs = random_npcs(roomspec);
res->paths = NULL;
return res;
}
Expand Down Expand Up @@ -178,13 +179,84 @@ int random_item_lookup(item_hash_t **dst, item_hash_t *src, int num_iters)

HASH_ITER(hh, src, current, tmp) {
if (i == num_iters) {
copy_item_to_hash(dst, src, tmp->item_id);
copy_item_to_hash(dst, src, current->item_id);
return SUCCESS;
}
i++;
}

return FAILURE;
}

/* See autogenerate.h */
npc_t *random_npcs(roomspec_t *room)
{
if (room == NULL) {
return NULL;
}

int num_npcs = rand() % MAX_NPCS;

//assuming that npcs are not automatically assigned to rooms
npc_t *hostiles = get_hostile_npcs();
npc_t *friendlies = get_friendly_npcs();
npc_t *generic = get_generic_npcs();


npc_t *combo = NULL;
npc_t *current = NULL;
npc_t *tmp = NULL;

strcat(room->long_desc, "These npcs are in the room: ");
for (int i = 0; i < num_npcs; i++) {
int random_num = rand() % 3;
if(random_num == 1)
{
random_npc_lookup(&current, hostiles, rand() % MAX_NPCS);
}
else if(random_num == 2)
{
random_npc_lookup(&current, friendlies, rand() % MAX_NPCS);
}
else
{
random_npc_lookup(&current, generic, rand() % MAX_NPCS);
}
HASH_ADD_STR(combo,npc_name,current);
}
HASH_ITER(hh, combo, current, tmp) {
strncat(room->long_desc, current->npc_name, MAX_NAME_LEN);
if (tmp != NULL)
strncat(room->long_desc, ", ", MAX_NAME_LEN);
}
strcat(room->long_desc, ".\n");

if (combo == NULL)
return NULL;

return combo;
}

/* See autogenerate.h */
int random_npc_lookup(npc_t **dst, npc_t *src, int num_iters)
{
npc_t *current = NULL;
npc_t *tmp = NULL;

int i = 0;
HASH_ITER(hh, src, current, tmp) {
if (i == num_iters) {
npc_t *new_npc = calloc(1, sizeof(npc_t));

new_npc->npc_name = calloc(MAX_NAME_LEN + 1, sizeof(char));
strncpy(new_npc->npc_name, current->npc_name, MAX_NAME_LEN);
new_npc->level = current->level;
new_npc->inventory = current->inventory;
new_npc->classification = current->classification;
HASH_ADD_STR(*dst, npc_name, new_npc);
return SUCCESS;
}
i++;
}

return FAILURE;
}
Loading