Blame


1 b0cb8082 2021-09-11 xhr /*
2 b0cb8082 2021-09-11 xhr * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
3 b0cb8082 2021-09-11 xhr *
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.
7 b0cb8082 2021-09-11 xhr *
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.
15 b0cb8082 2021-09-11 xhr */
16 b0cb8082 2021-09-11 xhr
17 b0cb8082 2021-09-11 xhr #include <json-c/json.h>
18 b0cb8082 2021-09-11 xhr
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>
22 b0cb8082 2021-09-11 xhr
23 b0cb8082 2021-09-11 xhr #include "isscrolls.h"
24 b0cb8082 2021-09-11 xhr
25 b0cb8082 2021-09-11 xhr void
26 361591bb 2021-09-16 xhr cmd_enter_the_fray(char *cmd)
27 b0cb8082 2021-09-11 xhr {
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 };
31 b0cb8082 2021-09-11 xhr int ret;
32 b0cb8082 2021-09-11 xhr
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");
35 c4133607 2021-09-11 xhr return;
36 b0cb8082 2021-09-11 xhr }
37 b0cb8082 2021-09-11 xhr
38 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
39 219a93cc 2021-09-16 xhr if (ret >= 10) {
40 b0cb8082 2021-09-11 xhr info:
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");
46 b0cb8082 2021-09-11 xhr return;
47 361591bb 2021-09-16 xhr } else if (ret <= -20) {
48 361591bb 2021-09-16 xhr return;
49 b0cb8082 2021-09-11 xhr }
50 b0cb8082 2021-09-11 xhr
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;
57 b0cb8082 2021-09-11 xhr } else
58 b0cb8082 2021-09-11 xhr goto info;
59 85780592 2021-09-11 xhr
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;
63 85780592 2021-09-11 xhr } else {
64 85780592 2021-09-11 xhr printf("You are already in a fight\n");
65 85780592 2021-09-11 xhr return;
66 85780592 2021-09-11 xhr }
67 b0cb8082 2021-09-11 xhr
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");
75 b0cb8082 2021-09-11 xhr } else
76 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
77 b0cb8082 2021-09-11 xhr
78 b0cb8082 2021-09-11 xhr update_prompt();
79 b0cb8082 2021-09-11 xhr }
80 b0cb8082 2021-09-11 xhr
81 b0cb8082 2021-09-11 xhr void
82 319d4050 2021-09-16 xhr cmd_end_the_fight(char *cmd)
83 b0cb8082 2021-09-11 xhr {
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 };
86 b0cb8082 2021-09-11 xhr int ret;
87 b0cb8082 2021-09-11 xhr
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");
90 b0cb8082 2021-09-11 xhr return;
91 b0cb8082 2021-09-11 xhr }
92 b0cb8082 2021-09-11 xhr
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");
95 51a1ad92 2021-09-11 xhr return;
96 51a1ad92 2021-09-11 xhr }
97 51a1ad92 2021-09-11 xhr
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");
105 b0cb8082 2021-09-11 xhr } else {
106 b0cb8082 2021-09-11 xhr printf("You lost the fight. Pay the price -> Rulebook\n");
107 b0cb8082 2021-09-11 xhr }
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();
112 babd3f1c 2021-09-13 xhr }
113 babd3f1c 2021-09-13 xhr
114 babd3f1c 2021-09-13 xhr void
115 e42eeec2 2021-09-14 xhr cmd_endure_harm(char *cmd)
116 babd3f1c 2021-09-13 xhr {
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;
120 babd3f1c 2021-09-13 xhr
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");
123 babd3f1c 2021-09-13 xhr return;
124 babd3f1c 2021-09-13 xhr }
125 babd3f1c 2021-09-13 xhr
126 e42eeec2 2021-09-14 xhr suffer = 0;
127 babd3f1c 2021-09-13 xhr
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;
132 e42eeec2 2021-09-14 xhr } else {
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");
140 e42eeec2 2021-09-14 xhr return;
141 e42eeec2 2021-09-14 xhr }
142 319d4050 2021-09-16 xhr
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;
148 e42eeec2 2021-09-14 xhr }
149 e42eeec2 2021-09-14 xhr
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);
163 babd3f1c 2021-09-13 xhr }
164 babd3f1c 2021-09-13 xhr
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;
168 babd3f1c 2021-09-13 xhr }
169 babd3f1c 2021-09-13 xhr
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");
175 babd3f1c 2021-09-13 xhr } else {
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");
179 babd3f1c 2021-09-13 xhr }
180 b0cb8082 2021-09-11 xhr }
181 b0cb8082 2021-09-11 xhr
182 b0cb8082 2021-09-11 xhr void
183 361591bb 2021-09-16 xhr cmd_strike(char *cmd)
184 b0cb8082 2021-09-11 xhr {
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 };
188 b0cb8082 2021-09-11 xhr int ret;
189 b0cb8082 2021-09-11 xhr
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");
192 b0cb8082 2021-09-11 xhr return;
193 b0cb8082 2021-09-11 xhr }
194 b0cb8082 2021-09-11 xhr
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");
197 51a1ad92 2021-09-11 xhr return;
198 51a1ad92 2021-09-11 xhr }
199 51a1ad92 2021-09-11 xhr
200 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
201 219a93cc 2021-09-16 xhr if (ret >= 10) {
202 b0cb8082 2021-09-11 xhr info:
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");
207 b0cb8082 2021-09-11 xhr return;
208 361591bb 2021-09-16 xhr } else if (ret <= -20)
209 361591bb 2021-09-16 xhr return;
210 b0cb8082 2021-09-11 xhr
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;
215 b0cb8082 2021-09-11 xhr } else
216 b0cb8082 2021-09-11 xhr goto info;
217 b0cb8082 2021-09-11 xhr
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();
228 f6892e74 2021-09-14 xhr } else {
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();
232 f6892e74 2021-09-14 xhr }
233 b0cb8082 2021-09-11 xhr }
234 b0cb8082 2021-09-11 xhr
235 b0cb8082 2021-09-11 xhr void
236 361591bb 2021-09-16 xhr cmd_clash(char *cmd)
237 b0cb8082 2021-09-11 xhr {
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 };
241 b0cb8082 2021-09-11 xhr int ret;
242 b0cb8082 2021-09-11 xhr
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");
245 b0cb8082 2021-09-11 xhr return;
246 b0cb8082 2021-09-11 xhr }
247 b0cb8082 2021-09-11 xhr
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");
250 51a1ad92 2021-09-11 xhr return;
251 51a1ad92 2021-09-11 xhr }
252 51a1ad92 2021-09-11 xhr
253 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
254 219a93cc 2021-09-16 xhr if (ret >= 10) {
255 b0cb8082 2021-09-11 xhr info:
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");
260 b0cb8082 2021-09-11 xhr return;
261 361591bb 2021-09-16 xhr } else if (ret <= -20)
262 361591bb 2021-09-16 xhr return;
263 b0cb8082 2021-09-11 xhr
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;
268 b0cb8082 2021-09-11 xhr } else
269 b0cb8082 2021-09-11 xhr goto info;
270 b0cb8082 2021-09-11 xhr
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();
280 c5376c59 2021-09-14 xhr } else {
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();
284 c5376c59 2021-09-14 xhr }
285 b0cb8082 2021-09-11 xhr }
286 b0cb8082 2021-09-11 xhr
287 b0cb8082 2021-09-11 xhr void
288 361591bb 2021-09-16 xhr cmd_battle(char *cmd)
289 b0cb8082 2021-09-11 xhr {
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 };
293 b0cb8082 2021-09-11 xhr int ret;
294 b0cb8082 2021-09-11 xhr
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");
297 51a1ad92 2021-09-11 xhr return;
298 51a1ad92 2021-09-11 xhr }
299 51a1ad92 2021-09-11 xhr
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");
302 b0cb8082 2021-09-11 xhr return;
303 b0cb8082 2021-09-11 xhr }
304 b0cb8082 2021-09-11 xhr
305 361591bb 2021-09-16 xhr ret = get_args_from_cmd(cmd, stat, &ival[1]);
306 219a93cc 2021-09-16 xhr if (ret >= 10) {
307 b0cb8082 2021-09-11 xhr info:
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");
315 b0cb8082 2021-09-11 xhr return;
316 361591bb 2021-09-16 xhr } else if (ret <= -20)
317 361591bb 2021-09-16 xhr return;
318 b0cb8082 2021-09-11 xhr
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;
329 b0cb8082 2021-09-11 xhr } else
330 b0cb8082 2021-09-11 xhr goto info;
331 b0cb8082 2021-09-11 xhr
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");
338 b0cb8082 2021-09-11 xhr else
339 b0cb8082 2021-09-11 xhr printf("Pay the price -> Rulebook\n");
340 b0cb8082 2021-09-11 xhr }
341 b0cb8082 2021-09-11 xhr
342 b0cb8082 2021-09-11 xhr void
343 51a1ad92 2021-09-11 xhr set_initiative(int what)
344 51a1ad92 2021-09-11 xhr {
345 51a1ad92 2021-09-11 xhr struct character *curchar = get_current_character();
346 51a1ad92 2021-09-11 xhr
347 51a1ad92 2021-09-11 xhr if (curchar == NULL) {
348 700a297e 2021-09-16 xhr log_debug("No character loaded. Cannot set initiative\n");
349 51a1ad92 2021-09-11 xhr return;
350 51a1ad92 2021-09-11 xhr }
351 51a1ad92 2021-09-11 xhr
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");
354 51a1ad92 2021-09-11 xhr return;
355 51a1ad92 2021-09-11 xhr }
356 51a1ad92 2021-09-11 xhr
357 51a1ad92 2021-09-11 xhr if (what == 1)
358 51a1ad92 2021-09-11 xhr curchar->fight->initiative = 1;
359 51a1ad92 2021-09-11 xhr else
360 51a1ad92 2021-09-11 xhr curchar->fight->initiative = 0;
361 51a1ad92 2021-09-11 xhr }
362 51a1ad92 2021-09-11 xhr
363 51a1ad92 2021-09-11 xhr void
364 b0cb8082 2021-09-11 xhr mark_fight_progress()
365 b0cb8082 2021-09-11 xhr {
366 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
367 b0cb8082 2021-09-11 xhr
368 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
369 b0cb8082 2021-09-11 xhr log_debug("No character loaded. Cannot calculate progress\n");
370 b0cb8082 2021-09-11 xhr return;
371 b0cb8082 2021-09-11 xhr }
372 b0cb8082 2021-09-11 xhr
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");
375 b0cb8082 2021-09-11 xhr return;
376 b0cb8082 2021-09-11 xhr }
377 b0cb8082 2021-09-11 xhr
378 a6733253 2021-09-11 xhr switch (curchar->fight->difficulty) {
379 b0cb8082 2021-09-11 xhr case 1:
380 a6733253 2021-09-11 xhr curchar->fight->progress += 3;
381 b0cb8082 2021-09-11 xhr break;
382 b0cb8082 2021-09-11 xhr case 2:
383 a6733253 2021-09-11 xhr curchar->fight->progress += 2;
384 b0cb8082 2021-09-11 xhr break;
385 b0cb8082 2021-09-11 xhr case 3:
386 a6733253 2021-09-11 xhr curchar->fight->progress += 1;
387 b0cb8082 2021-09-11 xhr break;
388 b0cb8082 2021-09-11 xhr case 4:
389 a6733253 2021-09-11 xhr curchar->fight->progress += 0.5;
390 b0cb8082 2021-09-11 xhr break;
391 b0cb8082 2021-09-11 xhr case 5:
392 a6733253 2021-09-11 xhr curchar->fight->progress += 0.25;
393 b0cb8082 2021-09-11 xhr break;
394 b0cb8082 2021-09-11 xhr default:
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");
397 b0cb8082 2021-09-11 xhr }
398 b0cb8082 2021-09-11 xhr
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;
402 b0cb8082 2021-09-11 xhr }
403 b0cb8082 2021-09-11 xhr
404 b0cb8082 2021-09-11 xhr update_prompt();
405 b0cb8082 2021-09-11 xhr }
406 b0cb8082 2021-09-11 xhr
407 b0cb8082 2021-09-11 xhr void
408 b0cb8082 2021-09-11 xhr save_fight()
409 b0cb8082 2021-09-11 xhr {
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;
414 b0cb8082 2021-09-11 xhr
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");
417 b0cb8082 2021-09-11 xhr return;
418 b0cb8082 2021-09-11 xhr }
419 b0cb8082 2021-09-11 xhr
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");
422 b0cb8082 2021-09-11 xhr return;
423 b0cb8082 2021-09-11 xhr }
424 b0cb8082 2021-09-11 xhr
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));
430 b0cb8082 2021-09-11 xhr
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();
435 b0cb8082 2021-09-11 xhr if (!root)
436 b0cb8082 2021-09-11 xhr log_errx(1, "Cannot create fight JSON object\n");
437 b0cb8082 2021-09-11 xhr
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);
441 b0cb8082 2021-09-11 xhr } else {
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);
447 fc6936c9 2021-09-16 xhr }
448 fc6936c9 2021-09-16 xhr
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);
457 b0cb8082 2021-09-11 xhr goto out;
458 b0cb8082 2021-09-11 xhr }
459 b0cb8082 2021-09-11 xhr }
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);
462 b0cb8082 2021-09-11 xhr }
463 b0cb8082 2021-09-11 xhr
464 b0cb8082 2021-09-11 xhr out:
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);
467 b0cb8082 2021-09-11 xhr else
468 b0cb8082 2021-09-11 xhr log_debug("Successfully saved %s\n", path);
469 b0cb8082 2021-09-11 xhr
470 b0cb8082 2021-09-11 xhr json_object_put(root);
471 b0cb8082 2021-09-11 xhr }
472 b0cb8082 2021-09-11 xhr
473 b0cb8082 2021-09-11 xhr void
474 b0cb8082 2021-09-11 xhr delete_fight(int id)
475 b0cb8082 2021-09-11 xhr {
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;
479 b0cb8082 2021-09-11 xhr
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");
483 b0cb8082 2021-09-11 xhr return;
484 b0cb8082 2021-09-11 xhr }
485 b0cb8082 2021-09-11 xhr
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);
489 fc6936c9 2021-09-16 xhr return;
490 fc6936c9 2021-09-16 xhr }
491 fc6936c9 2021-09-16 xhr
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);
499 b0cb8082 2021-09-11 xhr }
500 b0cb8082 2021-09-11 xhr }
501 b0cb8082 2021-09-11 xhr
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);
504 b0cb8082 2021-09-11 xhr else
505 b0cb8082 2021-09-11 xhr log_debug("Successfully saved %s\n", path);
506 b0cb8082 2021-09-11 xhr
507 b0cb8082 2021-09-11 xhr json_object_put(root);
508 b0cb8082 2021-09-11 xhr }
509 b0cb8082 2021-09-11 xhr
510 b0cb8082 2021-09-11 xhr void
511 b0cb8082 2021-09-11 xhr load_fight(int id)
512 b0cb8082 2021-09-11 xhr {
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;
517 b0cb8082 2021-09-11 xhr
518 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
519 b0cb8082 2021-09-11 xhr log_debug("No character loaded\n");
520 b0cb8082 2021-09-11 xhr return;
521 b0cb8082 2021-09-11 xhr }
522 b0cb8082 2021-09-11 xhr
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");
526 b0cb8082 2021-09-11 xhr return;
527 b0cb8082 2021-09-11 xhr }
528 b0cb8082 2021-09-11 xhr
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);
532 fc6936c9 2021-09-16 xhr return;
533 fc6936c9 2021-09-16 xhr }
534 fc6936c9 2021-09-16 xhr
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));
541 b0cb8082 2021-09-11 xhr
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);
549 b0cb8082 2021-09-11 xhr }
550 b0cb8082 2021-09-11 xhr }
551 b0cb8082 2021-09-11 xhr
552 b0cb8082 2021-09-11 xhr json_object_put(root);
553 b0cb8082 2021-09-11 xhr }
554 b0cb8082 2021-09-11 xhr
555 b0cb8082 2021-09-11 xhr void
556 b0cb8082 2021-09-11 xhr ask_for_fight_difficulty()
557 b0cb8082 2021-09-11 xhr {
558 b0cb8082 2021-09-11 xhr struct character *curchar = get_current_character();
559 b0cb8082 2021-09-11 xhr
560 b0cb8082 2021-09-11 xhr if (curchar == NULL) {
561 b0cb8082 2021-09-11 xhr log_debug("No character loaded\n");
562 b0cb8082 2021-09-11 xhr return;
563 b0cb8082 2021-09-11 xhr }
564 b0cb8082 2021-09-11 xhr
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");
571 b0cb8082 2021-09-11 xhr
572 a6733253 2021-09-11 xhr curchar->fight->difficulty = ask_for_value("Enter a value between 1 and 5: ", 5);
573 b0cb8082 2021-09-11 xhr }
574 b0cb8082 2021-09-11 xhr