-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjourney.c
344 lines (290 loc) · 8.8 KB
/
journey.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
/*
* Copyright (c) 2021 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_undertake_a_journey(char *cmd)
{
struct character *curchar = get_current_character();
char *ep;
long lval;
int ival[2] = { -1, -1 };
int ret;
CURCHAR_CHECK();
ival[0] = curchar->wits;
if (strlen(cmd) > 0) {
errno = 0;
lval = strtol(cmd, &ep, 10);
if (cmd[0] == '\0' || *ep != '\0') {
printf("Please provide a number as argument\n");
return;
}
if ((errno == ERANGE || lval <= 0 || lval > 10)) {
printf("Please provide a number between 1 and 10\n");
return;
}
ival[1] = lval;
log_debug("Arg provided %d\n", ival[1]);
}
if (curchar->journey_active == 0) {
ask_for_journey_difficulty();
curchar->journey_active = 1;
}
ret = action_roll(ival);
if (ret == STRONG || ret == STRONG_MATCH) {
printf("You reach a waypoint and can choose one option -> Rulebook\n");
mark_journey_progress(INCREASE);
} else if (ret == WEAK || ret == WEAK_MATCH) {
printf("You reach a waypoint, but suffer -1 supply\n");
change_char_value("supply", DECREASE, 1);
mark_journey_progress(INCREASE);
} else if (ret == MISS || ret == MISS_MATCH)
printf("Pay the price -> Rulebook\n");
update_prompt();
}
void
cmd_reach_your_destination(char *cmd)
{
struct character *curchar = get_current_character();
double dval[2] = { -1.0, -1.0 };
int ret;
CURCHAR_CHECK();
if (curchar->journey_active == 0) {
printf("You must start a journey with 'undertakeajourney' first\n");
return;
}
dval[0] = curchar->j->progress;
dval[1] = get_int_from_cmd(cmd);
ret = progress_roll(dval);
if (ret == STRONG || ret == STRONG_MATCH) {
printf("You reach your destination and the situation favors you -> "\
"Rulebook\n");
curchar->journey_active = 0;
curchar->j->progress = 0;
delete_journey(curchar->id);
} else if (ret == WEAK || ret == WEAK_MATCH) {
printf("You reach your destination but face an unforeseen complication "\
"-> Rulebook\n");
curchar->journey_active = 0;
curchar->j->progress = 0;
delete_journey(curchar->id);
} else if (ret == MISS || ret == MISS_MATCH) {
reach_your_destination_failed();
}
update_prompt();
}
void
mark_journey_progress(int what)
{
struct character *curchar = get_current_character();
double amount = 0;
CURCHAR_CHECK();
if (curchar->journey_active == 0) {
printf("You need start a journey before you can mark progress\n");
return;
}
switch (curchar->j->difficulty) {
case 1:
amount = 3.0;
break;
case 2:
amount = 2.0;
break;
case 3:
amount = 1.0;
break;
case 4:
amount = 0.5;
break;
case 5:
amount = 0.25;
break;
default:
curchar->j->difficulty = 1;
log_errx(1, "Unknown difficulty. This should not happen. Set it to 1\n");
}
if (what == INCREASE)
curchar->j->progress += amount;
else
curchar->j->progress -= amount;
if (curchar->j->progress > 10) {
printf("Your reached all milestones of your journey. Consider ending it\n");
curchar->j->progress = 10;
} else if (curchar->j->progress < 0)
curchar->j->progress = 0;
update_prompt();
}
void
reach_your_destination_failed(void)
{
struct character *curchar = get_current_character();
int a;
CURCHAR_CHECK();
if (curchar->journey_active == 0) {
log_debug("No active journey.\n");
return;
}
printf("Please decide what to do\n\n");
printf("1\t - End your journey and pay the price -> Rulebook\n");
printf("2\t - Continue your journey -> progress is lost, difficulty +1\n");
a = ask_for_value("Enter a value between 1 and 2: ", 2);
if (a == 1) {
curchar->journey_active = 0;
curchar->j->progress = 0;
delete_journey(curchar->id);
} else {
curchar->j->progress = 0;
if (curchar->j->difficulty < 5)
curchar->j->difficulty += 1;
}
}
void
save_journey(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 journey to save.\n");
return;
}
if (curchar->journey_active == 0) {
log_debug("No active journey 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->j->difficulty));
json_object_object_add(cobj, "progress",
json_object_new_double(curchar->j->progress));
ret = snprintf(path, sizeof(path), "%s/journey.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 journey JSON file found\n");
root = json_object_new_object();
if (!root)
log_errx(1, "Cannot create journey JSON object\n");
items = json_object_new_array();
json_object_array_add(items, cobj);
json_object_object_add(root, "journey", items);
} else {
/* Get existing character array from JSON */
if (!json_object_object_get_ex(root, "journey", &items)) {
log_debug("Cannot find a [journey] array in %s\n", path);
items = json_object_new_array();
json_object_object_add(root, "journey", 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 journey 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 journey 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_journey(int id)
{
char path[_POSIX_PATH_MAX];
json_object *root, *lid;
size_t temp_n, i;
int ret;
ret = snprintf(path, sizeof(path), "%s/journey.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 journey JSON file found\n");
return;
}
json_object *journey;
if (!json_object_object_get_ex(root, "journey", &journey)) {
log_debug("Cannot find a [journey] array in %s\n", path);
return;
}
temp_n = json_object_array_length(journey);
for (i = 0; i < temp_n; i++) {
json_object *temp = json_object_array_get_idx(journey, i);
json_object_object_get_ex(temp, "id", &lid);
if (id == json_object_get_int(lid)) {
json_object_array_del_idx(journey, i, 1);
log_debug("Deleted journey 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_journey(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/journey.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 journey JSON file found\n");
return;
}
json_object *journey;
if (!json_object_object_get_ex(root, "journey", &journey)) {
log_debug("Cannot find a [journey] array in %s\n", path);
return;
}
temp_n = json_object_array_length(journey);
for (i=0; i < temp_n; i++) {
json_object *temp = json_object_array_get_idx(journey, i);
json_object_object_get_ex(temp, "id", &lid);
if (id == json_object_get_int(lid)) {
log_debug("Loading journey for id: %d\n", json_object_get_int(lid));
curchar->j->difficulty = validate_int(temp, "difficulty", 0, 5, 1);
curchar->j->progress = validate_double(temp, "progress", 0, 10, 0);
}
}
json_object_put(root);
}