ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/los.C
(Generate patch)

Comparing deliantra/server/common/los.C (file contents):
Revision 1.43 by root, Sat Dec 20 04:02:15 2008 UTC vs.
Revision 1.77 by root, Sat Nov 17 23:40:00 2018 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / 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
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 6 *
8 * Deliantra is free software: you can redistribute it and/or modify 7 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 8 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 9 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 10 * option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, 12 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 15 * GNU General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23
24/* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */
25 23
26#include <global.h> 24#include <global.h>
27#include <cmath> 25#include <cmath>
28 26
29static void expand_lighted_sight (object *op); 27#define SEE_IN_DARK_RADIUS 2
28#define MAX_VISION 10 // maximum visible radius
30 29
30// los flags
31enum { 31enum {
32 LOS_XI = 0x01, 32 FLG_XI = 0x01, // we have an x-parent
33 LOS_YI = 0x02, 33 FLG_YI = 0x02, // we have an y-parent
34 FLG_BLOCKED = 0x04, // this space blocks the view
35 FLG_QUEUED = 0x80 // already queued in queue, or border
34}; 36};
35 37
38// it is important for performance reasons that this structure
39// has a size easily computable by the cpu (*8 is perfect).
40// it is possible to move culled and visible into flags, at
41// some speed loss.
36struct los_info 42struct los_info
37{ 43{
44 uint8 flags; // FLG_xxx
45 uint8 culled; // culled from "tree"
46 uint8 visible;
47 uint8 pad0;
48
38 sint8 xo, yo; // obscure angle 49 sint8 xo, yo; // obscure angle
39 sint8 xe, ye; // angle deviation 50 sint8 xe, ye; // angle deviation
40 uint8 culled; // culled from "tree"
41 uint8 queued; // already queued
42 uint8 visible;
43 uint8 flags; // LOS_XI/YI
44}; 51};
45 52
46// temporary storage for the los algorithm, 53// temporary storage for the los algorithm,
47// one los_info for each lightable map space 54// one los_info for each lightable map space
48static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; 55static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y];
49 56
50struct point 57struct point8
51{ 58{
52 sint8 x, y; 59 sint8 x, y;
53}; 60};
54 61
55// minimum size, but must be a power of two 62// minimum size, but must be a power of two
56#define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) 63#define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2)
57 64
58// a queue of spaces to calculate 65// a queue of spaces to calculate
59static point queue [QUEUE_LENGTH]; 66static point8 queue [QUEUE_LENGTH];
60static int q1, q2; // queue start, end 67static int q1, q2; // queue start, end
61 68
62/* 69/*
63 * Clears/initialises the los-array associated to the player 70 * Clears/initialises the los-array associated to the player
64 * controlling the object. 71 * controlling the object.
75enqueue (sint8 dx, sint8 dy, uint8 flags = 0) 82enqueue (sint8 dx, sint8 dy, uint8 flags = 0)
76{ 83{
77 sint8 x = LOS_X0 + dx; 84 sint8 x = LOS_X0 + dx;
78 sint8 y = LOS_Y0 + dy; 85 sint8 y = LOS_Y0 + dy;
79 86
80 if (x < 0 || x >= MAP_CLIENT_X) return;
81 if (y < 0 || y >= MAP_CLIENT_Y) return;
82
83 los_info &l = los[x][y]; 87 los_info &l = los[x][y];
84 88
85 l.flags |= flags; 89 l.flags |= flags;
86 90
87 if (l.queued) 91 if (expect_false (l.flags & FLG_QUEUED))
88 return; 92 return;
89 93
90 l.queued = 1; 94 l.flags |= FLG_QUEUED;
91 95
92 queue[q1].x = dx; 96 queue[q1].x = dx;
93 queue[q1].y = dy; 97 queue[q1].y = dy;
94 98
95 q1 = (q1 + 1) & (QUEUE_LENGTH - 1); 99 q1 = (q1 + 1) & (QUEUE_LENGTH - 1);
99// this is a variant of a spiral los algorithm taken from 103// this is a variant of a spiral los algorithm taken from
100// http://www.geocities.com/temerra/los_rays.html 104// http://www.geocities.com/temerra/los_rays.html
101// which has been simplified and changed considerably, but 105// which has been simplified and changed considerably, but
102// still is basically the same algorithm. 106// still is basically the same algorithm.
103static void 107static void
104do_los (object *op) 108calculate_los (player *pl)
105{ 109{
106 player *pl = op->contr; 110 {
107
108 int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2;
109
110 memset (los, 0, sizeof (los)); 111 memset (los, 0, sizeof (los));
112
113 // we keep one line for ourselves, for the border flag
114 // so the client area is actually MAP_CLIENT_(X|Y) - 2
115 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
116 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
117
118 // create borders, the corners are not touched
119 for (int dx = -half_x; dx <= half_x; ++dx)
120 los [dx + LOS_X0][LOS_Y0 - (half_y + 1)].flags =
121 los [dx + LOS_X0][LOS_Y0 + (half_y + 1)].flags = FLG_QUEUED;
122
123 for (int dy = -half_y; dy <= half_y; ++dy)
124 los [LOS_X0 - (half_x + 1)][dy + LOS_Y0].flags =
125 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
126
127 // now reset the los area and also add blocked flags
128 // which supposedly is faster than doing it inside the
129 // spiral path algorithm below, except when very little
130 // area is visible, in which case it is slower. which evens
131 // out los calculation times between large and small los maps.
132 // apply_lights also iterates over this area, maybe these
133 // two passes could be combined somehow.
134 unordered_mapwalk (mapwalk_buf, pl->viewpoint, -half_x, -half_y, half_x, half_y)
135 {
136 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
137 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
138 }
139 }
111 140
112 q1 = 0; q2 = 0; // initialise queue, not strictly required 141 q1 = 0; q2 = 0; // initialise queue, not strictly required
113 enqueue (0, 0); // enqueue center 142 enqueue (0, 0); // enqueue center
114 143
115 // treat the origin specially 144 // treat the origin specially
128 q2 = (q2 + 1) & (QUEUE_LENGTH - 1); 157 q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
129 158
130 sint8 x = LOS_X0 + dx; 159 sint8 x = LOS_X0 + dx;
131 sint8 y = LOS_Y0 + dy; 160 sint8 y = LOS_Y0 + dy;
132 161
133 //int distance = idistance (dx, dy); if (distance > max_radius) continue;//D
134 int distance = 0;//D
135
136 los_info &l = los[x][y]; 162 los_info &l = los[x][y];
137 163
138 if (expect_true (l.flags & (LOS_XI | LOS_YI))) 164 if (expect_true (l.flags & (FLG_XI | FLG_YI)))
139 { 165 {
140 l.culled = 1; 166 l.culled = 1;
167 l.xo = l.yo = l.xe = l.ye = 0;
141 168
142 // check contributing spaces, first horizontal 169 // check contributing spaces, first horizontal
143 if (expect_true (l.flags & LOS_XI)) 170 if (expect_true (l.flags & FLG_XI))
144 { 171 {
145 los_info *xi = &los[x - sign (dx)][y]; 172 los_info *xi = &los[x - sign (dx)][y];
146 173
147 // don't cull unless obscured 174 // don't cull unless obscured
148 l.culled &= !xi->visible; 175 l.culled &= !xi->visible;
149 176
150 /* merge input space */ 177 /* merge input space */
151 if (expect_false (xi->xo || xi->yo)) 178 if (expect_false (xi->xo || xi->yo))
152 { 179 {
153 // The X input can provide two main pieces of information: 180 // The X input can provide two main pieces of information:
154 // 1. Progressive X obscurity. 181 // 1. Progressive X obscurity.
155 // 2. Recessive Y obscurity. 182 // 2. Recessive Y obscurity.
156 183
157 // Progressive X obscurity, favouring recessive input angle 184 // Progressive X obscurity, favouring recessive input angle
158 if (xi->xe > 0 && l.xo == 0) 185 if (xi->xe > 0 && l.xo == 0)
173 } 200 }
174 } 201 }
175 } 202 }
176 203
177 // check contributing spaces, last vertical, identical structure 204 // check contributing spaces, last vertical, identical structure
178 if (expect_true (l.flags & LOS_YI)) 205 if (expect_true (l.flags & FLG_YI))
179 { 206 {
180 los_info *yi = &los[x][y - sign (dy)]; 207 los_info *yi = &los[x][y - sign (dy)];
181 208
182 // don't cull unless obscured 209 // don't cull unless obscured
183 l.culled &= !yi->visible; 210 l.culled &= !yi->visible;
184 211
185 /* merge input space */ 212 /* merge input space */
186 if (expect_false (yi->yo || yi->xo)) 213 if (expect_false (yi->yo || yi->xo))
187 { 214 {
188 // The Y input can provide two main pieces of information: 215 // The Y input can provide two main pieces of information:
189 // 1. Progressive Y obscurity. 216 // 1. Progressive Y obscurity.
190 // 2. Recessive X obscurity. 217 // 2. Recessive X obscurity.
191 218
192 // Progressive Y obscurity, favouring recessive input angle 219 // Progressive Y obscurity, favouring recessive input angle
193 if (yi->ye > 0 && l.yo == 0) 220 if (yi->ye > 0 && l.yo == 0)
207 l.xo = yi->xo; 234 l.xo = yi->xo;
208 } 235 }
209 } 236 }
210 } 237 }
211 238
212 // check whether this space blocks the view 239 if (l.flags & FLG_BLOCKED)
213 maptile *m = op->map;
214 sint16 nx = op->x + dx;
215 sint16 ny = op->y + dy;
216
217 if (expect_true (!xy_normalise (m, nx, ny))
218 || expect_false (m->at (nx, ny).flags () & P_BLOCKSVIEW))
219 { 240 {
220 l.xo = l.xe = abs (dx); 241 l.xo = l.xe = abs (dx);
221 l.yo = l.ye = abs (dy); 242 l.yo = l.ye = abs (dy);
222 243
223 // we obscure dependents, but might be visible 244 // we obscure dependents, but might be visible
224 // copy the los from the square towards the player, 245 // copy the los from the square towards the player,
225 // so outward diagonal corners are lit. 246 // so outward diagonal corners are lit.
226 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED; 247 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED;
248
227 l.visible = false; 249 l.visible = false;
228 } 250 }
229 else 251 else
230 { 252 {
231 // we are not blocked, so calculate visibility, by checking 253 // we are not blocked, so calculate visibility, by checking
232 // whether we are inside or outside the shadow 254 // whether we are inside or outside the shadow
233 l.visible = (l.xe <= 0 || l.xe > l.xo) 255 l.visible = (l.xe <= 0 || l.xe > l.xo)
234 && (l.ye <= 0 || l.ye > l.yo); 256 && (l.ye <= 0 || l.ye > l.yo);
235 257
236 pl->los[x][y] = l.culled ? LOS_BLOCKED 258 pl->los[x][y] = l.culled ? LOS_BLOCKED
237 : l.visible ? max (0, 2 - max_radius + distance) 259 : l.visible ? 0
238 : 3; 260 : 3;
239 } 261 }
240 262
241 } 263 }
242 264
243 // Expands by the unit length in each component's current direction. 265 // Expands by the unit length in each component's current direction.
244 // If a component has no direction, then it is expanded in both of its 266 // If a component has no direction, then it is expanded in both of its
245 // positive and negative directions. 267 // positive and negative directions.
246 if (!l.culled) 268 if (!l.culled)
247 { 269 {
248 if (dx >= 0) enqueue (dx + 1, dy, LOS_XI); 270 if (dx >= 0) enqueue (dx + 1, dy, FLG_XI);
249 if (dx <= 0) enqueue (dx - 1, dy, LOS_XI); 271 if (dx <= 0) enqueue (dx - 1, dy, FLG_XI);
250 if (dy >= 0) enqueue (dx, dy + 1, LOS_YI); 272 if (dy >= 0) enqueue (dx, dy + 1, FLG_YI);
251 if (dy <= 0) enqueue (dx, dy - 1, LOS_YI); 273 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
252 } 274 }
253 } 275 }
254} 276}
255 277
256/* returns true if op carries one or more lights
257 * This is a trivial function now days, but it used to
258 * be a bit longer. Probably better for callers to just
259 * check the op->glow_radius instead of calling this.
260 */
261int
262has_carried_lights (const object *op)
263{
264 /* op may glow! */
265 if (op->glow_radius > 0)
266 return 1;
267
268 return 0;
269}
270
271/* radius, distance => lightness adjust */ 278/* radius, distance => lightness adjust */
272static sint8 darkness[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; 279static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
280static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
273 281
274static struct darkness_init 282static struct los_init
275{ 283{
276 darkness_init () 284 los_init ()
277 { 285 {
286 assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two",
287 !(QUEUE_LENGTH & (QUEUE_LENGTH - 1))));
288
289 /* for lights */
278 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius) 290 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius)
279 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance) 291 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance)
280 { 292 {
281 // max intensity 293 // max intensity
282 int intensity = min (LOS_MAX, abs (radius) + 1); 294 int intensity = min (LOS_MAX, abs (radius) + 1);
283 295
284 // actual intensity 296 // actual intensity
285 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); 297 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
286 298
287 darkness [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 299 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
288 ? min (3, intensity) 300 ? min (3, intensity)
289 : LOS_MAX - intensity; 301 : LOS_MAX - intensity;
290 } 302 }
303
304 /* for general vision */
305 for (int radius = 0; radius <= MAX_VISION; ++radius)
306 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
307 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
291 } 308 }
292} darkness_init; 309} los_init;
293 310
311// the following functions cannot be static, due to c++ stupidity :/
312namespace {
313 // brighten area, ignore los
294sint8 314 sint8
315 los_brighten_nolos (sint8 b, sint8 l)
316 {
317 return min (b, l);
318 }
319
320 // brighten area, but respect los
321 sint8
295los_brighten (sint8 b, sint8 l) 322 los_brighten (sint8 b, sint8 l)
296{ 323 {
297 return b == LOS_BLOCKED ? b : min (b, l); 324 return b == LOS_BLOCKED ? b : min (b, l);
298} 325 }
299 326
327 // darken area, respect los
300sint8 328 sint8
301los_darken (sint8 b, sint8 l) 329 los_darken (sint8 b, sint8 l)
302{ 330 {
303 return max (b, l); 331 return max (b, l);
304} 332 }
333};
305 334
306template<sint8 change_it (sint8, sint8)> 335template<sint8 change_it (sint8, sint8)>
307static void 336static void
308apply_light (object *op, int dx, int dy, int light, const sint8 *darkness_table) 337apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table)
309{ 338{
310 // min or max the circular area around basex, basey 339 // min or max the circular area around basex, basey
311 player *pl = op->contr;
312
313 dx += LOS_X0; 340 dx += LOS_X0;
314 dy += LOS_Y0; 341 dy += LOS_Y0;
315 342
316 int hx = op->contr->ns->mapx / 2; 343 int hx = pl->ns->mapx / 2;
317 int hy = op->contr->ns->mapy / 2; 344 int hy = pl->ns->mapy / 2;
318 345
319 int ax0 = max (LOS_X0 - hx, dx - light); 346 int ax0 = max (LOS_X0 - hx, dx - light);
320 int ay0 = max (LOS_Y0 - hy, dy - light); 347 int ay0 = max (LOS_Y0 - hy, dy - light);
321 int ax1 = min (dx + light, LOS_X0 + hx); 348 int ax1 = min (dx + light, LOS_X0 + hx);
322 int ay1 = min (dy + light, LOS_Y0 + hy); 349 int ay1 = min (dy + light, LOS_Y0 + hy);
323 350
324 for (int ax = ax0; ax <= ax1; ax++) 351 for (int ax = ax0; ax <= ax1; ax++)
325 for (int ay = ay0; ay <= ay1; ay++) 352 for (int ay = ay0; ay <= ay1; ay++)
326 pl->los[ax][ay] = 353 pl->los[ax][ay] =
327 change_it (pl->los[ax][ay], darkness_table [idistance (ax - dx, ay - dy)]); 354 change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]);
328} 355}
329 356
330/* add light, by finding all (non-null) nearby light sources, then 357/* add light, by finding all (non-null) nearby light sources, then
331 * mark those squares specially. 358 * mark those squares specially.
332 */ 359 */
333static void 360static void
334apply_lights (object *op) 361apply_lights (player *pl)
335{ 362{
336 int darklevel, mflags, light, x1, y1; 363 object *op = pl->viewpoint;
337 maptile *m = op->map; 364 int darklevel = op->map->darklevel ();
338 sint16 nx, ny;
339 365
340 darklevel = m->darkness;
341
342 /* If the player can see in the dark, lower the darklevel for him */
343 if (QUERY_FLAG (op, FLAG_SEE_IN_DARK))
344 darklevel -= LOS_MAX / 2;
345
346 /* Do a sanity check. If not valid, some code below may do odd
347 * things.
348 */
349 if (darklevel > MAX_DARKNESS)
350 {
351 LOG (llevError, "Map darkness for %s on %s is too high (%d)\n", &op->name, &op->map->path, darklevel);
352 darklevel = MAX_DARKNESS;
353 }
354
355 int half_x = op->contr->ns->mapx / 2; 366 int half_x = pl->ns->mapx / 2;
356 int half_y = op->contr->ns->mapy / 2; 367 int half_y = pl->ns->mapy / 2;
357
358 int min_x = op->x - half_x - MAX_LIGHT_RADIUS;
359 int min_y = op->y - half_y - MAX_LIGHT_RADIUS;
360 int max_x = op->x + half_x + MAX_LIGHT_RADIUS;
361 int max_y = op->y + half_y + MAX_LIGHT_RADIUS;
362 368
363 int pass2 = 0; // negative lights have an extra pass 369 int pass2 = 0; // negative lights have an extra pass
364 370
371 maprect *rects = pl->viewpoint->map->split_to_tiles (
372 mapwalk_buf,
373 pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS,
374 pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS,
375 pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1,
376 pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1
377 );
378
379 /* If the player can see in the dark, increase light/vision radius */
380 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
381
365 if (darklevel < 1) 382 if (!darklevel)
366 pass2 = 1; 383 pass2 = 1;
367 else 384 else
368 { 385 {
369 /* first, make everything totally dark */ 386 /* first, make everything totally dark */
370 for (int dx = -half_x; dx <= half_x; dx++) 387 for (int dx = -half_x; dx <= half_x; dx++)
371 for (int dy = -half_x; dy <= half_y; dy++) 388 for (int dy = -half_x; dy <= half_y; dy++)
372 if (op->contr->los[dx + LOS_X0][dy + LOS_Y0] != LOS_BLOCKED)
373 op->contr->los[dx + LOS_X0][dy + LOS_Y0] = LOS_MAX; 389 max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX);
374 390
375 /* 391 /*
376 * Only process the area of interest. 392 * Only process the area of interest.
377 * the basex, basey values represent the position in the op->contr->los 393 * the basex, basey values represent the position in the op->contr->los
378 * array. Its easier to just increment them here (and start with the right 394 * array. Its easier to just increment them here (and start with the right
379 * value) than to recalculate them down below. 395 * value) than to recalculate them down below.
380 */ 396 */
381 for (int x = min_x; x <= max_x; x++) 397 for (maprect *r = rects; r->m; ++r)
382 for (int y = min_y; y <= max_y; y++) 398 rect_mapwalk (r, 0, 0)
383 { 399 {
384 maptile *m = op->map;
385 sint16 nx = x;
386 sint16 ny = y;
387
388 if (!xy_normalise (m, nx, ny))
389 continue;
390
391 mapspace &ms = m->at (nx, ny); 400 mapspace &ms = m->at (nx, ny);
392 ms.update (); 401 ms.update ();
393 sint8 light = ms.light; 402 sint8 light = ms.light;
394 403
395 if (expect_false (light)) 404 if (expect_false (light))
396 if (light < 0) 405 if (light < 0)
397 pass2 = 1; 406 pass2 = 1;
398 else 407 else
408 {
409 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
399 apply_light<los_brighten> (op, x - op->x, y - op->y, light, darkness [light + MAX_LIGHT_RADIUS]); 410 apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
411 }
400 } 412 }
401 413
402 /* grant some vision to the player, based on the darklevel */ 414 /* grant some vision to the player, based on outside, outdoor, and darklevel */
403 /* for outdoor maps, ensure some mininum visibility radius */
404 { 415 {
405 int light = clamp (MAX_DARKNESS - darklevel, op->map->outdoor ? 2 : 0, MAX_LIGHT_RADIUS); 416 int light;
406 417
418 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
419 light = MAX_DARKNESS - op->map->darkness;
420 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
421 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
422 else // outdoor and darkness <= 0 => start wide and decrease quickly
423 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
424
425 light = clamp (light + bonus, 0, MAX_VISION);
426
407 apply_light<los_brighten> (op, 0, 0, light, darkness [light + MAX_LIGHT_RADIUS]); 427 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
408 } 428 }
409 } 429 }
430
431 // when we fly high, we have some minimum viewable area around us, like x-ray
432 if (op->move_type & MOVE_FLY_HIGH)
433 apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]);
410 434
411 // possibly do 2nd pass for rare negative glow radii 435 // possibly do 2nd pass for rare negative glow radii
412 // for effect, those are always considered to be stronger than anything else 436 // for effect, those are always considered to be stronger than anything else
413 // but they can't darken a place completely 437 // but they can't darken a place completely
414 if (pass2) 438 if (pass2)
415 for (int x = min_x; x <= max_x; x++) 439 for (maprect *r = rects; r->m; ++r)
416 for (int y = min_y; y <= max_y; y++) 440 rect_mapwalk (r, 0, 0)
417 { 441 {
418 maptile *m = op->map;
419 sint16 nx = x;
420 sint16 ny = y;
421
422 if (!xy_normalise (m, nx, ny))
423 continue;
424
425 mapspace &ms = m->at (nx, ny); 442 mapspace &ms = m->at (nx, ny);
426 ms.update (); 443 ms.update ();
427 sint8 light = ms.light; 444 sint8 light = ms.light;
428 445
429 if (expect_false (light < 0)) 446 if (expect_false (light < 0))
447 {
448 light = clamp (light - bonus, 0, MAX_DARKNESS);
430 apply_light<los_darken> (op, x - op->x, y - op->y, -light, darkness [light + MAX_LIGHT_RADIUS]); 449 apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
450 }
431 } 451 }
432} 452}
433 453
434/* blinded_sight() - sets all viewable squares to blocked except 454/* blinded_sight() - sets all viewable squares to blocked except
435 * for the one the central one that the player occupies. A little 455 * for the one the central one that the player occupies. A little
436 * odd that you can see yourself (and what your standing on), but 456 * odd that you can see yourself (and what your standing on), but
437 * really need for any reasonable game play. 457 * really need for any reasonable game play.
438 */ 458 */
439static void 459static void
440blinded_sight (object *op) 460blinded_sight (player *pl)
441{ 461{
442 op->contr->los[LOS_X0][LOS_Y0] = 3; 462 pl->los[LOS_X0][LOS_Y0] = 1;
443} 463}
444 464
445/* 465/*
446 * update_los() recalculates the array which specifies what is 466 * update_los() recalculates the array which specifies what is
447 * visible for the given player-object. 467 * visible for the given player-object.
448 */ 468 */
449void 469void
450update_los (object *op) 470player::update_los ()
451{ 471{
452 if (QUERY_FLAG (op, FLAG_REMOVED)) 472 if (ob->flag [FLAG_REMOVED])//D really needed?
453 return; 473 return;
454 474
455 op->contr->clear_los (); 475 if (ob->flag [FLAG_WIZLOOK])
456 476 clear_los (0);
457 if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ )
458 memset (op->contr->los, 0, sizeof (op->contr->los));
459 else if (QUERY_FLAG (op, FLAG_BLIND)) /* player is blind */ 477 else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */
478 {
479 clear_los ();
460 blinded_sight (op); 480 blinded_sight (this);
481 }
461 else 482 else
462 { 483 {
463 do_los (op); 484 clear_los ();
485 calculate_los (this);
464 apply_lights (op); 486 apply_lights (this);
465 } 487 }
466 488
467 if (QUERY_FLAG (op, FLAG_XRAYS)) 489 if (viewpoint->flag [FLAG_XRAYS])
468 for (int dx = -2; dx <= 2; dx++) 490 for (int dx = -2; dx <= 2; dx++)
469 for (int dy = -2; dy <= 2; dy++) 491 for (int dy = -2; dy <= 2; dy++)
470 op->contr->los[dx + LOS_X0][dy + LOS_X0] = 0; 492 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
471} 493}
472 494
473/* update all_map_los is like update_all_los below, 495/* update all_map_los is like update_all_los below,
474 * but updates everyone on the map, no matter where they 496 * but updates everyone on the map, no matter where they
475 * are. This generally should not be used, as a per 497 * are. This generally should not be used, as a per
482 * change_map_light function 504 * change_map_light function
483 */ 505 */
484void 506void
485update_all_map_los (maptile *map) 507update_all_map_los (maptile *map)
486{ 508{
487 for_all_players (pl) 509 for_all_players_on_map (pl, map)
488 if (pl->ob && pl->ob->map == map)
489 pl->do_los = 1; 510 pl->do_los = 1;
490} 511}
491 512
492/* 513/*
493 * This function makes sure that update_los() will be called for all 514 * This function makes sure that update_los() will be called for all
494 * players on the given map within the next frame. 515 * players on the given map within the next frame.
502 * map is the map that changed, x and y are the coordinates. 523 * map is the map that changed, x and y are the coordinates.
503 */ 524 */
504void 525void
505update_all_los (const maptile *map, int x, int y) 526update_all_los (const maptile *map, int x, int y)
506{ 527{
528 map->at (x, y).invalidate ();
529
507 for_all_players (pl) 530 for_all_players (pl)
508 { 531 {
509 /* Player should not have a null map, but do this 532 /* Player should not have a null map, but do this
510 * check as a safety 533 * check as a safety
511 */ 534 */
512 if (!pl->ob || !pl->ob->map || !pl->ns) 535 if (!pl->ob || !pl->ob->map || !pl->ns)
513 continue; 536 continue;
514 537
515 /* Same map is simple case - see if pl is close enough. 538 rv_vector rv;
516 * Note in all cases, we did the check for same map first, 539
517 * and then see if the player is close enough and update 540 get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv);
518 * los if that is the case. If the player is on the
519 * corresponding map, but not close enough, then the
520 * player can't be on another map that may be closer,
521 * so by setting it up this way, we trim processing
522 * some.
523 */ 541
524 if (pl->ob->map == map) 542 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
525 {
526 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
527 pl->do_los = 1; 543 pl->do_los = 1;
528 }
529
530 /* Now we check to see if player is on adjacent
531 * maps to the one that changed and also within
532 * view. The tile_maps[] could be null, but in that
533 * case it should never match the pl->ob->map, so
534 * we want ever try to dereference any of the data in it.
535 *
536 * The logic for 0 and 3 is to see how far the player is
537 * from the edge of the map (height/width) - pl->ob->(x,y)
538 * and to add current position on this map - that gives a
539 * distance.
540 * For 1 and 2, we check to see how far the given
541 * coordinate (x,y) is from the corresponding edge,
542 * and then add the players location, which gives
543 * a distance.
544 */
545 else if (pl->ob->map == map->tile_map[0])
546 {
547 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2))
548 pl->do_los = 1;
549 }
550 else if (pl->ob->map == map->tile_map[2])
551 {
552 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2))
553 pl->do_los = 1;
554 }
555 else if (pl->ob->map == map->tile_map[1])
556 {
557 if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
558 pl->do_los = 1;
559 }
560 else if (pl->ob->map == map->tile_map[3])
561 {
562 if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
563 pl->do_los = 1;
564 }
565 } 544 }
545}
546
547static const int season_darkness[5][HOURS_PER_DAY] = {
548 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 */
549 { 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5 },
550 { 5, 5, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4 },
551 { 5, 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4 },
552 { 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4 },
553 { 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4 }
554};
555
556/*
557 * Tell players the time and compute the darkness level for all maps in the game.
558 * MUST be called exactly once per hour.
559 */
560void
561maptile::adjust_daylight ()
562{
563 timeofday_t tod;
564
565 get_tod (&tod);
566
567 // log the time to log-1 every hour, and to chat every day
568 {
569 char todbuf[512];
570
571 format_tod (todbuf, sizeof (todbuf), &tod);
572
573 for_all_players (pl)
574 pl->ns->send_msg (NDI_GREY, tod.hour == 15 ? CHAT_CHANNEL : LOG_CHANNEL, todbuf);
575 }
576
577 /* If the light level isn't changing, no reason to do all
578 * the work below.
579 */
580 sint8 new_darkness = season_darkness[tod.season][tod.hour];
581
582 if (new_darkness == maptile::outdoor_darkness)
583 return;
584
585 new_draw_info (NDI_GREY | NDI_UNIQUE | NDI_ALL, 1, 0,
586 new_darkness > maptile::outdoor_darkness
587 ? "It becomes darker."
588 : "It becomes brighter.");
589
590 maptile::outdoor_darkness = new_darkness;
591
592 // we simply update the los for all players, which is unnecessarily
593 // costly, but should do for the moment.
594 for_all_players (pl)
595 pl->do_los = 1;
566} 596}
567 597
568/* 598/*
569 * make_sure_seen: The object is supposed to be visible through walls, thus 599 * make_sure_seen: The object is supposed to be visible through walls, thus
570 * check if any players are nearby, and edit their LOS array. 600 * check if any players are nearby, and edit their LOS array.
574{ 604{
575 for_all_players (pl) 605 for_all_players (pl)
576 if (pl->ob->map == op->map && 606 if (pl->ob->map == op->map &&
577 pl->ob->y - pl->ns->mapy / 2 <= op->y && 607 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
578 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x) 608 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x)
579 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_X0] = 0; 609 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0;
580} 610}
581 611
582/* 612/*
583 * make_sure_not_seen: The object which is supposed to be visible through 613 * make_sure_not_seen: The object which is supposed to be visible through
584 * walls has just been removed from the map, so update the los of any 614 * walls has just been removed from the map, so update the los of any
591 if (pl->ob->map == op->map && 621 if (pl->ob->map == op->map &&
592 pl->ob->y - pl->ns->mapy / 2 <= op->y && 622 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
593 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x) 623 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x)
594 pl->do_los = 1; 624 pl->do_los = 1;
595} 625}
626

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines