-
Notifications
You must be signed in to change notification settings - Fork 2
/
delve.c
488 lines (412 loc) · 12.7 KB
/
delve.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
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
/*
* Copyright (c) 2021-22 Matthias Schmidt <xhr@giessen.ccc.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <json-c/json.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include "isscrolls.h"
void
cmd_discover_a_site(__attribute__((unused))char *cmd)
{
struct character *curchar = get_current_character();
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
ask_for_delve_difficulty();
curchar->delve_active = 1;
}
update_prompt();
}
void
ask_for_delve_difficulty(void)
{
struct character *curchar = get_current_character();
if (curchar == NULL) {
log_debug("No character loaded\n");
return;
}
printf("Please set a rank for your site\n\n");
printf("1\t - Troublesome site (3 progress per area)\n");
printf("2\t - Dangerous site (2 progress per area)\n");
printf("3\t - Formidable site (2 progress per area)\n");
printf("4\t - Extreme site (2 ticks per area)\n");
printf("5\t - Epic site (1 tick per area)\n\n");
curchar->delve->difficulty = ask_for_value("Enter a value between 1 and 5: ", 5);
}
void
cmd_delve_the_depths(char *cmd)
{
struct character *curchar = get_current_character();
char stat[MAX_STAT_LEN];
int ival[2] = { -1, -1 };
int ret, usedstat = 0;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
printf("You haven't discovered a site yet. Use 'discoverasite' first\n");
return;
}
ret = get_args_from_cmd(cmd, stat, &ival[1]);
if (ret >= 10) {
info:
printf("\nPlease specify the stat you'd like to use in this move\n\n");
printf("edge\t- You are navigating the area with haste\n");
printf("shadow\t- You are navigating the area with stealth or trickery\n");
printf("wits\t- You are navigating the area with observation, intuition,"\
"or expertise\n");
printf("Example: delvethedepths wits\n\n");
return;
} else if (ret <= -20) {
return;
}
if (strcasecmp(stat, "wits") == 0) {
ival[0] = curchar->wits;
usedstat = 1;
} else if (strcasecmp(stat, "shadow") == 0) {
ival[0] = curchar->shadow;
usedstat = 2;
} else if (strcasecmp(stat, "edge") == 0) {
ival[0] = curchar->edge;
usedstat = 3;
} else
goto info;
ret = action_roll(ival);
if (ret == 8 || ret == 18) {
printf("You mark progress, delve deeper and find an opportunity:\n");
mark_delve_progress(INCREASE);
read_oracle_from_json(ORACLE_DELVE_OPPORTUNITY, 0);
} else if (ret == 4 || ret == 14) {
printf("Rolling on the delve table with %s\n", stat);
if (usedstat == 1)
read_oracle_from_json(ORACLE_DELVE_THE_DEPTHS_WITS, 0);
else if (usedstat == 2)
read_oracle_from_json(ORACLE_DELVE_THE_DEPTHS_SHADOW, 0);
else if (usedstat == 3)
read_oracle_from_json(ORACLE_DELVE_THE_DEPTHS_EDGE, 0);
} else if (ret == 2 || ret == 12) {
printf("You reveal a danger:\n");
read_oracle_from_json(ORACLE_DELVE_DANGER, 0);
}
update_prompt();
}
void
cmd_check_your_gear(char *cmd)
{
struct character *curchar = get_current_character();
int ival[2] = { -1, -1 };
int ret;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
printf("You must start a delve with 'delvethedepths' first\n");
return;
}
if (curchar->supply <= 0) {
printf("You don't have any supply left. You cannot make this move\n");
return;
}
ival[0] = curchar->supply;
ival[1] = get_int_from_cmd(cmd);
ret = action_roll(ival);
if (ret == 8 || ret == 18) {
printf("You have the needed gear\n");
change_char_value("momentum", INCREASE, 1);
} else if (ret == 4 || ret == 14) {
printf("You have the needed gear, but suffer -1 supply\n");
change_char_value("momentum", INCREASE, 1);
change_char_value("supply", DECREASE, 1);
} else if (ret == 2 || ret == 12) {
printf("You don't have the needed gear and the situation grows more "\
"perilous -> Rulebook\n");
}
}
void
cmd_escape_the_depths(char *cmd)
{
struct character *curchar = get_current_character();
char stat[MAX_STAT_LEN];
int ival[2] = { -1, -1 };
int ret;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
printf("You must start a delve with 'delvethedepths' first\n");
return;
}
ret = get_args_from_cmd(cmd, stat, &ival[1]);
if (ret >= 10) {
info:
printf("\nPlease specify the stat you'd like to use in this move\n\n");
printf("edge\t- If you find the fastest way out\n");
printf("heart\t- If you steel yourself against the horrors\n");
printf("iron\t- If you fight your way out\n");
printf("wits\t- If you find retrace the steps or locate an alternate path\n");
printf("shadow\t- If you keep out of sight\n");
printf("Example: escapethedepths wits\n\n");
return;
} else if (ret <= -20) {
return;
}
ival[0] = return_char_stat(stat,
STAT_EDGE|STAT_HEART|STAT_IRON|STAT_WITS|STAT_SHADOW);
if (ival[0] == -1)
goto info;
ret = action_roll(ival);
if (ret == 8 || ret == 18) {
printf("You make your way safely out\n");
change_char_value("momentum", INCREASE, 1);
curchar->delve_active = 0;
curchar->delve->progress = 0;
delete_delve(curchar->id);
} else if (ret == 4 || ret == 14) {
printf("You make your way out, but this place exacts its price.\n");
printf("Choose one from the Rulebook\n");
curchar->delve_active = 0;
curchar->delve->progress = 0;
delete_delve(curchar->id);
} else if (ret == 2 || ret == 12) {
printf("A dire threat or imposing obstacle stands in your way\n");
printf("Reveal a danger and if you success, you make your way out!\n");
read_oracle_from_json(ORACLE_DELVE_DANGER, 0);
}
update_prompt();
}
void
cmd_locate_your_objective(char *cmd)
{
struct character *curchar = get_current_character();
double dval[2] = { -1.0, -1.0 };
int ret;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
printf("You must start a delve with 'delvethedepths' first\n");
return;
}
dval[0] = curchar->delve->progress;
dval[1] = get_int_from_cmd(cmd);
ret = progress_roll(dval);
if (ret == 8 || ret == 18) {
printf("You locate your objective and the situation favors you -> "\
"Rulebook\n");
curchar->delve_active = 0;
curchar->delve->progress = 0;
delete_delve(curchar->id);
} else if (ret == 4 || ret == 14) {
printf("You locate your objective but face an unforeseen complication "\
"-> Rulebook\n");
curchar->delve_active = 0;
curchar->delve->progress = 0;
delete_delve(curchar->id);
} else if (ret == 2 || ret == 12) {
locate_your_objective_failed();
}
update_prompt();
}
void
locate_your_objective_failed(void)
{
struct character *curchar = get_current_character();
int a;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
log_debug("No active delve.\n");
return;
}
printf("Please decide what to do\n\n");
printf("1\t - End your delve and pay the price -> Rulebook\n");
printf("2\t - Continue your delve -> progress is lost, difficulty +1\n");
a = ask_for_value("Enter a value between 1 and 2: ", 2);
if (a == 1) {
curchar->delve_active = 0;
curchar->delve->progress = 0;
delete_delve(curchar->id);
} else {
curchar->delve->progress = 0;
if (curchar->delve->difficulty < 5)
curchar->delve->difficulty += 1;
}
}
void
mark_delve_progress(int what)
{
struct character *curchar = get_current_character();
double amount = 0;
CURCHAR_CHECK();
if (curchar->delve_active == 0) {
printf("You need start a delve before you can mark progress\n");
return;
}
switch (curchar->delve->difficulty) {
case 1:
amount = 3;
break;
case 2:
amount = 2;
break;
case 3:
amount = 1;
break;
case 4:
amount = 0.5;
break;
case 5:
amount = 0.25;
break;
default:
curchar->delve->difficulty = 1;
log_errx(1, "Unknown difficulty. This should not happen. Set it to 1\n");
}
if (what == INCREASE)
curchar->delve->progress += amount;
else
curchar->delve->progress -= amount;
if (curchar->delve->progress > 10) {
printf("Your reached all milestones of your delve. Consider ending it\n");
curchar->delve->progress = 10;
} else if (curchar->delve->progress < 0)
curchar->delve->progress = 0;
update_prompt();
}
void
save_delve(void)
{
struct character *curchar = get_current_character();
char path[_POSIX_PATH_MAX];
json_object *root, *items, *id;
size_t temp_n, i;
int ret;
if (curchar == NULL) {
log_debug("No character loaded. No delve to save.\n");
return;
}
if (curchar->delve_active == 0) {
log_debug("No active delve to save.\n");
return;
}
json_object *cobj = json_object_new_object();
json_object_object_add(cobj, "id", json_object_new_int(curchar->id));
json_object_object_add(cobj, "difficulty",
json_object_new_int(curchar->delve->difficulty));
json_object_object_add(cobj, "progress",
json_object_new_double(curchar->delve->progress));
ret = snprintf(path, sizeof(path), "%s/delve.json", get_isscrolls_dir());
if (ret < 0 || (size_t)ret >= sizeof(path)) {
log_errx(1, "Path truncation happened. Buffer to short to fit %s\n", path);
}
if ((root = json_object_from_file(path)) == NULL) {
log_debug("No delve JSON file found\n");
root = json_object_new_object();
if (!root)
log_errx(1, "Cannot create delve JSON object\n");
items = json_object_new_array();
json_object_array_add(items, cobj);
json_object_object_add(root, "delve", items);
} else {
/* Get existing character array from JSON */
if (!json_object_object_get_ex(root, "delve", &items)) {
log_debug("Cannot find a [delve] array in %s\n", path);
items = json_object_new_array();
json_object_object_add(root, "delve", items);
}
temp_n = json_object_array_length(items);
for (i = 0; i < temp_n; i++) {
json_object *temp = json_object_array_get_idx(items, i);
json_object_object_get_ex(temp, "id", &id);
if (curchar->id == json_object_get_int(id)) {
log_debug("Update delve entry for %s\n", curchar->name);
json_object_array_del_idx(items, i, 1);
json_object_array_add(items, cobj);
goto out;
}
}
log_debug("No delve entry for %s found, adding new one\n", curchar->name);
json_object_array_add(items, cobj);
}
out:
if (json_object_to_file(path, root))
printf("Error saving %s\n", path);
else
log_debug("Successfully saved %s\n", path);
json_object_put(root);
}
void
delete_delve(int id)
{
char path[_POSIX_PATH_MAX];
json_object *root, *lid;
size_t temp_n, i;
int ret;
ret = snprintf(path, sizeof(path), "%s/delve.json", get_isscrolls_dir());
if (ret < 0 || (size_t)ret >= sizeof(path)) {
log_errx(1, "Path truncation happened. Buffer to short to fit %s\n", path);
}
if ((root = json_object_from_file(path)) == NULL) {
log_debug("No delve JSON file found\n");
return;
}
json_object *delve;
if (!json_object_object_get_ex(root, "delve", &delve)) {
log_debug("Cannot find a [delve] array in %s\n", path);
return;
}
temp_n = json_object_array_length(delve);
for (i = 0; i < temp_n; i++) {
json_object *temp = json_object_array_get_idx(delve, i);
json_object_object_get_ex(temp, "id", &lid);
if (id == json_object_get_int(lid)) {
json_object_array_del_idx(delve, i, 1);
log_debug("Deleted delve entry for %d\n", id);
}
}
if (json_object_to_file(path, root))
printf("Error saving %s\n", path);
else
log_debug("Successfully saved %s\n", path);
json_object_put(root);
}
void
load_delve(int id)
{
struct character *curchar = get_current_character();
char path[_POSIX_PATH_MAX];
json_object *root, *lid;
size_t temp_n, i;
int ret;
if (curchar == NULL) {
log_debug("No character loaded\n");
return;
}
ret = snprintf(path, sizeof(path), "%s/delve.json", get_isscrolls_dir());
if (ret < 0 || (size_t)ret >= sizeof(path)) {
log_errx(1, "Path truncation happened. Buffer to short to fit %s\n", path);
}
if ((root = json_object_from_file(path)) == NULL) {
log_debug("No delve JSON file found\n");
return;
}
json_object *delve;
if (!json_object_object_get_ex(root, "delve", &delve)) {
log_debug("Cannot find a [delve] array in %s\n", path);
return;
}
temp_n = json_object_array_length(delve);
for (i=0; i < temp_n; i++) {
json_object *temp = json_object_array_get_idx(delve, i);
json_object_object_get_ex(temp, "id", &lid);
if (id == json_object_get_int(lid)) {
log_debug("Loading delve for id: %d\n", json_object_get_int(lid));
curchar->delve->difficulty = validate_int(temp, "difficulty", 0, 5, 1);
curchar->delve->progress = validate_double(temp, "progress", 0, 10, 0);
}
}
json_object_put(root);
}