/
evidence.c
334 lines (275 loc) · 9.4 KB
/
evidence.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
#include "gameDefs.h"
/*
Function: initEvidence
Purpose: function that initializes fields of the given type to value in parameters or default values.
in: location of the old evidence to be initialized
in: the value of the data
in: the class type of evidence
out: location of the new evidence that is initialized
return: FUNC_SUCCESS if success, MEMORY_ALLOCATION_ERROR for memory allocating failing
*/
int initEvidence(EvidenceType** evidence, double dataValue, int classType){
EvidenceType* newEvidence = malloc(sizeof(EvidenceType));
memAllocCheck(newEvidence);
newEvidence->evidenceClass = classType;
newEvidence->data = dataValue;
*evidence = newEvidence;
return FUNC_SUCCESS;
}
/*
Function: initEvidenceList
Purpose: function that initializes the three fields of the given list parameter to default values.
in: location of the old evidence list to be initialized
out: location of the new evidence list that is initialized
return: void
*/
void initEvidenceList(EvidenceLinkedList* list){
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
/*
Function: appendEvidence
Purpose: adds the evidence type (evidenceNode) to the back of the evidence linked list in parameter list.
in: location of the old evidence list to add evidenceNode in
out: location of the new room array that has evidenceNode added
in: evidenceNode is evidence type we want to add
return: void
*/
void appendEvidence(EvidenceLinkedList* list, EvidenceNodeType* evidenceNode){
evidenceNode->next = NULL;
evidenceNode->prev = NULL;
if(list->head == NULL){
list->head = evidenceNode;
list->tail = evidenceNode;
}else {
list->tail->next=evidenceNode;
evidenceNode->prev = list->tail;
list->tail = evidenceNode;
}
list->size++;
}
/*
Function: initEvidenceNode
Purpose: function that initializes fields of the given type to default values.
in: location of the old node to be initialized
in: evidenceType which holds the data of the EvidenceNode
out: location of the new evidence node that is initialized
return: FUNC_SUCCESS if success, MEMORY_ALLOCATION_ERROR for memory allocating failing
*/
int initEvidenceNode(EvidenceNodeType** node, EvidenceType* evidence){
EvidenceNodeType* evidenceNode = malloc(sizeof(EvidenceNodeType));
memAllocCheck(evidenceNode);
evidenceNode->data = evidence;
evidenceNode->next = NULL;
evidenceNode->prev = NULL;
*node = evidenceNode;
return FUNC_SUCCESS;
}
/*
Function: cleanEvidenceListData
Purpose: function that traverses the given list and deallocates its data only
in: location of the evidence list to clean its data only
return: void
*/
void cleanEvidenceListData(EvidenceLinkedList* list) {
//Free Hunter Evidence Data
EvidenceNodeType *currNode = list->head;
while (currNode != NULL) {
free(currNode->data);
currNode = currNode->next;
}
}
/*
Function: cleanEvidenceList
Purpose: function that traverses the given list and deallocates its nodes only.
in: location of the evidence list to clean its nodes only
return: void
*/
void cleanEvidenceListNodes(EvidenceLinkedList *list) {
//Free Hunter Evidence Nodes
EvidenceNodeType *currNode = list->head;
EvidenceNodeType *nextNode = NULL;
while (currNode != NULL) {
nextNode = currNode->next;
free(currNode);
currNode = nextNode;
}
}
void dropEvidence(GhostType* ghost){
int evidenceOptions[3] = {-1, -1, -1};
GhostClassType ghostClass = ghost->ghostClass;
if (ghostClass == POLTERGEIST) {
evidenceOptions[0] = EMF;
evidenceOptions[1] = TEMPERATURE;
evidenceOptions[2] = FINGERPRINTS;
} else if (ghostClass == BANSHEE) {
evidenceOptions[0] = EMF;
evidenceOptions[1] = TEMPERATURE;
evidenceOptions[2] = SOUND;
} else if (ghostClass == BULLIES) {
evidenceOptions[0] = EMF;
evidenceOptions[1] = FINGERPRINTS;
evidenceOptions[2] = SOUND;
} else if (ghostClass == PHANTOM) {
evidenceOptions[0] = TEMPERATURE;
evidenceOptions[1] = FINGERPRINTS;
evidenceOptions[2] = SOUND;
}
EvidenceClassType evidenceClass = makeChoice(evidenceOptions, 3);
int isGhostly = randInt(0,2);
double evidenceData;
if(isGhostly == 0){
generateGhostlyEvidence(evidenceClass, &evidenceData);
}else{
generateStdEvidence(evidenceClass, &evidenceData);
}
EvidenceType* newEvidence;
EvidenceNodeType* newEvidenceNode;
initEvidence(&newEvidence, evidenceData, evidenceClass);
initEvidenceNode(&newEvidenceNode, newEvidence);
appendEvidence(ghost->currRoom->evidence, newEvidenceNode);
}
/*
Function: generateStdEvidence
Purpose: generates a random ghostly evidence
in: the evidence class type used (since each class has a different range)
return: the data value
*/
void generateGhostlyEvidence(EvidenceClassType class, double* dataVal){
switch(class){
case EMF:
*dataVal = randDouble(4.70, 5.00);
break;
case TEMPERATURE:
*dataVal = randDouble(-10.00, 1.00);
break;
case FINGERPRINTS:
*dataVal = 1.00;
break;
case SOUND:
*dataVal = randDouble(65.00, 75.00);
break;
default:
break;
}
}
/*
Function: generateStdEvidence
Purpose: generates a random standard evidence
in: the evidence class type used (since each class has a different range)
return: the data value
*/
void generateStdEvidence(EvidenceClassType class, double* dataVal){
switch(class){
case EMF:
*dataVal = randDouble(0,4.90);
break;
case TEMPERATURE:
*dataVal = randDouble(0, 27);
break;
case FINGERPRINTS:
*dataVal = 0.00f;
break;
case SOUND:
*dataVal = randDouble(40, 70);
break;
default:
break;
}
}
/*
Function: getEvidenceClassName
Purpose: finds the evidence class type based on the evidence type
out: the evidence type as a string
in: the evidence class
return: void
*/
void getEvidenceClassName(char* evidenceType, EvidenceClassType class){
switch (class) {
case EMF:
strcpy(evidenceType, "EMF");
break;
case TEMPERATURE:
strcpy(evidenceType, "TEMPERATURE");
break;
case FINGERPRINTS:
strcpy(evidenceType, "FINGERPRINTS");
break;
case SOUND:
strcpy(evidenceType, "SOUND");
break;
default:
break;
}
}
void pickUpEvidence(HunterType* hunter){
EvidenceNodeType* newEvidenceNode = NULL;
// If Room Contains No Evidence, Collect Standard Evidence
if(hunter->currRoom->evidence->size == 0){
double standardEvidence;
generateStdEvidence(hunter->readableEvidence, &standardEvidence);
EvidenceType* newEvidence;
initEvidence(&newEvidence, standardEvidence, hunter->readableEvidence);
initEvidenceNode(&newEvidenceNode, newEvidence);
appendEvidence(hunter->evidenceCollected, newEvidenceNode);
}else{
findRoomEvidence(hunter->currRoom->evidence, hunter->readableEvidence, &newEvidenceNode);
if(newEvidenceNode != NULL){
removeEvidenceNode(hunter->currRoom->evidence, newEvidenceNode);
appendEvidence(hunter->evidenceCollected, newEvidenceNode);
if(isGhostly(newEvidenceNode->data, hunter->readableEvidence) == 0){
hunter->boredomTimer = BOREDOM_MAX;
hunter->ghostlyEvidenceCollected++;
}
}
}
}
void findRoomEvidence(EvidenceLinkedList* list, EvidenceClassType evidenceClass, EvidenceNodeType** node){
EvidenceNodeType* currNode = list->head;
while(currNode != NULL){
if(currNode->data->evidenceClass == evidenceClass){
*node = currNode;
break;
}
currNode = currNode->next;
}
}
/*
Function: removeEvidenceNode
Purpose: removes an evidence node from an evidence list
in: location of the list to remove the evidence node from
out: location of the list with removed evidence node
in: location of the evidence node to be removed
return: void
*/
void removeEvidenceNode(EvidenceLinkedList* list, EvidenceNodeType* node){
if(node != NULL){
EvidenceNodeType* prev = node->prev;
EvidenceNodeType* next = node->next;
if(prev == NULL)
list->head = next;
else
prev->next = next;
if(next == NULL)
list->tail = prev;
else
next->prev = prev;
}
}
/*
Function: getRandomEvidence
Purpose: picks a random value from 0 to the end of the list, traverses the list that many times
then gets the node in that position. (for choice to be random)
in: location of the linked list to traverse
return: returns the random node
*/
void getRandomEvidence(EvidenceLinkedList* list, EvidenceType** evidence){
EvidenceNodeType* currNode = list->head;
int randomVal = randInt(1, list->size-1);
for(int i = 0; i < randomVal; ++i){
if(currNode->next != NULL)
currNode = currNode->next;
}
*evidence = currNode->data;
}