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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines