ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.C
Revision: 1.55
Committed: Fri Nov 6 12:49:19 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.54: +0 -2 lines
Log Message:
make effectively static symbols actually static, part 1

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 it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
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 #include <cstring>
42
43 /**
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 static void
50 esrv_print_msg (client *ns, int color, const char *str)
51 {
52 ns->send_msg (color, "info", str);
53 }
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 static void
65 esrv_print_ext_msg (client *ns, int color, uint8 type, uint8 subtype, const char *message)
66 {
67 ns->send_packet_printf ("drawextinfo %d %u %u %s", color, type, subtype, message);
68 }
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 static void
81 print_message (int colr, const object *pl, const char *tmp)
82 {
83 if (!tmp)
84 tmp = "[NULL]";
85
86 if (!pl || (pl->type == PLAYER && pl->contr == NULL))
87 return;
88
89 if (pl->type == PLAYER)
90 esrv_print_msg (pl->contr->ns, colr, (char *)tmp);
91 }
92
93 bool
94 client::msg_suppressed (const char *msg)
95 {
96 if (!pl)
97 return false;
98
99 if (pl->outputs_count <= 1 || !pl->outputs_sync)
100 return false;
101
102 int len = strlen (msg);
103
104 if (len > MSG_BUF_SIZE)
105 return false;
106
107 msg_buf *lru = msgbuf;
108 for (msg_buf *buf = msgbuf; buf < msgbuf + MSG_BUF_COUNT; ++buf)
109 {
110 if (len == buf->len && !memcmp (msg, buf->msg, len))
111 {
112 // found matching buf, see if expired
113 if (buf->expire <= pticks || !buf->count)
114 {
115 // yes, take over matching buffer, print
116 buf->expire = pticks + pl->outputs_sync;
117 buf->count = pl->outputs_count;
118
119 return false;
120 }
121
122 // no, suppress
123 --buf->count;
124 return true;
125 }
126
127 if (lru->expire > buf->expire)
128 lru = buf;
129 }
130
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 }
139
140 /**
141 * Sends message to player(s).
142 *
143 * flags is various flags - mostly color, plus a few specials.
144 *
145 * pri is unused.
146 *
147 * pl can be passed as NULL - in fact, this will be done if NDI_ALL is set
148 * in the flags.
149 *
150 * If message is black, and not NDI_UNIQUE, gets sent through output buffers.
151 *
152 */
153 void
154 new_draw_info (int flags, int pri, const object *op, const char *buf)
155 {
156 if (flags & NDI_ALL)
157 {
158 for_all_players (pl)
159 new_draw_info (flags & ~NDI_ALL, 0, pl->ob, buf);
160 }
161 else
162 {
163 if (!op || !op->contr || !op->contr->ns)
164 return;
165
166 if ((flags & (NDI_COLOR_MASK | NDI_UNIQUE)) != NDI_BLACK
167 || !op->contr->ns->msg_suppressed (buf))
168 print_message (flags & NDI_COLOR_MASK, op, buf);
169 }
170 }
171
172 /**
173 * Wrapper for new_draw_info printf-like.
174 *
175 * This is a pretty trivial function, but it allows us to use printf style
176 * formatting, so instead of the calling function having to do it, we do
177 * it here. It may also have advantages in the future for reduction of
178 * client/server bandwidth (client could keep track of various strings
179 */
180 void
181 new_draw_info_format (int flags, int pri, const object *pl, const char *format, ...)
182 {
183 va_list ap;
184 va_start (ap, format);
185 new_draw_info (flags, pri, pl, vformat (format, ap));
186 va_end (ap);
187 }
188
189 void
190 draw_ext_info (int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *message, const char *oldmessage)
191 {
192 if (!pl || (pl->type != PLAYER) || (pl->contr == NULL))
193 return;
194
195 if (!CLIENT_SUPPORT_READABLES (pl->contr->ns, type))
196 {
197 char *buf = (char *) malloc (strlen (oldmessage == NULL ? message : oldmessage) + 1);
198
199 if (buf == NULL)
200 LOG (llevError, "info::draw_ext_info -> Out of memory!");
201 else
202 {
203 strcpy (buf, oldmessage == NULL ? message : oldmessage);
204 strip_media_tag (buf);
205 new_draw_info (flags, pri, pl, buf);
206 free (buf);
207 }
208 }
209 else
210 esrv_print_ext_msg (pl->contr->ns, flags & NDI_COLOR_MASK, type, subtype, message);
211 }
212
213 void
214 draw_ext_info_format (int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *old_format, char *new_format, ...)
215 {
216 va_list ap;
217 va_start (ap, new_format);
218
219 if (!pl || !pl->is_player ())
220 return;
221
222 // TODO: just assume, and rip out?
223 if (!CLIENT_SUPPORT_READABLES (pl->contr->ns, type))
224 {
225 LOG (llevDebug, "Non supported extension text type for client.\n");
226 new_draw_info (flags, pri, pl, vformat (old_format, ap));
227 }
228 else
229 {
230 char *buf = vformat (new_format, ap);
231 strip_media_tag (buf);
232 esrv_print_ext_msg (pl->contr->ns, flags & NDI_COLOR_MASK, type, subtype, buf);
233 }
234
235 va_end (ap);
236 }
237
238 /**
239 * Writes to everyone on the map *except* op. This is useful for emotions.
240 */
241 void
242 new_info_map_except (int color, maptile * map, object *op, const char *str)
243 {
244 for_all_players (pl)
245 if (pl->ob->map == map && pl->ob != op)
246 new_draw_info (color, 0, pl->ob, str);
247 }
248
249 /**
250 * Writes to everyone on the map except op1 and op2
251 */
252 void
253 new_info_map_except2 (int color, maptile * map, object *op1, object *op2, const char *str)
254 {
255 for_all_players (pl)
256 if (pl->ob->map == map && pl->ob != op1 && pl->ob != op2)
257 new_draw_info (color, 0, pl->ob, str);
258 }
259
260 /**
261 * Writes to everyone on the specified map
262 */
263 void
264 new_info_map (int color, maptile * map, const char *str)
265 {
266 for_all_players (pl)
267 if (pl->ob->map == map)
268 new_draw_info (color, 0, pl->ob, str);
269 }
270
271 /**
272 * Sets player title.
273 */
274 void
275 set_title (object *pl, char *buf)
276 {
277 /* Eneq(@csd.uu.se): Let players define their own titles. */
278 if (pl->contr->own_title[0] == '\0')
279 sprintf (buf, "Player: %s the %s", (const char *) pl->name, (const char *) pl->contr->title);
280 else
281 sprintf (buf, "Player: %s %s", (const char *) pl->name, (const char *) pl->contr->own_title);
282 }
283
284 // formerly a macro, used only by magic map, so optimised it out
285 static inline faceidx
286 GET_MAP_FACE (mapspace &ms, int layer)
287 {
288 if (object *op = ms.faces_obj [layer])
289 return op->face;
290 else
291 return 0;
292 }
293
294 /**
295 * Helper for magic map creation.
296 *
297 * Takes a player, the map_mark array and an x and y starting position.
298 * pl is the player.
299 * px, py are offsets from the player.
300 *
301 * This function examines all the adjacant spaces next to px, py.
302 * It updates the map_mark arrow with the color and high bits set
303 * for various code values.
304 */
305 static void
306 magic_mapping_mark_recursive (object *pl, char *map_mark, int px, int py)
307 {
308 for (int dx = -1; dx <= 1; dx++)
309 {
310 for (int dy = -1; dy <= 1; dy++)
311 {
312 int x = px + dx;
313 int y = py + dy;
314
315 if (abs (x) >= MAGIC_MAP_HALF || abs (y) >= MAGIC_MAP_HALF)
316 continue;
317
318 mapxy pos (pl);
319 pos.move (x, y);
320
321 if (!pos.normalise ())
322 continue;
323
324 mapspace &ms = pos.ms ();
325
326 if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] == 0)
327 {
328 int mflags = ms.flags ();
329
330 int f = GET_MAP_FACE (ms, 0);
331 if (f == blank_face)
332 {
333 f = GET_MAP_FACE (ms, 1);
334 if (f == blank_face)
335 f = GET_MAP_FACE (ms, 2);
336 }
337
338 int magicmap = faces [f].magicmap;
339
340 /* Should probably have P_NO_MAGIC here also, but then shops don't
341 * work.
342 */
343 if (mflags & P_BLOCKSVIEW)
344 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
345 else
346 {
347 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
348 magic_mapping_mark_recursive (pl, map_mark, x, y);
349 }
350 }
351 }
352 }
353 }
354
355 /**
356 * Creates magic map for player.
357 *
358 * Note: For improved magic mapping display, the space that blocks
359 * the view is now marked with value 2. Any dependencies of map_mark
360 * being nonzero have been changed to check for 1. Also, since
361 * map_mark is a char value, putting 2 in should cause no problems.
362 *
363 * This function examines the map the player is on, and determines what
364 * is visible. 2 is set for walls or objects that blocks view. 1
365 * is for open spaces. map_mark should already have been initialised
366 * to zero before this is called.
367 * strength is an initial strength*2 rectangular area that we automatically
368 * see in/penetrate through.
369 */
370 void
371 magic_mapping_mark (object *pl, char *map_mark, int strength)
372 {
373 for (int x = -strength; x < strength; x++)
374 {
375 for (int y = -strength; y < strength; y++)
376 {
377 mapxy pos (pl);
378 pos.move (x, y);
379
380 if (!pos.normalise ())
381 continue;
382
383 mapspace &ms = pos.ms ();
384
385 int mflags = ms.flags ();
386
387 int f = GET_MAP_FACE (ms, 0);
388 if (f == blank_face)
389 {
390 f = GET_MAP_FACE (ms, 1);
391 if (f == blank_face)
392 f = GET_MAP_FACE (ms, 2);
393 }
394
395 int magicmap = faces [f].magicmap;
396
397 if (mflags & P_BLOCKSVIEW)
398 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
399 else
400 {
401 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
402 magic_mapping_mark_recursive (pl, map_mark, x, y);
403 }
404 }
405 }
406 }
407
408 /**
409 * Creates and sends magic map to player.
410 *
411 * The following function is a lot messier than it really should be,
412 * but there is no real easy solution.
413 *
414 * Mark Wedel
415 */
416 void
417 draw_magic_map (object *pl)
418 {
419 char *map_mark = (char *)calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1);
420 int xmin, xmax, ymin, ymax;
421
422 if (pl->type != PLAYER)
423 {
424 LOG (llevError, "Non player object called draw_map.\n");
425 return;
426 }
427
428 /* First, we figure out what spaces are 'reachable' by the player */
429 magic_mapping_mark (pl, map_mark, 3);
430
431 /* We now go through and figure out what spaces have been
432 * marked, and thus figure out rectangular region we send
433 * to the client (eg, if only a 10x10 area is visible, we only
434 * want to send those 100 spaces.)
435 */
436 xmin = MAGIC_MAP_SIZE;
437 ymin = MAGIC_MAP_SIZE;
438 xmax = 0;
439 ymax = 0;
440
441 for (int x = 0; x < MAGIC_MAP_SIZE; x++)
442 for (int y = 0; y < MAGIC_MAP_SIZE; y++)
443 if (map_mark[x + pl->map->width * y] | FACE_FLOOR)
444 {
445 xmin = x < xmin ? x : xmin;
446 xmax = x > xmax ? x : xmax;
447 ymin = y < ymin ? y : ymin;
448 ymax = y > ymax ? y : ymax;
449 }
450
451 packet sl;
452 sl.printf ("magicmap %d %d %d %d ", (xmax - xmin + 1), (ymax - ymin + 1),
453 MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin);
454
455 for (int y = ymin; y <= ymax; y++)
456 for (int x = xmin; x <= xmax; x++)
457 sl << uint8 (map_mark[x + MAGIC_MAP_SIZE * y] & ~FACE_FLOOR);
458
459 pl->contr->ns->send_packet (sl);
460
461 free (map_mark);
462 }
463