-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.c
executable file
·324 lines (261 loc) · 9.53 KB
/
items.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
/*******************************************************************************
Filename: items.c
Author: David C. Drake (https://davidcdrake.com)
Description: Functions governing items for the text-based RPG "Words of Power."
*******************************************************************************/
#include "wop.h"
/*******************************************************************************
Function: HandleItemMenuInput
Description: Displays the player's inventory and prompts the player to choose
which item to use.
Inputs: None.
Outputs: SUCCESS if an item is used, FAILURE otherwise.
*******************************************************************************/
int HandleItemMenuInput(void) {
int i, input, temp = 0;
for (i = 0; i < NUM_ITEM_TYPES; i++) {
if (g_player.inventory[i] > 0) {
temp++;
printf("[%d] %s (%d)\n", temp, GetItemName(i), g_player.inventory[i]);
}
}
if (temp == 0) {
printf("You have no items.\n");
FlushInput();
return FAILURE;
} else {
temp++;
printf("[%d] Cancel (return to previous menu)\n", temp);
GetIntInput(&input, 1, temp);
temp = 0;
for (i = 0; i < NUM_ITEM_TYPES; i++) {
if (g_player.inventory[i] > 0) {
temp++;
if (temp == input) {
UseItem(&g_player, i);
return SUCCESS;
}
}
}
}
return FAILURE; // No item was used: player changed their mind.
}
/*******************************************************************************
Function: UseItem
Description: Handles the use of an item by a given character (player or NPC).
Inputs: p_gc - Pointer to the user of the item.
item - Integer representing the type of item to be used.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int UseItem(game_character_t *p_gc, int item) {
if (p_gc->inventory[item] < 1) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
switch(item) {
case HEALING_POTION:
printf("%s drinks a healing potion and regains %d hit points.\n",
Capitalize(GetNameDefinite(p_gc)),
HealGameCharacter(p_gc, RandomInt(DEFAULT_HP / 2, DEFAULT_HP)));
break;
case FOOD:
printf("%s eats food and regains %d hit points.\n",
Capitalize(GetNameDefinite(p_gc)),
HealGameCharacter(p_gc,
RandomInt(DEFAULT_HP / 4, DEFAULT_HP / 2)));
break;
default:
PRINT_ERROR_MESSAGE;
return FAILURE;
}
p_gc->inventory[item]--;
FlushInput();
return SUCCESS;
}
/*******************************************************************************
Function: PrintInventory
Description: Prints the name and quantity of each item owned by a given game
character. Indicates which, if any, are equipped.
Inputs: p_gc - Pointer to the game character of interest.
Outputs: Number of item types described (or -1 if an error is encountered).
*******************************************************************************/
int PrintInventory(game_character_t *p_gc) {
int i, num_item_types_described = 0;
if (p_gc == NULL) {
PRINT_ERROR_MESSAGE;
return -1;
}
printf("Inventory: ");
for (i = 0; i < NUM_ITEM_TYPES; i++) {
if (p_gc->inventory[i] > 0) {
if (num_item_types_described > 0) {
printf(", ");
if (num_item_types_described % 3 == 0) {
printf("\n\t");
}
}
if (p_gc->inventory[i] == 1) {
printf("%s", GetItemName(i));
} else {
printf("%d %s", p_gc->inventory[i], GetItemNamePlural(i));
}
if (p_gc->equipped_items[i] > 0) {
if (p_gc->inventory[i] == 1) {
printf("(equipped)");
} else {
printf("(%d equipped)", p_gc->equipped_items[i]);
}
}
num_item_types_described++;
}
}
printf(".\n");
return num_item_types_described;
}
/*******************************************************************************
Function: GetItemName
Description: Given an item type, returns the name of that item.
Inputs: item - Integer representing the item of interest.
Outputs: The item's name as a pointer to an array of characters.
*******************************************************************************/
char *GetItemName(int item) {
static char itemName[SHORT_STR_LEN + 1];
switch (item) {
case FOOD:
strcpy(itemName, "Food");
break;
case HEALING_POTION:
strcpy(itemName, "Healing Potion");
break;
case GLOWING_MUSHROOM:
strcpy(itemName, "Glowing Mushroom");
break;
default:
PRINT_ERROR_MESSAGE;
strcpy(itemName, "???");
}
return itemName;
}
/*******************************************************************************
Function: GetItemNamePlural
Description: Given an item type, returns the plural name of that item.
Inputs: item - Integer representing the item of interest.
Outputs: The item's plural name as a pointer to an array of characters.
*******************************************************************************/
char *GetItemNamePlural(int item) {
static char itemName[SHORT_STR_LEN + 1];
strcpy(itemName, GetItemName(item));
if (item != FOOD) {
strcat(itemName, "s");
}
return itemName;
}
/*******************************************************************************
Function: GiveGold
Description: Transfers gold from one game character to another.
Inputs: giver - Pointer to the GC giving gold.
receiver - Pointer to the GC receiving gold.
amount - Amount of gold to be transferred.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int GiveGold(game_character_t *giver, game_character_t *receiver, int amount) {
if (giver == NULL || receiver == NULL || giver->gold < amount) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
giver->gold -= amount;
receiver->gold += amount;
printf("%s gives %d gold to %s.\n", giver->name, amount, receiver->name);
FlushInput();
return SUCCESS;
}
/*******************************************************************************
Function: AddItem
Description: Adds a given item to a given game character's inventory.
Inputs: receiver - Pointer to the GC receiving the item.
item - Integer representing the type of item to be added.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int AddItem(game_character_t *receiver, int item) {
if (receiver == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
receiver->inventory[item]++;
if (receiver->type == PLAYER) {
printf("You discover: %s\n", GetItemName(item));
}
return SUCCESS;
}
/*******************************************************************************
Function: GiveItem
Description: Transfers an item from one game character to another.
Inputs: giver - Pointer to the GC giving the item.
receiver - Pointer to the GC receiving the item.
item - Integer representing the type of item to be given.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int GiveItem(game_character_t *giver, game_character_t *receiver, int item) {
if (giver == NULL || receiver == NULL || giver->inventory[item] <= 0) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
giver->inventory[item]--;
if (giver->equipped_items[item] > giver->inventory[item]) {
giver->equipped_items[item]--;
}
receiver->inventory[item]++;
printf("%s gives %s to %s\n",
giver->name,
GetItemName(item),
receiver->name);
FlushInput();
return SUCCESS;
}
/*******************************************************************************
Function: GiveItems
Description: Transfers a specified amount of a given item from one game
character to another.
Inputs: giver - Pointer to the GC giving the item.
receiver - Pointer to the GC receiving the item.
item - Integer representing the type of item to be given.
amount - Quantity of the item to be transferred.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int GiveItems(game_character_t *giver, game_character_t *receiver, int item,
int amount) {
if (giver == NULL || receiver == NULL || giver->inventory[item] < amount) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
giver->inventory[item] -= amount;
if (giver->equipped_items[item] > giver->inventory[item]) {
giver->equipped_items[item] = giver->inventory[item];
}
receiver->inventory[item] += amount;
printf("%s gives %d %s to %s.\n", giver->name, amount,
GetItemNamePlural(item), receiver->name);
FlushInput();
return SUCCESS;
}
/*******************************************************************************
Function: GetItemValue
Description: Returns the standard market value of a given item.
Inputs: item - Integer representing the item of interest.
Outputs: The standard market value of the item of interest (or -1 if an
error is encountered).
*******************************************************************************/
int GetItemValue(int item) {
switch (item) {
case FOOD:
return 1;
case HEALING_POTION:
return 15;
case GLOWING_MUSHROOM:
return 20;
default:
PRINT_ERROR_MESSAGE;
break;
}
return -1;
}