ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.C
Revision: 1.45
Committed: Sun Jun 24 04:09:29 2007 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.44: +5 -3 lines
Log Message:
first rough cut of msg rewriting, introducing cfpod, a haxored pod variant for crossfire messgaes. where will it end?

File Contents

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