1 /* NetHack 3.6 light.c $NHDT-Date: 1446191876 2015/10/30 07:57:56 $ $NHDT-Branch: master $:$NHDT-Revision: 1.28 $ */
2 /* Copyright (c) Dean Luick, 1994 */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* JNetHack Copyright */
6 /* For 3.6-, Copyright (c) SHIRAKATA Kentaro, 2002-2016 */
7 /* JNetHack may be freely redistributed. See license for details. */
10 #include "lev.h" /* for checking save modes */
13 * Mobile light sources.
15 * This implementation minimizes memory at the expense of extra
18 * Light sources are "things" that have a physical position and range.
19 * They have a type, which gives us information about them. Currently
20 * they are only attached to objects and monsters. Note well: the
21 * polymorphed-player handling assumes that both youmonst.m_id and
22 * youmonst.mx will always remain 0.
24 * Light sources, like timers, either follow game play (RANGE_GLOBAL) or
25 * stay on a level (RANGE_LEVEL). Light sources are unique by their
26 * (type, id) pair. For light sources attached to objects, this id
27 * is a pointer to the object.
29 * The major working function is do_light_sources(). It is called
30 * when the vision system is recreating its "could see" array. Here
31 * we add a flag (TEMP_LIT) to the array for all locations that are lit
32 * via a light source. The bad part of this is that we have to
33 * re-calculate the LOS of each light source every time the vision
34 * system runs. Even if the light sources and any topology (vision blocking
35 * positions) have not changed. The good part is that no extra memory
36 * is used, plus we don't have to figure out how far the sources have moved,
37 * or if the topology has changed.
39 * The structure of the save/restore mechanism is amazingly similar to
40 * the timer save/restore. This is because they both have the same
41 * principals of having pointers into objects that must be recalculated
42 * across saves and restores.
46 #define LSF_SHOW 0x1 /* display the light source */
47 #define LSF_NEEDS_FIXUP 0x2 /* need oid fixup */
49 static light_source *light_base = 0;
51 STATIC_DCL void FDECL(write_ls, (int, light_source *));
52 STATIC_DCL int FDECL(maybe_write_ls, (int, int, BOOLEAN_P));
54 /* imported from vision.c, for small circles */
55 extern char circle_data[];
56 extern char circle_start[];
58 /* Create a new light source. */
60 new_light_source(x, y, range, type, id)
67 if (range > MAX_RADIUS || range < 1) {
68 impossible("new_light_source: illegal range %d", range);
72 ls = (light_source *) alloc(sizeof(light_source));
74 ls->next = light_base;
83 vision_full_recalc = 1; /* make the source show up */
87 * Delete a light source. This assumes only one light source is attached
88 * to an object at a time.
91 del_light_source(type, id)
95 light_source *curr, *prev;
99 /* need to be prepared for dealing a with light source which
100 has only been partially restored during a level change
101 (in particular: chameleon vs prot. from shape changers) */
104 tmp_id.a_uint = id->a_obj->o_id;
107 tmp_id.a_uint = id->a_monst->m_id;
114 for (prev = 0, curr = light_base; curr; prev = curr, curr = curr->next) {
115 if (curr->type != type)
118 == ((curr->flags & LSF_NEEDS_FIXUP) ? tmp_id.a_obj : id->a_obj)) {
120 prev->next = curr->next;
122 light_base = curr->next;
124 free((genericptr_t) curr);
125 vision_full_recalc = 1;
129 impossible("del_light_source: not found type=%d, id=%s", type,
130 fmt_ptr((genericptr_t) id->a_obj));
133 /* Mark locations that are temporarily lit via mobile light sources. */
135 do_light_sources(cs_rows)
138 int x, y, min_x, max_x, max_y, offset;
140 short at_hero_range = 0;
144 for (ls = light_base; ls; ls = ls->next) {
145 ls->flags &= ~LSF_SHOW;
148 * Check for moved light sources. It may be possible to
149 * save some effort if an object has not moved, but not in
150 * the current setup -- we need to recalculate for every
153 if (ls->type == LS_OBJECT) {
154 if (get_obj_location(ls->id.a_obj, &ls->x, &ls->y, 0))
155 ls->flags |= LSF_SHOW;
156 } else if (ls->type == LS_MONSTER) {
157 if (get_mon_location(ls->id.a_monst, &ls->x, &ls->y, 0))
158 ls->flags |= LSF_SHOW;
161 /* minor optimization: don't bother with duplicate light sources */
163 if (ls->x == u.ux && ls->y == u.uy) {
164 if (at_hero_range >= ls->range)
165 ls->flags &= ~LSF_SHOW;
167 at_hero_range = ls->range;
170 if (ls->flags & LSF_SHOW) {
172 * Walk the points in the circle and see if they are
173 * visible from the center. If so, mark'em.
175 * Kevin's tests indicated that doing this brute-force
176 * method is faster for radius <= 3 (or so).
178 limits = circle_ptr(ls->range);
179 if ((max_y = (ls->y + ls->range)) >= ROWNO)
181 if ((y = (ls->y - ls->range)) < 0)
183 for (; y <= max_y; y++) {
185 offset = limits[abs(y - ls->y)];
186 if ((min_x = (ls->x - offset)) < 0)
188 if ((max_x = (ls->x + offset)) >= COLNO)
191 if (ls->x == u.ux && ls->y == u.uy) {
193 * If the light source is located at the hero, then
194 * we can use the COULD_SEE bits already calculated
195 * by the vision system. More importantly than
196 * this optimization, is that it allows the vision
197 * system to correct problems with clear_path().
198 * The function clear_path() is a simple LOS
199 * path checker that doesn't go out of its way
200 * make things look "correct". The vision system
203 for (x = min_x; x <= max_x; x++)
204 if (row[x] & COULD_SEE)
207 for (x = min_x; x <= max_x; x++)
208 if ((ls->x == x && ls->y == y)
209 || clear_path((int) ls->x, (int) ls->y, x, y))
217 /* (mon->mx == 0) implies migrating */
218 #define mon_is_local(mon) ((mon)->mx > 0)
221 find_mid(nid, fmflags)
229 if (fmflags & FM_FMON)
230 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
231 if (!DEADMONSTER(mtmp) && mtmp->m_id == nid)
233 if (fmflags & FM_MIGRATE)
234 for (mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon)
235 if (mtmp->m_id == nid)
237 if (fmflags & FM_MYDOGS)
238 for (mtmp = mydogs; mtmp; mtmp = mtmp->nmon)
239 if (mtmp->m_id == nid)
241 return (struct monst *) 0;
244 /* Save all light sources of the given range. */
246 save_light_sources(fd, mode, range)
249 int count, actual, is_global;
250 light_source **prev, *curr;
252 if (perform_bwrite(mode)) {
253 count = maybe_write_ls(fd, range, FALSE);
254 bwrite(fd, (genericptr_t) &count, sizeof count);
255 actual = maybe_write_ls(fd, range, TRUE);
257 panic("counted %d light sources, wrote %d! [range=%d]", count,
261 if (release_data(mode)) {
262 for (prev = &light_base; (curr = *prev) != 0;) {
263 if (!curr->id.a_monst) {
264 impossible("save_light_sources: no id! [range=%d]", range);
267 switch (curr->type) {
269 is_global = !obj_is_local(curr->id.a_obj);
272 is_global = !mon_is_local(curr->id.a_monst);
276 impossible("save_light_sources: bad type (%d) [range=%d]",
280 /* if global and not doing local, or vice versa, remove it */
281 if (is_global ^ (range == RANGE_LEVEL)) {
283 free((genericptr_t) curr);
285 prev = &(*prev)->next;
292 * Pull in the structures from disk, but don't recalculate the object
296 restore_light_sources(fd)
302 /* restore elements */
303 mread(fd, (genericptr_t) &count, sizeof count);
305 while (count-- > 0) {
306 ls = (light_source *) alloc(sizeof(light_source));
307 mread(fd, (genericptr_t) ls, sizeof(light_source));
308 ls->next = light_base;
313 /* Relink all lights that are so marked. */
315 relink_light_sources(ghostly)
322 for (ls = light_base; ls; ls = ls->next) {
323 if (ls->flags & LSF_NEEDS_FIXUP) {
324 if (ls->type == LS_OBJECT || ls->type == LS_MONSTER) {
326 if (!lookup_id_mapping(ls->id.a_uint, &nid))
327 impossible("relink_light_sources: no id mapping");
330 if (ls->type == LS_OBJECT) {
332 ls->id.a_obj = find_oid(nid);
335 ls->id.a_monst = find_mid(nid, FM_EVERYWHERE);
338 impossible("relink_light_sources: cant find %c_id %d",
341 impossible("relink_light_sources: bad type (%d)", ls->type);
343 ls->flags &= ~LSF_NEEDS_FIXUP;
349 * Part of the light source save routine. Count up the number of light
350 * sources that would be written. If write_it is true, actually write
351 * the light source out.
354 maybe_write_ls(fd, range, write_it)
358 int count = 0, is_global;
361 for (ls = light_base; ls; ls = ls->next) {
362 if (!ls->id.a_monst) {
363 impossible("maybe_write_ls: no id! [range=%d]", range);
368 is_global = !obj_is_local(ls->id.a_obj);
371 is_global = !mon_is_local(ls->id.a_monst);
375 impossible("maybe_write_ls: bad type (%d) [range=%d]", ls->type,
379 /* if global and not doing local, or vice versa, count it */
380 if (is_global ^ (range == RANGE_LEVEL)) {
391 light_sources_sanity_check()
398 for (ls = light_base; ls; ls = ls->next) {
400 panic("insane light source: no id!");
401 if (ls->type == LS_OBJECT) {
402 otmp = (struct obj *) ls->id.a_obj;
404 if (find_oid(auint) != otmp)
405 panic("insane light source: can't find obj #%u!", auint);
406 } else if (ls->type == LS_MONSTER) {
407 mtmp = (struct monst *) ls->id.a_monst;
409 if (find_mid(auint, FM_EVERYWHERE) != mtmp)
410 panic("insane light source: can't find mon #%u!", auint);
412 panic("insane light source: bad ls type %d", ls->type);
417 /* Write a light source structure to disk. */
427 if (ls->type == LS_OBJECT || ls->type == LS_MONSTER) {
428 if (ls->flags & LSF_NEEDS_FIXUP) {
429 bwrite(fd, (genericptr_t) ls, sizeof(light_source));
431 /* replace object pointer with id for write, then put back */
433 if (ls->type == LS_OBJECT) {
436 ls->id.a_uint = otmp->o_id;
437 if (find_oid((unsigned) ls->id.a_uint) != otmp)
438 impossible("write_ls: can't find obj #%u!",
440 } else { /* ls->type == LS_MONSTER */
441 mtmp = (struct monst *) ls->id.a_monst;
443 ls->id.a_uint = mtmp->m_id;
444 if (find_mid((unsigned) ls->id.a_uint, FM_EVERYWHERE) != mtmp)
445 impossible("write_ls: can't find mon #%u!",
448 ls->flags |= LSF_NEEDS_FIXUP;
449 bwrite(fd, (genericptr_t) ls, sizeof(light_source));
451 ls->flags &= ~LSF_NEEDS_FIXUP;
454 impossible("write_ls: bad type (%d)", ls->type);
458 /* Change light source's ID from src to dest. */
460 obj_move_light_source(src, dest)
461 struct obj *src, *dest;
465 for (ls = light_base; ls; ls = ls->next)
466 if (ls->type == LS_OBJECT && ls->id.a_obj == src)
472 /* return true if there exist any light sources */
476 return (boolean) (light_base != (light_source *) 0);
480 * Snuff an object light source if at (x,y). This currently works
481 * only for burning light sources.
484 snuff_light_source(x, y)
490 for (ls = light_base; ls; ls = ls->next)
492 * Is this position check valid??? Can I assume that the positions
493 * will always be correct because the objects would have been
494 * updated with the last vision update? [Is that recent enough???]
496 if (ls->type == LS_OBJECT && ls->x == x && ls->y == y) {
498 if (obj_is_burning(obj)) {
499 /* The only way to snuff Sunsword is to unwield it. Darkness
500 * scrolls won't affect it. (If we got here because it was
501 * dropped or thrown inside a monster, this won't matter
502 * anyway because it will go out when dropped.)
504 if (artifact_light(obj))
506 end_burn(obj, obj->otyp != MAGIC_LAMP);
508 * The current ls element has just been removed (and
509 * ls->next is now invalid). Return assuming that there
510 * is only one light source attached to each object.
517 /* Return TRUE if object sheds any light at all. */
522 /* so far, only burning objects shed light */
523 return obj_is_burning(obj);
526 /* Return TRUE if sheds light AND will be snuffed by end_burn(). */
531 return (boolean) (obj->lamplit && (obj->otyp == MAGIC_LAMP
533 || artifact_light(obj)));
536 /* copy the light source(s) attached to src, and attach it/them to dest */
538 obj_split_light_source(src, dest)
539 struct obj *src, *dest;
541 light_source *ls, *new_ls;
543 for (ls = light_base; ls; ls = ls->next)
544 if (ls->type == LS_OBJECT && ls->id.a_obj == src) {
546 * Insert the new source at beginning of list. This will
547 * never interfere us walking down the list - we are already
548 * past the insertion point.
550 new_ls = (light_source *) alloc(sizeof(light_source));
552 if (Is_candle(src)) {
553 /* split candles may emit less light than original group */
554 ls->range = candle_light_range(src);
555 new_ls->range = candle_light_range(dest);
556 vision_full_recalc = 1; /* in case range changed */
558 new_ls->id.a_obj = dest;
559 new_ls->next = light_base;
561 dest->lamplit = 1; /* now an active light source */
565 /* light source `src' has been folded into light source `dest';
566 used for merging lit candles and adding candle(s) to lit candelabrum */
568 obj_merge_light_sources(src, dest)
569 struct obj *src, *dest;
573 /* src == dest implies adding to candelabrum */
575 end_burn(src, TRUE); /* extinguish candles */
577 for (ls = light_base; ls; ls = ls->next)
578 if (ls->type == LS_OBJECT && ls->id.a_obj == dest) {
579 ls->range = candle_light_range(dest);
580 vision_full_recalc = 1; /* in case range changed */
585 /* light source `obj' is being made brighter or dimmer */
587 obj_adjust_light_radius(obj, new_radius)
593 for (ls = light_base; ls; ls = ls->next)
594 if (ls->type == LS_OBJECT && ls->id.a_obj == obj) {
595 if (new_radius != ls->range)
596 vision_full_recalc = 1;
597 ls->range = new_radius;
600 impossible("obj_adjust_light_radius: can't find %s", xname(obj));
603 /* Candlelight is proportional to the number of candles;
604 minimum range is 2 rather than 1 for playability. */
606 candle_light_range(obj)
611 if (obj->otyp == CANDELABRUM_OF_INVOCATION) {
613 * The special candelabrum emits more light than the
614 * corresponding number of candles would.
615 * 1..3 candles, range 2 (minimum range);
616 * 4..6 candles, range 3 (normal lamp range);
617 * 7 candles, range 4 (bright).
619 radius = (obj->spe < 4) ? 2 : (obj->spe < 7) ? 3 : 4;
620 } else if (Is_candle(obj)) {
622 * Range is incremented by powers of 7 so that it will take
623 * wizard mode quantities of candles to get more light than
624 * from a lamp, without imposing an arbitrary limit.
625 * 1..6 candles, range 2;
626 * 7..48 candles, range 3;
627 * 49..342 candles, range 4; &c.
631 radius = 1; /* always incremented at least once */
637 /* we're only called for lit candelabrum or candles */
638 /* impossible("candlelight for %d?", obj->otyp); */
639 radius = 3; /* lamp's value */
644 /* light emitting artifact's range depends upon its curse/bless state */
646 arti_light_radius(obj)
650 * Used by begin_burn() when setting up a new light source
651 * (obj->lamplit will already be set by this point) and
652 * also by bless()/unbless()/uncurse()/curse() to decide
653 * whether to call obj_adjust_light_radius().
656 /* sanity check [simplifies usage by bless()/curse()/&c] */
657 if (!obj->lamplit || !artifact_light(obj))
660 /* cursed radius of 1 is not noticeable for an item that's
661 carried by the hero but is if it's carried by a monster
662 or left lit on the floor (not applicable for Sunsword) */
663 return (obj->blessed ? 3 : !obj->cursed ? 2 : 1);
666 /* adverb describing lit artifact's light; depends on curse/bless state */
668 arti_light_description(obj)
671 switch (arti_light_radius(obj)) {
674 return "brilliantly"; /* blessed */
676 return "
\83L
\83\89\83L
\83\89\82Æ"; /* blessed */
680 return "brightly"; /* uncursed */
682 return "
\96¾
\82é
\82"; /* uncursed */
686 return "dimly"; /* cursed */
688 return "
\94\96\88Ã
\82"; /* cursed */
696 return "
\95s
\8ev
\8bc
\82É";
706 win = create_nhwindow(NHW_MENU); /* corner text window */
710 Sprintf(buf, "Mobile light sources: hero @ (%2d,%2d)", u.ux, u.uy);
715 putstr(win, 0, "location range flags type id");
716 putstr(win, 0, "-------- ----- ------ ---- -------");
717 for (ls = light_base; ls; ls = ls->next) {
718 Sprintf(buf, " %2d,%2d %2d 0x%04x %s %s", ls->x, ls->y,
719 ls->range, ls->flags,
720 (ls->type == LS_OBJECT
722 : ls->type == LS_MONSTER
723 ? (mon_is_local(ls->id.a_monst)
725 : (ls->id.a_monst == &youmonst)
727 /* migrating monster */
730 fmt_ptr(ls->id.a_void));
734 putstr(win, 0, "<none>");
736 display_nhwindow(win, FALSE);
737 destroy_nhwindow(win);