Debian Codegraph @

server/c_misc.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Various functions. Handles misc. input request - things like hash table, malloc, maps,
17 * who, etc.
18 */
19
20 #define WANT_UNARMED_SKILLS
21
22 #include "global.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29
30 #undef SS_STATISTICS
31 #include "loader.h"
32 #include "shstr.h"
33 #include "sounds.h"
34 #include "sproto.h"
35 #include "version.h"
36
37 /**
38 * This is the 'maps' command.
39 *
40 * @param op
41 * player requesting the information.
42 * @param search
43 * optional substring to search for.
44 */
45 void map_info(object *op, const char *search) {
46 if ( *search != 0 ) {
47 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MAPS,
48 i18n(op, "Maps matching search: '%s'"), search);
49 }
50 if (QUERY_FLAG(op, FLAG_WIZ)) {
51 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MAPS,
52 i18n(op, "[fixed]Path Reset In (HH:MM:SS) Pl IM TO"));
53 } else {
54 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MAPS,
55 i18n(op, "[fixed]Path Reset In (HH:MM)"));
56 }
57
58 /* Allow a comma-separate list of search strings; more complicated because of the const */
59 char *to_be_freed = NULL;
60 char *search_array[64];
61 int search_array_count = 0;
62 if ( search[0] ) {
63 to_be_freed = strdup(search);
64 if ( !to_be_freed ) {
65 search_array[0] = to_be_freed;
66 search_array_count = 1;
67 } else {
68 search_array_count = split_string(to_be_freed,search_array,64,',');
69 }
70 }
71
72 for (mapstruct *m = first_map; m != NULL; m = m->next) {
73 bool match = TRUE;
74 if ( search_array_count ) {
75 match = FALSE;
76 for (int i=0; i<search_array_count; ++i) {
77 if ( strstr(m->path,search_array[i]) ) {
78 match=TRUE;
79 break;
80 }
81 }
82 }
83 if ( !match ) continue; /* Skip unwanted maps */
84
85 /* Print out the last 26 characters of the map name... */
86 char map_path[MAX_BUF];
87 if (strlen(m->path) <= 26) {
88 strcpy(map_path, m->path);
89 } else {
90 safe_strncpy(map_path, m->path + strlen(m->path) - 26, sizeof(map_path));
91 }
92
93 uint32_t ttr = MAP_WHEN_RESET(m) - seconds() > 0 ? MAP_WHEN_RESET(m) - seconds() : 0;
94 if ( !m->reset_timeout && !m->unique ) set_map_reset_time(m);
95 if (m->players) {
96 ttr = (m->unique ? 0 : m->reset_timeout) + 300;
97 }
98 else if (m->timeout) {
99 ttr = (m->unique ? 0 : m->reset_timeout) + m->timeout;
100 }
101 const uint32_t hh = ttr/3600, mm = (ttr%3600)/60, ss = ttr%60;
102 if (QUERY_FLAG(op, FLAG_WIZ)) {
103 draw_ext_info_format(
104 NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MAPS,
105 i18n(op, "[fixed]%-26.26s %2d:%02d:%02d %2d %2d %4d"),
106 map_path, hh, mm, ss, m->players, m->in_memory, m->timeout);
107 } else {
108 draw_ext_info_format(
109 NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MAPS,
110 i18n(op, "[fixed]%-26.26s %2d:%02d%s"), map_path, hh, mm,
111 m->players ? " (in use)" : "");
112 }
113 }
114 if ( to_be_freed ) free( to_be_freed );
115 }
116
117 /**
118 * This is the 'language' command.
119 *
120 * @param op
121 * player requesting the information.
122 * @param params
123 * optional language code ("en", "fr", etc.)
124 */
125 void command_language(object *op, const char *params) {
126 int language = -1;
127
128 if (!op->contr)
129 return;
130
131 if (*params == '\0' || (!strcmp(params, ""))) {
132 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
133 i18n(op, "Your current language is set to: English."));
134 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
135 i18n(op, "Available languages:"));
136 i18n_list_languages(op);
137 return;
138 }
139
140 language = i18n_find_language_by_code(params);
141
142 /* Error out if unknown language. */
143 if (language == -1) {
144 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
145 i18n(op, "Unknown language."));
146 return;
147 }
148
149 op->contr->language = language;
150
151 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
152 i18n(op, "Your current language is set to English."));
153 }
154
155 /**
156 * This command dumps the body information for object *op.
157 * it doesn't care what the params are.
158 *
159 * This is mostly meant as a debug command.
160 *
161 * This is the 'body' command.
162 *
163 * @param op
164 * player to display body info for.
165 * @param params
166 * unused
167 */
168 void command_body(object *op, const char *params) {
169 int i;
170
171 /* Too hard to try and make a header that lines everything up, so just
172 * give a description.
173 */
174 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
175 i18n(op, "The first column is the name of the body location."));
176
177 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
178 i18n(op, "The second column is how many of those locations your body has."));
179
180 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
181 i18n(op, "The third column is how many slots in that location are available."));
182
183 for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
184 /* really debugging - normally body_used should not be set to anything
185 * if body_info isn't also set.
186 */
187 if (op->body_info[i] || op->body_used[i]) {
188 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
189 i18n(op, "[fixed]%-30s %5d %5d"),
190 i18n(op, body_locations[i].use_name), op->body_info[i], op->body_used[i]);
191 }
192 }
193 if (!QUERY_FLAG(op, FLAG_USE_ARMOUR))
194 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
195 i18n(op, "You are not allowed to wear armor."));
196 if (!QUERY_FLAG(op, FLAG_USE_WEAPON))
197 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_BODY,
198 i18n(op, "You are not allowed to use weapons."));
199 }
200
201 /**
202 * Display the message of the day.
203 *
204 * @param op
205 * player requesting the motd.
206 * @param params
207 * unused.
208 */
209 void command_motd(object *op, const char *params) {
210 display_motd(op);
211 }
212
213 /**
214 * Display the server rules.
215 *
216 * @param op
217 * player requesting the rules.
218 * @param params
219 * unused.
220 */
221 void command_rules(object *op, const char *params) {
222 send_rules(op);
223 }
224
225 /**
226 * Display the server news.
227 *
228 * @param op
229 * player requesting the news.
230 * @param params
231 * unused.
232 */
233 void command_news(object *op, const char *params) {
234 send_news(op);
235 }
236
237 /**
238 * Sends various memory-related statistics.
239 *
240 * @param op
241 * player requesting the information.
242 */
243 static void malloc_info(object *op) {
244 int ob_used = object_count_used(), ob_free = object_count_free(), players, nrofmaps;
245 int nrm = 0, mapmem = 0, anr, anims, sum_alloc = 0, sum_used = 0, i, tlnr, alnr;
246 treasurelist *tl;
247 player *pl;
248 mapstruct *m;
249 archetype *at;
250 artifactlist *al;
251
252 for (tl = first_treasurelist, tlnr = 0; tl != NULL; tl = tl->next, tlnr++)
253 ;
254 for (al = first_artifactlist, alnr = 0; al != NULL; al = al->next, alnr++)
255 ;
256
257 for (at = first_archetype, anr = 0, anims = 0; at != NULL; at = at->more == NULL ? at->next : at->more, anr++)
258 ;
259
260 for (i = 1; i < num_animations; i++)
261 anims += animations[i].num_animations;
262
263 for (pl = first_player, players = 0; pl != NULL; pl = pl->next, players++)
264 ;
265
266 for (m = first_map, nrofmaps = 0; m != NULL; m = m->next, nrofmaps++)
267 if (m->in_memory == MAP_IN_MEMORY) {
268 mapmem += map_size(m) * (sizeof(object *) + sizeof(MapSpace));
269 nrm++;
270 }
271
272 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
273 i18n(op, "Sizeof: object=%d player=%d map=%d"),
274 sizeof(object), sizeof(player), sizeof(mapstruct));
275
276 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
277 i18n(op, "[fixed]Objects:"));
278
279 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
280 i18n(op, "[fixed]%6d used"), ob_used);
281
282 if (ob_used != nrofallocobjects - nroffreeobjects) {
283 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
284 i18n(op, "[fixed] (used list mismatch: %d)"),
285 nrofallocobjects - nroffreeobjects);
286 }
287
288 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
289 i18n(op, "[fixed]%6d free (%.2f%% of %d allocated)"),
290 ob_free, (float)ob_free / nrofallocobjects * 100, nrofallocobjects);
291
292 if (ob_free != nroffreeobjects) {
293 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
294 i18n(op, "[fixed] (free list mismatch: %d)"),
295 nroffreeobjects);
296 }
297
298 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
299 i18n(op, "[fixed]%6d on active list"),
300 object_count_active());
301
302 i = (ob_used*sizeof(object));
303 sum_used += i;
304 sum_alloc += i;
305
306 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
307 i18n(op, "[fixed] object total: %11d"), i);
308
309 i = (ob_free*sizeof(object));
310 sum_alloc += i;
311
312 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
313 i18n(op, "[fixed]%4d players: %8d"),
314 players, i = (players*sizeof(player)));
315
316 sum_alloc += i;
317 sum_used += i;
318
319 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
320 i18n(op, "[fixed]%4d maps allocated: %8d"),
321 nrofmaps, i = (nrofmaps*sizeof(mapstruct)));
322
323 sum_alloc += i;
324 sum_used += nrm*sizeof(mapstruct);
325
326 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
327 i18n(op, "[fixed]%4d maps in memory: %8d"),
328 nrm, mapmem);
329
330 sum_alloc += mapmem;
331 sum_used += mapmem;
332
333 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
334 i18n(op, "[fixed]%4d archetypes: %8d"),
335 anr, i = (anr*sizeof(archetype)));
336
337 sum_alloc += i;
338 sum_used += i;
339
340 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
341 i18n(op, "[fixed]%4d animations: %8d"),
342 anims, i = (anims*sizeof(uint16_t)));
343
344 sum_alloc += i;
345 sum_used += i;
346
347 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
348 i18n(op, "[fixed]%4d treasurelists %8d"),
349 tlnr, i = (tlnr*sizeof(treasurelist)));
350
351 sum_alloc += i;
352 sum_used += i;
353
354 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
355 i18n(op, "[fixed]%4ld treasures %8d"),
356 nroftreasures, i = (nroftreasures*sizeof(treasure)));
357
358 sum_alloc += i;
359 sum_used += i;
360
361 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
362 i18n(op, "[fixed]%4ld artifacts %8d"),
363 nrofartifacts, i = (nrofartifacts*sizeof(artifact)));
364
365 sum_alloc += i;
366 sum_used += i;
367
368 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
369 i18n(op, "[fixed]%4ld artifacts strngs %8d"),
370 nrofallowedstr, i = (nrofallowedstr*sizeof(linked_char)));
371
372 sum_alloc += i;
373 sum_used += i;
374
375 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
376 i18n(op, "[fixed]%4d artifactlists %8d"),
377 alnr, i = (alnr*sizeof(artifactlist)));
378
379 sum_alloc += i;
380 sum_used += i;
381
382 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
383 i18n(op, "[fixed]Total space allocated:%8d"),
384 sum_alloc);
385
386 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_MALLOC,
387 i18n(op, "[fixed]Total space used: %8d"),
388 sum_used);
389 }
390
391 /**
392 * 'whereami' command.
393 *
394 * Pretty much identical to current map_info(), but on a bigger scale
395 *
396 * This function returns the name of the players current region, and
397 * a description of it. It is there merely for flavour text.
398 *
399 * @param op
400 * player wanting information.
401 */
402 void current_region_info(object *op) {
403 /*
404 * Ok I /suppose/ I should write a seperate function for this, but it isn't
405 * going to be /that/ slow, and won't get called much
406 */
407 region *r = get_region_by_name(get_name_of_region_for_map(op->map));
408
409 /* This should only be possible if regions are not operating on this server. */
410 if (!r)
411 return;
412
413 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
414 i18n(op, "You are in %s.\n%s"),
415 get_region_longname(r), get_region_msg(r));
416 }
417
418 /**
419 * 'mapinfo' command.
420 *
421 * @param op
422 * player requesting the information.
423 */
424 void current_map_info(object *op) {
425 mapstruct *m = op->map;
426
427 if (!m)
428 return;
429
430 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
431 "%s (%s) in %s",
432 m->name, m->path, get_region_longname(get_region_by_map(m)));
433
434 if (QUERY_FLAG(op, FLAG_WIZ)) {
435 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
436 i18n(op, "players:%d difficulty:%d size:%dx%d start:%dx%d timeout %d"),
437 m->players, m->difficulty,
438 MAP_WIDTH(m), MAP_HEIGHT(m),
439 MAP_ENTER_X(m), MAP_ENTER_Y(m),
440 MAP_TIMEOUT(m));
441 }
442 if (m->msg)
443 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE, m->msg);
444 }
445
446 #ifdef DEBUG_MALLOC_LEVEL
447 /**
448 * Checks the server heap.
449 *
450 * @param op
451 * player checking.
452 * @param parms
453 * ignored.
454 */
455 void command_malloc_verify(object *op, char *parms) {
456 extern int malloc_verify(void);
457
458 if (!malloc_verify())
459 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
460 i18n(op, "Heap is corrupted."));
461 else
462 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
463 i18n(op, "Heap checks out OK."));
464
465 return 1;
466 }
467 #endif
468
469 /**
470 * 'whereabouts' command.
471 *
472 * Displays how many players are in which regions.
473 *
474 * @param op
475 * player requesting information.
476 * @param params
477 * unused.
478 */
479 void command_whereabouts(object *op, const char *params) {
480 region *reg;
481 player *pl;
482
483 /*
484 * reset the counter on the region, then use it to store the number of
485 * players there.
486 * I don't know how thread-safe this would be, I suspect not very....
487 */
488 for (reg = first_region; reg != NULL; reg = reg->next) {
489 reg->counter = 0;
490 }
491 for (pl = first_player; pl != NULL; pl = pl->next)
492 if (pl->ob->map != NULL)
493 get_region_by_map(pl->ob->map)->counter++;
494
495 /* we only want to print out by places with a 'longname' field...*/
496 for (reg = first_region; reg != NULL; reg = reg->next) {
497 if (reg->longname == NULL && reg->counter > 0) {
498 if (reg->parent != NULL) {
499 reg->parent->counter += reg->counter;
500 reg->counter = 0;
501 } else /*uh oh, we shouldn't be here. */
502 LOG(llevError, "command_whereabouts() Region %s with no longname has no parent\n", reg->name);
503 }
504 }
505 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
506 i18n(op, "In the world currently there are:"));
507
508 for (reg = first_region; reg != NULL; reg = reg->next)
509 if (reg->counter > 0) {
510 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
511 i18n(op, "%u players in %s"),
512 reg->counter, get_region_longname(reg));
513 }
514 }
515
516 /** Utility structure for the 'who' command. */
517 typedef struct {
518 char namebuf[MAX_BUF];
519 int login_order;
520 } chars_names;
521
522 /**
523 * Local function for qsort comparison.
524 *
525 * @param c1
526 * @param c2
527 * players to compare.
528 * @return
529 * -1, 0 or 1 depending on c1 and c2's order.
530 */
531 static int name_cmp(const chars_names *c1, const chars_names *c2) {
532 return strcasecmp(c1->namebuf, c2->namebuf);
533 }
534
535 /**
536 * Displays the players in a region or party. If both are NULL, all players are listed.
537 *
538 * @param op
539 * who is asking for player list.
540 * @param reg
541 * region to display players of.
542 * @param party
543 * party to list.
544 */
545 void list_players(object *op, region *reg, partylist *party) {
546 player *pl;
547 uint16_t i;
548 char *format;
549 int num_players = 0, num_wiz = 0, num_afk = 0, num_bot = 0;
550 chars_names *chars = NULL;
551
552 if (op == NULL || QUERY_FLAG(op, FLAG_WIZ))
553 format = settings.who_wiz_format;
554 else
555 format = settings.who_format;
556
557 for (pl = first_player; pl != NULL; pl = pl->next) {
558 if (pl->ob->map == NULL)
559 continue;
560 if (pl->hidden && !QUERY_FLAG(op, FLAG_WIZ))
561 continue;
562
563 if (reg && !region_is_child_of_region(get_region_by_map(pl->ob->map), reg))
564 continue;
565 if (party && pl->party != party)
566 continue;
567
568 if (pl->state == ST_PLAYING || pl->state == ST_GET_PARTY_PASSWORD) {
569 num_players++;
570 chars = (chars_names *)realloc(chars, num_players*sizeof(chars_names));
571 if (chars == NULL) {
572 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_WHO,
573 i18n(op, "who failed - out of memory!"));
574 return;
575 }
576 sprintf(chars[num_players-1].namebuf, "%s", pl->ob->name);
577 chars[num_players-1].login_order = num_players;
578
579 /* Check for WIZ's & AFK's*/
580 if (QUERY_FLAG(pl->ob, FLAG_WIZ))
581 num_wiz++;
582
583 if (QUERY_FLAG(pl->ob, FLAG_AFK))
584 num_afk++;
585
586 if (pl->socket.is_bot)
587 num_bot++;
588 }
589 }
590 if (first_player != (player *)NULL) {
591 if (reg == NULL && party == NULL)
592 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_WHO,
593 i18n(op, "Total Players (%d) -- WIZ(%d) AFK(%d) BOT(%d)"),
594 num_players, num_wiz, num_afk, num_bot);
595 else if (party == NULL)
596 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_WHO,
597 i18n(op, "Total Players in %s (%d) -- WIZ(%d) AFK(%d) BOT(%d)"),
598 reg->longname ? reg->longname : reg->name, num_players, num_wiz, num_afk, num_bot);
599 else
600 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_WHO,
601 i18n(op, "Total Players in party %s (%d) -- WIZ(%d) AFK(%d) BOT(%d)"),
602 party->partyname, num_players, num_wiz, num_afk, num_bot);
603 }
604 qsort(chars, num_players, sizeof(chars_names), (int (*)(const void *, const void *))name_cmp);
605 for (i = 0; i < num_players; i++)
606 display_who_entry(op, find_player(chars[i].namebuf), format);
607 free(chars);
608 }
609
610 /**
611 * 'who' command.
612 *
613 * @param op
614 * player requesting the information.
615 * @param params
616 * optional region to limit the information to.
617 */
618 void command_who(object *op, const char *params) {
619 region *reg;
620
621 reg = get_region_from_string(params);
622 list_players(op, reg, NULL);
623 }
624
625 /**
626 * Display a line of 'who' to op, about pl, using the formatting specified by format.
627 *
628 * @param op
629 * player getting the information.
630 * @param pl
631 * player to display information for.
632 * @param format
633 * format to display.
634 */
635 void display_who_entry(object *op, player *pl, const char *format) {
636 char tmpbuf[MAX_BUF];
637 char outbuf[MAX_BUF];
638 size_t i;
639
640 strcpy(outbuf, "[fixed]");
641
642 if (pl == NULL) {
643 LOG(llevError, "display_who_entry(): I was passed a null player\n");
644 return;
645 }
646 for (i = 0; i <= strlen(format); i++) {
647 if (format[i] == '%') {
648 i++;
649 get_who_escape_code_value(tmpbuf, sizeof(tmpbuf), format[i], pl);
650 strcat(outbuf, tmpbuf);
651 } else if (format[i] == '_') {
652 strcat(outbuf, " "); /* allow '_' to be used in place of spaces */
653 } else {
654 snprintf(tmpbuf, sizeof(tmpbuf), "%c", format[i]);
655 strcat(outbuf, tmpbuf);
656 }
657 }
658 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_WHO, outbuf);
659 }
660
661 /**
662 * Returns the value of the escape code used in the who format specifier.
663 *
664 * Specifier values are:
665 * - N Name of character
666 * - t title of character
667 * - c count
668 * - n newline
669 * - h \<Hostile\> if character is hostile, nothing otherwise
670 * - d \<WIZ\> if character is a dm, nothing otherwise
671 * - a \<AFK\> if character is afk, nothing otherwise
672 * - b \<BOT\> if character is a bot, nothing otherwise
673 * - l the level of the character
674 * - m the map path the character is currently on
675 * - M the map name of the map the character is currently on
676 * - r the region name (eg scorn, wolfsburg)
677 * - R the regional title (eg The Kingdom of Scorn, The Port of Wolfsburg)
678 * - i player's ip address
679 * - % a literal %
680 * - _ a literal underscore
681 *
682 * @param[out] return_val
683 * buffer that will contain the information.
684 * @param size
685 * length of return_val.
686 * @param letter
687 * format specifier.
688 * @param pl
689 * player to get information for.
690 */
691 void get_who_escape_code_value(char *return_val, int size, const char letter, player *pl) {
692 switch (letter) {
693 case 'N':
694 strlcpy(return_val, pl->ob->name, size);
695 break;
696
697 case 't':
698 player_get_title(pl, return_val, size);
699 break;
700
701 case 'c':
702 snprintf(return_val, size, "%u", pl->ob->count);
703 break;
704
705 case 'n':
706 snprintf(return_val, size, "\n");
707 break;
708
709 case 'h':
710 strlcpy(return_val, pl->peaceful ? "" : " <Hostile>", size);
711 break;
712
713 case 'l':
714 snprintf(return_val, size, "%d", pl->ob->level);
715 break;
716
717 case 'd':
718 strlcpy(return_val, (QUERY_FLAG(pl->ob, FLAG_WIZ) ? " <WIZ>" : ""), size);
719 break;
720
721 case 'a':
722 strlcpy(return_val, (QUERY_FLAG(pl->ob, FLAG_AFK) ? " <AFK>" : ""), size);
723 break;
724
725 case 'b':
726 strlcpy(return_val, (pl->socket.is_bot == 1) ? " <BOT>" : "", size);
727 break;
728
729 case 'm':
730 strlcpy(return_val, pl->ob->map->path, size);
731 break;
732
733 case 'M':
734 strlcpy(return_val, pl->ob->map->name ? pl->ob->map->name : "Untitled", size);
735 break;
736
737 case 'r':
738 strlcpy(return_val, get_name_of_region_for_map(pl->ob->map), size);
739 break;
740
741 case 'R':
742 strlcpy(return_val, get_region_longname(get_region_by_map(pl->ob->map)), size);
743 break;
744
745 case 'i':
746 strlcpy(return_val, pl->socket.host, size);
747 break;
748
749 case '%':
750 snprintf(return_val, size, "%%");
751 break;
752
753 case '_':
754 snprintf(return_val, size, "_");
755 break;
756 }
757 }
758
759 /**
760 * Toggles the afk status of a player. 'afk' command.
761 *
762 * @param op
763 * player to toggle status for.
764 * @param params
765 * unused.
766 */
767 void command_afk(object *op, const char *params) {
768 if (QUERY_FLAG(op, FLAG_AFK)) {
769 CLEAR_FLAG(op, FLAG_AFK);
770 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
771 i18n(op, "You are no longer AFK"));
772 } else {
773 SET_FLAG(op, FLAG_AFK);
774 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
775 i18n(op, "You are now AFK"));
776 }
777 }
778
779 /**
780 * Display memory information.
781 *
782 * @param op
783 * player requesting information.
784 * @param params
785 * unused.
786 */
787 void command_malloc(object *op, const char *params) {
788 malloc_info(op);
789 }
790
791 /**
792 * 'mapinfo' command.
793 *
794 * @param op
795 * player requesting the information.
796 * @param params
797 * unused.
798 */
799 void command_mapinfo(object *op, const char *params) {
800 current_map_info(op);
801 }
802
803 /**
804 * 'whereami' command.
805 *
806 * @param op
807 * player requesting the information.
808 * @param params
809 * unused.
810 */
811 void command_whereami(object *op, const char *params) {
812 current_region_info(op);
813 }
814
815 /**
816 * 'maps' command.
817 *
818 * @param op
819 * player requesting the information.
820 * @param params
821 * region to restrict to.
822 */
823 void command_maps(object *op, const char *params) {
824 map_info(op, params);
825 }
826
827 /**
828 * Various string-related statistics.
829 *
830 * @param op
831 * player asking for information.
832 * @param params
833 * unused.
834 */
835 void command_strings(object *op, const char *params) {
836 char stats[HUGE_BUF];
837
838 ss_dump_statistics(stats, sizeof(stats));
839 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_LAST,
840 "[fixed]%s\n",
841 stats);
842
843 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_LAST,
844 ss_dump_table(SS_DUMP_TOTALS, stats, sizeof(stats)));
845 }
846
847 /**
848 * Players asks for the time.
849 *
850 * @param op
851 * player asking for information.
852 * @param params
853 * unused.
854 */
855 void command_time(object *op, const char *params) {
856 time_info(op);
857 }
858
859 /**
860 * Archetype-related statistics. Wizard 'archs' command.
861 *
862 * @param op
863 * player asking for information.
864 * @param params
865 * unused.
866 */
867 void command_archs(object *op, const char *params) {
868 arch_info(op);
869 }
870
871 /**
872 * Player is asking for the hiscore.
873 *
874 * @param op
875 * player asking for information.
876 * @param params
877 * unused.
878 */
879 void command_hiscore(object *op, const char *params) {
880 hiscore_display(op, op == NULL ? 9999 : 50, params);
881 }
882
883 /**
884 * Player wants to see/change the debug level.
885 *
886 * @param op
887 * player asking for information.
888 * @param params
889 * new debug value.
890 */
891 void command_debug(object *op, const char *params) {
892 int i;
893
894 if (*params == '\0' || !sscanf(params, "%d", &i)) {
895 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
896 i18n(op, "Global debug level is %d."),
897 settings.debug);
898 return;
899 }
900 settings.debug = (enum LogLevel)FABS(i);
901 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
902 i18n(op, "Debug level set to %d."),
903 i);
904 }
905
906
907 /**
908 * Wizard toggling wall-crossing.
909 *
910 * @param op
911 * player asking for information.
912 * @param params
913 * unused.
914 */
915 void command_wizpass(object *op, const char *params) {
916 int i;
917
918 if (!op)
919 return;
920
921 if (*params == '\0')
922 i = (QUERY_FLAG(op, FLAG_WIZPASS)) ? 0 : 1;
923 else
924 i = onoff_value(params);
925
926 if (i) {
927 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
928 i18n(op, "You will now walk through walls."));
929 SET_FLAG(op, FLAG_WIZPASS);
930 } else {
931 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
932 i18n(op, "You will now be stopped by walls."));
933 CLEAR_FLAG(op, FLAG_WIZPASS);
934 }
935 }
936
937 /**
938 * Wizard toggling "cast everywhere" ability.
939 *
940 * @param op
941 * player asking for information.
942 * @param params
943 * unused.
944 */
945 void command_wizcast(object *op, const char *params) {
946 int i;
947
948 if (!op)
949 return;
950
951 if (*params == '\0')
952 i = (QUERY_FLAG(op, FLAG_WIZCAST)) ? 0 : 1;
953 else
954 i = onoff_value(params);
955
956 if (i) {
957 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
958 i18n(op, "You can now cast spells anywhere."));
959 SET_FLAG(op, FLAG_WIZCAST);
960 } else {
961 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
962 i18n(op, "You now cannot cast spells in no-magic areas."));
963 CLEAR_FLAG(op, FLAG_WIZCAST);
964 }
965 }
966
967 /**
968 * Various object-related statistics.
969 *
970 * @param op
971 * player asking for information.
972 * @param params
973 * unused.
974 */
975 void command_dumpallobjects(object *op, const char *params) {
976 object_dump_all();
977 }
978
979 /**
980 * Various friendly object-related statistics.
981 *
982 * @param op
983 * player asking for information.
984 * @param params
985 * unused.
986 */
987 void command_dumpfriendlyobjects(object *op, const char *params) {
988 dump_friendly_objects();
989 }
990
991 /**
992 * Various archetypes-related statistics.
993 *
994 * @param op
995 * player asking for information.
996 * @param params
997 * unused.
998 */
999 void command_dumpallarchetypes(object *op, const char *params) {
1000 dump_all_archetypes();
1001 }
1002
1003 /**
1004 * Various string-related statistics.
1005 *
1006 * @param op
1007 * player asking for information.
1008 * @param params
1009 * unused.
1010 */
1011 void command_ssdumptable(object *op, const char *params) {
1012 ss_dump_table(SS_DUMP_TABLE, NULL, 0);
1013 }
1014
1015 /**
1016 * Various map-related statistics.
1017 *
1018 * @param op
1019 * player asking for information.
1020 * @param params
1021 * unused.
1022 */
1023 void command_dumpmap(object *op, const char *params) {
1024 if (op)
1025 dump_map(op->map);
1026 }
1027
1028 /**
1029 * Various map-related statistics.
1030 *
1031 * @param op
1032 * player asking for information.
1033 * @param params
1034 * unused.
1035 */
1036 void command_dumpallmaps(object *op, const char *params) {
1037 dump_all_maps();
1038 }
1039
1040 /**
1041 * Various LOS-related statistics.
1042 *
1043 * @param op
1044 * player asking for information.
1045 * @param params
1046 * unused.
1047 */
1048 void command_printlos(object *op, const char *params) {
1049 if (op)
1050 print_los(op);
1051 }
1052
1053
1054 /**
1055 * Server version.
1056 *
1057 * @param op
1058 * player asking for information.
1059 * @param params
1060 * unused.
1061 */
1062 void command_version(object *op, const char *params) {
1063 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN,
1064 MSG_TYPE_ADMIN_VERSION, "Crossfire "FULL_VERSION);
1065 }
1066
1067 /**
1068 * Change the player's listen level.
1069 *
1070 * @param op
1071 * player asking for information.
1072 * @param params
1073 * unused.
1074 */
1075 void command_listen(object *op, const char *params) {
1076 int i;
1077
1078 if (*params == '\0' || !sscanf(params, "%d", &i)) {
1079 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1080 i18n(op, "Set listen to what (presently %d)?"),
1081 op->contr->listening);
1082 return;
1083 }
1084 if (i < 0) {
1085 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1086 i18n(op, "Verbose level should be positive."));
1087 return;
1088 }
1089 op->contr->listening = (char)i;
1090 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1091 i18n(op, "Your verbose level is now %d."),
1092 i);
1093 }
1094
1095 /**
1096 * Prints out some useful information for the character. Everything we print
1097 * out can be determined by the docs, so we aren't revealing anything extra -
1098 * rather, we are making it convenient to find the values. params have
1099 * no meaning here.
1100 *
1101 * @param pl
1102 * player asking for information.
1103 * @param params
1104 * unused.
1105 */
1106 void command_statistics(object *pl, const char *params) {
1107 char buf[MAX_BUF];
1108 uint32_t hours, minutes;
1109 uint64_t seconds; /* 64 bit to prevent overflows an intermediate results */
1110
1111 if (!pl->contr)
1112 return;
1113 safe_strncpy(buf, i18n(pl, "[fixed] Experience: %"), sizeof(buf));
1114 strcat(buf, FMT64);
1115 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1116 buf,
1117 pl->stats.exp);
1118 safe_strncpy(buf, i18n(pl, "[fixed] Next Level: %"), sizeof(buf));
1119 strcat(buf, FMT64);
1120 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1121 buf,
1122 level_exp(pl->level+1, pl->expmul));
1123
1124 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1125 i18n(pl, "[fixed]\nStat Nat/Real/Max"));
1126
1127 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1128 i18n(pl, "[fixed]Str %2d/ %3d/%3d"),
1129 pl->contr->orig_stats.Str, pl->stats.Str, 20+pl->arch->clone.stats.Str);
1130 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1131 i18n(pl, "[fixed]Dex %2d/ %3d/%3d"),
1132 pl->contr->orig_stats.Dex, pl->stats.Dex, 20+pl->arch->clone.stats.Dex);
1133 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1134 i18n(pl, "[fixed]Con %2d/ %3d/%3d"),
1135 pl->contr->orig_stats.Con, pl->stats.Con, 20+pl->arch->clone.stats.Con);
1136 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1137 i18n(pl, "[fixed]Int %2d/ %3d/%3d"),
1138 pl->contr->orig_stats.Int, pl->stats.Int, 20+pl->arch->clone.stats.Int);
1139 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1140 i18n(pl, "[fixed]Wis %2d/ %3d/%3d"),
1141 pl->contr->orig_stats.Wis, pl->stats.Wis, 20+pl->arch->clone.stats.Wis);
1142 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1143 i18n(pl, "[fixed]Pow %2d/ %3d/%3d"),
1144 pl->contr->orig_stats.Pow, pl->stats.Pow, 20+pl->arch->clone.stats.Pow);
1145 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1146 i18n(pl, "[fixed]Cha %2d/ %3d/%3d"),
1147 pl->contr->orig_stats.Cha, pl->stats.Cha, 20+pl->arch->clone.stats.Cha);
1148 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1149 i18n(pl, "\nAttack Mode: %s"),
1150 i18n(pl, pl->contr->peaceful ? "Peaceful" : "Hostile"));
1151 /**
1152 * Add preliminary damage per second calculations here
1153 */
1154 float weap_speed = pl->weapon_speed; // This is the number of attacks per tick.
1155 if (weap_speed < 0.0f)
1156 weap_speed = 0.0f;
1157 if (weap_speed > 1.0f)
1158 weap_speed = 1.0f;
1159 // We will initially calculate the damage if every attack you perform hits.
1160 // This will serve as a baseline for future calculations
1161 float dps = (1000000.0f / max_time) * weap_speed * pl->stats.dam;
1162 // TODO: Account for opposing AC in calculations, make some sort of table/chart.
1163 // Then we round the floating-point.
1164 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1165 i18n(pl, "\n\nDam/Sec: %4d"), (int)(dps + 0.5f));
1166
1167 /* max_time is in microseconds - thus divide by 1000000.
1168 * Need 64 bit values, as otherwise ticks_played * max_time
1169 * can easily overflow.
1170 * Note the message displayed here isn't really
1171 * perfect, since if max_time has been changed since the player started,
1172 * the time estimates use the current value. But I'm presuming that
1173 * max_time won't change very often. MSW 2009-12-01
1174 */
1175 seconds = (uint64_t)pl->contr->ticks_played * (uint64_t)max_time / 1000000;
1176 minutes = (uint32_t)seconds / 60;
1177 hours = minutes / 60;
1178 minutes = minutes % 60;
1179
1180 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_STATISTICS,
1181 "You have played this character for %u ticks, which amounts "
1182 "to %d hours and %d minutes.",
1183 pl->contr->ticks_played, hours, minutes);
1184
1185
1186 /* Can't think of anything else to print right now */
1187 }
1188
1189 /**
1190 * Wrapper to fix a player.
1191 *
1192 * @param op
1193 * player asking to be fixed.
1194 * @param params
1195 * unused.
1196 */
1197 void command_fix_me(object *op, const char *params) {
1198 object_sum_weight(op);
1199 fix_object(op);
1200 }
1201
1202 /**
1203 * Display all known players.
1204 *
1205 * @param op
1206 * player asking for information.
1207 * @param params
1208 * unused.
1209 */
1210 void command_players(object *op, const char *params) {
1211 char buf[MAX_BUF];
1212 char *t;
1213 DIR *dir;
1214
1215 snprintf(buf, sizeof(buf), "%s/%s/", settings.localdir, settings.playerdir);
1216 t = buf+strlen(buf);
1217 if ((dir = opendir(buf)) != NULL) {
1218 const struct dirent *entry;
1219
1220 while ((entry = readdir(dir)) != NULL) {
1221 /* skip '.' , '..' */
1222 if (!((entry->d_name[0] == '.' && entry->d_name[1] == '\0')
1223 || (entry->d_name[0] == '.' && entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) {
1224 struct stat st;
1225
1226 strcpy(t, entry->d_name);
1227 if (stat(buf, &st) == 0) {
1228 /* This was not posix compatible
1229 * if ((st.st_mode & S_IFMT)==S_IFDIR) {
1230 */
1231 if (S_ISDIR(st.st_mode)) {
1232 struct tm *tm = localtime(&st.st_mtime);
1233
1234 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1235 i18n(op, "[fixed]%s\t%04d %02d %02d %02d %02d %02d"),
1236 entry->d_name,
1237 1900+tm->tm_year,
1238 1+tm->tm_mon,
1239 tm->tm_mday,
1240 tm->tm_hour,
1241 tm->tm_min,
1242 tm->tm_sec);
1243 }
1244 }
1245 }
1246 }
1247 }
1248 closedir(dir);
1249 }
1250
1251 /**
1252 * Players wants to change the apply mode, ie how to handle applying an item when no body slot available.
1253 *
1254 * @param op
1255 * player asking for change.
1256 * @param params
1257 * new mode.
1258 */
1259 void command_applymode(object *op, const char *params) {
1260 unapplymode unapply = op->contr->unapply;
1261 static const char *const types[] = {
1262 "nochoice",
1263 "never",
1264 "always"
1265 };
1266
1267 if (*params == '\0') {
1268 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1269 i18n(op, "applymode is set to %s"),
1270 types[op->contr->unapply]);
1271 return;
1272 }
1273
1274 if (!strcmp(params, "nochoice"))
1275 op->contr->unapply = unapply_nochoice;
1276 else if (!strcmp(params, "never"))
1277 op->contr->unapply = unapply_never;
1278 else if (!strcmp(params, "always"))
1279 op->contr->unapply = unapply_always;
1280 else {
1281 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1282 i18n(op, "applymode: Unknown options %s, valid options are nochoice, never, always"),
1283 params);
1284 return;
1285 }
1286 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1287 i18n(op, "applymode%s set to %s"),
1288 (unapply == op->contr->unapply ? "" : " now"),
1289 types[op->contr->unapply]);
1290 }
1291
1292 /**
1293 * Player wants to change the bowmode, how arrows are fired.
1294 *
1295 * @param op
1296 * player asking for change.
1297 * @param params
1298 * new mode.
1299 */
1300 void command_bowmode(object *op, const char *params) {
1301 bowtype_t oldtype = op->contr->bowtype;
1302 static const char *const types[] = {
1303 "normal",
1304 "threewide",
1305 "spreadshot",
1306 "firenorth",
1307 "firene",
1308 "fireeast",
1309 "firese",
1310 "firesouth",
1311 "firesw",
1312 "firewest",
1313 "firenw",
1314 "bestarrow"
1315 };
1316 int i, found;
1317
1318 if (*params == '\0') {
1319 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1320 i18n(op, "bowmode is set to %s"),
1321 types[op->contr->bowtype]);
1322 return;
1323 }
1324
1325 for (i = 0, found = 0; i <= bow_bestarrow; i++) {
1326 if (!strcmp(params, types[i])) {
1327 found++;
1328 op->contr->bowtype = i;
1329 break;
1330 }
1331 }
1332 if (!found) {
1333 StringBuffer *buf = stringbuffer_new();
1334 stringbuffer_append_printf(buf, "bowmode: Unknown options %s, valid options are:", params);
1335 for (i = 0; i <= bow_bestarrow; i++) {
1336 stringbuffer_append_string(buf, " ");
1337 stringbuffer_append_string(buf, types[i]);
1338 if (i < bow_nw)
1339 stringbuffer_append_string(buf, ",");
1340 else
1341 stringbuffer_append_string(buf, ".");
1342 }
1343 char *result = stringbuffer_finish(buf);
1344 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG, result);
1345 free(result);
1346 return;
1347 }
1348 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1349 i18n(op, "bowmode%s set to %s"),
1350 (oldtype == op->contr->bowtype ? "" : " now"),
1351 types[op->contr->bowtype]);
1352 return;
1353 }
1354
1355 /**
1356 * Player wants to change prefered unarmed skill
1357 *
1358 * @param op
1359 * player asking for change.
1360 * @param params
1361 * new mode.
1362 */
1363 void command_unarmed_skill(object *op, const char *params) {
1364 object *skill;
1365 size_t i;
1366
1367 if (*params == '\0') {
1368 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1369 "unarmed skill is set to %s",
1370 op->contr->unarmed_skill ? op->contr->unarmed_skill: "nothing");
1371 return;
1372 }
1373
1374 /* find_skill_by_name() will ready any skill tools - which
1375 * is OK for us because no unarmed skills require skill tools,
1376 * but this could be an issue if you reuse this code for other skills.
1377 */
1378 skill = find_skill_by_name(op, params);
1379
1380 if (!skill) {
1381 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
1382 "You do not know any such skill called %s",
1383 params);
1384 return;
1385 }
1386 for (i = 0; i < sizeof(unarmed_skills); i++)
1387 if (skill->subtype == unarmed_skills[i])
1388 break;
1389 if (i == sizeof(unarmed_skills)) {
1390 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR,
1391 "%s is not an unarmed skill!",
1392 skill->name);
1393 return;
1394
1395 }
1396
1397 if (op->contr->unarmed_skill)
1398 free_string(op->contr->unarmed_skill);
1399
1400 /* Taking actual skill name is better than taking params,
1401 * as params could be something more than an exact skill name.
1402 */
1403 op->contr->unarmed_skill = add_string(skill->name);
1404
1405 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1406 "unarmed skill is now set to %s",
1407 op->contr->unarmed_skill);
1408 }
1409
1410
1411 /**
1412 * Player wants to change how her pets behave.
1413 *
1414 * @param op
1415 * player asking for change.
1416 * @param params
1417 * new mode.
1418 */
1419 void command_petmode(object *op, const char *params) {
1420 petmode_t oldtype = op->contr->petmode;
1421 static const char *const types[] = {
1422 "normal",
1423 "sad",
1424 "defend",
1425 "arena"
1426 };
1427
1428 if (*params == '\0') {
1429 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1430 i18n(op, "petmode is set to %s"),
1431 types[op->contr->petmode]);
1432 return;
1433 }
1434
1435 if (!strcmp(params, "normal"))
1436 op->contr->petmode = pet_normal;
1437 else if (!strcmp(params, "sad"))
1438 op->contr->petmode = pet_sad;
1439 else if (!strcmp(params, "defend"))
1440 op->contr->petmode = pet_defend;
1441 else if (!strcmp(params, "arena"))
1442 op->contr->petmode = pet_arena;
1443 else {
1444 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1445 i18n(op, "petmode: Unknown options %s, valid options are normal, sad (seek and destroy), defend, arena"),
1446 params);
1447 return;
1448 }
1449 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1450 i18n(op, "petmode%s set to %s"),
1451 (oldtype == op->contr->petmode ? "" : " now"),
1452 types[op->contr->petmode]);
1453 }
1454
1455 /**
1456 * Players wants to know her pets.
1457 *
1458 * @param op
1459 * player asking for information.
1460 * @param params
1461 * unused.
1462 */
1463 void command_showpets(object *op, const char *params) {
1464 objectlink *obl, *next;
1465 int counter = 0, target = 0;
1466 int have_shown_pet = 0;
1467 if (*params != '\0')
1468 target = atoi(params);
1469
1470 for (obl = first_friendly_object; obl != NULL; obl = next) {
1471 object *ob = obl->ob;
1472
1473 next = obl->next;
1474 if (object_get_owner(ob) == op) {
1475 if (target == 0) {
1476 if (counter == 0)
1477 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1478 i18n(op, "Pets:"));
1479 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1480 i18n(op, "%d %s - level %d"),
1481 ++counter, ob->name, ob->level);
1482 } else if (!have_shown_pet && ++counter == target) {
1483 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1484 i18n(op, "[fixed]level %d %s"),
1485 ob->level, ob->name);
1486 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1487 i18n(op, "[fixed]%d/%d HP, %d/%d SP"),
1488 ob->stats.hp, ob->stats.maxhp, ob->stats.sp, ob->stats.maxsp);
1489
1490 /* this is not a nice way to do this, it should be made to be more like the statistics command */
1491 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1492 i18n(op, "[fixed]Str %d"),
1493 ob->stats.Str);
1494 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1495 i18n(op, "[fixed]Dex %d"),
1496 ob->stats.Dex);
1497 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1498 i18n(op, "[fixed]Con %d"),
1499 ob->stats.Con);
1500 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1501 i18n(op, "[fixed]Int %d"),
1502 ob->stats.Int);
1503 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1504 i18n(op, "[fixed]Wis %d"),
1505 ob->stats.Wis);
1506 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1507 i18n(op, "[fixed]Cha %d"),
1508 ob->stats.Cha);
1509 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1510 i18n(op, "[fixed]Pow %d"),
1511 ob->stats.Pow);
1512 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1513 i18n(op, "[fixed]wc %d damage %d ac %d"),
1514 ob->stats.wc, ob->stats.dam, ob->stats.ac);
1515 have_shown_pet = 1;
1516 }
1517 }
1518 }
1519 if (counter == 0)
1520 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1521 i18n(op, "You have no pets."));
1522 else if (target != 0 && have_shown_pet == 0)
1523 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1524 i18n(op, "No such pet."));
1525 }
1526
1527 /**
1528 * Player wants to change how keys are used.
1529 *
1530 * @param op
1531 * player asking for information.
1532 * @param params
1533 * unused.
1534 */
1535 void command_usekeys(object *op, const char *params) {
1536 usekeytype oldtype = op->contr->usekeys;
1537 static const char *const types[] = {
1538 "inventory",
1539 "keyrings",
1540 "containers"
1541 };
1542
1543 if (*params == '\0') {
1544 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1545 i18n(op, "usekeys is set to %s"),
1546 types[op->contr->usekeys]);
1547 return;
1548 }
1549
1550 if (!strcmp(params, "inventory"))
1551 op->contr->usekeys = key_inventory;
1552 else if (!strcmp(params, "keyrings"))
1553 op->contr->usekeys = keyrings;
1554 else if (!strcmp(params, "containers"))
1555 op->contr->usekeys = containers;
1556 else {
1557 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1558 i18n(op, "usekeys: Unknown option %s, valid options are inventory, keyrings, containers"),
1559 params);
1560 return;
1561 }
1562 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
1563 i18n(op, "usekeys%s set to %s"),
1564 (oldtype == op->contr->usekeys ? "" : " now"),
1565 types[op->contr->usekeys]);
1566 }
1567
1568 /**
1569 * Players wants to know her resistances.
1570 *
1571 * @param op
1572 * player asking for information.
1573 * @param params
1574 * unused.
1575 */
1576 void command_resistances(object *op, const char *params) {
1577 int i;
1578 if (!op)
1579 return;
1580
1581 for (i = 0; i < NROFATTACKS; i++) {
1582 if (i == ATNR_INTERNAL)
1583 continue;
1584
1585 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1586 i18n(op, "[fixed]%-20s %+5d"),
1587 attacktype_desc[i], op->resist[i]);
1588 }
1589
1590 /* If dragon player, let's display natural resistances */
1591 if (is_dragon_pl(op)) {
1592 int attack;
1593 object *tmp;
1594
1595 tmp = object_find_by_type_and_arch_name(op, FORCE, "dragon_skin_force");
1596 if (tmp != NULL) {
1597 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1598 i18n(op, "\nNatural skin resistances:"));
1599
1600 for (attack = 0; attack < NROFATTACKS; attack++) {
1601 if (atnr_is_dragon_enabled(attack)) {
1602 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1603 i18n(op, "%s: %d"),
1604 change_resist_msg[attack], tmp->resist[attack]);
1605 }
1606 }
1607 }
1608 }
1609 }
1610
1611 /**
1612 * Player wants to know available help topics.
1613 *
1614 * @param op
1615 * player asking for information.
1616 * @param what
1617 * - 1: wizard topics.
1618 * - 3: misc topics.
1619 * - other: regular commands.
1620 */
1621 static void help_topics(object *op, int what) {
1622 DIR *dirp;
1623 struct dirent *de;
1624 char filename[MAX_BUF], line[HUGE_BUF];
1625 char suffix[MAX_BUF];
1626 int namelen;
1627 const char *language;
1628
1629 language = i18n_get_language_code(op->contr->language);
1630 snprintf(suffix, sizeof(suffix), ".%s", language);
1631
1632 switch (what) {
1633 case 1:
1634 snprintf(filename, sizeof(filename), "%s/wizhelp", settings.datadir);
1635 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1636 i18n(op, " Wiz commands:"));
1637 break;
1638
1639 case 3:
1640 snprintf(filename, sizeof(filename), "%s/mischelp", settings.datadir);
1641 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1642 i18n(op, " Misc help:"));
1643 break;
1644
1645 default:
1646 snprintf(filename, sizeof(filename), "%s/help", settings.datadir);
1647 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1648 i18n(op, " Commands:"));
1649 break;
1650 }
1651 if (!(dirp = opendir(filename)))
1652 return;
1653
1654 line[0] = '\0';
1655 for (de = readdir(dirp); de; de = readdir(dirp)) {
1656 namelen = NAMLEN(de);
1657
1658 if (namelen <= 2
1659 && *de->d_name == '.'
1660 && (namelen == 1 || de->d_name[1] == '.'))
1661 continue;
1662 if (strstr(de->d_name, suffix)) {
1663 strcat(line, strtok(de->d_name, "."));
1664 strcat(line, " ");
1665 }
1666 }
1667 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1668 line);
1669 closedir(dirp);
1670 }
1671
1672 /**
1673 * Helper function to display commands.
1674 *
1675 * @param op
1676 * player asking for information.
1677 * @param what
1678 * - 1: display wizard commands.
1679 * - 2: display communication commands.
1680 * - other: display regular commands.
1681 */
1682 static void show_commands(object *op, int what) {
1683 char line[HUGE_BUF];
1684 int i, size;
1685 command_array_struct *ap;
1686
1687 switch (what) {
1688 case 1:
1689 ap = WizCommands;
1690 size = WizCommandsSize;
1691 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1692 i18n(op, " Wiz commands:"));
1693 break;
1694
1695 case 2:
1696 ap = CommunicationCommands;
1697 size = CommunicationCommandSize;
1698 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1699 i18n(op, " Communication commands:"));
1700 break;
1701
1702 default:
1703 ap = Commands;
1704 size = CommandsSize;
1705 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1706 i18n(op, " Commands:"));
1707 break;
1708 }
1709
1710 line[0] = '\0';
1711 for (i = 0; i < size; i++) {
1712 strcat(line, ap[i].name);
1713 strcat(line, " ");
1714 }
1715 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, line);
1716 }
1717
1718 /**
1719 * Find an appropriate help file. Will search regular commands, and wizard ones
1720 * if asked for. Specified language is tried, as well as English.
1721 * 'path' is altered whatever the return value.
1722 * @param name command name.
1723 * @param language player language.
1724 * @param wiz if 1 the wizard-related files are searched, else no.
1725 * @param path buffer to contain the found help file.
1726 * @param length length of path.
1727 * @return 1 if a file was found in which case path contains the path, else 0.
1728 */
1729 static int find_help_file(const char *name, const char *language, int wiz, char *path, int length) {
1730 struct stat st;
1731
1732 snprintf(path, length, "%s/help/%s.%s", settings.datadir, name, language);
1733 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
1734 return 1;
1735 }
1736
1737 if (strcmp(language, "en")) {
1738 snprintf(path, length, "%s/help/%s.en", settings.datadir, name);
1739 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
1740 return 1;
1741 }
1742 }
1743
1744 if (!wiz)
1745 return 0;
1746
1747 snprintf(path, length, "%s/wizhelp/%s.%s", settings.datadir, name, language);
1748 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
1749 return 1;
1750 }
1751 if (strcmp(language, "en")) {
1752 snprintf(path, length, "%s/wizhelp/%s.en", settings.datadir, name);
1753 if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
1754 return 1;
1755 }
1756 }
1757
1758 return 0;
1759 }
1760
1761 /**
1762 * Player is asking for some help.
1763 *
1764 * @param op
1765 * player asking for information.
1766 * @param params
1767 * what kind of help to ask for.
1768 */
1769 void command_help(object *op, const char *params) {
1770 FILE *fp;
1771 char filename[MAX_BUF], line[MAX_BUF];
1772 int len;
1773 const char *language;
1774
1775 /*
1776 * Main help page?
1777 */
1778 if (*params == '\0') {
1779 snprintf(filename, sizeof(filename), "%s/def_help", settings.datadir);
1780 if ((fp = fopen(filename, "r")) == NULL) {
1781 LOG(llevError, "Cannot open help file %s: %s\n", filename, strerror(errno));
1782 return;
1783 }
1784 while (fgets(line, MAX_BUF, fp)) {
1785 line[MAX_BUF-1] = '\0';
1786 len = strlen(line)-1;
1787 if (line[len] == '\n')
1788 line[len] = '\0';
1789 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, line);
1790 }
1791 fclose(fp);
1792 return;
1793 }
1794
1795 /*
1796 * Topics list
1797 */
1798 if (!strcmp(params, "topics")) {
1799 help_topics(op, 3);
1800 help_topics(op, 0);
1801 if (QUERY_FLAG(op, FLAG_WIZ))
1802 help_topics(op, 1);
1803 return;
1804 }
1805
1806 /*
1807 * Commands list
1808 */
1809 if (!strcmp(params, "commands")) {
1810 show_commands(op, 0);
1811 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "\n");
1812 show_commands(op, 2); /* show comm commands */
1813 if (QUERY_FLAG(op, FLAG_WIZ)) {
1814 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "\n");
1815 show_commands(op, 1);
1816 }
1817 return;
1818 }
1819
1820 /*
1821 * User wants info about command
1822 */
1823 if (strchr(params, '.') || strchr(params, ' ') || strchr(params, '/')) {
1824 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1825 i18n(op, "Illegal characters in '%s'"),
1826 params);
1827 return;
1828 }
1829
1830 language = i18n_get_language_code(op->contr->language);
1831
1832 if (!find_help_file(params, language, QUERY_FLAG(op, FLAG_WIZ), filename, sizeof(filename))) {
1833 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1834 i18n(op, "No help available on '%s'"),
1835 params);
1836 return;
1837 }
1838
1839 /*
1840 * Found that. Just cat it to screen.
1841 */
1842 if ((fp = fopen(filename, "r")) == NULL) {
1843 LOG(llevError, "Cannot open help file %s: %s\n", filename, strerror(errno));
1844 return;
1845 }
1846
1847 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO,
1848 i18n(op, "Help about '%s'"),
1849 params);
1850
1851 while (fgets(line, MAX_BUF, fp)) {
1852 line[MAX_BUF-1] = '\0';
1853 len = strlen(line)-1;
1854 if (line[len] == '\n')
1855 line[len] = '\0';
1856 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, line);
1857 }
1858 fclose(fp);
1859 }
1860
1861 /**
1862 * Utility function to convert a reply to a yes/no or on/off value.
1863 *
1864 * @param line
1865 * string to check.
1866 * @retval 1
1867 * line is one of on y k s d.
1868 * @retval 0
1869 * other value.
1870 */
1871 int onoff_value(const char *line) {
1872 int i;
1873
1874 if (sscanf(line, "%d", &i))
1875 return (i != 0);
1876
1877 switch (line[0]) {
1878 case 'o':
1879 switch (line[1]) {
1880 case 'n':
1881 return 1; /* on */
1882 default:
1883 return 0; /* o[ff] */
1884 }
1885
1886 case 'y': /* y[es] */
1887 case 'k': /* k[ylla] */
1888 case 's':
1889 case 'd':
1890 return 1;
1891
1892 case 'n': /* n[o] */
1893 case 'e': /* e[i] */
1894 case 'u':
1895 default:
1896 return 0;
1897 }
1898 }
1899
1900 /**
1901 * Tell players to use the 'delete' command. Too many new players have
1902 * accidentally deleted their characters because they didn't carefully read
1903 * the warning that the original 'quit' command gave.
1904 */
1905 void command_quit(object* op, const char* params) {
1906 draw_ext_info(
1907 NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1908 i18n(op,
1909 "To leave the game, sleep in (apply) a bed to reality. To "
1910 "permenantly delete your character, use the 'delete' command."));
1911 }
1912
1913 /**
1914 * Player wants to totally delete her character.
1915 *
1916 * @param op
1917 * player wanting to delete her character.
1918 * @param params
1919 * unused.
1920 */
1921 void command_delete(object *op, const char *params) {
1922 if (QUERY_FLAG(op, FLAG_WAS_WIZ)) {
1923 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_ADMIN_DM, "Can't quit when in DM mode.");
1924 return;
1925 }
1926
1927 send_query(&op->contr->socket, CS_QUERY_SINGLECHAR,
1928 i18n(op, "Quitting will delete your character.\nAre you sure you want to delete your character (y/n):"));
1929
1930 player_set_state(op->contr, ST_CONFIRM_QUIT);
1931 }
1932
1933 /**
1934 * Player wants to change sound status.
1935 *
1936 * @param op
1937 * player asking for change.
1938 * @param params
1939 * unused.
1940 */
1941 void command_sound(object *op, const char *params) {
1942 if (!(op->contr->socket.sound&SND_MUTE)) {
1943 op->contr->socket.sound = op->contr->socket.sound|SND_MUTE;
1944 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1945 i18n(op, "Sounds are turned off."));
1946 } else {
1947 op->contr->socket.sound = op->contr->socket.sound&~SND_MUTE;
1948 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1949 i18n(op, "The sounds are enabled."));
1950 }
1951 return;
1952 }
1953
1954 /**
1955 * A player just entered her name.
1956 *
1957 * Perhaps these should be in player.c, but that file is
1958 * already a bit big.
1959 *
1960 * @param op
1961 * player we're getting the name of.
1962 */
1963 void receive_player_name(object *op) {
1964 if (!check_name(op->contr, op->contr->write_buf+1)) {
1965 get_name(op);
1966 return;
1967 }
1968 FREE_AND_COPY(op->name, op->contr->write_buf+1);
1969 FREE_AND_COPY(op->name_pl, op->contr->write_buf+1);
1970 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE, op->contr->write_buf);
1971 op->contr->name_changed = 1;
1972 get_password(op);
1973 }
1974
1975 /**
1976 * A player just entered her password, including for changing it.
1977 *
1978 * @param op
1979 * player.
1980 */
1981 void receive_player_password(object *op) {
1982 unsigned int pwd_len = strlen(op->contr->write_buf);
1983
1984 if (pwd_len <= 1 || pwd_len > 17) {
1985 if (op->contr->state == ST_CHANGE_PASSWORD_OLD
1986 || op->contr->state == ST_CHANGE_PASSWORD_NEW
1987 || op->contr->state == ST_CHANGE_PASSWORD_CONFIRM) {
1988 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
1989 i18n(op, "Password changed cancelled."));
1990 player_set_state(op->contr, ST_PLAYING);
1991 } else
1992 get_name(op);
1993 return;
1994 }
1995 /* To hide the password better */
1996 /* With currently clients, not sure if this is really the case - MSW */
1997 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE, " ");
1998
1999 if (checkbanned(op->name, op->contr->socket.host)) {
2000 LOG(llevInfo, "Banned player tried to add: [%s@%s]\n", op->name, op->contr->socket.host);
2001 draw_ext_info(NDI_UNIQUE|NDI_RED, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2002 i18n(op, "You are not allowed to play."));
2003 get_name(op);
2004 return;
2005 }
2006
2007 if (op->contr->state == ST_CONFIRM_PASSWORD) {
2008 if (!check_password(op->contr->write_buf+1, op->contr->password)) {
2009 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2010 i18n(op, "The passwords did not match."));
2011 get_name(op);
2012 return;
2013 }
2014 LOG(llevInfo, "LOGIN: New player named %s from ip %s\n", op->name, op->contr->socket.host);
2015 display_motd(op);
2016 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2017 i18n(op, "\nWelcome, Brave New Warrior!\n"));
2018 roll_again(op);
2019 player_set_state(op->contr, ST_ROLL_STAT);
2020 return;
2021 }
2022
2023 if (op->contr->state == ST_CHANGE_PASSWORD_OLD) {
2024 if (!check_password(op->contr->write_buf+1, op->contr->password)) {
2025 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2026 i18n(op, "You entered the wrong current password."));
2027 player_set_state(op->contr, ST_PLAYING);
2028 } else {
2029 send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, i18n(op, "Please enter your new password, or blank to cancel:"));
2030 player_set_state(op->contr, ST_CHANGE_PASSWORD_NEW);
2031 }
2032 return;
2033 }
2034
2035 if (op->contr->state == ST_CHANGE_PASSWORD_NEW) {
2036 safe_strncpy(op->contr->new_password, newhash(op->contr->write_buf+1),
2037 sizeof(op->contr->new_password));
2038 send_query(&op->contr->socket, CS_QUERY_HIDEINPUT,
2039 i18n(op, "Please confirm your new password, or blank to cancel:"));
2040 player_set_state(op->contr, ST_CHANGE_PASSWORD_CONFIRM);
2041 return;
2042 }
2043
2044 if (op->contr->state == ST_CHANGE_PASSWORD_CONFIRM) {
2045 if (!check_password(op->contr->write_buf+1, op->contr->new_password)) {
2046 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2047 i18n(op, "The new passwords don't match!"));
2048 } else {
2049 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2050 i18n(op, "Password changed."));
2051 strncpy(op->contr->password, op->contr->new_password, 13);
2052 }
2053 player_set_state(op->contr, ST_PLAYING);
2054 return;
2055 }
2056
2057 safe_strncpy(op->contr->password, newhash(op->contr->write_buf+1),
2058 sizeof(op->contr->password));
2059 player_set_state(op->contr, ST_ROLL_STAT);
2060 check_login(op, TRUE);
2061 }
2062
2063 /**
2064 * Player wishes to change her title.
2065 *
2066 * @param op
2067 * player asking for change.
2068 * @param params
2069 * new title.
2070 * @return
2071 * 1.
2072 */
2073 void command_title(object *op, const char *params) {
2074 char buf[MAX_BUF];
2075
2076 if (settings.set_title == FALSE) {
2077 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2078 i18n(op, "You cannot change your title."));
2079 return;
2080 }
2081
2082 /* dragon players cannot change titles */
2083 if (is_dragon_pl(op)) {
2084 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2085 i18n(op, "Dragons cannot change titles."));
2086 return;
2087 }
2088
2089 if (*params == '\0') {
2090 char tmp[MAX_BUF];
2091
2092 player_get_title(op->contr, tmp, sizeof(tmp));
2093 snprintf(buf, sizeof(buf), i18n(op, "Your title is '%s'."), tmp);
2094 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG, buf);
2095 return;
2096 }
2097 if (strcmp(params, "clear") == 0 || strcmp(params, "default") == 0) {
2098 if (!player_has_own_title(op->contr))
2099 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2100 i18n(op, "Your title is the default title."));
2101 else
2102 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2103 i18n(op, "Title set to default."));
2104 player_set_own_title(op->contr, "");
2105 return;
2106 }
2107
2108 if ((int)strlen(params) >= MAX_NAME) {
2109 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2110 i18n(op, "Title too long."));
2111 return;
2112 }
2113 player_set_own_title(op->contr, params);
2114 }
2115
2116 /**
2117 * Player wants to get saved.
2118 *
2119 * @param op
2120 * player.
2121 * @param params
2122 * unused.
2123 */
2124 void command_save(object *op, const char *params) {
2125 if (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL)&P_NO_CLERIC) {
2126 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2127 i18n(op, "You can not save on unholy ground."));
2128 } else if (!op->stats.exp) {
2129 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2130 i18n(op, "You don't deserve to save yet."));
2131 } else {
2132 if (save_player(op, 1))
2133 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2134 i18n(op, "You have been saved."));
2135 else
2136 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2137 i18n(op, "SAVE FAILED!"));
2138 }
2139 }
2140
2141 /**
2142 * Player toggles her peaceful status.
2143 *
2144 * @param op
2145 * player.
2146 * @param params
2147 * unused.
2148 */
2149 void command_peaceful(object *op, const char *params) {
2150 if ((op->contr->peaceful = !op->contr->peaceful))
2151 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2152 i18n(op, "You will not attack other players."));
2153 else
2154 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2155 i18n(op, "You will attack other players."));
2156 }
2157
2158 /**
2159 * Player wants to change how soon she'll flee.
2160 *
2161 * @param op
2162 * player.
2163 * @param params
2164 * new value.
2165 */
2166 void command_wimpy(object *op, const char *params) {
2167 int i;
2168
2169 if (*params == '\0' || !sscanf(params, "%d", &i)) {
2170 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2171 i18n(op, "Your current wimpy level is %d."),
2172 op->run_away);
2173 return;
2174 }
2175
2176 if (i < 0 || i > 100) {
2177 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2178 i18n(op, "Wimpy level should be between 1 and 100."),
2179 i);
2180 return;
2181 }
2182
2183 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_CONFIG,
2184 i18n(op, "Your new wimpy level is %d."),
2185 i);
2186 op->run_away = i;
2187 }
2188
2189 /**
2190 * Player toggles her braced status.
2191 *
2192 * @param op
2193 * player.
2194 * @param params
2195 * brace status (on/off).
2196 */
2197 void command_brace(object *op, const char *params) {
2198 if (*params == '\0')
2199 op->contr->braced = !op->contr->braced;
2200 else
2201 op->contr->braced = onoff_value(params);
2202
2203 if (op->contr->braced)
2204 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2205 i18n(op, "You are braced."));
2206 else
2207 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2208 i18n(op, "Not braced."));
2209
2210 fix_object(op);
2211 }
2212
2213 /**
2214 * Player wants to get rid of pets.
2215 *
2216 * @param op
2217 * player.
2218 * @param params
2219 * unused.
2220 */
2221 void command_kill_pets(object *op, const char *params) {
2222 objectlink *obl, *next;
2223 int counter = 0, removecount = 0;
2224
2225 if (*params == '\0') {
2226 pets_terminate_all(op);
2227 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2228 i18n(op, "Your pets have been killed."));
2229 } else {
2230 int target = atoi(params);
2231 for (obl = first_friendly_object; obl != NULL; obl = next) {
2232 object *ob = obl->ob;
2233 next = obl->next;
2234 if (object_get_owner(ob) == op)
2235 if (++counter == target || (target == 0 && !strcasecmp(ob->name, params))) {
2236 if (!QUERY_FLAG(ob, FLAG_REMOVED))
2237 object_remove(ob);
2238 remove_friendly_object(ob);
2239 object_free_drop_inventory(ob);
2240 removecount++;
2241 }
2242 }
2243 if (removecount != 0)
2244 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2245 i18n(op, "Killed %d pets."),
2246 removecount);
2247 else
2248 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2249 i18n(op, "Couldn't find any suitable pets to kill."));
2250 }
2251 }
2252
2253 /**
2254 * Player is asking to change password.
2255 *
2256 * @param pl
2257 * player.
2258 * @param params
2259 * unused.
2260 */
2261 void command_passwd(object *pl, const char *params) {
2262 /* If old client, this is the way you change your password. */
2263 if (pl->contr->socket.login_method < 1){
2264 send_query(&pl->contr->socket, CS_QUERY_HIDEINPUT, i18n(pl, "Password change.\nPlease enter your current password, or empty string to cancel."));
2265
2266 player_set_state(pl->contr, ST_CHANGE_PASSWORD_OLD);
2267 }
2268 /* If new client (login_method = 2) or jxclient (login_method = 1), changing the password does nothing anyway, so error out */
2269 else{
2270 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
2271 i18n(pl, "passwd is maintained for older clients that do not support the account system. Please use the 'Password' button in your character selection screen to change your password."));
2272 }
2273 }
2274
2275 /**
2276 * Player is trying to harvest something.
2277 * @param pl
2278 * player trying to harvest.
2279 * @param dir
2280 * direction.
2281 * @param skill
2282 * skill being used.
2283 */
2284 void do_harvest(object *pl, int dir, object *skill) {
2285 int16_t x, y;
2286 int count = 0, proba; /* Probability to get the item, 100 based. */
2287 int level, exp, check_exhaust = 0;
2288 object *found[10]; /* Found items that can be harvested. */
2289 mapstruct *map;
2290 object *item, *inv, *harvested;
2291 sstring trace, ttool, tspeed, race, tool, slevel, sexp;
2292 float speed;
2293
2294 x = pl->x+freearr_x[dir];
2295 y = pl->y+freearr_y[dir];
2296 map = pl->map;
2297
2298 if (!IS_PLAYER(pl))
2299 return;
2300
2301 if (!map)
2302 return;
2303
2304 if (get_map_flags(map, &map, x, y, &x, &y)&P_OUT_OF_MAP) {
2305 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You can't %s anything here.", skill->slaying);
2306 return;
2307 }
2308
2309 if (!pl->chosen_skill || pl->chosen_skill->skill != skill->skill)
2310 return;
2311
2312 trace = object_get_value(pl->chosen_skill, "harvest_race");
2313 ttool = object_get_value(pl->chosen_skill, "harvest_tool");
2314 tspeed = object_get_value(pl->chosen_skill, "harvest_speed");
2315 if (!trace || strcmp(trace, "") == 0 || !ttool || strcmp(ttool, "") == 0 || !tspeed || strcmp(tspeed, "") == 0) {
2316 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You can't %s anything here.", skill->slaying);
2317 LOG(llevError, "do_harvest: tool %s without harvest_[race|tool|speed]\n", pl->chosen_skill->name);
2318 return;
2319 }
2320
2321 item = GET_MAP_OB(map, x, y);
2322 while (item && count < 10) {
2323 FOR_INV_PREPARE(item, inv) {
2324 if (object_get_value(inv, "harvestable") == NULL)
2325 continue;
2326 race = object_get_value(inv, "harvest_race");
2327 tool = object_get_value(inv, "harvest_tool");
2328 slevel = object_get_value(inv, "harvest_level");
2329 sexp = object_get_value(inv, "harvest_exp");
2330 if (race && (!slevel || !sexp)) {
2331 LOG(llevError, "do_harvest: item %s without harvest_[level|exp]\n", inv->name);
2332 continue;
2333 }
2334 if (race == trace && (!tool || tool == ttool))
2335 found[count++] = inv;
2336 } FOR_INV_FINISH();
2337 item = item->above;
2338 }
2339 if (count == 0) {
2340 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You can't %s anything here.", skill->slaying);
2341 return;
2342 }
2343
2344 inv = found[rndm(0, count-1)];
2345 assert(inv);
2346 item = inv->env;
2347 assert(item);
2348
2349 slevel = object_get_value(inv, "harvest_level");
2350 sexp = object_get_value(inv, "harvest_exp");
2351 level = atoi(slevel);
2352 exp = atoi(sexp);
2353
2354 speed = atof(tspeed);
2355 if (speed < 0)
2356 speed = -speed*pl->speed;
2357 pl->speed_left -= speed;
2358
2359
2360 /* Now we found something to harvest, randomly try to get it. */
2361 if (level > skill->level+10) {
2362 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You fail to %s anything.", skill->slaying);
2363 return;
2364 }
2365
2366 if (level >= skill->level)
2367 /* Up to 10 more levels, 1 to 11 percent probability. */
2368 proba = 10+skill->level-level;
2369 else if (skill->level <= level+10)
2370 proba = 10+(skill->level-level)*2;
2371 else
2372 proba = 30;
2373
2374 if (proba <= random_roll(0, 100, pl, 1)) {
2375 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You fail to %s anything.", skill->slaying);
2376 return;
2377 }
2378
2379 /* Check the new item can fit into inventory.
2380 * Fixes bug #3060474: fishing puts more fishes into inventory than you can carry. */
2381 if (((uint32_t)(pl->weight + pl->carrying + inv->weight)) > get_weight_limit(pl->stats.Str)) {
2382 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You are carrying too much to %s a %s.", skill->slaying, inv->name);
2383 return;
2384 }
2385
2386 /* Ok, got it. */
2387 if (inv->nrof == 0) {
2388 harvested = object_new();
2389 object_copy_with_inv(inv, harvested);
2390 } else {
2391 if (count == 1 && inv->nrof == 1) {
2392 check_exhaust = 1;
2393 }
2394 harvested = object_split(inv, 1, NULL, 0);
2395 }
2396 object_set_value(harvested, "harvestable", NULL, 0);
2397 if (QUERY_FLAG(harvested, FLAG_MONSTER)) {
2398 int spot = object_find_free_spot(harvested, pl->map, pl->x, pl->y, 0, SIZEOFFREE);
2399 if (spot == -1) {
2400 /* Better luck next time...*/
2401 object_remove(harvested);
2402 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You fail to %s anything.", skill->slaying);
2403 return;
2404 }
2405 object_insert_in_map_at(harvested, pl->map, NULL, 0, pl->x+freearr_x[spot], pl->y+freearr_y[spot]);
2406 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You %s a %s!", skill->slaying, harvested->name);
2407 } else {
2408 harvested = object_insert_in_ob(harvested, pl);
2409 draw_ext_info_format(NDI_WHITE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE, "You %s some %s", skill->slaying, harvested->name);
2410 }
2411
2412 /* Get exp */
2413 change_exp(pl, exp, skill->name, SK_EXP_ADD_SKILL);
2414
2415 if (check_exhaust) {
2416 sstring replacement = object_get_value(item, "harvest_exhaust_replacement");
2417 if (replacement) {
2418 if (replacement[0] != '-') {
2419 archetype *other = find_archetype(replacement);
2420 if (other) {
2421 object *final = object_create_arch(other);
2422 object_insert_in_map_at(final, map, item, INS_BELOW_ORIGINATOR, item->x, item->y);
2423 }
2424 }
2425 object_remove(item);
2426 object_free(item, FREE_OBJ_FREE_INVENTORY);
2427 }
2428 }
2429
2430 return;
2431 }