2 b0cb8082 2021-09-11 xhr * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
4 b0cb8082 2021-09-11 xhr * Permission to use, copy, modify, and distribute this software for any
5 b0cb8082 2021-09-11 xhr * purpose with or without fee is hereby granted, provided that the above
6 b0cb8082 2021-09-11 xhr * copyright notice and this permission notice appear in all copies.
8 b0cb8082 2021-09-11 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 b0cb8082 2021-09-11 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 b0cb8082 2021-09-11 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 b0cb8082 2021-09-11 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 b0cb8082 2021-09-11 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 b0cb8082 2021-09-11 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 b0cb8082 2021-09-11 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 b0cb8082 2021-09-11 xhr #include <json-c/json.h>
19 e42eeec2 2021-09-14 xhr #include <errno.h>
20 b0cb8082 2021-09-11 xhr #include <limits.h>
21 b0cb8082 2021-09-11 xhr #include <string.h>
23 b0cb8082 2021-09-11 xhr #include "isscrolls.h"
26 361591bb 2021-09-16 xhr cmd_enter_the_fray(char *cmd)
28 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
29 361591bb 2021-09-16 xhr char stat[MAX_STAT_LEN];
30 b0cb8082 2021-09-11 xhr int ival[2] = { -1, -1 };
33 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
34 b0cb8082 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
38 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
39 219a93cc 2021-09-16 xhr if (ret >= 10) {
41 b0cb8082 2021-09-11 xhr printf("\nPlease specify the stat you'd like to use in this move\n\n");
42 b0cb8082 2021-09-11 xhr printf("heart\t- You are facing off against your foe\n");
43 b0cb8082 2021-09-11 xhr printf("shadow \t- You are moving into position against or strike without warning\n");
44 b0cb8082 2021-09-11 xhr printf("wits\t- You are ambushed\n");
45 b0cb8082 2021-09-11 xhr printf("Example: enterthefray wits\n\n");
47 361591bb 2021-09-16 xhr } else if (ret <= -20) {
51 a14b5708 2021-09-16 xhr if (strcasecmp(stat, "wits") == 0) {
52 361591bb 2021-09-16 xhr ival[0] = curchar->wits;
53 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "shadow") == 0) {
54 b0cb8082 2021-09-11 xhr ival[0] = curchar->shadow;
55 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "heart") == 0) {
56 b0cb8082 2021-09-11 xhr ival[0] = curchar->heart;
60 85780592 2021-09-11 xhr if (curchar->fight_active == 0) {
61 85780592 2021-09-11 xhr ask_for_fight_difficulty();
62 85780592 2021-09-11 xhr curchar->fight_active = 1;
64 85780592 2021-09-11 xhr printf("You are already in a fight\n");
68 b0cb8082 2021-09-11 xhr ret = action_roll(ival);
69 b0cb8082 2021-09-11 xhr if (ret == 8) {
70 b0cb8082 2021-09-11 xhr change_char_value("momentum", INCREASE, 2);
71 51a1ad92 2021-09-11 xhr set_initiative(1);
72 b0cb8082 2021-09-11 xhr printf("You have initiative\n");
73 b0cb8082 2021-09-11 xhr } else if (ret == 4) {
74 b0cb8082 2021-09-11 xhr printf("You may choose one boost -> Rulebook\n");
76 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
78 b0cb8082 2021-09-11 xhr update_prompt();
82 319d4050 2021-09-16 xhr cmd_end_the_fight(char *cmd)
84 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
85 319d4050 2021-09-16 xhr double dval[2] = { -1.0, -1.0 };
88 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
89 b0cb8082 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
93 51a1ad92 2021-09-11 xhr if (curchar->fight_active == 0) {
94 51a1ad92 2021-09-11 xhr printf("You are not in a fight. Enter one with enterthefray\n");
98 319d4050 2021-09-16 xhr dval[0] = curchar->fight->progress;
99 319d4050 2021-09-16 xhr dval[1] = get_int_from_cmd(cmd);
100 319d4050 2021-09-16 xhr ret = progress_roll(dval);
101 b0cb8082 2021-09-11 xhr if (ret == 8) {
102 b0cb8082 2021-09-11 xhr printf("The foe is no longer in the fight -> Rulebook\n");
103 b0cb8082 2021-09-11 xhr } else if (ret == 4) {
104 b0cb8082 2021-09-11 xhr printf("The foe is no longer in the fight, but you must chose one option -> Rulebook\n");
106 b0cb8082 2021-09-11 xhr printf("You lost the fight. Pay the price -> Rulebook\n");
108 b0cb8082 2021-09-11 xhr curchar->fight_active = 0;
109 a6733253 2021-09-11 xhr curchar->fight->progress = 0;
110 b0cb8082 2021-09-11 xhr delete_fight(curchar->id);
111 b0cb8082 2021-09-11 xhr update_prompt();
115 e42eeec2 2021-09-14 xhr cmd_endure_harm(char *cmd)
117 babd3f1c 2021-09-13 xhr struct character *curchar = get_current_character();
118 babd3f1c 2021-09-13 xhr int ival[2] = { -1, -1 };
119 e42eeec2 2021-09-14 xhr int ret, hr, suffer;
121 babd3f1c 2021-09-13 xhr if (curchar == NULL) {
122 babd3f1c 2021-09-13 xhr printf("No character loaded. Use 'cd' to load a character\n");
128 e42eeec2 2021-09-14 xhr /* We are in a fight, so we can suffer harm equal to our foe's rank */
129 e42eeec2 2021-09-14 xhr if (curchar->fight_active == 1) {
130 e42eeec2 2021-09-14 xhr hr = curchar->health - curchar->fight->difficulty;
131 e42eeec2 2021-09-14 xhr suffer = curchar->fight->difficulty;
133 e42eeec2 2021-09-14 xhr /* We are not in a fight, so the player can specify the amount of
134 e42eeec2 2021-09-14 xhr * harm to suffer */
135 319d4050 2021-09-16 xhr ival[1] = get_int_from_cmd(cmd);
136 319d4050 2021-09-16 xhr if (ival[1] == -1) {
137 e42eeec2 2021-09-14 xhr /* We are not in a fight and there is not argument provided */
138 e42eeec2 2021-09-14 xhr printf("Please specify the amount of harm you want to suffer\n\n");
139 e42eeec2 2021-09-14 xhr printf("Example: endureharm 2\n");
143 319d4050 2021-09-16 xhr hr = curchar->health - ival[1];
144 319d4050 2021-09-16 xhr suffer = ival[1];
145 319d4050 2021-09-16 xhr log_debug("Arg provided %d, hr: %d\n", ival[1], hr);
146 319d4050 2021-09-16 xhr /* Reset ival[1] since we don't need a bonus */
147 319d4050 2021-09-16 xhr ival[1] = -1;
150 e42eeec2 2021-09-14 xhr if (hr >= 0) {
151 e42eeec2 2021-09-14 xhr curchar->health -= suffer;
152 babd3f1c 2021-09-13 xhr printf("You suffer %d harm and your health is down to %d\n",
153 e42eeec2 2021-09-14 xhr suffer, curchar->health);
154 babd3f1c 2021-09-13 xhr } else if (hr < 0) {
155 babd3f1c 2021-09-13 xhr /* Health is 0, so suffer -momentum equal to remaining health */
156 babd3f1c 2021-09-13 xhr log_debug("hr < 0: %d\n", hr);
157 babd3f1c 2021-09-13 xhr curchar->health = 0;
158 babd3f1c 2021-09-13 xhr curchar->momentum -= (hr * (-1));
159 3f749338 2021-09-13 xhr printf("You suffer %d harm and since your health is %d, your "\
160 babd3f1c 2021-09-13 xhr "momentum is down to %d\n",
161 e42eeec2 2021-09-14 xhr suffer, curchar->health,
162 babd3f1c 2021-09-13 xhr curchar->momentum);
165 babd3f1c 2021-09-13 xhr ival[0] = curchar->iron;
166 babd3f1c 2021-09-13 xhr if (curchar->heart > curchar->iron) {
167 babd3f1c 2021-09-13 xhr ival[0] = curchar->heart;
170 babd3f1c 2021-09-13 xhr ret = action_roll(ival);
171 babd3f1c 2021-09-13 xhr if (ret == 8) {
172 babd3f1c 2021-09-13 xhr printf("You need to choose one option -> Rulebook\n");
173 babd3f1c 2021-09-13 xhr } else if (ret == 4) {
174 babd3f1c 2021-09-13 xhr printf("You press on\n");
176 babd3f1c 2021-09-13 xhr change_char_value("momentum", DECREASE, 1);
177 babd3f1c 2021-09-13 xhr if (curchar->health == 0)
178 babd3f1c 2021-09-13 xhr printf("Mark either maimed or wounded or on the oracle table -> Rulebook\n");
183 361591bb 2021-09-16 xhr cmd_strike(char *cmd)
185 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
186 361591bb 2021-09-16 xhr char stat[MAX_STAT_LEN];
187 b0cb8082 2021-09-11 xhr int ival[2] = { -1, -1 };
190 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
191 b0cb8082 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
195 51a1ad92 2021-09-11 xhr if (curchar->fight_active == 0) {
196 51a1ad92 2021-09-11 xhr printf("You are not in a fight. Enter one with enterthefray\n");
200 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
201 219a93cc 2021-09-16 xhr if (ret >= 10) {
203 b0cb8082 2021-09-11 xhr printf("Please specify the stat you'd like to use in this move\n\n");
204 b0cb8082 2021-09-11 xhr printf("iron\t- You attack in close quarters\n");
205 b0cb8082 2021-09-11 xhr printf("edge\t- You attack at range\n");
206 b0cb8082 2021-09-11 xhr printf("Example: strike iron\n");
208 361591bb 2021-09-16 xhr } else if (ret <= -20)
211 a14b5708 2021-09-16 xhr if (strcasecmp(stat, "iron") == 0) {
212 b0cb8082 2021-09-11 xhr ival[0] = curchar->iron;
213 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "edge") == 0) {
214 b0cb8082 2021-09-11 xhr ival[0] = curchar->edge;
218 b0cb8082 2021-09-11 xhr ret = action_roll(ival);
219 b0cb8082 2021-09-11 xhr if (ret == 8) {
220 b0cb8082 2021-09-11 xhr printf("You inflict +1 harm and retain initiative\n");
221 51a1ad92 2021-09-11 xhr set_initiative(1);
222 b0cb8082 2021-09-11 xhr mark_fight_progress();
223 b0cb8082 2021-09-11 xhr mark_fight_progress();
224 b0cb8082 2021-09-11 xhr } else if (ret == 4) {
225 b0cb8082 2021-09-11 xhr printf("You inflict harm and lose initiative\n");
226 51a1ad92 2021-09-11 xhr set_initiative(0);
227 b0cb8082 2021-09-11 xhr mark_fight_progress();
229 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
230 c5376c59 2021-09-14 xhr set_initiative(0);
231 c5376c59 2021-09-14 xhr update_prompt();
236 361591bb 2021-09-16 xhr cmd_clash(char *cmd)
238 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
239 361591bb 2021-09-16 xhr char stat[MAX_STAT_LEN];
240 b0cb8082 2021-09-11 xhr int ival[2] = { -1, -1 };
243 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
244 b0cb8082 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
248 51a1ad92 2021-09-11 xhr if (curchar->fight_active == 0) {
249 51a1ad92 2021-09-11 xhr printf("You are not in a fight. Enter one with enterthefray\n");
253 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
254 219a93cc 2021-09-16 xhr if (ret >= 10) {
256 b0cb8082 2021-09-11 xhr printf("Please specify the stat you'd like to use in this move\n\n");
257 b0cb8082 2021-09-11 xhr printf("iron\t- You fight in close quarters\n");
258 b0cb8082 2021-09-11 xhr printf("edge\t- You fight at range\n");
259 b0cb8082 2021-09-11 xhr printf("Example: clash iron\n");
261 361591bb 2021-09-16 xhr } else if (ret <= -20)
264 a14b5708 2021-09-16 xhr if (strcasecmp(stat, "iron") == 0) {
265 b0cb8082 2021-09-11 xhr ival[0] = curchar->iron;
266 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "edge") == 0) {
267 b0cb8082 2021-09-11 xhr ival[0] = curchar->edge;
271 b0cb8082 2021-09-11 xhr ret = action_roll(ival);
272 b0cb8082 2021-09-11 xhr if (ret == 8) {
273 cc8359d1 2021-09-11 xhr printf("You inflict harm, regain initiative and can choose one option -> Rulebook\n");
274 51a1ad92 2021-09-11 xhr set_initiative(1);
275 b0cb8082 2021-09-11 xhr mark_fight_progress();
276 b0cb8082 2021-09-11 xhr } else if (ret == 4) {
277 b0cb8082 2021-09-11 xhr printf("You inflict harm and lose initiative. Pay the price -> Rulebook\n");
278 51a1ad92 2021-09-11 xhr set_initiative(0);
279 b0cb8082 2021-09-11 xhr mark_fight_progress();
281 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
282 c5376c59 2021-09-14 xhr set_initiative(0);
283 c5376c59 2021-09-14 xhr update_prompt();
288 361591bb 2021-09-16 xhr cmd_battle(char *cmd)
290 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
291 361591bb 2021-09-16 xhr char stat[MAX_STAT_LEN];
292 b0cb8082 2021-09-11 xhr int ival[2] = { -1, -1 };
295 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
296 b0cb8082 2021-09-11 xhr printf("No character loaded. Use 'cd' to load a character\n");
300 51a1ad92 2021-09-11 xhr if (curchar->fight_active == 0) {
301 51a1ad92 2021-09-11 xhr printf("You are not in a fight. Enter one with enterthefray\n");
305 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
306 219a93cc 2021-09-16 xhr if (ret >= 10) {
308 b0cb8082 2021-09-11 xhr printf("Please specify the stat you'd like to use in this move\n\n");
309 b0cb8082 2021-09-11 xhr printf("edge\t- Fight at range, or using your speed and the terrain\n");
310 b0cb8082 2021-09-11 xhr printf("heart\t- Fight depending on your courage, allies, or companions\n");
311 b0cb8082 2021-09-11 xhr printf("iron\t- Fight in close to overpower your opponents\n");
312 b0cb8082 2021-09-11 xhr printf("shadow\t- Fight using trickery to befuddle your opponents\n");
313 b0cb8082 2021-09-11 xhr printf("wits\t- Fight using careful tactics to outsmart your opponents\n\n");
314 b0cb8082 2021-09-11 xhr printf("Example: battle iron\n");
316 361591bb 2021-09-16 xhr } else if (ret <= -20)
319 a14b5708 2021-09-16 xhr if (strcasecmp(stat, "iron") == 0) {
320 b0cb8082 2021-09-11 xhr ival[0] = curchar->iron;
321 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "wits") == 0) {
322 b0cb8082 2021-09-11 xhr ival[0] = curchar->wits;
323 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "edge") == 0) {
324 a14b5708 2021-09-16 xhr ival[0] = curchar->edge;
325 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "shadow") == 0) {
326 b0cb8082 2021-09-11 xhr ival[0] = curchar->shadow;
327 a14b5708 2021-09-16 xhr } else if (strcasecmp(stat, "heart") == 0) {
328 b0cb8082 2021-09-11 xhr ival[0] = curchar->heart;
332 b0cb8082 2021-09-11 xhr ret = action_roll(ival);
333 b0cb8082 2021-09-11 xhr if (ret == 8) {
334 b0cb8082 2021-09-11 xhr change_char_value("momentum", INCREASE, 2);
335 b0cb8082 2021-09-11 xhr printf("You achieve your objective unconditionally\n");
336 b0cb8082 2021-09-11 xhr } else if (ret == 4) /* weak hit */
337 b0cb8082 2021-09-11 xhr printf("You achieve your objective, but not without a cost -> Rulebook\n");
339 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
343 51a1ad92 2021-09-11 xhr set_initiative(int what)
345 51a1ad92 2021-09-11 xhr struct character *curchar = get_current_character();
347 51a1ad92 2021-09-11 xhr if (curchar == NULL) {
348 700a297e 2021-09-16 xhr log_debug("No character loaded. Cannot set initiative\n");
352 51a1ad92 2021-09-11 xhr if (curchar->fight_active == 0) {
353 51a1ad92 2021-09-11 xhr printf("You need start a fight before you can mark progress\n");
357 51a1ad92 2021-09-11 xhr if (what == 1)
358 51a1ad92 2021-09-11 xhr curchar->fight->initiative = 1;
360 51a1ad92 2021-09-11 xhr curchar->fight->initiative = 0;
364 b0cb8082 2021-09-11 xhr mark_fight_progress()
366 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
368 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
369 b0cb8082 2021-09-11 xhr log_debug("No character loaded. Cannot calculate progress\n");
373 b0cb8082 2021-09-11 xhr if (curchar->fight_active == 0) {
374 b0cb8082 2021-09-11 xhr printf("You need start a fight before you can mark progress\n");
378 a6733253 2021-09-11 xhr switch (curchar->fight->difficulty) {
380 a6733253 2021-09-11 xhr curchar->fight->progress += 3;
383 a6733253 2021-09-11 xhr curchar->fight->progress += 2;
386 a6733253 2021-09-11 xhr curchar->fight->progress += 1;
389 a6733253 2021-09-11 xhr curchar->fight->progress += 0.5;
392 a6733253 2021-09-11 xhr curchar->fight->progress += 0.25;
395 b82c1531 2021-09-16 xhr curchar->fight->difficulty = 1;
396 b82c1531 2021-09-16 xhr log_errx(1, "Unknown difficulty. This should not happen. Set to 1\n");
399 a6733253 2021-09-11 xhr if (curchar->fight->progress > 10) {
400 b0cb8082 2021-09-11 xhr printf("Your fight is successful. Consider ending it\n");
401 a6733253 2021-09-11 xhr curchar->fight->progress = 10;
404 b0cb8082 2021-09-11 xhr update_prompt();
408 b0cb8082 2021-09-11 xhr save_fight()
410 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
411 b0cb8082 2021-09-11 xhr char path[_POSIX_PATH_MAX];
412 d5faed8f 2021-09-16 xhr json_object *root, *items, *id;
413 b0cb8082 2021-09-11 xhr int temp_n, i;
415 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
416 b0cb8082 2021-09-11 xhr log_debug("No character loaded. No fight to save.\n");
420 b0cb8082 2021-09-11 xhr if (curchar->fight_active == 0) {
421 b0cb8082 2021-09-11 xhr log_debug("No active fight to save.\n");
425 b0cb8082 2021-09-11 xhr json_object *cobj = json_object_new_object();
426 b0cb8082 2021-09-11 xhr json_object_object_add(cobj, "id", json_object_new_int(curchar->id));
427 a6733253 2021-09-11 xhr json_object_object_add(cobj, "difficulty", json_object_new_int(curchar->fight->difficulty));
428 a6733253 2021-09-11 xhr json_object_object_add(cobj, "progress", json_object_new_double(curchar->fight->progress));
429 51a1ad92 2021-09-11 xhr json_object_object_add(cobj, "initiative", json_object_new_int(curchar->fight->initiative));
431 b0cb8082 2021-09-11 xhr snprintf(path, sizeof(path), "%s/fight.json", get_isscrolls_dir());
432 b0cb8082 2021-09-11 xhr if ((root = json_object_from_file(path)) == NULL) {
433 b0cb8082 2021-09-11 xhr log_debug("No fight JSON file found\n");
434 b0cb8082 2021-09-11 xhr root = json_object_new_object();
436 b0cb8082 2021-09-11 xhr log_errx(1, "Cannot create fight JSON object\n");
438 b0cb8082 2021-09-11 xhr items = json_object_new_array();
439 b0cb8082 2021-09-11 xhr json_object_array_add(items, cobj);
440 b0cb8082 2021-09-11 xhr json_object_object_add(root, "fight", items);
442 b0cb8082 2021-09-11 xhr /* Get existing character array from JSON */
443 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "fight", &items)) {
444 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [fight] array in %s. Create one\n", path);
445 fc6936c9 2021-09-16 xhr items = json_object_new_array();
446 fc6936c9 2021-09-16 xhr json_object_object_add(root, "fight", items);
449 b0cb8082 2021-09-11 xhr temp_n = json_object_array_length(items);
450 b0cb8082 2021-09-11 xhr for (i = 0; i < temp_n; i++) {
451 b0cb8082 2021-09-11 xhr json_object *temp = json_object_array_get_idx(items, i);
452 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &id);
453 b0cb8082 2021-09-11 xhr if (curchar->id == json_object_get_int(id)) {
454 b0cb8082 2021-09-11 xhr log_debug("Update fight entry for %s\n", curchar->name);
455 b0cb8082 2021-09-11 xhr json_object_array_del_idx(items, i, 1);
456 b0cb8082 2021-09-11 xhr json_object_array_add(items, cobj);
460 b0cb8082 2021-09-11 xhr log_debug("No fight entry for %s found, adding new one\n", curchar->name);
461 b0cb8082 2021-09-11 xhr json_object_array_add(items, cobj);
465 b0cb8082 2021-09-11 xhr if (json_object_to_file(path, root))
466 b0cb8082 2021-09-11 xhr printf("Error saving %s\n", path);
468 b0cb8082 2021-09-11 xhr log_debug("Successfully saved %s\n", path);
470 b0cb8082 2021-09-11 xhr json_object_put(root);
474 b0cb8082 2021-09-11 xhr delete_fight(int id)
476 b0cb8082 2021-09-11 xhr char path[_POSIX_PATH_MAX];
477 d5faed8f 2021-09-16 xhr json_object *root, *lid;
478 b0cb8082 2021-09-11 xhr int temp_n, i;
480 b0cb8082 2021-09-11 xhr snprintf(path, sizeof(path), "%s/fight.json", get_isscrolls_dir());
481 b0cb8082 2021-09-11 xhr if ((root = json_object_from_file(path)) == NULL) {
482 b0cb8082 2021-09-11 xhr log_debug("No fight JSON file found\n");
486 fc6936c9 2021-09-16 xhr json_object *fight;
487 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "fight", &fight)) {
488 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [fight] array in %s\n", path);
492 b0cb8082 2021-09-11 xhr temp_n = json_object_array_length(fight);
493 b0cb8082 2021-09-11 xhr for (i = 0; i < temp_n; i++) {
494 b0cb8082 2021-09-11 xhr json_object *temp = json_object_array_get_idx(fight, i);
495 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
496 b0cb8082 2021-09-11 xhr if (id == json_object_get_int(lid)) {
497 b0cb8082 2021-09-11 xhr json_object_array_del_idx(fight, i, 1);
498 b0cb8082 2021-09-11 xhr log_debug("Deleted fight entry for %d\n", id);
502 b0cb8082 2021-09-11 xhr if (json_object_to_file(path, root))
503 b0cb8082 2021-09-11 xhr printf("Error saving %s\n", path);
505 b0cb8082 2021-09-11 xhr log_debug("Successfully saved %s\n", path);
507 b0cb8082 2021-09-11 xhr json_object_put(root);
511 b0cb8082 2021-09-11 xhr load_fight(int id)
513 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
514 b0cb8082 2021-09-11 xhr char path[_POSIX_PATH_MAX];
515 d5faed8f 2021-09-16 xhr json_object *root, *lid;
516 b0cb8082 2021-09-11 xhr int temp_n, i;
518 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
519 b0cb8082 2021-09-11 xhr log_debug("No character loaded\n");
523 b0cb8082 2021-09-11 xhr snprintf(path, sizeof(path), "%s/fight.json", get_isscrolls_dir());
524 b0cb8082 2021-09-11 xhr if ((root = json_object_from_file(path)) == NULL) {
525 b0cb8082 2021-09-11 xhr log_debug("No fight JSON file found\n");
529 fc6936c9 2021-09-16 xhr json_object *fight;
530 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "fight", &fight)) {
531 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [fight] array in %s\n", path);
535 b0cb8082 2021-09-11 xhr temp_n = json_object_array_length(fight);
536 b0cb8082 2021-09-11 xhr for (i=0; i < temp_n; i++) {
537 b0cb8082 2021-09-11 xhr json_object *temp = json_object_array_get_idx(fight, i);
538 d5faed8f 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
539 b0cb8082 2021-09-11 xhr if (id == json_object_get_int(lid)) {
540 b0cb8082 2021-09-11 xhr log_debug("Loading fight for id: %d\n", json_object_get_int(lid));
542 61d535c3 2021-09-16 xhr json_object *cval;
543 61d535c3 2021-09-16 xhr json_object_object_get_ex(temp, "difficulty", &cval);
544 61d535c3 2021-09-16 xhr curchar->fight->difficulty = json_object_get_int(cval);
545 61d535c3 2021-09-16 xhr json_object_object_get_ex(temp, "progress", &cval);
546 61d535c3 2021-09-16 xhr curchar->fight->progress = json_object_get_double(cval);
547 61d535c3 2021-09-16 xhr json_object_object_get_ex(temp, "initiative", &cval);
548 61d535c3 2021-09-16 xhr curchar->fight->initiative = json_object_get_int(cval);
552 b0cb8082 2021-09-11 xhr json_object_put(root);
556 b0cb8082 2021-09-11 xhr ask_for_fight_difficulty()
558 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
560 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
561 b0cb8082 2021-09-11 xhr log_debug("No character loaded\n");
565 b0cb8082 2021-09-11 xhr printf("Please set a difficulty for your fight\n\n");
566 b0cb8082 2021-09-11 xhr printf("1\t - Troublesome foe (3 progress per harm)\n");
567 b0cb8082 2021-09-11 xhr printf("2\t - Dangerous foe (2 progress per harm)\n");
568 b0cb8082 2021-09-11 xhr printf("3\t - Formidable foe (2 progress per harm)\n");
569 b0cb8082 2021-09-11 xhr printf("4\t - Extreme foe (2 ticks per harm)\n");
570 b0cb8082 2021-09-11 xhr printf("5\t - Epic foe (1 tick per harm)\n\n");
572 a6733253 2021-09-11 xhr curchar->fight->difficulty = ask_for_value("Enter a value between 1 and 5: ", 5);