ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.C
Revision: 1.44
Committed: Tue Jun 5 13:05:03 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.43: +0 -12 lines
Log Message:
- improve observe
- remove more cruft code
- archetype loading almost works again, reloading probbaly still borked.

File Contents

# Content
1 /*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */
24
25 /**
26 * \file
27 * Basic client output functions.
28 *
29 * \date 2003-12-02
30 *
31 * This file implements some of the simpler output functions to the
32 * client. Basically, things like sending text strings along
33 */
34
35 #include <global.h>
36 #include <sproto.h>
37 #include <stdarg.h>
38 #include <spells.h>
39 #include <skills.h>
40
41 /**
42 * Draws a normal message on the client. It is pretty
43 * much the same thing as the draw_info above, but takes a color
44 * parameter. the esrv_drawinfo functions should probably be
45 * replaced with this, just using black as the color.
46 */
47 static void
48 esrv_print_msg (client *ns, int color, const char *str)
49 {
50 ns->send_packet_printf ("drawinfo %d %s", color, str);
51 }
52
53 /**
54 * Draws an extended message on the client.
55 * ns the socket to send message to
56 * color color informations (used mainly if client does not support message type)
57 * type,
58 * subtype type and subtype of text message
59 * intro Intro message to send with main message if client does not support the message type
60 * message The main message
61 */
62 static void
63 esrv_print_ext_msg (client *ns, int color, uint8 type, uint8 subtype, const char *message)
64 {
65 ns->send_packet_printf ("drawextinfo %d %hhu %hhu %s", color, type, subtype, message);
66 }
67
68 /**
69 * Frontend for esrv_print_msg
70 * \param colr message color
71 * \param pl player to send to. Can be NULL
72 * \param tmp message to send. Can be NULL
73 *
74 * If pl is NULL or without contr set, writes message to log.
75 *
76 * Else sends message to player via esrv_print_msg
77 */
78 static void
79 print_message (int colr, const object *pl, const char *tmp)
80 {
81 if (!tmp)
82 tmp = "[NULL]";
83
84 if (!pl || (pl->type == PLAYER && pl->contr == NULL))
85 return;
86
87 if (pl->type == PLAYER)
88 esrv_print_msg (pl->contr->ns, colr, (char *) tmp);
89 }
90
91 bool
92 client::msg_suppressed (const char *msg)
93 {
94 if (!pl)
95 return false;
96
97 if (pl->outputs_count <= 1 || !pl->outputs_sync)
98 return false;
99
100 int len = strlen (msg);
101
102 if (len > MSG_BUF_SIZE)
103 return false;
104
105 msg_buf *lru = msgbuf;
106 for (msg_buf *buf = msgbuf; buf < msgbuf + MSG_BUF_COUNT; ++buf)
107 {
108 if (len == buf->len && !memcmp (msg, buf->msg, len))
109 {
110 // found matching buf, see if expired
111 if (buf->expire <= pticks || !buf->count)
112 {
113 // yes, take over matching buffer, print
114 buf->expire = pticks + pl->outputs_sync;
115 buf->count = pl->outputs_count;
116
117 return false;
118 }
119
120 // no, suppress
121 --buf->count;
122 return true;
123 }
124
125 if (lru->expire > buf->expire)
126 lru = buf;
127 }
128
129 // new message, evoke oldest buffer
130 lru->expire = pticks + pl->outputs_sync;
131 lru->count = pl->outputs_count;
132 lru->len = len;
133 memcpy (lru->msg, msg, len);
134
135 return false;
136 }
137
138 /**
139 * Sends message to player(s).
140 *
141 * flags is various flags - mostly color, plus a few specials.
142 *
143 * pri is priority. It is a little odd - the lower the value, the more
144 * important it is. Thus, 0 gets sent no matter what. Otherwise, the
145 * value must be less than the listening level that the player has set.
146 * Unfortunately, there is no clear guideline on what each level does what.
147 *
148 * pl can be passed as NULL - in fact, this will be done if NDI_ALL is set
149 * in the flags.
150 *
151 * If message is black, and not NDI_UNIQUE, gets sent through output buffers.
152 *
153 */
154 void
155 new_draw_info (int flags, int pri, const object *op, const char *buf)
156 {
157 if (flags & NDI_ALL)
158 {
159 for_all_players (pl)
160 new_draw_info (flags & ~NDI_ALL, pri, pl->ob, buf);
161 }
162 else
163 {
164 if (!op || !op->contr || !op->contr->ns)
165 return;
166
167 if (pri >= op->contr->listening)
168 return;
169
170 if ((flags & (NDI_COLOR_MASK | NDI_UNIQUE)) != NDI_BLACK
171 || !op->contr->ns->msg_suppressed (buf))
172 print_message (flags & NDI_COLOR_MASK, op, buf);
173 }
174 }
175
176 /**
177 * Wrapper for new_draw_info printf-like.
178 *
179 * This is a pretty trivial function, but it allows us to use printf style
180 * formatting, so instead of the calling function having to do it, we do
181 * it here. It may also have advantages in the future for reduction of
182 * client/server bandwidth (client could keep track of various strings
183 */
184 void
185 new_draw_info_format (int flags, int pri, const object *pl, const char *format, ...)
186 {
187 char buf[HUGE_BUF];
188
189 va_list ap;
190 va_start (ap, format);
191 vsnprintf (buf, HUGE_BUF, format, ap);
192 va_end (ap);
193
194 new_draw_info (flags, pri, pl, buf);
195 }
196
197 void
198 draw_ext_info (int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *message, const char *oldmessage)
199 {
200
201 if (!pl || (pl->type != PLAYER) || (pl->contr == NULL))
202 return;
203
204 if (pri >= pl->contr->listening)
205 return;
206
207 if (!CLIENT_SUPPORT_READABLES (pl->contr->ns, type))
208 {
209 char *buf = (char *) malloc (strlen (oldmessage == NULL ? message : oldmessage) + 1);
210
211 if (buf == NULL)
212 LOG (llevError, "info::draw_ext_info -> Out of memory!");
213 else
214 {
215 strcpy (buf, oldmessage == NULL ? message : oldmessage);
216 strip_media_tag (buf);
217 new_draw_info (flags, pri, pl, buf);
218 free (buf);
219 }
220 }
221 else
222 esrv_print_ext_msg (pl->contr->ns, flags & NDI_COLOR_MASK, type, subtype, message);
223 }
224
225 void
226 draw_ext_info_format (int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *old_format, char *new_format, ...)
227 {
228 char buf[HUGE_BUF];
229
230 if (!pl || (pl->type != PLAYER) || (pl->contr == NULL))
231 return;
232
233 if (pri >= pl->contr->listening)
234 return;
235
236 if (!CLIENT_SUPPORT_READABLES (pl->contr->ns, type))
237 {
238 va_list ap;
239
240 LOG (llevDebug, "Non supported extension text type for client.\n");
241 va_start (ap, new_format);
242 vsnprintf (buf, HUGE_BUF, old_format, ap);
243 va_end (ap);
244 new_draw_info (flags, pri, pl, buf);
245 return;
246 }
247 else
248 {
249 va_list ap;
250
251 va_start (ap, new_format);
252 vsnprintf (buf, HUGE_BUF, new_format, ap);
253 va_end (ap);
254 strip_media_tag (buf);
255 esrv_print_ext_msg (pl->contr->ns, flags & NDI_COLOR_MASK, type, subtype, buf);
256 }
257 }
258
259 /**
260 * Writes to everyone on the map *except* op. This is useful for emotions.
261 */
262
263 void
264 new_info_map_except (int color, maptile * map, object *op, const char *str)
265 {
266 for_all_players (pl)
267 if (pl->ob != NULL && pl->ob->map == map && pl->ob != op)
268 new_draw_info (color, 0, pl->ob, str);
269 }
270
271 /**
272 * Writes to everyone on the map except op1 and op2
273 */
274 void
275 new_info_map_except2 (int color, maptile * map, object *op1, object *op2, const char *str)
276 {
277 for_all_players (pl)
278 if (pl->ob != NULL && pl->ob->map == map && pl->ob != op1 && pl->ob != op2)
279 new_draw_info (color, 0, pl->ob, str);
280 }
281
282 /**
283 * Writes to everyone on the specified map
284 */
285 void
286 new_info_map (int color, maptile * map, const char *str)
287 {
288 for_all_players (pl)
289 if (pl->ob != NULL && pl->ob->map == map)
290 new_draw_info (color, 0, pl->ob, str);
291 }
292
293 /**
294 * Sets player title.
295 */
296 void
297 set_title (object *pl, char *buf)
298 {
299 /* Eneq(@csd.uu.se): Let players define their own titles. */
300 if (pl->contr->own_title[0] == '\0')
301 sprintf (buf, "Player: %s the %s", (const char *) pl->name, (const char *) pl->contr->title);
302 else
303 sprintf (buf, "Player: %s %s", (const char *) pl->name, (const char *) pl->contr->own_title);
304 }
305
306 // formerly a macro, used only by magic map, so optimised it out
307 static inline faceidx
308 GET_MAP_FACE (maptile *m, int x, int y, int layer)
309 {
310 if (object *op = GET_MAP_FACE_OBJ (m, x, y, layer))
311 return op->face;
312 else
313 return 0;
314 }
315
316 /**
317 * Helper for magic map creation.
318 *
319 * Takes a player, the map_mark array and an x and y starting position.
320 * pl is the player.
321 * px, py are offsets from the player.
322 *
323 * This function examines all the adjacant spaces next to px, py.
324 * It updates the map_mark arrow with the color and high bits set
325 * for various code values.
326 */
327 static void
328 magic_mapping_mark_recursive (object *pl, char *map_mark, int px, int py)
329 {
330 int x, y, dx, dy, mflags;
331 sint16 nx, ny;
332 maptile *mp;
333
334 for (dx = -1; dx <= 1; dx++)
335 {
336 for (dy = -1; dy <= 1; dy++)
337 {
338 x = px + dx;
339 y = py + dy;
340
341 if (FABS (x) >= MAGIC_MAP_HALF || FABS (y) >= MAGIC_MAP_HALF)
342 continue;
343
344 mp = pl->map;
345 nx = pl->x + x;
346 ny = pl->y + y;
347
348 mflags = get_map_flags (pl->map, &mp, nx, ny, &nx, &ny);
349 if (mflags & P_OUT_OF_MAP)
350 continue;
351
352 if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] == 0)
353 {
354 int f = GET_MAP_FACE (mp, nx, ny, 0);
355 if (f == blank_face)
356 {
357 f = GET_MAP_FACE (mp, nx, ny, 1);
358 if (f == blank_face)
359 f = GET_MAP_FACE (mp, nx, ny, 2);
360 }
361
362 int magicmap = faces [f].magicmap;
363
364 /* Should probably have P_NO_MAGIC here also, but then shops don't
365 * work.
366 */
367 if (mflags & P_BLOCKSVIEW)
368 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
369 else
370 {
371 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
372 magic_mapping_mark_recursive (pl, map_mark, x, y);
373 }
374 }
375 }
376 }
377 }
378
379 /**
380 * Creates magic map for player.
381 *
382 * Note: For improved magic mapping display, the space that blocks
383 * the view is now marked with value 2. Any dependencies of map_mark
384 * being nonzero have been changed to check for 1. Also, since
385 * map_mark is a char value, putting 2 in should cause no problems.
386 *
387 * This function examines the map the player is on, and determines what
388 * is visible. 2 is set for walls or objects that blocks view. 1
389 * is for open spaces. map_mark should already have been initialised
390 * to zero before this is called.
391 * strength is an initial strength*2 rectangular area that we automatically
392 * see in/penetrate through.
393 */
394 void
395 magic_mapping_mark (object *pl, char *map_mark, int strength)
396 {
397 int x, y, mflags;
398 sint16 nx, ny;
399 maptile *mp;
400
401 for (x = -strength; x < strength; x++)
402 {
403 for (y = -strength; y < strength; y++)
404 {
405 mp = pl->map;
406 nx = pl->x + x;
407 ny = pl->y + y;
408
409 mflags = get_map_flags (pl->map, &mp, nx, ny, &nx, &ny);
410 if (mflags & P_OUT_OF_MAP)
411 continue;
412
413 int f = GET_MAP_FACE (mp, nx, ny, 0);
414 if (f == blank_face)
415 {
416 f = GET_MAP_FACE (mp, nx, ny, 1);
417 if (f == blank_face)
418 f = GET_MAP_FACE (mp, nx, ny, 2);
419 }
420
421 int magicmap = faces [f].magicmap;
422
423 if (mflags & P_BLOCKSVIEW)
424 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
425 else
426 {
427 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
428 magic_mapping_mark_recursive (pl, map_mark, x, y);
429 }
430 }
431 }
432 }
433
434 /**
435 * Creates and sends magic map to player.
436 *
437 * The following function is a lot messier than it really should be,
438 * but there is no real easy solution.
439 *
440 * Mark Wedel
441 */
442 void
443 draw_magic_map (object *pl)
444 {
445 char *map_mark = (char *)calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1);
446 int xmin, xmax, ymin, ymax;
447
448 if (pl->type != PLAYER)
449 {
450 LOG (llevError, "Non player object called draw_map.\n");
451 return;
452 }
453
454 /* First, we figure out what spaces are 'reachable' by the player */
455 magic_mapping_mark (pl, map_mark, 3);
456
457 /* We now go through and figure out what spaces have been
458 * marked, and thus figure out rectangular region we send
459 * to the client (eg, if only a 10x10 area is visible, we only
460 * want to send those 100 spaces.)
461 */
462 xmin = MAGIC_MAP_SIZE;
463 ymin = MAGIC_MAP_SIZE;
464 xmax = 0;
465 ymax = 0;
466
467 for (int x = 0; x < MAGIC_MAP_SIZE; x++)
468 for (int y = 0; y < MAGIC_MAP_SIZE; y++)
469 if (map_mark[x + pl->map->width * y] | FACE_FLOOR)
470 {
471 xmin = x < xmin ? x : xmin;
472 xmax = x > xmax ? x : xmax;
473 ymin = y < ymin ? y : ymin;
474 ymax = y > ymax ? y : ymax;
475 }
476
477 packet sl;
478 sl.printf ("magicmap %d %d %d %d ", (xmax - xmin + 1), (ymax - ymin + 1),
479 MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin);
480
481 for (int y = ymin; y <= ymax; y++)
482 for (int x = xmin; x <= xmax; x++)
483 sl << uint8 (map_mark[x + MAGIC_MAP_SIZE * y] & ~FACE_FLOOR);
484
485 pl->contr->ns->send_packet (sl);
486
487 free (map_mark);
488 }
489
490 /**
491 * Send a kill log record to sockets
492 */
493 void
494 Log_Kill (const char *Who, const char *What, int WhatType, const char *With, int WithType)
495 {
496 size_t len;
497 char buf[MAX_BUF];
498
499 if (With != NULL)
500 snprintf (buf, MAX_BUF, "%s\t%s\t%d\t%s\t%d\n", Who, What, WhatType, With, WithType);
501 else
502 snprintf (buf, MAX_BUF, "%s\t%s\t%d\n", Who, What, WhatType);
503
504 len = strlen (buf);
505 }
506