Blame


1 834ec24f 2021-09-06 xhr /*
2 834ec24f 2021-09-06 xhr * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
3 834ec24f 2021-09-06 xhr *
4 834ec24f 2021-09-06 xhr * Permission to use, copy, modify, and distribute this software for any
5 834ec24f 2021-09-06 xhr * purpose with or without fee is hereby granted, provided that the above
6 834ec24f 2021-09-06 xhr * copyright notice and this permission notice appear in all copies.
7 834ec24f 2021-09-06 xhr *
8 834ec24f 2021-09-06 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 834ec24f 2021-09-06 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 834ec24f 2021-09-06 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 834ec24f 2021-09-06 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 834ec24f 2021-09-06 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 834ec24f 2021-09-06 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 834ec24f 2021-09-06 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 834ec24f 2021-09-06 xhr */
16 834ec24f 2021-09-06 xhr
17 2203b74d 2021-09-07 xhr #include <sys/queue.h>
18 2203b74d 2021-09-07 xhr
19 04d8c7d3 2021-09-08 xhr #include <errno.h>
20 834ec24f 2021-09-06 xhr #include <limits.h>
21 faed5c06 2021-09-02 xhr #include <stdio.h>
22 c184771b 2021-09-01 xhr #include <stdlib.h>
23 ee9774ad 2021-09-02 xhr #include <string.h>
24 c184771b 2021-09-01 xhr
25 834ec24f 2021-09-06 xhr #include <json-c/json.h>
26 12d2e10b 2021-09-02 xhr #include <readline/readline.h>
27 12d2e10b 2021-09-02 xhr #include <readline/history.h>
28 12d2e10b 2021-09-02 xhr
29 c184771b 2021-09-01 xhr #include "isscrolls.h"
30 c184771b 2021-09-01 xhr
31 834ec24f 2021-09-06 xhr static struct character *curchar = NULL;
32 2203b74d 2021-09-07 xhr struct listhead *headp;
33 2203b74d 2021-09-07 xhr LIST_HEAD(listhead, entry) head = LIST_HEAD_INITIALIZER(head);
34 c184771b 2021-09-01 xhr
35 834ec24f 2021-09-06 xhr void
36 8a47a564 2021-09-07 xhr cmd_create_character(char *name)
37 834ec24f 2021-09-06 xhr {
38 142b407e 2021-09-07 xhr struct entry *e;
39 8a47a564 2021-09-07 xhr struct character *c;
40 bb81a173 2021-09-08 xhr char p[MAX_PROMPT_LEN];
41 632aee00 2021-09-08 xhr
42 73942dbe 2021-09-08 xhr /* There is already a character loaded, so save and free it */
43 632aee00 2021-09-08 xhr if (curchar != NULL) {
44 8b801c9b 2021-09-11 xhr save_character();
45 8b801c9b 2021-09-11 xhr free_character();
46 632aee00 2021-09-08 xhr curchar = NULL;
47 632aee00 2021-09-08 xhr }
48 834ec24f 2021-09-06 xhr
49 a20b3d4e 2021-09-08 xhr if (character_exists(name) == 0) {
50 a20b3d4e 2021-09-08 xhr printf("Sorry, there is already a character named %s\n", name);
51 a20b3d4e 2021-09-08 xhr return;
52 a20b3d4e 2021-09-08 xhr }
53 a20b3d4e 2021-09-08 xhr
54 8a47a564 2021-09-07 xhr log_debug("Attempt to create a character named %s\n", name);
55 8a47a564 2021-09-07 xhr if ((c = create_character(name)) != NULL) {
56 d6d6a902 2021-09-08 xhr curchar = c;
57 d6d6a902 2021-09-08 xhr print_character();
58 834ec24f 2021-09-06 xhr snprintf(p, sizeof(p), "%s > ", c->name);
59 834ec24f 2021-09-06 xhr set_prompt(p);
60 142b407e 2021-09-07 xhr
61 142b407e 2021-09-07 xhr if ((e = malloc(sizeof(struct entry))) == NULL)
62 142b407e 2021-09-07 xhr log_errx(1, "cannot allocate memory\n");
63 142b407e 2021-09-07 xhr
64 142b407e 2021-09-07 xhr e->id = c->id;
65 142b407e 2021-09-07 xhr snprintf(e->name, sizeof(e->name), "%s", c->name);
66 142b407e 2021-09-07 xhr LIST_INSERT_HEAD(&head, e, entries);
67 0c0efc11 2021-09-08 xhr }
68 0c0efc11 2021-09-08 xhr }
69 0c0efc11 2021-09-08 xhr
70 0c0efc11 2021-09-08 xhr void
71 0c0efc11 2021-09-08 xhr cmd_ls(__attribute__((unused)) char *unused)
72 0c0efc11 2021-09-08 xhr {
73 0c0efc11 2021-09-08 xhr struct entry *np;
74 0c0efc11 2021-09-08 xhr
75 0c0efc11 2021-09-08 xhr LIST_FOREACH(np, &head, entries) {
76 0c0efc11 2021-09-08 xhr printf("%s\n", np->name);
77 834ec24f 2021-09-06 xhr }
78 834ec24f 2021-09-06 xhr }
79 834ec24f 2021-09-06 xhr
80 2b8ffc2d 2021-09-08 xhr void
81 2b8ffc2d 2021-09-08 xhr cmd_delete_character(__attribute__((unused)) char *unused)
82 7eab0b32 2021-09-07 xhr {
83 73942dbe 2021-09-08 xhr struct entry *np = NULL;
84 7eab0b32 2021-09-07 xhr
85 2b8ffc2d 2021-09-08 xhr if (curchar == NULL) {
86 be4af772 2021-09-09 xhr printf("No character loaded. Use 'cd' to load a character\n");
87 2b8ffc2d 2021-09-08 xhr return;
88 2b8ffc2d 2021-09-08 xhr }
89 2b8ffc2d 2021-09-08 xhr
90 73942dbe 2021-09-08 xhr /* Save list entry of the to be deleted char in np */
91 7eab0b32 2021-09-07 xhr LIST_FOREACH(np, &head, entries) {
92 2b8ffc2d 2021-09-08 xhr if (np->id == curchar->id)
93 2b8ffc2d 2021-09-08 xhr break;
94 7eab0b32 2021-09-07 xhr }
95 7eab0b32 2021-09-07 xhr
96 2b8ffc2d 2021-09-08 xhr delete_saved_character(curchar->id);
97 2b8ffc2d 2021-09-08 xhr
98 8b801c9b 2021-09-11 xhr free_character();
99 2b8ffc2d 2021-09-08 xhr curchar = NULL;
100 2b8ffc2d 2021-09-08 xhr
101 2b8ffc2d 2021-09-08 xhr if (np != NULL) {
102 2b8ffc2d 2021-09-08 xhr LIST_REMOVE(np, entries);
103 2b8ffc2d 2021-09-08 xhr free(np);
104 2b8ffc2d 2021-09-08 xhr } else
105 2b8ffc2d 2021-09-08 xhr log_debug("Found a list entry but cannot delete it\n");
106 2b8ffc2d 2021-09-08 xhr
107 2b8ffc2d 2021-09-08 xhr set_prompt("> ");
108 7eab0b32 2021-09-07 xhr }
109 7eab0b32 2021-09-07 xhr
110 834ec24f 2021-09-06 xhr void
111 834ec24f 2021-09-06 xhr cmd_cd(char *character)
112 834ec24f 2021-09-06 xhr {
113 2203b74d 2021-09-07 xhr int id = -1;
114 2203b74d 2021-09-07 xhr
115 834ec24f 2021-09-06 xhr if (character == NULL)
116 834ec24f 2021-09-06 xhr return;
117 834ec24f 2021-09-06 xhr
118 c7d26844 2021-09-07 xhr if (strlen(character) == 0 && curchar != NULL) {
119 73942dbe 2021-09-08 xhr /* We got no argument and there is a character loaded */
120 c7d26844 2021-09-07 xhr log_debug("Switch to ~ and save character %s\n", character);
121 834ec24f 2021-09-06 xhr set_prompt("> ");
122 8b801c9b 2021-09-11 xhr save_character();
123 8b801c9b 2021-09-11 xhr free_character();
124 834ec24f 2021-09-06 xhr curchar = NULL;
125 c7d26844 2021-09-07 xhr } else if (strlen(character) == 0 && curchar == NULL) {
126 2203b74d 2021-09-07 xhr /* We got no argument and there is no character loaded */
127 31c6f1e2 2021-09-08 xhr printf("Provide the name of a character as argument\n\n");
128 31c6f1e2 2021-09-08 xhr printf("Example: cd Delkash - load the character named Delkash\n");
129 c7d26844 2021-09-07 xhr return;
130 c7d26844 2021-09-07 xhr } else if (strlen(character) != 0 && curchar == NULL) {
131 2203b74d 2021-09-07 xhr /* We got an argument and there is no character loaded */
132 7eab0b32 2021-09-07 xhr id = return_character_id(character);
133 2203b74d 2021-09-07 xhr if (id != -1) {
134 5d0c8c1e 2021-09-11 xhr if (load_character(id) == -1) {
135 2203b74d 2021-09-07 xhr log_debug("No character object for %s with ID %d\n", character, id);
136 2203b74d 2021-09-07 xhr return;
137 2203b74d 2021-09-07 xhr }
138 2203b74d 2021-09-07 xhr } else
139 2203b74d 2021-09-07 xhr printf("No character named %s found.\n", character);
140 c7d26844 2021-09-07 xhr } else if (strlen(character) != 0 && curchar != NULL) {
141 2203b74d 2021-09-07 xhr /* We got an argument and there is a character loaded */
142 7eab0b32 2021-09-07 xhr id = return_character_id(character);
143 2203b74d 2021-09-07 xhr if (id != -1) {
144 8b801c9b 2021-09-11 xhr save_character();
145 8b801c9b 2021-09-11 xhr free_character();
146 2203b74d 2021-09-07 xhr curchar = NULL;
147 2203b74d 2021-09-07 xhr
148 5d0c8c1e 2021-09-11 xhr if (load_character(id) == -1) {
149 2203b74d 2021-09-07 xhr log_debug("No character object for %s with ID %d\n", character, id);
150 2203b74d 2021-09-07 xhr return;
151 2203b74d 2021-09-07 xhr }
152 2203b74d 2021-09-07 xhr } else
153 2203b74d 2021-09-07 xhr printf("No character named %s found.\n", character);
154 834ec24f 2021-09-06 xhr }
155 c184771b 2021-09-01 xhr }
156 c184771b 2021-09-01 xhr
157 43b54703 2021-09-08 xhr void
158 43b54703 2021-09-08 xhr cmd_increase_value(char *value)
159 43b54703 2021-09-08 xhr {
160 111285eb 2021-09-08 xhr change_char_value(value, INCREASE, 1);
161 3a81c81c 2021-09-08 xhr }
162 3a81c81c 2021-09-08 xhr
163 3a81c81c 2021-09-08 xhr void
164 3a81c81c 2021-09-08 xhr cmd_decrease_value(char *value)
165 3a81c81c 2021-09-08 xhr {
166 111285eb 2021-09-08 xhr change_char_value(value, DECREASE, 1);
167 3a81c81c 2021-09-08 xhr }
168 3a81c81c 2021-09-08 xhr
169 3a81c81c 2021-09-08 xhr void
170 3dd6e83c 2021-09-08 xhr cmd_toogle(char *value)
171 3dd6e83c 2021-09-08 xhr {
172 3dd6e83c 2021-09-08 xhr if (curchar == NULL) {
173 be4af772 2021-09-09 xhr printf("No character loaded. Use 'cd' to load a character\n");
174 3dd6e83c 2021-09-08 xhr return;
175 3dd6e83c 2021-09-08 xhr }
176 3dd6e83c 2021-09-08 xhr
177 3dd6e83c 2021-09-08 xhr if (value == NULL || strlen(value) == 0) {
178 3dd6e83c 2021-09-08 xhr printf("Please specify the stat you want to toogle\n");
179 b4fc3adb 2021-09-08 xhr printf("\nExample: toggle wounded\n");
180 b4fc3adb 2021-09-08 xhr printf("\nYou can toggle the following values:\n\n");
181 3dd6e83c 2021-09-08 xhr printf("-Wounded\n-Unprepared\n-Shaken\n-Encumbered\n-Maimed\n-Cursed\n");
182 b4fc3adb 2021-09-08 xhr printf("-Corrupted\n-Tormented\n");
183 3dd6e83c 2021-09-08 xhr return;
184 3dd6e83c 2021-09-08 xhr }
185 3dd6e83c 2021-09-08 xhr
186 3dd6e83c 2021-09-08 xhr if (strcasecmp(value, "wounded") == 0) {
187 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->wounded);
188 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "unprepared") == 0) {
189 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->unprepared);
190 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "shaken") == 0) {
191 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->shaken);
192 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "encumbered") == 0) {
193 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->encumbered);
194 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "maimed") == 0) {
195 f6f08425 2021-09-08 xhr if (curchar->maimed == 1) {
196 f6f08425 2021-09-08 xhr printf("Maimed is a permanent bane and cannot be changed\n");
197 f6f08425 2021-09-08 xhr return;
198 f6f08425 2021-09-08 xhr }
199 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->maimed);
200 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "cursed") == 0) {
201 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->cursed);
202 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "corrupted") == 0) {
203 f6f08425 2021-09-08 xhr if (curchar->corrupted == 1) {
204 f6f08425 2021-09-08 xhr printf("Corrupted is a permanent bane and cannot be changed\n");
205 f6f08425 2021-09-08 xhr return;
206 f6f08425 2021-09-08 xhr }
207 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->corrupted);
208 3dd6e83c 2021-09-08 xhr } else if (strcasecmp(value, "tormented") == 0) {
209 3dd6e83c 2021-09-08 xhr toggle_value(value, &curchar->tormented);
210 3dd6e83c 2021-09-08 xhr }
211 68583e72 2021-09-08 xhr
212 68583e72 2021-09-08 xhr set_max_momentum();
213 8a2e76b6 2021-09-10 xhr }
214 8a2e76b6 2021-09-10 xhr
215 8a2e76b6 2021-09-10 xhr
216 8a2e76b6 2021-09-10 xhr void
217 fcaa7fb2 2021-09-11 xhr update_prompt()
218 8a2e76b6 2021-09-10 xhr {
219 8a2e76b6 2021-09-10 xhr char p[MAX_PROMPT_LEN];
220 b0cb8082 2021-09-11 xhr char j[MAX_PROMPT_LEN];
221 b0cb8082 2021-09-11 xhr char f[MAX_PROMPT_LEN];
222 51a1ad92 2021-09-11 xhr char i[5];
223 8a2e76b6 2021-09-10 xhr
224 8a2e76b6 2021-09-10 xhr if (curchar == NULL) {
225 8a2e76b6 2021-09-10 xhr printf("No character loaded. Use 'cd' to load a character\n");
226 8a2e76b6 2021-09-10 xhr return;
227 8a2e76b6 2021-09-10 xhr }
228 8a2e76b6 2021-09-10 xhr
229 b0cb8082 2021-09-11 xhr memset(j, 0, sizeof(j));
230 b0cb8082 2021-09-11 xhr memset(f, 0, sizeof(f));
231 51a1ad92 2021-09-11 xhr memset(i, 0, sizeof(i));
232 b0cb8082 2021-09-11 xhr
233 20cd37e2 2021-09-11 xhr if (curchar->journey_active == 1) {
234 20cd37e2 2021-09-11 xhr if (curchar->j->difficulty < 4)
235 b0cb8082 2021-09-11 xhr snprintf(j, sizeof(j), "Journey %.0f/10 > ",
236 b0cb8082 2021-09-11 xhr curchar->j->progress);
237 20cd37e2 2021-09-11 xhr else
238 b0cb8082 2021-09-11 xhr snprintf(j, sizeof(j), "Journey %.2f/10 > ",
239 b0cb8082 2021-09-11 xhr curchar->j->progress);
240 b0cb8082 2021-09-11 xhr }
241 51a1ad92 2021-09-11 xhr
242 b0cb8082 2021-09-11 xhr if (curchar->fight_active == 1) {
243 51a1ad92 2021-09-11 xhr if (curchar->fight->initiative == 1)
244 51a1ad92 2021-09-11 xhr snprintf(i, 5, "%s", " [I]");
245 51a1ad92 2021-09-11 xhr
246 a6733253 2021-09-11 xhr if (curchar->fight->difficulty < 4)
247 51a1ad92 2021-09-11 xhr snprintf(f, sizeof(f), "Fight %.0f/10%s > ",
248 51a1ad92 2021-09-11 xhr curchar->fight->progress, i);
249 b0cb8082 2021-09-11 xhr else
250 51a1ad92 2021-09-11 xhr snprintf(f, sizeof(f), "Fight %.2f/10%s > ",
251 51a1ad92 2021-09-11 xhr curchar->fight->progress, i);
252 b0cb8082 2021-09-11 xhr }
253 b0cb8082 2021-09-11 xhr
254 b0cb8082 2021-09-11 xhr snprintf(p, sizeof(p), "%s > %s%s", curchar->name, j, f);
255 8a2e76b6 2021-09-10 xhr
256 8a2e76b6 2021-09-10 xhr set_prompt(p);
257 3dd6e83c 2021-09-08 xhr }
258 3dd6e83c 2021-09-08 xhr
259 3dd6e83c 2021-09-08 xhr void
260 3dd6e83c 2021-09-08 xhr toggle_value(const char *desc, int *value)
261 3dd6e83c 2021-09-08 xhr {
262 68583e72 2021-09-08 xhr int new = !(*value);
263 3dd6e83c 2021-09-08 xhr
264 2bc4629d 2021-09-08 xhr if (curchar == NULL) {
265 be4af772 2021-09-09 xhr printf("No character loaded. Use 'cd' to load a character\n");
266 2bc4629d 2021-09-08 xhr return;
267 2bc4629d 2021-09-08 xhr }
268 2bc4629d 2021-09-08 xhr
269 b4fc3adb 2021-09-08 xhr printf("Toggle %s from %d to %d\n", desc, *value, new);
270 3dd6e83c 2021-09-08 xhr *value = new;
271 68583e72 2021-09-08 xhr }
272 fc6b172b 2021-09-08 xhr
273 68583e72 2021-09-08 xhr void
274 68583e72 2021-09-08 xhr set_max_momentum()
275 68583e72 2021-09-08 xhr {
276 68583e72 2021-09-08 xhr int mm;
277 68583e72 2021-09-08 xhr
278 68583e72 2021-09-08 xhr if (curchar == NULL)
279 68583e72 2021-09-08 xhr return;
280 68583e72 2021-09-08 xhr
281 d4da07d5 2021-09-08 xhr /* Max momentum is 10 minus all the debilities */
282 d4da07d5 2021-09-08 xhr mm = 10 - curchar->wounded - curchar->unprepared - curchar->shaken -
283 d4da07d5 2021-09-08 xhr curchar->encumbered - curchar->maimed -
284 d4da07d5 2021-09-08 xhr curchar->cursed - curchar->corrupted - curchar->tormented;
285 68583e72 2021-09-08 xhr
286 18a7dafb 2021-09-08 xhr if (mm != curchar->max_momentum) {
287 a1e2c7f5 2021-09-16 xhr printf("Your max momentum changed from %d to %d\n",
288 a1e2c7f5 2021-09-16 xhr curchar->max_momentum, mm);
289 18a7dafb 2021-09-08 xhr curchar->max_momentum = mm;
290 18a7dafb 2021-09-08 xhr }
291 3dd6e83c 2021-09-08 xhr
292 d4da07d5 2021-09-08 xhr /* Reset momentum is +2 and reduced by 1 for each debility. It cannot fall
293 d4da07d5 2021-09-08 xhr * lower than 0
294 d4da07d5 2021-09-08 xhr */
295 d4da07d5 2021-09-08 xhr mm = 2 - curchar->wounded - curchar->unprepared - curchar->shaken -
296 d4da07d5 2021-09-08 xhr curchar->encumbered - curchar->maimed -
297 d4da07d5 2021-09-08 xhr curchar->cursed - curchar->corrupted - curchar->tormented;
298 2bc4629d 2021-09-08 xhr
299 d4da07d5 2021-09-08 xhr if (mm < 0)
300 d4da07d5 2021-09-08 xhr mm = 0;
301 18a7dafb 2021-09-08 xhr if (mm != curchar->momentum_reset) {
302 a1e2c7f5 2021-09-16 xhr printf("Your reset momentum changed from %d to %d\n",
303 a1e2c7f5 2021-09-16 xhr curchar->momentum_reset, mm);
304 18a7dafb 2021-09-08 xhr curchar->momentum_reset = mm;
305 18a7dafb 2021-09-08 xhr }
306 d4da07d5 2021-09-08 xhr
307 2bc4629d 2021-09-08 xhr }
308 2bc4629d 2021-09-08 xhr
309 2bc4629d 2021-09-08 xhr void
310 04d8c7d3 2021-09-08 xhr change_char_value(const char *value, int what, int howmany)
311 3a81c81c 2021-09-08 xhr {
312 3a81c81c 2021-09-08 xhr const char *event[2] = { "increase", "decrease" };
313 3a81c81c 2021-09-08 xhr
314 43b54703 2021-09-08 xhr if (curchar == NULL) {
315 be4af772 2021-09-09 xhr printf("No character loaded. Use 'cd' to load a character\n");
316 43b54703 2021-09-08 xhr return;
317 43b54703 2021-09-08 xhr }
318 43b54703 2021-09-08 xhr
319 43b54703 2021-09-08 xhr if (value == NULL || strlen(value) == 0) {
320 3a81c81c 2021-09-08 xhr printf("Please specify the stat you want to %s\n", event[what]);
321 a8d52d34 2021-09-08 xhr printf("\nExample: %s wits\t- %s 'wits' by 1\n", event[what], event[what]);
322 3a81c81c 2021-09-08 xhr printf("\nYou can change the following values:\n\n");
323 23709431 2021-09-08 xhr printf("-Edge\n-Heart\n-Iron\n-Shadow\n-Wits\n-Momentum\n-Health\n-Spirit\n");
324 23709431 2021-09-08 xhr printf("-Supply\n-Exp\n");
325 43b54703 2021-09-08 xhr return;
326 43b54703 2021-09-08 xhr }
327 43b54703 2021-09-08 xhr
328 23709431 2021-09-08 xhr if (strcasecmp(value, "edge") == 0) {
329 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->edge, 4, 0, howmany, what);
330 43b54703 2021-09-08 xhr return;
331 23709431 2021-09-08 xhr } else if (strcasecmp(value, "heart") == 0) {
332 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->heart, 4, 0, howmany, what);
333 43b54703 2021-09-08 xhr return;
334 23709431 2021-09-08 xhr } else if (strcasecmp(value, "iron") == 0) {
335 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->iron, 4, 0, howmany, what);
336 43b54703 2021-09-08 xhr return;
337 23709431 2021-09-08 xhr } else if (strcasecmp(value, "shadow") == 0) {
338 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->shadow, 4, 0, howmany, what);
339 43b54703 2021-09-08 xhr return;
340 23709431 2021-09-08 xhr } else if (strcasecmp(value, "wits") == 0) {
341 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->wits, 4, 0, howmany, what);
342 43b54703 2021-09-08 xhr return;
343 23709431 2021-09-08 xhr } else if (strcasecmp(value, "exp") == 0) {
344 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->exp, 30, 0, howmany, what);
345 43b54703 2021-09-08 xhr return;
346 23709431 2021-09-08 xhr } else if (strcasecmp(value, "momentum") == 0) {
347 a1e2c7f5 2021-09-16 xhr modify_value(value, &curchar->momentum, curchar->max_momentum, -6,
348 a1e2c7f5 2021-09-16 xhr howmany, what);
349 43b54703 2021-09-08 xhr return;
350 23709431 2021-09-08 xhr } else if (strcasecmp(value, "health") == 0) {
351 30e14501 2021-09-08 xhr if (curchar->wounded == 1) {
352 30e14501 2021-09-08 xhr printf("You are wounded, you cannot increase health\n");
353 30e14501 2021-09-08 xhr return;
354 30e14501 2021-09-08 xhr }
355 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->health, 5, 0, howmany, what);
356 43b54703 2021-09-08 xhr return;
357 23709431 2021-09-08 xhr } else if (strcasecmp(value, "spirit") == 0) {
358 30e14501 2021-09-08 xhr if (curchar->shaken == 1) {
359 30e14501 2021-09-08 xhr printf("You are shaken, you cannot increase spirit\n");
360 30e14501 2021-09-08 xhr return;
361 30e14501 2021-09-08 xhr }
362 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->spirit, 5, 0, howmany, what);
363 43b54703 2021-09-08 xhr return;
364 23709431 2021-09-08 xhr } else if (strcasecmp(value, "supply") == 0) {
365 30e14501 2021-09-08 xhr if (curchar->unprepared == 1) {
366 30e14501 2021-09-08 xhr printf("You are unprepared, you cannot increase supply\n");
367 30e14501 2021-09-08 xhr return;
368 30e14501 2021-09-08 xhr }
369 04d8c7d3 2021-09-08 xhr modify_value(value, &curchar->supply, 5, 0, howmany, what);
370 43b54703 2021-09-08 xhr return;
371 43b54703 2021-09-08 xhr } else {
372 43b54703 2021-09-08 xhr printf("Unknown value\n");
373 43b54703 2021-09-08 xhr return;
374 43b54703 2021-09-08 xhr }
375 43b54703 2021-09-08 xhr
376 43b54703 2021-09-08 xhr }
377 43b54703 2021-09-08 xhr
378 43b54703 2021-09-08 xhr void
379 a1e2c7f5 2021-09-16 xhr modify_value(const char *str, int *value, int max, int min, int howmany,
380 a1e2c7f5 2021-09-16 xhr int what)
381 43b54703 2021-09-08 xhr {
382 3a81c81c 2021-09-08 xhr if (what == 0) {
383 99cccb30 2021-09-09 xhr if (*value >= max) {
384 3a81c81c 2021-09-08 xhr return;
385 3a81c81c 2021-09-08 xhr }
386 04d8c7d3 2021-09-08 xhr *value += howmany;
387 04d8c7d3 2021-09-08 xhr printf("Increasing %s from %d to %d\n", str, *value - howmany, *value);
388 3a81c81c 2021-09-08 xhr } else {
389 99cccb30 2021-09-09 xhr if (*value <= min) {
390 3a81c81c 2021-09-08 xhr return;
391 3a81c81c 2021-09-08 xhr }
392 04d8c7d3 2021-09-08 xhr *value -= howmany;
393 04d8c7d3 2021-09-08 xhr printf("Decreasing %s from %d to %d\n", str, *value + howmany, *value);
394 43b54703 2021-09-08 xhr }
395 43b54703 2021-09-08 xhr }
396 43b54703 2021-09-08 xhr
397 2b8ffc2d 2021-09-08 xhr int
398 2b8ffc2d 2021-09-08 xhr return_character_id(const char *name)
399 2b8ffc2d 2021-09-08 xhr {
400 2b8ffc2d 2021-09-08 xhr struct entry *np;
401 2b8ffc2d 2021-09-08 xhr int id = -1;
402 2b8ffc2d 2021-09-08 xhr
403 2b8ffc2d 2021-09-08 xhr LIST_FOREACH(np, &head, entries) {
404 23709431 2021-09-08 xhr if (strcasecmp(np->name, name) == 0) {
405 2b8ffc2d 2021-09-08 xhr id = np->id;
406 2b8ffc2d 2021-09-08 xhr }
407 2b8ffc2d 2021-09-08 xhr }
408 2b8ffc2d 2021-09-08 xhr
409 2b8ffc2d 2021-09-08 xhr return id;
410 2b8ffc2d 2021-09-08 xhr }
411 2b8ffc2d 2021-09-08 xhr
412 c184771b 2021-09-01 xhr void
413 142b407e 2021-09-07 xhr save_current_character()
414 834ec24f 2021-09-06 xhr {
415 8b801c9b 2021-09-11 xhr save_character();
416 834ec24f 2021-09-06 xhr }
417 834ec24f 2021-09-06 xhr
418 834ec24f 2021-09-06 xhr void
419 8b801c9b 2021-09-11 xhr save_character()
420 834ec24f 2021-09-06 xhr {
421 834ec24f 2021-09-06 xhr char path[_POSIX_PATH_MAX];
422 142b407e 2021-09-07 xhr json_object *root, *items;
423 0e4454b5 2021-09-07 xhr int temp_n, i;
424 834ec24f 2021-09-06 xhr
425 8b801c9b 2021-09-11 xhr if (curchar == NULL) {
426 834ec24f 2021-09-06 xhr log_debug("Nothing to save here\n");
427 834ec24f 2021-09-06 xhr return;
428 834ec24f 2021-09-06 xhr }
429 834ec24f 2021-09-06 xhr
430 ae84ce7e 2021-09-11 xhr save_journey();
431 ae84ce7e 2021-09-11 xhr save_fight();
432 ae84ce7e 2021-09-11 xhr
433 142b407e 2021-09-07 xhr json_object *cobj = json_object_new_object();
434 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "name", json_object_new_string(curchar->name));
435 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "id", json_object_new_int(curchar->id));
436 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "edge", json_object_new_int(curchar->edge));
437 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "heart", json_object_new_int(curchar->heart));
438 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "iron", json_object_new_int(curchar->iron));
439 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "shadow", json_object_new_int(curchar->shadow));
440 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "wits", json_object_new_int(curchar->wits));
441 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "exp", json_object_new_int(curchar->exp));
442 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "momentum",
443 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->momentum));
444 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "max_momentum",
445 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->max_momentum));
446 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "momentum_reset",
447 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->momentum_reset));
448 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "health", json_object_new_int(curchar->health));
449 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "spirit", json_object_new_int(curchar->spirit));
450 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "supply", json_object_new_int(curchar->supply));
451 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "wounded",
452 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->wounded));
453 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "unprepared",
454 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->unprepared));
455 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "shaken",
456 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->shaken));
457 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "encumbered",
458 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->encumbered));
459 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "maimed", json_object_new_int(curchar->maimed));
460 8b801c9b 2021-09-11 xhr json_object_object_add(cobj, "cursed", json_object_new_int(curchar->cursed));
461 d2b9d86f 2021-09-13 xhr json_object_object_add(cobj, "dead", json_object_new_int(curchar->dead));
462 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "corrupted",
463 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->corrupted));
464 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "tormented",
465 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->tormented));
466 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "exp_used",
467 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->exp_used));
468 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "bonds",
469 a1e2c7f5 2021-09-16 xhr json_object_new_double(curchar->bonds));
470 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "journey_active",
471 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->journey_active));
472 a1e2c7f5 2021-09-16 xhr json_object_object_add(cobj, "fight_active",
473 a1e2c7f5 2021-09-16 xhr json_object_new_int(curchar->fight_active));
474 071fbda1 2021-09-06 xhr
475 142b407e 2021-09-07 xhr snprintf(path, sizeof(path), "%s/characters.json", get_isscrolls_dir());
476 834ec24f 2021-09-06 xhr if ((root = json_object_from_file(path)) == NULL) {
477 142b407e 2021-09-07 xhr log_debug("No character JSON file found\n");
478 834ec24f 2021-09-06 xhr root = json_object_new_object();
479 834ec24f 2021-09-06 xhr if (!root)
480 834ec24f 2021-09-06 xhr log_errx(1, "Cannot create JSON object\n");
481 834ec24f 2021-09-06 xhr
482 071fbda1 2021-09-06 xhr items = json_object_new_array();
483 142b407e 2021-09-07 xhr json_object_array_add(items, cobj);
484 071fbda1 2021-09-06 xhr json_object_object_add(root, "characters", items);
485 071fbda1 2021-09-06 xhr } else {
486 0e4454b5 2021-09-07 xhr /* Get existing character array from JSON */
487 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "characters", &items)) {
488 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [characters] array in %s\n", path);
489 fc6936c9 2021-09-16 xhr items = json_object_new_array();
490 fc6936c9 2021-09-16 xhr json_object_object_add(root, "characters", items);
491 fc6936c9 2021-09-16 xhr }
492 fc6936c9 2021-09-16 xhr
493 0e4454b5 2021-09-07 xhr temp_n = json_object_array_length(items);
494 0e4454b5 2021-09-07 xhr for (i = 0; i < temp_n; i++) {
495 0e4454b5 2021-09-07 xhr json_object *temp = json_object_array_get_idx(items, i);
496 17410fed 2021-09-16 xhr json_object *id;
497 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "id", &id);
498 8b801c9b 2021-09-11 xhr if (curchar->id == json_object_get_int(id)) {
499 8b801c9b 2021-09-11 xhr log_debug("Update character entry for %s\n", curchar->name);
500 43b54703 2021-09-08 xhr json_object_array_del_idx(items, i, 1);
501 43b54703 2021-09-08 xhr json_object_array_add(items, cobj);
502 0e4454b5 2021-09-07 xhr goto out;
503 0e4454b5 2021-09-07 xhr }
504 0e4454b5 2021-09-07 xhr }
505 8b801c9b 2021-09-11 xhr log_debug("No entry for %s found, adding new one\n", curchar->name);
506 4b7dacb0 2021-09-10 xhr json_object_array_add(items, cobj);
507 071fbda1 2021-09-06 xhr }
508 834ec24f 2021-09-06 xhr
509 0e4454b5 2021-09-07 xhr out:
510 2b8ffc2d 2021-09-08 xhr if (json_object_to_file(path, root))
511 2b8ffc2d 2021-09-08 xhr printf("Error saving %s\n", path);
512 2b8ffc2d 2021-09-08 xhr else
513 2b8ffc2d 2021-09-08 xhr log_debug("Successfully saved %s\n", path);
514 2b8ffc2d 2021-09-08 xhr
515 2b8ffc2d 2021-09-08 xhr json_object_put(root);
516 2b8ffc2d 2021-09-08 xhr }
517 2b8ffc2d 2021-09-08 xhr
518 2b8ffc2d 2021-09-08 xhr void
519 2b8ffc2d 2021-09-08 xhr delete_saved_character(int id)
520 2b8ffc2d 2021-09-08 xhr {
521 2b8ffc2d 2021-09-08 xhr char path[_POSIX_PATH_MAX];
522 17410fed 2021-09-16 xhr json_object *root, *lid;
523 2b8ffc2d 2021-09-08 xhr int temp_n, i;
524 2b8ffc2d 2021-09-08 xhr
525 2b8ffc2d 2021-09-08 xhr LIST_INIT(&head);
526 2b8ffc2d 2021-09-08 xhr
527 2b8ffc2d 2021-09-08 xhr snprintf(path, sizeof(path), "%s/characters.json", get_isscrolls_dir());
528 2b8ffc2d 2021-09-08 xhr if ((root = json_object_from_file(path)) == NULL) {
529 2b8ffc2d 2021-09-08 xhr log_debug("No character JSON file found\n");
530 2b8ffc2d 2021-09-08 xhr return;
531 2b8ffc2d 2021-09-08 xhr }
532 2b8ffc2d 2021-09-08 xhr
533 fc6936c9 2021-09-16 xhr json_object *characters;
534 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "characters", &characters)) {
535 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [characters] array in %s\n", path);
536 fc6936c9 2021-09-16 xhr return;
537 fc6936c9 2021-09-16 xhr }
538 fc6936c9 2021-09-16 xhr
539 2b8ffc2d 2021-09-08 xhr temp_n = json_object_array_length(characters);
540 2b8ffc2d 2021-09-08 xhr for (i = 0; i < temp_n; i++) {
541 2b8ffc2d 2021-09-08 xhr json_object *temp = json_object_array_get_idx(characters, i);
542 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
543 2b8ffc2d 2021-09-08 xhr if (id == json_object_get_int(lid)) {
544 2b8ffc2d 2021-09-08 xhr json_object_array_del_idx(characters, i, 1);
545 2b8ffc2d 2021-09-08 xhr log_debug("Deleted character entry for %d\n", id);
546 2b8ffc2d 2021-09-08 xhr }
547 2b8ffc2d 2021-09-08 xhr }
548 2b8ffc2d 2021-09-08 xhr
549 834ec24f 2021-09-06 xhr if (json_object_to_file(path, root))
550 834ec24f 2021-09-06 xhr printf("Error saving %s\n", path);
551 834ec24f 2021-09-06 xhr else
552 834ec24f 2021-09-06 xhr log_debug("Successfully saved %s\n", path);
553 071fbda1 2021-09-06 xhr
554 071fbda1 2021-09-06 xhr json_object_put(root);
555 03cac127 2021-09-07 xhr }
556 03cac127 2021-09-07 xhr
557 03cac127 2021-09-07 xhr void
558 142b407e 2021-09-07 xhr load_characters_list()
559 03cac127 2021-09-07 xhr {
560 2203b74d 2021-09-07 xhr struct entry *e;
561 03cac127 2021-09-07 xhr char path[_POSIX_PATH_MAX];
562 142b407e 2021-09-07 xhr json_object *root;
563 17410fed 2021-09-16 xhr json_object *lid, *name;
564 03cac127 2021-09-07 xhr int temp_n, i;
565 03cac127 2021-09-07 xhr
566 142b407e 2021-09-07 xhr LIST_INIT(&head);
567 142b407e 2021-09-07 xhr
568 142b407e 2021-09-07 xhr snprintf(path, sizeof(path), "%s/characters.json", get_isscrolls_dir());
569 03cac127 2021-09-07 xhr if ((root = json_object_from_file(path)) == NULL) {
570 142b407e 2021-09-07 xhr log_debug("No character JSON file found\n");
571 03cac127 2021-09-07 xhr return;
572 03cac127 2021-09-07 xhr }
573 03cac127 2021-09-07 xhr
574 17410fed 2021-09-16 xhr json_object *characters;
575 17410fed 2021-09-16 xhr if (!json_object_object_get_ex(root, "characters", &characters)) {
576 17410fed 2021-09-16 xhr log_debug("Cannot find a [characters] array in %s\n", path);
577 17410fed 2021-09-16 xhr return;
578 17410fed 2021-09-16 xhr }
579 03cac127 2021-09-07 xhr temp_n = json_object_array_length(characters);
580 03cac127 2021-09-07 xhr for (i=0; i < temp_n; i++) {
581 03cac127 2021-09-07 xhr json_object *temp = json_object_array_get_idx(characters, i);
582 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
583 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "name", &name);
584 142b407e 2021-09-07 xhr log_debug("Add %s to list with id: %d\n", json_object_get_string(name), json_object_get_int(lid));
585 2203b74d 2021-09-07 xhr
586 2203b74d 2021-09-07 xhr if ((e = malloc(sizeof(struct entry))) == NULL)
587 2203b74d 2021-09-07 xhr log_errx(1, "cannot allocate memory\n");
588 2203b74d 2021-09-07 xhr
589 142b407e 2021-09-07 xhr e->id = json_object_get_int(lid);
590 2203b74d 2021-09-07 xhr snprintf(e->name, sizeof(e->name), "%s", json_object_get_string(name));
591 2203b74d 2021-09-07 xhr LIST_INSERT_HEAD(&head, e, entries);
592 03cac127 2021-09-07 xhr }
593 693e2264 2021-09-07 xhr
594 693e2264 2021-09-07 xhr json_object_put(root);
595 834ec24f 2021-09-06 xhr }
596 834ec24f 2021-09-06 xhr
597 5d0c8c1e 2021-09-11 xhr int
598 2203b74d 2021-09-07 xhr load_character(int id)
599 834ec24f 2021-09-06 xhr {
600 834ec24f 2021-09-06 xhr struct character *c;
601 2203b74d 2021-09-07 xhr char path[_POSIX_PATH_MAX];
602 17410fed 2021-09-16 xhr json_object *root, *lid, *name;
603 142b407e 2021-09-07 xhr int temp_n, i;
604 834ec24f 2021-09-06 xhr
605 2203b74d 2021-09-07 xhr if (id <= 0)
606 5d0c8c1e 2021-09-11 xhr return -1;
607 2203b74d 2021-09-07 xhr
608 142b407e 2021-09-07 xhr snprintf(path, sizeof(path), "%s/characters.json", get_isscrolls_dir());
609 2203b74d 2021-09-07 xhr if ((root = json_object_from_file(path)) == NULL) {
610 142b407e 2021-09-07 xhr log_debug("No character JSON file found\n");
611 5d0c8c1e 2021-09-11 xhr return -1;
612 2203b74d 2021-09-07 xhr }
613 2203b74d 2021-09-07 xhr
614 3677cf7a 2021-09-10 xhr if ((c = calloc(1, sizeof(struct character))) == NULL)
615 961a1396 2021-09-08 xhr log_errx(1, "calloc");
616 961a1396 2021-09-08 xhr
617 3677cf7a 2021-09-10 xhr if ((c->name = calloc(1, MAX_CHAR_LEN)) == NULL)
618 961a1396 2021-09-08 xhr log_errx(1, "calloc");
619 961a1396 2021-09-08 xhr
620 4b7dacb0 2021-09-10 xhr if ((c->j = calloc(1, sizeof(struct journey))) == NULL)
621 4b7dacb0 2021-09-10 xhr log_errx(1, "calloc");
622 4b7dacb0 2021-09-10 xhr
623 a6733253 2021-09-11 xhr if ((c->fight = calloc(1, sizeof(struct fight))) == NULL)
624 b0cb8082 2021-09-11 xhr log_errx(1, "calloc");
625 b0cb8082 2021-09-11 xhr
626 fc6936c9 2021-09-16 xhr json_object *characters;
627 fc6936c9 2021-09-16 xhr if (!json_object_object_get_ex(root, "characters", &characters)) {
628 fc6936c9 2021-09-16 xhr log_debug("Cannot find a [characters] array in %s\n", path);
629 fc6936c9 2021-09-16 xhr return -1;
630 fc6936c9 2021-09-16 xhr }
631 fc6936c9 2021-09-16 xhr
632 142b407e 2021-09-07 xhr temp_n = json_object_array_length(characters);
633 142b407e 2021-09-07 xhr for (i=0; i < temp_n; i++) {
634 142b407e 2021-09-07 xhr json_object *temp = json_object_array_get_idx(characters, i);
635 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "id", &lid);
636 142b407e 2021-09-07 xhr if (id == json_object_get_int(lid)) {
637 17410fed 2021-09-16 xhr json_object_object_get_ex(temp, "name", &name);
638 2203b74d 2021-09-07 xhr
639 a1e2c7f5 2021-09-16 xhr log_debug("Loading character %s, id: %d\n", json_object_get_string(name),
640 a1e2c7f5 2021-09-16 xhr json_object_get_int(lid));
641 2203b74d 2021-09-07 xhr
642 bb81a173 2021-09-08 xhr snprintf(c->name, MAX_CHAR_LEN, "%s", json_object_get_string(name));
643 142b407e 2021-09-07 xhr c->id = id;
644 373f08a5 2021-09-16 xhr json_object *cval;
645 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "edge", &cval);
646 373f08a5 2021-09-16 xhr c->edge = json_object_get_int(cval);
647 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "heart", &cval);
648 373f08a5 2021-09-16 xhr c->heart = json_object_get_int(cval);
649 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "iron", &cval);
650 373f08a5 2021-09-16 xhr c->iron = json_object_get_int(cval);
651 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "shadow", &cval);
652 373f08a5 2021-09-16 xhr c->shadow = json_object_get_int(cval);
653 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "wits", &cval);
654 373f08a5 2021-09-16 xhr c->wits = json_object_get_int(cval);
655 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "exp", &cval);
656 373f08a5 2021-09-16 xhr c->exp = json_object_get_int(cval);
657 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "health", &cval);
658 373f08a5 2021-09-16 xhr c->health = json_object_get_int(cval);
659 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "spirit", &cval);
660 373f08a5 2021-09-16 xhr c->spirit = json_object_get_int(cval);
661 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "supply", &cval);
662 373f08a5 2021-09-16 xhr c->supply = json_object_get_int(cval);
663 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "wounded", &cval);
664 373f08a5 2021-09-16 xhr c->wounded = json_object_get_int(cval);
665 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "shaken", &cval);
666 373f08a5 2021-09-16 xhr c->shaken = json_object_get_int(cval);
667 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "maimed", &cval);
668 373f08a5 2021-09-16 xhr c->maimed = json_object_get_int(cval);
669 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "cursed", &cval);
670 373f08a5 2021-09-16 xhr c->cursed = json_object_get_int(cval);
671 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "dead", &cval);
672 373f08a5 2021-09-16 xhr c->dead = json_object_get_int(cval);
673 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "bonds", &cval);
674 373f08a5 2021-09-16 xhr c->bonds = json_object_get_double(cval);
675 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "corrupted", &cval);
676 373f08a5 2021-09-16 xhr c->corrupted = json_object_get_int(cval);
677 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "tormented", &cval);
678 373f08a5 2021-09-16 xhr c->tormented = json_object_get_int(cval);
679 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "exp_used", &cval);
680 373f08a5 2021-09-16 xhr c->exp_used = json_object_get_int(cval);
681 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "unprepared", &cval);
682 373f08a5 2021-09-16 xhr c->unprepared= json_object_get_int(cval);
683 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "momentum", &cval);
684 373f08a5 2021-09-16 xhr c->momentum = json_object_get_int(cval);
685 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "encumbered", &cval);
686 373f08a5 2021-09-16 xhr c->encumbered = json_object_get_int(cval);
687 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "max_momentum", &cval);
688 373f08a5 2021-09-16 xhr c->max_momentum = json_object_get_int(cval);
689 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "momentum_reset", &cval);
690 373f08a5 2021-09-16 xhr c->momentum_reset = json_object_get_int(cval);
691 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "journey_active", &cval);
692 373f08a5 2021-09-16 xhr c->journey_active = json_object_get_int(cval);
693 373f08a5 2021-09-16 xhr json_object_object_get_ex(temp, "fight_active", &cval);
694 373f08a5 2021-09-16 xhr c->fight_active = json_object_get_int(cval);
695 142b407e 2021-09-07 xhr }
696 142b407e 2021-09-07 xhr }
697 142b407e 2021-09-07 xhr
698 5d0c8c1e 2021-09-11 xhr curchar = c;
699 5d0c8c1e 2021-09-11 xhr
700 5d0c8c1e 2021-09-11 xhr load_journey(c->id);
701 5d0c8c1e 2021-09-11 xhr load_fight(c->id);
702 5d0c8c1e 2021-09-11 xhr update_prompt();
703 5d0c8c1e 2021-09-11 xhr print_character();
704 dc9d9521 2021-09-16 xhr
705 dc9d9521 2021-09-16 xhr json_object_put(root);
706 5d0c8c1e 2021-09-11 xhr
707 5d0c8c1e 2021-09-11 xhr return 0;
708 834ec24f 2021-09-06 xhr }
709 834ec24f 2021-09-06 xhr
710 834ec24f 2021-09-06 xhr void
711 834ec24f 2021-09-06 xhr cmd_print_current_character(__attribute__((unused)) char *unused)
712 834ec24f 2021-09-06 xhr {
713 834ec24f 2021-09-06 xhr if (curchar == NULL) {
714 be4af772 2021-09-09 xhr printf("No character loaded. Use 'cd' to load a character\n");
715 834ec24f 2021-09-06 xhr } else
716 d6d6a902 2021-09-08 xhr print_character();
717 834ec24f 2021-09-06 xhr }
718 834ec24f 2021-09-06 xhr
719 834ec24f 2021-09-06 xhr void
720 d6d6a902 2021-09-08 xhr print_character()
721 c184771b 2021-09-01 xhr {
722 d6d6a902 2021-09-08 xhr if (curchar == NULL) {
723 030e6fb5 2021-09-06 xhr log_debug("Nothing to print here\n");
724 030e6fb5 2021-09-06 xhr return;
725 030e6fb5 2021-09-06 xhr }
726 030e6fb5 2021-09-06 xhr
727 d6d6a902 2021-09-08 xhr log_debug("Character ID: %d\n", curchar->id);
728 d2b9d86f 2021-09-13 xhr printf("Name: %s (Exp: %d/30) Saved exp: %d ", curchar->name,
729 d2b9d86f 2021-09-13 xhr curchar->exp, curchar->exp_used);
730 d2b9d86f 2021-09-13 xhr if (curchar->dead == 1)
731 d2b9d86f 2021-09-13 xhr printf("[DECEASED]\n");
732 d2b9d86f 2021-09-13 xhr else
733 d2b9d86f 2021-09-13 xhr printf("\n");
734 d2b9d86f 2021-09-13 xhr
735 8401d074 2021-09-08 xhr printf("\nEdge: %d Heart: %d Iron: %d Shadow: %d Wits %d\n\n",
736 d6d6a902 2021-09-08 xhr curchar->edge, curchar->heart, curchar->iron, curchar->shadow, curchar->wits);
737 2bc4629d 2021-09-08 xhr printf("Momentum: %d/%d [%d] Health: %d/5 Spirit: %d/5 Supply: %d/5\n",
738 a1e2c7f5 2021-09-16 xhr curchar->momentum, curchar-> max_momentum, curchar->momentum_reset,
739 a1e2c7f5 2021-09-16 xhr curchar->health, curchar->spirit, curchar->supply);
740 8401d074 2021-09-08 xhr
741 8401d074 2021-09-08 xhr printf("\nWounded:\t%d Unprepared:\t%d Encumbered:\t%d Shaken:\t%d\n",
742 d6d6a902 2021-09-08 xhr curchar->wounded, curchar->unprepared, curchar->encumbered, curchar->shaken);
743 8401d074 2021-09-08 xhr printf("Corrupted:\t%d Tormented:\t%d Corrupted:\t%d Maimed:\t%d\n",
744 d6d6a902 2021-09-08 xhr curchar->corrupted, curchar->tormented, curchar->corrupted, curchar->maimed);
745 8401d074 2021-09-08 xhr
746 be9e9b67 2021-09-13 xhr printf("\nBonds: %.2f\n", curchar->bonds);
747 f50d7612 2021-09-10 xhr
748 f50d7612 2021-09-10 xhr if (curchar->journey_active == 1) {
749 a1e2c7f5 2021-09-16 xhr printf("\nActive Journey: Difficulty: %d Progress: %.2f/10\n",
750 a1e2c7f5 2021-09-16 xhr curchar->j->difficulty, curchar->j->progress);
751 2c40be59 2021-09-11 xhr }
752 2c40be59 2021-09-11 xhr if (curchar->fight_active == 1) {
753 a1e2c7f5 2021-09-16 xhr printf("\nActive Fight: Difficulty: %d Progress: %.2f/10\n",
754 a1e2c7f5 2021-09-16 xhr curchar->fight->difficulty, curchar->fight->progress);
755 f50d7612 2021-09-10 xhr }
756 c184771b 2021-09-01 xhr }
757 c184771b 2021-09-01 xhr
758 8ec6b849 2021-09-11 xhr void
759 8ec6b849 2021-09-11 xhr ask_for_journey_difficulty()
760 8ec6b849 2021-09-11 xhr {
761 8ec6b849 2021-09-11 xhr if (curchar == NULL) {
762 8ec6b849 2021-09-11 xhr log_debug("No character loaded\n");
763 8ec6b849 2021-09-11 xhr return;
764 8ec6b849 2021-09-11 xhr }
765 8ec6b849 2021-09-11 xhr
766 8ec6b849 2021-09-11 xhr printf("Please set a difficulty for your journey\n\n");
767 8ec6b849 2021-09-11 xhr printf("1\t - Troublesome journey (3 progress per waypoint)\n");
768 8ec6b849 2021-09-11 xhr printf("2\t - Dangerous journey (2 progress per waypoint)\n");
769 8ec6b849 2021-09-11 xhr printf("3\t - Formidable journey (2 progress per waypoint)\n");
770 8ec6b849 2021-09-11 xhr printf("4\t - Extreme journey (2 ticks per waypoint)\n");
771 8ec6b849 2021-09-11 xhr printf("5\t - Epic journey (1 tick per waypoint)\n\n");
772 8ec6b849 2021-09-11 xhr
773 8ec6b849 2021-09-11 xhr curchar->j->difficulty = ask_for_value("Enter a value between 1 and 5: ", 5);
774 8ec6b849 2021-09-11 xhr }
775 8ec6b849 2021-09-11 xhr
776 ee9774ad 2021-09-02 xhr int
777 8ec6b849 2021-09-11 xhr validate_range(int temp, int max)
778 ee9774ad 2021-09-02 xhr {
779 8ec6b849 2021-09-11 xhr if (temp < 1 || temp > max) {
780 8ec6b849 2021-09-11 xhr printf("Invalid range. The value has to be between 1 and %d\n", max);
781 ee9774ad 2021-09-02 xhr return -1;
782 ee9774ad 2021-09-02 xhr }
783 ee9774ad 2021-09-02 xhr
784 ee9774ad 2021-09-02 xhr return 0;
785 ee9774ad 2021-09-02 xhr }
786 ee9774ad 2021-09-02 xhr
787 12d2e10b 2021-09-02 xhr void
788 8b801c9b 2021-09-11 xhr free_character()
789 12d2e10b 2021-09-02 xhr {
790 8b801c9b 2021-09-11 xhr if (curchar == NULL) {
791 8b801c9b 2021-09-11 xhr log_debug("No character loaded\n");
792 8b801c9b 2021-09-11 xhr return;
793 00a86ceb 2021-09-11 xhr }
794 8b801c9b 2021-09-11 xhr
795 8b801c9b 2021-09-11 xhr if (curchar->name != NULL) {
796 8b801c9b 2021-09-11 xhr free(curchar->name);
797 8b801c9b 2021-09-11 xhr curchar->name = NULL;
798 775648f0 2021-09-10 xhr }
799 8b801c9b 2021-09-11 xhr if (curchar->j != NULL) {
800 8b801c9b 2021-09-11 xhr free(curchar->j);
801 8b801c9b 2021-09-11 xhr curchar->j = NULL;
802 775648f0 2021-09-10 xhr }
803 8b801c9b 2021-09-11 xhr if (curchar->fight != NULL) {
804 8b801c9b 2021-09-11 xhr free(curchar->fight);
805 8b801c9b 2021-09-11 xhr curchar->fight = NULL;
806 8b801c9b 2021-09-11 xhr }
807 8b801c9b 2021-09-11 xhr if (curchar != NULL) {
808 8b801c9b 2021-09-11 xhr free(curchar);
809 8b801c9b 2021-09-11 xhr curchar = NULL;
810 8b801c9b 2021-09-11 xhr }
811 12d2e10b 2021-09-02 xhr }
812 12d2e10b 2021-09-02 xhr
813 12d2e10b 2021-09-02 xhr int
814 8ec6b849 2021-09-11 xhr ask_for_value(const char *attribute, int max)
815 12d2e10b 2021-09-02 xhr {
816 12d2e10b 2021-09-02 xhr char *line;
817 12d2e10b 2021-09-02 xhr int temp = -1;
818 12d2e10b 2021-09-02 xhr
819 12d2e10b 2021-09-02 xhr again:
820 12d2e10b 2021-09-02 xhr line = readline(attribute);
821 12d2e10b 2021-09-02 xhr temp = atoi(line);
822 8ec6b849 2021-09-11 xhr if (validate_range(temp, max) == -1) {
823 12d2e10b 2021-09-02 xhr goto again;
824 12d2e10b 2021-09-02 xhr }
825 12d2e10b 2021-09-02 xhr
826 12d2e10b 2021-09-02 xhr free(line);
827 12d2e10b 2021-09-02 xhr return temp;
828 a20b3d4e 2021-09-08 xhr }
829 a20b3d4e 2021-09-08 xhr
830 a20b3d4e 2021-09-08 xhr int
831 a20b3d4e 2021-09-08 xhr character_exists(const char *name)
832 a20b3d4e 2021-09-08 xhr {
833 a20b3d4e 2021-09-08 xhr struct entry *np;
834 a20b3d4e 2021-09-08 xhr
835 a20b3d4e 2021-09-08 xhr if (strlen(name) == 0)
836 a20b3d4e 2021-09-08 xhr return -1;
837 a20b3d4e 2021-09-08 xhr
838 a20b3d4e 2021-09-08 xhr LIST_FOREACH(np, &head, entries) {
839 f8ef750c 2021-09-08 xhr if (strcasecmp(name, np->name) == 0) {
840 a20b3d4e 2021-09-08 xhr return 0;
841 a20b3d4e 2021-09-08 xhr }
842 a20b3d4e 2021-09-08 xhr }
843 a20b3d4e 2021-09-08 xhr
844 a20b3d4e 2021-09-08 xhr return -1;
845 12d2e10b 2021-09-02 xhr }
846 12d2e10b 2021-09-02 xhr
847 c184771b 2021-09-01 xhr struct character *
848 8a47a564 2021-09-07 xhr create_character(const char *name)
849 c184771b 2021-09-01 xhr {
850 c184771b 2021-09-01 xhr struct character *c;
851 c184771b 2021-09-01 xhr
852 c184771b 2021-09-01 xhr c = init_character_struct();
853 c184771b 2021-09-01 xhr
854 8a47a564 2021-09-07 xhr if (strlen(name) == 0) {
855 8a47a564 2021-09-07 xhr printf("Enter a name for your character: ");
856 8a47a564 2021-09-07 xhr c->name = readline(NULL);
857 9640b718 2021-09-09 xhr if (strlen(c->name) == 0) {
858 9640b718 2021-09-09 xhr printf("Please provide a longer name\n");
859 8b801c9b 2021-09-11 xhr free_character();
860 ac61f9dc 2021-09-09 xhr return NULL;
861 9640b718 2021-09-09 xhr }
862 a20b3d4e 2021-09-08 xhr if (character_exists(c->name) == 0) {
863 a20b3d4e 2021-09-08 xhr printf("Sorry, there is already a character named %s\n", c->name);
864 8b801c9b 2021-09-11 xhr free_character();
865 a20b3d4e 2021-09-08 xhr return NULL;
866 a20b3d4e 2021-09-08 xhr }
867 8a47a564 2021-09-07 xhr } else {
868 3677cf7a 2021-09-10 xhr if ((c->name = calloc(1, MAX_CHAR_LEN)) == NULL)
869 3677cf7a 2021-09-10 xhr log_errx(1, "calloc");
870 bb81a173 2021-09-08 xhr snprintf(c->name, MAX_CHAR_LEN, "%s", name);
871 8a47a564 2021-09-07 xhr printf("Creating a character named %s\n", c->name);
872 8a47a564 2021-09-07 xhr }
873 ee9774ad 2021-09-02 xhr
874 ee9774ad 2021-09-02 xhr printf("Now distribute the following values to your attributes: 3,2,2,1,1\n");
875 f203ff08 2021-09-08 xhr
876 8ec6b849 2021-09-11 xhr c->edge = ask_for_value("Edge : ", 4);
877 8ec6b849 2021-09-11 xhr c->heart = ask_for_value("Heart : ", 4);
878 8ec6b849 2021-09-11 xhr c->iron = ask_for_value("Iron : ", 4);
879 8ec6b849 2021-09-11 xhr c->wits = ask_for_value("Wits : ", 4);
880 8ec6b849 2021-09-11 xhr c->shadow = ask_for_value("Shadow : ", 4);
881 ee9774ad 2021-09-02 xhr
882 c184771b 2021-09-01 xhr return c;
883 c184771b 2021-09-01 xhr }
884 7badf7da 2021-09-08 xhr
885 7badf7da 2021-09-08 xhr struct character *
886 7badf7da 2021-09-08 xhr init_character_struct()
887 7badf7da 2021-09-08 xhr {
888 7badf7da 2021-09-08 xhr struct character *c;
889 7badf7da 2021-09-08 xhr
890 3677cf7a 2021-09-10 xhr if ((c = calloc(1, sizeof(struct character))) == NULL)
891 7badf7da 2021-09-08 xhr log_errx(1, "calloc");
892 7badf7da 2021-09-08 xhr
893 1df0f789 2021-09-10 xhr if ((c->j = calloc(1, sizeof(struct journey))) == NULL)
894 1df0f789 2021-09-10 xhr log_errx(1, "calloc");
895 1df0f789 2021-09-10 xhr
896 a6733253 2021-09-11 xhr if ((c->fight = calloc(1, sizeof(struct fight))) == NULL)
897 b0cb8082 2021-09-11 xhr log_errx(1, "calloc");
898 b0cb8082 2021-09-11 xhr
899 7badf7da 2021-09-08 xhr c->id = random();
900 7badf7da 2021-09-08 xhr c->name = NULL;
901 7badf7da 2021-09-08 xhr c->edge = c->heart = c->iron = c->shadow = c->wits = c->exp = 0;
902 2bc4629d 2021-09-08 xhr c->momentum = c->momentum_reset = 2;
903 7badf7da 2021-09-08 xhr c->max_momentum = 10;
904 7badf7da 2021-09-08 xhr c->health = c->spirit = c->supply = 5;
905 7badf7da 2021-09-08 xhr c->wounded = c->unprepared = c->shaken = c->encumbered = c->maimed = 0;
906 7badf7da 2021-09-08 xhr c->cursed = c->corrupted = c->tormented = c->exp_used = c->bonds = 0;
907 d2b9d86f 2021-09-13 xhr c->dead = 0;
908 67f6b276 2021-09-10 xhr
909 67f6b276 2021-09-10 xhr c->j->id = c->id;
910 1df0f789 2021-09-10 xhr c->j->difficulty = -1;
911 15d8381d 2021-09-11 xhr c->j->progress = 0.0;
912 1df0f789 2021-09-10 xhr c->journey_active = 0;
913 7badf7da 2021-09-08 xhr
914 a6733253 2021-09-11 xhr c->fight->id = c->id;
915 a6733253 2021-09-11 xhr c->fight->difficulty = -1;
916 a6733253 2021-09-11 xhr c->fight->progress = 0.0;
917 51a1ad92 2021-09-11 xhr c->fight->initiative = 0;
918 b0cb8082 2021-09-11 xhr c->fight_active = 0;
919 b0cb8082 2021-09-11 xhr
920 7badf7da 2021-09-08 xhr return c;
921 7badf7da 2021-09-08 xhr }
922 7badf7da 2021-09-08 xhr
923 f267c8e7 2021-09-08 xhr struct character *
924 f267c8e7 2021-09-08 xhr get_current_character()
925 f267c8e7 2021-09-08 xhr {
926 f267c8e7 2021-09-08 xhr return curchar;
927 f267c8e7 2021-09-08 xhr }