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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines