ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.C
Revision: 1.51
Committed: Fri Aug 29 02:07:10 2008 UTC (15 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_7, rel-2_72, rel-2_73, rel-2_71, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_79, rel-2_78
Changes since 1.50: +2 -14 lines
Log Message:
remove listen(ing)?

File Contents

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