Debian Codegraph @

socket/info.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 * Basic client output functions.
17 *
18 * \date 2003-12-02
19 *
20 * This file implements some of the simpler output functions to the
21 * client. Basically, things like sending text strings along
22 */
23
24 #include "global.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28
29 #include "skills.h"
30 #include "spells.h"
31 #include "sproto.h"
32
33 /**
34 * Draws an extended message on the client.
35 *
36 * @param ns The socket to send message to
37 *
38 * @param color Color informations (used mainly if client does not support message type)
39 *
40 * @param type The type of text message
41 *
42 * @param subtype The subtype of text message
43 *
44 * @param message The main message
45 *
46 * make this non-static, so other files can use this to send messages to client before
47 * player has been added.
48 */
49 void print_ext_msg(socket_struct *ns, int color, uint8_t type, uint8_t subtype, const char *message) {
50 SockList sl;
51
52 SockList_Init(&sl);
53 SockList_AddPrintf(&sl, "drawextinfo %d %hhu %hhu %s", color, type, subtype, message);
54 Send_With_Handling(ns, &sl);
55 SockList_Term(&sl);
56 }
57
58 /**
59 * Sends message to player(s).
60 *
61 * @param flags Various flags - mostly color, plus a few specials. If NDI_ALL
62 * or NDI_ALL_DMS is set, pl is ignored and the message is resent to all
63 * players or all DMs individually.
64 *
65 * @param pri Priority. The lower the value, the more important it is. Thus,
66 * 0 gets sent no matter what. Otherwise, the value must be less than the
67 * listening level that the player has set. Unfortunately, there is no clear
68 * guideline on what each level does what.
69 *
70 * @param pl Who to send the message to. pl may be NULL, particularly if
71 * flags has NDI_ALL or NDI_DMS set since it is ignored in that case. If
72 * NDI_ALL and NDI_DMS are not set, when pl is NULL, nobody gets the message.
73 * This is sometimes useful when a function that can be invoked by a player
74 * is automatically called by some other mechanism. For example, if a player
75 * tries to put something in a container, they should get a message that tells
76 * them they cannot put it in an unsuitable container, but when auto-pickup is
77 * at work, all containers are tried, even unsuitable ones, and so messages
78 * should not be sent to the player.
79 *
80 * @param type The type MSG_TYPE for the type of message.
81 *
82 * @param subtype The subtype of the message.
83 *
84 * @param message The message to send.
85 */
86 void draw_ext_info(
87 int flags, int pri, const object *pl, uint8_t type,
88 uint8_t subtype, const char *message) {
89
90 if ((flags&NDI_ALL) || (flags&NDI_ALL_DMS)) {
91 player *tmppl;
92
93 for (tmppl = first_player; tmppl != NULL; tmppl = tmppl->next) {
94 if ((flags&NDI_ALL_DMS) && !QUERY_FLAG(tmppl->ob, FLAG_WIZ))
95 continue;
96 draw_ext_info((flags&~NDI_ALL&~NDI_ALL_DMS), pri, tmppl->ob, type, subtype, message);
97 }
98
99 LOG(llevInfo, "-- %s\n", message);
100 return;
101 }
102
103 if (!pl || (pl->type == PLAYER && pl->contr == NULL))
104 return;
105 if (pl->type != PLAYER)
106 return;
107 if (pri >= pl->contr->listening)
108 return;
109
110 print_ext_msg(&pl->contr->socket, flags&NDI_COLOR_MASK, type, subtype, message);
111 }
112
113 /**
114 * Sends message to player(s).
115 *
116 * This function is the same as draw_ext_info, but takes varargs format.
117 * Otherwise, the meaning of all the fields is the same. This is perhaps not
118 * the most efficient as we do vsnprintf on both the old and newbuf, but it
119 * simplifies the code greatly since we can just call draw_ext_info.
120 *
121 * @param flags Various flags - mostly color, plus a few specials.
122 *
123 * @param pri Priority. It is a little odd - the lower the value, the more
124 * important it is. Thus, 0 gets sent no matter what. Otherwise, the
125 * value must be less than the listening level that the player has set.
126 * Unfortunately, there is no clear guideline on what each level does what.
127 *
128 * @param pl Can be passed as NULL - in fact, this will be done if NDI_ALL is
129 * set in the flags.
130 *
131 * @param type The type MSG_TYPE for the type of message.
132 *
133 * @param subtype The type MSG_TYPE for the subtype of message.
134 *
135 * @param format The message to send, with optional format specifiers.
136 */
137 void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format, ...) {
138 char buf[HUGE_BUF];
139 va_list ap;
140
141 va_start(ap, format);
142 vsnprintf(buf, HUGE_BUF, format, ap);
143 va_end(ap);
144
145 draw_ext_info(flags, pri, pl, type, subtype, buf);
146 }
147
148 /**
149 * Writes to everyone on the specified map
150 *
151 * @param color The color of the message
152 *
153 * @param map The map where the players receiving the message are located
154 *
155 * @param type The message type
156 *
157 * @param subtype The message subtype
158 *
159 * @param str1 The message to be sent
160 */
161 void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1) {
162 player *pl;
163
164 for (pl = first_player; pl != NULL; pl = pl->next)
165 if (pl->ob != NULL && pl->ob->map == map) {
166 draw_ext_info(color, 0, pl->ob, type, subtype, str1);
167 }
168 }
169
170 /**
171 * Writes to everyone on the map except *op. This is useful for emotions.
172 *
173 * @param color The color of the message
174 *
175 * @param map The map where the players receiving the message are located
176 *
177 * @param op The player object to skip sending the message to on the map
178 *
179 * @param type The message type
180 *
181 * @param subtype The message subtype
182 *
183 * @param str1 The message to be sent
184 */
185 void ext_info_map_except(int color, const mapstruct *map, const object *op, uint8_t type, uint8_t subtype, const char *str1) {
186 player *pl;
187
188 for (pl = first_player; pl != NULL; pl = pl->next)
189 if (pl->ob != NULL && pl->ob->map == map && pl->ob != op) {
190 draw_ext_info(color, 0, pl->ob, type, subtype, str1);
191 }
192 }
193
194 /**
195 * Writes to everyone on the map except op1 and op2
196 *
197 * @param color The message color
198 *
199 * @param map The map where the players receiving the message are located
200 *
201 * @param op1 The first player object to skip sending the message to
202 *
203 * @param op2 The second player object to skip sending the message to
204 *
205 * @param type The message type
206 *
207 * @param subtype The message subtype
208 *
209 * @param str1 The message to be sent
210 */
211 void ext_info_map_except2(int color, const mapstruct *map, const object *op1, const object *op2, int type, int subtype, const char *str1) {
212 player *pl;
213
214 for (pl = first_player; pl != NULL; pl = pl->next)
215 if (pl->ob != NULL && pl->ob->map == map
216 && pl->ob != op1 && pl->ob != op2) {
217 draw_ext_info(color, 0, pl->ob, type, subtype, str1);
218 }
219 }
220
221 /**
222 * Get player's current range attack in obuf.
223 *
224 * @param pl The player who is being evaluated
225 *
226 * @param obuf The buffer for the range attack string
227 *
228 * @param len The length of the buffer
229 */
230 void rangetostring(const object *pl, char *obuf, size_t len) {
231 char name[MAX_BUF];
232
233 switch (pl->contr->shoottype) {
234 case range_none:
235 strncpy(obuf, "Range: nothing", len);
236 break;
237
238 case range_bow: {
239 object *op;
240
241 for (op = pl->inv; op; op = op->below)
242 if (op->type == BOW && QUERY_FLAG(op, FLAG_APPLIED))
243 break;
244 if (op == NULL)
245 break;
246
247 query_base_name(op, 0, name, MAX_BUF);
248 snprintf(obuf, len, "Range: %s (%s)", name, op->race ? op->race : "nothing");
249 }
250 break;
251
252 case range_magic:
253 if (settings.casting_time == TRUE && pl->casting_time > -1) {
254 if (pl->casting_time == 0)
255 snprintf(obuf, len, "Range: Holding spell (%s)", pl->spell->name);
256 else
257 snprintf(obuf, len, "Range: Casting spell (%s)", pl->spell->name);
258 }
259 else
260 snprintf(obuf, len, "Range: spell (%s)", pl->contr->ranges[range_magic]->name);
261 break;
262
263 case range_misc:
264 if (pl->contr->ranges[range_misc])
265 query_base_name(pl->contr->ranges[range_misc], 0, name, MAX_BUF);
266 else
267 strncpy(name, "none", MAX_BUF);
268 snprintf(obuf, len, "Range: %s", name);
269 break;
270
271 /* range_scroll is only used for controlling golems. If the
272 * the player does not have a golem, reset some things.
273 */
274 case range_golem:
275 if (pl->contr->ranges[range_golem] != NULL)
276 snprintf(obuf, len, "Range: golem (%s)", pl->contr->ranges[range_golem]->name);
277 else {
278 pl->contr->shoottype = range_none;
279 strncpy(obuf, "Range: nothing", len);
280 }
281 break;
282
283 case range_skill:
284 snprintf(obuf, len, "Skill: %s", pl->chosen_skill != NULL ? pl->chosen_skill->name : "none");
285 break;
286
287 case range_builder:
288 query_base_name(pl->contr->ranges[range_builder], 0, name, MAX_BUF);
289 snprintf(obuf, len, "Builder: %s", name);
290 break;
291
292 default:
293 strncpy(obuf, "Range: illegal", len);
294 }
295 }
296
297 /**
298 * Sets player title.
299 */
300 void set_title(const object *pl, char *buf, size_t len) {
301 char *p;
302
303 snprintf(buf, len, "Player: %s ", pl->name);
304 p = strchr(buf, '\0');
305 player_get_title(pl->contr, p, (buf+len)-p);
306 }
307
308 /**
309 * Helper for magic map creation.
310 *
311 * Takes a player, the map_mark array and an x and y starting position.
312 *
313 * @param pl The player creating the magic map.
314 *
315 * @param map_mark Holds flags for each tile analyzed
316 *
317 * @param px The x offset from the player to start calculations
318 *
319 * @param py The y offset from the player to start calculations
320 *
321 * This function examines all the adjacant spaces next to px, py.
322 * It updates the map_mark arrow with the color and high bits set
323 * for various code values.
324 */
325 static void magic_mapping_mark_recursive(object *pl, char *map_mark, int px, int py) {
326 int x, y, dx, dy, mflags, l;
327 int16_t nx, ny;
328 mapstruct *mp;
329 const Face *f;
330 object *ob;
331
332 for (dx = -1; dx <= 1; dx++) {
333 for (dy = -1; dy <= 1; dy++) {
334 x = px+dx;
335 y = py+dy;
336
337 if (FABS(x) >= MAGIC_MAP_HALF || FABS(y) >= MAGIC_MAP_HALF)
338 continue;
339
340 mp = pl->map;
341 nx = pl->x+x;
342 ny = pl->y+y;
343
344 mflags = get_map_flags(pl->map, &mp, nx, ny, &nx, &ny);
345 if (mflags&P_OUT_OF_MAP)
346 continue;
347
348 if (map_mark[MAGIC_MAP_HALF+x+MAGIC_MAP_SIZE*(MAGIC_MAP_HALF+y)] == 0) {
349 for (l = 0; l < MAP_LAYERS; l++) {
350 ob = GET_MAP_FACE_OBJ(mp, nx, ny, l);
351 if (ob && !ob->invisible && ob->face != blank_face)
352 break;
353 }
354 if (ob)
355 f = ob->face;
356 else
357 f = blank_face;
358
359 /* Should probably have P_NO_MAGIC here also, but then shops don't
360 * work.
361 */
362 if (mflags&P_BLOCKSVIEW)
363 map_mark[MAGIC_MAP_HALF+x+MAGIC_MAP_SIZE*(MAGIC_MAP_HALF+y)] = FACE_WALL|(f ? f->magicmap : 0);
364 else {
365 map_mark[MAGIC_MAP_HALF+x+MAGIC_MAP_SIZE*(MAGIC_MAP_HALF+y)] = FACE_FLOOR|(f ? f->magicmap : 0);
366 magic_mapping_mark_recursive(pl, map_mark, x, y);
367 }
368 }
369 }
370 }
371 }
372
373 /**
374 * Creates magic map for player.
375 *
376 * @param pl The player making the magic map
377 *
378 * @param map_mark Holds flags for each tile analyzed. Should have been
379 * initialized to zero before this function is called.
380 *
381 * @param strength An initial strength*2 rectangular area that we
382 * automatically see in/penetrate through.
383 *
384 * Note: For improved magic mapping display, the space that blocks
385 * the view is now marked with value 2. Any dependencies of map_mark
386 * being nonzero have been changed to check for 1. Also, since
387 * map_mark is a char value, putting 2 in should cause no problems.
388 *
389 * This function examines the map the player is on, and determines what
390 * is visible. 2 is set for walls or objects that blocks view. 1
391 * is for open spaces.
392 */
393 void magic_mapping_mark(object *pl, char *map_mark, int strength) {
394 int x, y, mflags, l;
395 int16_t nx, ny;
396 mapstruct *mp;
397 const Face *f;
398 object *ob;
399
400 for (x = -strength; x < strength; x++) {
401 for (y = -strength; y < strength; y++) {
402 mp = pl->map;
403 nx = pl->x+x;
404 ny = pl->y+y;
405 mflags = get_map_flags(pl->map, &mp, nx, ny, &nx, &ny);
406 if (mflags&P_OUT_OF_MAP)
407 continue;
408 else {
409 for (l = 0; l < MAP_LAYERS; l++) {
410 ob = GET_MAP_FACE_OBJ(mp, nx, ny, l);
411 if (ob && !ob->invisible && ob->face != blank_face)
412 break;
413 }
414 if (ob)
415 f = ob->face;
416 else
417 f = blank_face;
418 }
419
420 if (mflags&P_BLOCKSVIEW)
421 map_mark[MAGIC_MAP_HALF+x+MAGIC_MAP_SIZE*(MAGIC_MAP_HALF+y)] = FACE_WALL|(f ? f->magicmap : 0);
422 else {
423 map_mark[MAGIC_MAP_HALF+x+MAGIC_MAP_SIZE*(MAGIC_MAP_HALF+y)] = FACE_FLOOR|(f ? f->magicmap : 0);
424 magic_mapping_mark_recursive(pl, map_mark, x, y);
425 }
426 }
427 }
428 }
429
430 /**
431 * Creates and sends magic map to player.
432 *
433 * @ param pl The player to send the magic map to
434 *
435 * The following function is a lot messier than it really should be,
436 * but there is no real easy solution.
437 *
438 * Mark Wedel
439 */
440 void draw_magic_map(object *pl) {
441 int x, y;
442 char map_mark[MAGIC_MAP_SIZE*MAGIC_MAP_SIZE];
443 int xmin, xmax, ymin, ymax;
444 SockList sl;
445
446 if (pl->type != PLAYER) {
447 LOG(llevError, "Non player object called draw_map.\n");
448 return;
449 }
450
451 /* First, we figure out what spaces are 'reachable' by the player */
452 memset(map_mark, 0, MAGIC_MAP_SIZE*MAGIC_MAP_SIZE);
453 magic_mapping_mark(pl, map_mark, 3);
454
455 /* We now go through and figure out what spaces have been
456 * marked, and thus figure out rectangular region we send
457 * to the client (eg, if only a 10x10 area is visible, we only
458 * want to send those 100 spaces.)
459 */
460 xmin = MAGIC_MAP_SIZE;
461 ymin = MAGIC_MAP_SIZE;
462 xmax = 0;
463 ymax = 0;
464 for (x = 0; x < MAGIC_MAP_SIZE; x++) {
465 for (y = 0; y < MAGIC_MAP_SIZE; y++) {
466 if (map_mark[x+MAGIC_MAP_SIZE*y]&~FACE_FLOOR) {
467 xmin = MIN(x, xmin);
468 xmax = MAX(x, xmax);
469 ymin = MIN(y, ymin);
470 ymax = MAX(y, ymax);
471 }
472 }
473 }
474
475 SockList_Init(&sl);
476 SockList_AddPrintf(&sl, "magicmap %d %d %d %d ", xmin <= xmax ? xmax-xmin+1 : 0, ymin <= ymax ? ymax-ymin+1 : 0, MAGIC_MAP_HALF-xmin, MAGIC_MAP_HALF-ymin);
477
478 for (y = ymin; y <= ymax; y++) {
479 for (x = xmin; x <= xmax; x++) {
480 SockList_AddChar(&sl, map_mark[x+MAGIC_MAP_SIZE*y]&~FACE_FLOOR);
481 } /* x loop */
482 } /* y loop */
483
484 Send_With_Handling(&pl->contr->socket, &sl);
485 SockList_Term(&sl);
486 }