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

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.47 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.33 *
4 root 1.50 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.43 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.33 *
8 root 1.52 * 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 pippijn 1.33 *
13 root 1.46 * 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 pippijn 1.33 *
18 root 1.52 * 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 root 1.43 *
22 root 1.47 * The authors can be reached via e-mail to <support@deliantra.net>
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 root 1.51 * pri is unused.
146 elmex 1.1 *
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 root 1.5 void
154 root 1.41 new_draw_info (int flags, int pri, const object *op, const char *buf)
155 elmex 1.1 {
156 root 1.5 if (flags & NDI_ALL)
157 root 1.42 {
158     for_all_players (pl)
159 root 1.51 new_draw_info (flags & ~NDI_ALL, 0, pl->ob, buf);
160 root 1.42 }
161 root 1.41 else
162 root 1.5 {
163 root 1.41 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 elmex 1.1 }
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 root 1.5 void
181     new_draw_info_format (int flags, int pri, const object *pl, const char *format, ...)
182 elmex 1.1 {
183 root 1.5 va_list ap;
184     va_start (ap, format);
185 root 1.53 new_draw_info (flags, pri, pl, vformat (format, ap));
186 root 1.5 va_end (ap);
187 elmex 1.1 }
188    
189     /**
190     * Writes to everyone on the map *except* op. This is useful for emotions.
191     */
192 root 1.5 void
193 root 1.8 new_info_map_except (int color, maptile * map, object *op, const char *str)
194 root 1.5 {
195 root 1.24 for_all_players (pl)
196 root 1.49 if (pl->ob->map == map && pl->ob != op)
197 root 1.31 new_draw_info (color, 0, pl->ob, str);
198 elmex 1.1 }
199    
200     /**
201     * Writes to everyone on the map except op1 and op2
202     */
203 root 1.56 static void
204 root 1.8 new_info_map_except2 (int color, maptile * map, object *op1, object *op2, const char *str)
205 root 1.5 {
206 root 1.24 for_all_players (pl)
207 root 1.49 if (pl->ob->map == map && pl->ob != op1 && pl->ob != op2)
208 root 1.31 new_draw_info (color, 0, pl->ob, str);
209 elmex 1.1 }
210    
211     /**
212     * Writes to everyone on the specified map
213     */
214 root 1.5 void
215 root 1.8 new_info_map (int color, maptile * map, const char *str)
216 root 1.5 {
217 root 1.24 for_all_players (pl)
218 root 1.49 if (pl->ob->map == map)
219 root 1.31 new_draw_info (color, 0, pl->ob, str);
220 elmex 1.1 }
221    
222     /**
223     * Sets player title.
224     */
225 root 1.5 void
226     set_title (object *pl, char *buf)
227 elmex 1.1 {
228 root 1.5 /* Eneq(@csd.uu.se): Let players define their own titles. */
229     if (pl->contr->own_title[0] == '\0')
230     sprintf (buf, "Player: %s the %s", (const char *) pl->name, (const char *) pl->contr->title);
231     else
232     sprintf (buf, "Player: %s %s", (const char *) pl->name, (const char *) pl->contr->own_title);
233 elmex 1.1 }
234    
235 root 1.36 // formerly a macro, used only by magic map, so optimised it out
236     static inline faceidx
237 root 1.54 GET_MAP_FACE (mapspace &ms, int layer)
238 root 1.36 {
239 root 1.54 if (object *op = ms.faces_obj [layer])
240 root 1.36 return op->face;
241     else
242     return 0;
243     }
244    
245 elmex 1.1 /**
246     * Helper for magic map creation.
247     *
248     * Takes a player, the map_mark array and an x and y starting position.
249     * pl is the player.
250     * px, py are offsets from the player.
251     *
252     * This function examines all the adjacant spaces next to px, py.
253     * It updates the map_mark arrow with the color and high bits set
254     * for various code values.
255     */
256 root 1.5 static void
257     magic_mapping_mark_recursive (object *pl, char *map_mark, int px, int py)
258 elmex 1.1 {
259 root 1.54 for (int dx = -1; dx <= 1; dx++)
260 root 1.5 {
261 root 1.54 for (int dy = -1; dy <= 1; dy++)
262 root 1.5 {
263 root 1.54 int x = px + dx;
264     int y = py + dy;
265 root 1.5
266 root 1.54 if (abs (x) >= MAGIC_MAP_HALF || abs (y) >= MAGIC_MAP_HALF)
267 root 1.5 continue;
268    
269 root 1.54 mapxy pos (pl);
270     pos.move (x, y);
271 root 1.5
272 root 1.54 if (!pos.normalise ())
273 root 1.5 continue;
274    
275 root 1.54 mapspace &ms = pos.ms ();
276    
277 root 1.5 if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] == 0)
278     {
279 root 1.54 int mflags = ms.flags ();
280    
281     int f = GET_MAP_FACE (ms, 0);
282 root 1.5 if (f == blank_face)
283 root 1.35 {
284 root 1.54 f = GET_MAP_FACE (ms, 1);
285 root 1.35 if (f == blank_face)
286 root 1.54 f = GET_MAP_FACE (ms, 2);
287 root 1.35 }
288    
289     int magicmap = faces [f].magicmap;
290 root 1.5
291     /* Should probably have P_NO_MAGIC here also, but then shops don't
292     * work.
293     */
294     if (mflags & P_BLOCKSVIEW)
295 root 1.35 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
296 root 1.5 else
297     {
298 root 1.35 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
299 root 1.5 magic_mapping_mark_recursive (pl, map_mark, x, y);
300 root 1.2 }
301     }
302     }
303 elmex 1.1 }
304     }
305    
306     /**
307     * Creates magic map for player.
308     *
309 root 1.54 * Note: For improved magic mapping display, the space that blocks
310     * the view is now marked with value 2. Any dependencies of map_mark
311     * being nonzero have been changed to check for 1. Also, since
312 elmex 1.1 * map_mark is a char value, putting 2 in should cause no problems.
313     *
314     * This function examines the map the player is on, and determines what
315 root 1.54 * is visible. 2 is set for walls or objects that blocks view. 1
316     * is for open spaces. map_mark should already have been initialised
317 elmex 1.1 * to zero before this is called.
318     * strength is an initial strength*2 rectangular area that we automatically
319     * see in/penetrate through.
320     */
321 root 1.56 static void
322 root 1.5 magic_mapping_mark (object *pl, char *map_mark, int strength)
323 elmex 1.1 {
324 root 1.54 for (int x = -strength; x < strength; x++)
325 root 1.5 {
326 root 1.54 for (int y = -strength; y < strength; y++)
327 root 1.5 {
328 root 1.54 mapxy pos (pl);
329     pos.move (x, y);
330 root 1.35
331 root 1.54 if (!pos.normalise ())
332 root 1.5 continue;
333 root 1.35
334 root 1.54 mapspace &ms = pos.ms ();
335    
336     int mflags = ms.flags ();
337    
338     int f = GET_MAP_FACE (ms, 0);
339 root 1.35 if (f == blank_face)
340 root 1.5 {
341 root 1.54 f = GET_MAP_FACE (ms, 1);
342 root 1.5 if (f == blank_face)
343 root 1.54 f = GET_MAP_FACE (ms, 2);
344 root 1.2 }
345    
346 root 1.35 int magicmap = faces [f].magicmap;
347    
348 root 1.5 if (mflags & P_BLOCKSVIEW)
349 root 1.35 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
350 root 1.5 else
351     {
352 root 1.35 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
353 root 1.5 magic_mapping_mark_recursive (pl, map_mark, x, y);
354 root 1.2 }
355     }
356 elmex 1.1 }
357     }
358    
359     /**
360     * Creates and sends magic map to player.
361     *
362     * The following function is a lot messier than it really should be,
363     * but there is no real easy solution.
364     *
365     * Mark Wedel
366     */
367 root 1.5 void
368     draw_magic_map (object *pl)
369 elmex 1.1 {
370 root 1.13 char *map_mark = (char *)calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1);
371 root 1.5 int xmin, xmax, ymin, ymax;
372    
373     if (pl->type != PLAYER)
374     {
375     LOG (llevError, "Non player object called draw_map.\n");
376     return;
377     }
378    
379     /* First, we figure out what spaces are 'reachable' by the player */
380     magic_mapping_mark (pl, map_mark, 3);
381    
382     /* We now go through and figure out what spaces have been
383     * marked, and thus figure out rectangular region we send
384     * to the client (eg, if only a 10x10 area is visible, we only
385     * want to send those 100 spaces.)
386     */
387     xmin = MAGIC_MAP_SIZE;
388     ymin = MAGIC_MAP_SIZE;
389     xmax = 0;
390     ymax = 0;
391 root 1.13
392     for (int x = 0; x < MAGIC_MAP_SIZE; x++)
393     for (int y = 0; y < MAGIC_MAP_SIZE; y++)
394 root 1.25 if (map_mark[x + pl->map->width * y] | FACE_FLOOR)
395 root 1.5 {
396 root 1.13 xmin = x < xmin ? x : xmin;
397     xmax = x > xmax ? x : xmax;
398     ymin = y < ymin ? y : ymin;
399     ymax = y > ymax ? y : ymax;
400 root 1.2 }
401 elmex 1.1
402 root 1.15 packet sl;
403 root 1.13 sl.printf ("magicmap %d %d %d %d ", (xmax - xmin + 1), (ymax - ymin + 1),
404     MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin);
405    
406     for (int y = ymin; y <= ymax; y++)
407     for (int x = xmin; x <= xmax; x++)
408     sl << uint8 (map_mark[x + MAGIC_MAP_SIZE * y] & ~FACE_FLOOR);
409    
410 root 1.23 pl->contr->ns->send_packet (sl);
411 root 1.5
412     free (map_mark);
413 elmex 1.1 }
414