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