/
room.c
185 lines (151 loc) · 5.11 KB
/
room.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
#include "gameDefs.h"
/*
Function: initRoomList
Purpose: initializes the fields of the given RoomListType structure
in: location of the old room array to be initialized
out: location of the new room array that is initialized
return: void
*/
void initRoomList(RoomLinkedList* list){
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
#pragma clang diagnostic push
#pragma ide diagnostic ignored "ConstantFunctionResult"
/*
Function: initRoom
Purpose: initializes the fields of the given roomType structure
in: location of the old room to be initialized
out: location of the new room that is initialized
in: name of the room
return: FUNC_SUCCESS if successful
*/
int initRoom(RoomType** room, char* name){
// Allocate Memory For Room
RoomType* newRoom = malloc(sizeof(RoomType));
memAllocCheck(newRoom);
//Set Room Name
strcpy(newRoom->name, name);
newRoom->amountHuntersInRoom = 0;
//Set LinkList Of Connected Rooms
RoomLinkedList* connectedRooms = malloc(sizeof(RoomLinkedList));
memAllocCheck(connectedRooms);
initRoomList(connectedRooms);
newRoom->adjacentRooms = connectedRooms;
//Set LinkList of Evidence
EvidenceLinkedList* evidenceInRoom = malloc(sizeof(EvidenceLinkedList));
memAllocCheck(evidenceInRoom);
initEvidenceList(evidenceInRoom);
newRoom->evidence = evidenceInRoom;
//Clear Garbage Data From Array
for(int i = 0; i < MAX_HUNTERS; ++i){
newRoom->huntersInRoom[i] = NULL;
}
newRoom->ghost = NULL;
sem_init(&newRoom->semaphore, 0, 1);
*room = newRoom;
return FUNC_SUCCESS;
}
#pragma clang diagnostic pop
/*
Function: initRoomNode
Purpose: initializes the fields of the given roomNode structure
in: location of the old room node to be initialized
out: location of the new room node that is initialized
in: the room type (so that it's data can be accessed)
return: FUNC_SUCCESS if success, MEMORY_ALLOCATION_ERROR for memory allocating failing
*/
int initRoomNode(RoomNodeType** node, RoomType* room){
RoomNodeType* roomNode = malloc(sizeof(RoomNodeType));
memAllocCheck(roomNode);
roomNode->room = room;
roomNode->next = NULL;
roomNode->prev = NULL;
*node = roomNode;
return FUNC_SUCCESS;
}
/*
Function: appendRoom
Purpose: function that adds the room node directly at the back (i.e. the end) of the given list.
in: location of the room list to add onto
out: location of the room list with room node added.
in: room being added at end of list.
return: void
*/
void appendRoom(RoomLinkedList* list, RoomType* room){
RoomNodeType* newNode;
initRoomNode(&newNode, room);
if(list->head == NULL){
list->head = newNode;
list->tail = newNode;
}else {
list->tail->next=newNode;
newNode->prev = list->tail;
list->tail = newNode;
}
list->size++;
}
/*
Function: connectRooms
Purpose: connect the two different room types given in the parameter and connect them together (adjacent rooms)
in: location of roomA
in: location of roomB
out: location of roomA after adjacent rooms list was modified to store roomB
out: location of roomB after adjacent rooms list was modified to store roomA
return: returns the random node
*/
void connectRooms(RoomType* roomA, RoomType* roomB){
appendRoom(roomB->adjacentRooms, roomA);
appendRoom(roomA->adjacentRooms, roomB);
}
/*
Function: cleanRoomListData
Purpose: function that traverses the given list and deallocates its data only
in: location of the room list to clean its data only
return: void
*/
void cleanRoomListData(RoomLinkedList* list){
RoomNodeType* currNode = list->head;
while(currNode != NULL){
cleanEvidenceListData(currNode->room->evidence);
cleanEvidenceListNodes(currNode->room->evidence);
free(currNode->room->evidence);
cleanRoomListNodes(currNode->room->adjacentRooms);
free(currNode->room->adjacentRooms);
free(currNode->room);
currNode = currNode->next;
}
}
/*
Function: cleanRoomList
Purpose: function that traverses the given list and deallocates its nodes only.
in: location of the room list to clean its nodes only
return: void
*/
void cleanRoomListNodes(RoomLinkedList* list){
RoomNodeType* currNode = list->head;
RoomNodeType* nextNode = NULL;
while(currNode != NULL){
nextNode = currNode->next;
free(currNode);
currNode = nextNode;
}
}
/*
Function: getRandomRoom
Purpose: traverse through the room list a given amount of times to select a random room
in: location of the room list to traverse
in: location of room node
out: location of room node that is picked
return: void
*/
void getRandomRoom(RoomLinkedList* roomList, RoomNodeType** room){
RoomNodeType* currNode = roomList->head;
int randomVal = randInt(1, roomList->size-1);
for(int i = 0; i < randomVal; ++i){
if(currNode->next != NULL)
currNode = currNode->next;
}
*room = currNode;
}