ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/info.C
Revision: 1.68
Committed: Wed Nov 16 23:42:03 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.67: +1 -1 lines
Log Message:
copyright update 2016

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 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 * Frontend for esrv_print_msg
57 * \param colr message color
58 * \param pl player to send to. Can be NULL
59 * \param tmp message to send. Can be NULL
60 *
61 * If pl is NULL or without contr set, writes message to log.
62 *
63 * Else sends message to player via esrv_print_msg
64 */
65 static void
66 print_message (int colr, const object *pl, const char *tmp)
67 {
68 if (!tmp)
69 tmp = "[NULL]";
70
71 if (!pl || (pl->type == PLAYER && pl->contr == NULL))
72 return;
73
74 if (pl->type == PLAYER)
75 esrv_print_msg (pl->contr->ns, colr, (char *)tmp);
76 }
77
78 bool
79 client::msg_suppressed (const char *msg)
80 {
81 if (!pl)
82 return false;
83
84 if (pl->outputs_count <= 1 || !pl->outputs_sync)
85 return false;
86
87 int len = strlen (msg);
88
89 if (len > MSG_BUF_SIZE)
90 return false;
91
92 msg_buf *lru = msgbuf;
93 for (msg_buf *buf = msgbuf; buf < msgbuf + MSG_BUF_COUNT; ++buf)
94 {
95 if (len == buf->len && !memcmp (msg, buf->msg, len))
96 {
97 // found matching buf, see if expired
98 if (buf->expire <= server_tick || !buf->count)
99 {
100 // yes, take over matching buffer, print
101 buf->expire = server_tick + pl->outputs_sync;
102 buf->count = pl->outputs_count;
103
104 return false;
105 }
106
107 // no, suppress
108 --buf->count;
109 return true;
110 }
111
112 if (lru->expire > buf->expire)
113 lru = buf;
114 }
115
116 // new message, evoke oldest buffer
117 lru->expire = server_tick + pl->outputs_sync;
118 lru->count = pl->outputs_count;
119 lru->len = len;
120 memcpy (lru->msg, msg, len);
121
122 return false;
123 }
124
125 /**
126 * Sends message to player(s).
127 *
128 * flags is various flags - mostly color, plus a few specials.
129 *
130 * pri is unused.
131 *
132 * pl can be passed as NULL - in fact, this will be done if NDI_ALL is set
133 * in the flags.
134 *
135 * If message is black, and not NDI_UNIQUE, gets sent through output buffers.
136 *
137 */
138 void
139 new_draw_info (int flags, int pri, const object *op, const char *buf)
140 {
141 if (flags & NDI_ALL)
142 {
143 for_all_players (pl)
144 new_draw_info (flags & ~NDI_ALL, 0, pl->ob, buf);
145 }
146 else
147 {
148 if (!op || !op->contr || !op->contr->ns)
149 return;
150
151 if ((flags & (NDI_COLOR_MASK | NDI_UNIQUE)) != NDI_BLACK
152 || !op->contr->ns->msg_suppressed (buf))
153 print_message (flags & NDI_COLOR_MASK, op, buf);
154 }
155 }
156
157 /**
158 * Wrapper for new_draw_info printf-like.
159 *
160 * This is a pretty trivial function, but it allows us to use printf style
161 * formatting, so instead of the calling function having to do it, we do
162 * it here. It may also have advantages in the future for reduction of
163 * client/server bandwidth (client could keep track of various strings
164 */
165 void
166 new_draw_info_format (int flags, int pri, const object *pl, const char *format, ...)
167 {
168 va_list ap;
169 va_start (ap, format);
170 new_draw_info (flags, pri, pl, vformat (format, ap));
171 va_end (ap);
172 }
173
174 /**
175 * Writes to everyone on the map *except* op. This is useful for emotions.
176 */
177 void
178 new_info_map_except (int color, maptile * map, object *op, const char *str)
179 {
180 for_all_players (pl)
181 if (pl->ob->map == map && pl->ob != op)
182 new_draw_info (color, 0, pl->ob, str);
183 }
184
185 /**
186 * Writes to everyone on the specified map
187 */
188 void
189 new_info_map (int color, maptile * map, const char *str)
190 {
191 for_all_players (pl)
192 if (pl->ob->map == map)
193 new_draw_info (color, 0, pl->ob, str);
194 }
195
196 /**
197 * Sets player title.
198 */
199 void
200 set_title (object *pl, char *buf)
201 {
202 /* Eneq(@csd.uu.se): Let players define their own titles. */
203 if (pl->contr->own_title[0] == '\0')
204 sprintf (buf, "Player: %s the %s", (const char *) pl->name, (const char *) pl->contr->title);
205 else
206 sprintf (buf, "Player: %s %s", (const char *) pl->name, (const char *) pl->contr->own_title);
207 }
208
209 // formerly a macro, used only by magic map, so optimised it out
210 static inline faceidx
211 GET_MAP_FACE (mapspace &ms, int layer)
212 {
213 if (object *op = ms.faces_obj [layer])
214 return op->face;
215 else
216 return 0;
217 }
218
219 /**
220 * Helper for magic map creation.
221 *
222 * Takes a player, the map_mark array and an x and y starting position.
223 * pl is the player.
224 * px, py are offsets from the player.
225 *
226 * This function examines all the adjacant spaces next to px, py.
227 * It updates the map_mark arrow with the color and high bits set
228 * for various code values.
229 */
230 static void
231 magic_mapping_mark_recursive (object *pl, char *map_mark, int px, int py)
232 {
233 for (int dx = -1; dx <= 1; dx++)
234 {
235 for (int dy = -1; dy <= 1; dy++)
236 {
237 int x = px + dx;
238 int y = py + dy;
239
240 if (abs (x) >= MAGIC_MAP_HALF || abs (y) >= MAGIC_MAP_HALF)
241 continue;
242
243 mapxy pos (pl);
244 pos.move (x, y);
245
246 if (!pos.normalise ())
247 continue;
248
249 mapspace &ms = pos.ms ();
250
251 if (map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] == 0)
252 {
253 int mflags = ms.flags ();
254
255 int f = GET_MAP_FACE (ms, 0);
256 if (f == blank_face)
257 {
258 f = GET_MAP_FACE (ms, 1);
259 if (f == blank_face)
260 f = GET_MAP_FACE (ms, 2);
261 }
262
263 int magicmap = faces [f].magicmap;
264
265 /* Should probably have P_NO_MAGIC here also, but then shops don't
266 * work.
267 */
268 if (mflags & P_BLOCKSVIEW)
269 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
270 else
271 {
272 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
273 magic_mapping_mark_recursive (pl, map_mark, x, y);
274 }
275 }
276 }
277 }
278 }
279
280 /**
281 * Creates magic map for player.
282 *
283 * Note: For improved magic mapping display, the space that blocks
284 * the view is now marked with value 2. Any dependencies of map_mark
285 * being nonzero have been changed to check for 1. Also, since
286 * map_mark is a char value, putting 2 in should cause no problems.
287 *
288 * This function examines the map the player is on, and determines what
289 * is visible. 2 is set for walls or objects that blocks view. 1
290 * is for open spaces. map_mark should already have been initialised
291 * to zero before this is called.
292 * strength is an initial strength*2 rectangular area that we automatically
293 * see in/penetrate through.
294 */
295 static void
296 magic_mapping_mark (object *pl, char *map_mark, int strength)
297 {
298 for (int x = -strength; x < strength; x++)
299 {
300 for (int y = -strength; y < strength; y++)
301 {
302 mapxy pos (pl);
303 pos.move (x, y);
304
305 if (!pos.normalise ())
306 continue;
307
308 mapspace &ms = pos.ms ();
309
310 int mflags = ms.flags ();
311
312 int f = GET_MAP_FACE (ms, 0);
313 if (f == blank_face)
314 {
315 f = GET_MAP_FACE (ms, 1);
316 if (f == blank_face)
317 f = GET_MAP_FACE (ms, 2);
318 }
319
320 int magicmap = faces [f].magicmap;
321
322 if (mflags & P_BLOCKSVIEW)
323 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_WALL | magicmap;
324 else
325 {
326 map_mark[MAGIC_MAP_HALF + x + MAGIC_MAP_SIZE * (MAGIC_MAP_HALF + y)] = FACE_FLOOR | magicmap;
327 magic_mapping_mark_recursive (pl, map_mark, x, y);
328 }
329 }
330 }
331 }
332
333 /**
334 * Creates and sends magic map to player.
335 *
336 * The following function is a lot messier than it really should be,
337 * but there is no real easy solution.
338 *
339 * Mark Wedel
340 */
341 void
342 draw_magic_map (object *pl)
343 {
344 int xmin, xmax, ymin, ymax;
345
346 if (pl->type != PLAYER)
347 {
348 LOG (llevError, "Non player object called draw_map.\n");
349 return;
350 }
351
352 char *map_mark = (char *)calloc (MAGIC_MAP_SIZE * MAGIC_MAP_SIZE, 1);
353 assert(("Out of memory!", map_mark != NULL));
354
355 /* First, we figure out what spaces are 'reachable' by the player */
356 magic_mapping_mark (pl, map_mark, 3);
357
358 /* We now go through and figure out what spaces have been
359 * marked, and thus figure out rectangular region we send
360 * to the client (eg, if only a 10x10 area is visible, we only
361 * want to send those 100 spaces.)
362 */
363 xmin = MAGIC_MAP_SIZE;
364 ymin = MAGIC_MAP_SIZE;
365 xmax = 0;
366 ymax = 0;
367
368 for (int x = 0; x < MAGIC_MAP_SIZE; x++)
369 for (int y = 0; y < MAGIC_MAP_SIZE; y++)
370 if (map_mark[x + pl->map->width * y] | FACE_FLOOR)
371 {
372 xmin = x < xmin ? x : xmin;
373 xmax = x > xmax ? x : xmax;
374 ymin = y < ymin ? y : ymin;
375 ymax = y > ymax ? y : ymax;
376 }
377
378 packet sl;
379 sl.printf ("magicmap %d %d %d %d ", (xmax - xmin + 1), (ymax - ymin + 1),
380 MAGIC_MAP_HALF - xmin, MAGIC_MAP_HALF - ymin);
381
382 for (int y = ymin; y <= ymax; y++)
383 for (int x = xmin; x <= xmax; x++)
384 sl << uint8 (map_mark[x + MAGIC_MAP_SIZE * y] & ~FACE_FLOOR);
385
386 pl->contr->ns->send_packet (sl);
387
388 free (map_mark);
389 }
390