Source:NetHack 3.4.3/src/region.c

From NetHackWiki
(Redirected from Source:Ref/inside region)
Jump to: navigation, search

Below is the full text to src/region.c from NetHack 3.4.3. To link to a particular line, write [[region.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)region.c	3.4	2002/10/15	*/
  2. /* Copyright (c) 1996 by Jean-Christophe Collet	 */
  3. /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

  1. #include "hack.h"
  2. #include "lev.h"
  3.  
  4. /*
  5. * This should really go into the level structure, but
  6. * I'll start here for ease. It *WILL* move into the level
  7. * structure eventually.
  8. */
  9.  
  10. static NhRegion **regions;
  11. static int n_regions = 0;
  12. static int max_regions = 0;
  13.  
  14. #define NO_CALLBACK (-1)
  15.  
  16. boolean FDECL(inside_gas_cloud, (genericptr,genericptr));
  17. boolean FDECL(expire_gas_cloud, (genericptr,genericptr));
  18. boolean FDECL(inside_rect, (NhRect *,int,int));
  19. boolean FDECL(inside_region, (NhRegion *,int,int));
  20. NhRegion *FDECL(create_region, (NhRect *,int));
  21. void FDECL(add_rect_to_reg, (NhRegion *,NhRect *));
  22. void FDECL(add_mon_to_reg, (NhRegion *,struct monst *));
  23. void FDECL(remove_mon_from_reg, (NhRegion *,struct monst *));
  24. boolean FDECL(mon_in_region, (NhRegion *,struct monst *));
  25.  
  26. #if 0
  27. NhRegion *FDECL(clone_region, (NhRegion *));
  28. #endif
  29. void FDECL(free_region, (NhRegion *));
  30. void FDECL(add_region, (NhRegion *));
  31. void FDECL(remove_region, (NhRegion *));
  32.  
  33. #if 0
  34. void FDECL(replace_mon_regions, (struct monst *,struct monst *));
  35. void FDECL(remove_mon_from_regions, (struct monst *));
  36. NhRegion *FDECL(create_msg_region, (XCHAR_P,XCHAR_P,XCHAR_P,XCHAR_P,
  37. 				    const char *,const char *));
  38. boolean FDECL(enter_force_field, (genericptr,genericptr));
  39. NhRegion *FDECL(create_force_field, (XCHAR_P,XCHAR_P,int,int));
  40. #endif
  41.  
  42. static void FDECL(reset_region_mids, (NhRegion *));
  43.  
  44. static callback_proc callbacks[] = {
  45. #define INSIDE_GAS_CLOUD 0
  46. inside_gas_cloud,
  47. #define EXPIRE_GAS_CLOUD 1
  48. expire_gas_cloud
  49. };

inside_rect

  1. /* Should be inlined. */
  2. boolean
  3. inside_rect(r, x, y)
  4. NhRect *r;
  5. int x, y;
  6. {
  7. return (x >= r->lx && x <= r->hx && y >= r->ly && y <= r->hy);
  8. }

inside_region

  1. /*
  2. * Check if a point is inside a region.
  3. */
  4. boolean
  5. inside_region(reg, x, y)
  6. NhRegion *reg;
  7. int x, y;
  8. {
  9. int i;
  10.  
  11. if (reg == NULL || !inside_rect(&(reg->bounding_box), x, y))
  12. 	return FALSE;
  13. for (i = 0; i < reg->nrects; i++)
  14. 	if (inside_rect(&(reg->rects[i]), x, y))
  15. 	    return TRUE;
  16. return FALSE;
  17. }

create_region

  1. /*
  2. * Create a region. It does not activate it.
  3. */
  4. NhRegion *
  5. create_region(rects, nrect)
  6. NhRect *rects;
  7. int nrect;
  8. {
  9. int i;
  10. NhRegion *reg;
  11.  
  12. reg = (NhRegion *) alloc(sizeof (NhRegion));
  13. /* Determines bounding box */
  14. if (nrect > 0) {
  15. 	reg->bounding_box = rects[0];
  16. } else {
  17. 	reg->bounding_box.lx = 99;
  18. 	reg->bounding_box.ly = 99;
  19. 	reg->bounding_box.hx = 0;
  20. 	reg->bounding_box.hy = 0;
  21. }
  22. reg->nrects = nrect;
  23. reg->rects = nrect > 0 ? (NhRect *)alloc((sizeof (NhRect)) * nrect) : NULL;
  24. for (i = 0; i < nrect; i++) {
  25. 	if (rects[i].lx < reg->bounding_box.lx)
  26. 	    reg->bounding_box.lx = rects[i].lx;
  27. 	if (rects[i].ly < reg->bounding_box.ly)
  28. 	    reg->bounding_box.ly = rects[i].ly;
  29. 	if (rects[i].hx > reg->bounding_box.hx)
  30. 	    reg->bounding_box.hx = rects[i].hx;
  31. 	if (rects[i].hy > reg->bounding_box.hy)
  32. 	    reg->bounding_box.hy = rects[i].hy;
  33. 	reg->rects[i] = rects[i];
  34. }
  35. reg->ttl = -1;		/* Defaults */
  36. reg->attach_2_u = FALSE;
  37. reg->attach_2_m = 0;
  38. /* reg->attach_2_o = NULL; */
  39. reg->enter_msg = NULL;
  40. reg->leave_msg = NULL;
  41. reg->expire_f = NO_CALLBACK;
  42. reg->enter_f = NO_CALLBACK;
  43. reg->can_enter_f = NO_CALLBACK;
  44. reg->leave_f = NO_CALLBACK;
  45. reg->can_leave_f = NO_CALLBACK;
  46. reg->inside_f = NO_CALLBACK;
  47. clear_hero_inside(reg);
  48. clear_heros_fault(reg);
  49. reg->n_monst = 0;
  50. reg->max_monst = 0;
  51. reg->monsters = NULL;
  52. reg->arg = NULL;
  53. return reg;
  54. }

add_rect_to_reg

  1. /*
  2. * Add rectangle to region.
  3. */
  4. void
  5. add_rect_to_reg(reg, rect)
  6. NhRegion *reg;
  7. NhRect *rect;
  8. {
  9. NhRect *tmp_rect;
  10.  
  11. tmp_rect = (NhRect *) alloc(sizeof (NhRect) * (reg->nrects + 1));
  12. if (reg->nrects > 0) {
  13. 	(void) memcpy((genericptr_t) tmp_rect, (genericptr_t) reg->rects,
  14. 		      (sizeof (NhRect) * reg->nrects));
  15. 	free((genericptr_t) reg->rects);
  16. }
  17. tmp_rect[reg->nrects] = *rect;
  18. reg->nrects++;
  19. reg->rects = tmp_rect;
  20. /* Update bounding box if needed */
  21. if (reg->bounding_box.lx > rect->lx)
  22. 	reg->bounding_box.lx = rect->lx;
  23. if (reg->bounding_box.ly > rect->ly)
  24. 	reg->bounding_box.ly = rect->ly;
  25. if (reg->bounding_box.hx < rect->hx)
  26. 	reg->bounding_box.hx = rect->hx;
  27. if (reg->bounding_box.hy < rect->hy)
  28. 	reg->bounding_box.hy = rect->hy;
  29. }

add_mon_to_reg

  1. /*
  2. * Add a monster to the region
  3. */
  4. void
  5. add_mon_to_reg(reg, mon)
  6. NhRegion *reg;
  7. struct monst *mon;
  8. {
  9. int i;
  10. unsigned *tmp_m;
  11.  
  12. if (reg->max_monst <= reg->n_monst) {
  13. 	tmp_m = (unsigned *)
  14. 		    alloc(sizeof (unsigned) * (reg->max_monst + MONST_INC));
  15. 	if (reg->max_monst > 0) {
  16. 	    for (i = 0; i < reg->max_monst; i++)
  17. 		tmp_m[i] = reg->monsters[i];
  18. 	    free((genericptr_t) reg->monsters);
  19. 	}
  20. 	reg->monsters = tmp_m;
  21. 	reg->max_monst += MONST_INC;
  22. }
  23. reg->monsters[reg->n_monst++] = mon->m_id;
  24. }

remove_mon_from_reg

  1. /*
  2. * Remove a monster from the region list (it left or died...)
  3. */
  4. void
  5. remove_mon_from_reg(reg, mon)
  6. NhRegion *reg;
  7. struct monst *mon;
  8. {
  9. register int i;
  10.  
  11. for (i = 0; i < reg->n_monst; i++)
  12. 	if (reg->monsters[i] == mon->m_id) {
  13. 	    reg->n_monst--;
  14. 	    reg->monsters[i] = reg->monsters[reg->n_monst];
  15. 	    return;
  16. 	}
  17. }

mon_in_region

  1. /*
  2. * Check if a monster is inside the region.
  3. * It's probably quicker to check with the region internal list
  4. * than to check for coordinates.
  5. */
  6. boolean
  7. mon_in_region(reg, mon)
  8. NhRegion *reg;
  9. struct monst *mon;
  10. {
  11. int i;
  12.  
  13. for (i = 0; i < reg->n_monst; i++)
  14. 	if (reg->monsters[i] == mon->m_id)
  15. 	    return TRUE;
  16. return FALSE;
  17. }
  18.  
  19. #if 0
  20. /* not yet used */

clone_region

  1. /*
  2. * Clone (make a standalone copy) the region.
  3. */
  4. NhRegion *
  5. clone_region(reg)
  6. NhRegion *reg;
  7. {
  8. NhRegion *ret_reg;
  9.  
  10. ret_reg = create_region(reg->rects, reg->nrects);
  11. ret_reg->ttl = reg->ttl;
  12. ret_reg->attach_2_u = reg->attach_2_u;
  13. ret_reg->attach_2_m = reg->attach_2_m;
  14. /* ret_reg->attach_2_o = reg->attach_2_o; */
  15. ret_reg->expire_f = reg->expire_f;
  16. ret_reg->enter_f = reg->enter_f;
  17. ret_reg->can_enter_f = reg->can_enter_f;
  18. ret_reg->leave_f = reg->leave_f;
  19. ret_reg->can_leave_f = reg->can_leave_f;
  20. ret_reg->player_flags = reg->player_flags;	/* set/clear_hero_inside,&c*/
  21. ret_reg->n_monst = reg->n_monst;
  22. if (reg->n_monst > 0) {
  23. 	ret_reg->monsters = (unsigned *)
  24. 				alloc((sizeof (unsigned)) * reg->n_monst);
  25. 	(void) memcpy((genericptr_t) ret_reg->monsters, (genericptr_t) reg->monsters,
  26. 		      sizeof (unsigned) * reg->n_monst);
  27. } else
  28. 	ret_reg->monsters = NULL;
  29. return ret_reg;
  30. }
  31.  
  32. #endif	/*0*/

free_region

  1. /*
  2. * Free mem from region.
  3. */
  4. void
  5. free_region(reg)
  6. NhRegion *reg;
  7. {
  8. if (reg) {
  9. 	if (reg->rects)
  10. 	    free((genericptr_t) reg->rects);
  11. 	if (reg->monsters)
  12. 	    free((genericptr_t) reg->monsters);
  13. 	free((genericptr_t) reg);
  14. }
  15. }

add_region

  1. /*
  2. * Add a region to the list.
  3. * This actually activates the region.
  4. */
  5. void
  6. add_region(reg)
  7. NhRegion *reg;
  8. {
  9. NhRegion **tmp_reg;
  10. int i, j;
  11.  
  12. if (max_regions <= n_regions) {
  13. 	tmp_reg = regions;
  14. 	regions = (NhRegion **)alloc(sizeof (NhRegion *) * (max_regions + 10));
  15. 	if (max_regions > 0) {
  16. 	    (void) memcpy((genericptr_t) regions, (genericptr_t) tmp_reg,
  17. 			  max_regions * sizeof (NhRegion *));
  18. 	    free((genericptr_t) tmp_reg);
  19. 	}
  20. 	max_regions += 10;
  21. }
  22. regions[n_regions] = reg;
  23. n_regions++;
  24. /* Check for monsters inside the region */
  25. for (i = reg->bounding_box.lx; i <= reg->bounding_box.hx; i++)
  26. 	for (j = reg->bounding_box.ly; j <= reg->bounding_box.hy; j++) {
  27. 	    /* Some regions can cross the level boundaries */
  28. 	    if (!isok(i,j))
  29. 		continue;
  30. 	    if (MON_AT(i, j) && inside_region(reg, i, j))
  31. 		add_mon_to_reg(reg, level.monsters[i][j]);
  32. 	    if (reg->visible && cansee(i, j))
  33. 		newsym(i, j);
  34. 	}
  35. /* Check for player now... */
  36. if (inside_region(reg, u.ux, u.uy)) 
  37. 	set_hero_inside(reg);
  38. else
  39. 	clear_hero_inside(reg);
  40. }

remove_region

  1. /*
  2. * Remove a region from the list & free it.
  3. */
  4. void
  5. remove_region(reg)
  6. NhRegion *reg;
  7. {
  8. register int i, x, y;
  9.  
  10. for (i = 0; i < n_regions; i++)
  11. 	if (regions[i] == reg)
  12. 	    break;
  13. if (i == n_regions)
  14. 	return;
  15.  
  16. /* Update screen if necessary */
  17. if (reg->visible)
  18. 	for (x = reg->bounding_box.lx; x <= reg->bounding_box.hx; x++)
  19. 	    for (y = reg->bounding_box.ly; y <= reg->bounding_box.hy; y++)
  20. 		if (isok(x,y) && inside_region(reg, x, y) && cansee(x, y))
  21. 		    newsym(x, y);
  22.  
  23. free_region(reg);
  24. regions[i] = regions[n_regions - 1];
  25. regions[n_regions - 1] = (NhRegion *) 0;
  26. n_regions--;
  27. }

clear_regions

  1. /*
  2. * Remove all regions and clear all related data (This must be down
  3. * when changing level, for instance).
  4. */
  5. void
  6. clear_regions()
  7. {
  8. register int i;
  9.  
  10. for (i = 0; i < n_regions; i++)
  11. 	free_region(regions[i]);
  12. n_regions = 0;
  13. if (max_regions > 0)
  14. 	free((genericptr_t) regions);
  15. max_regions = 0;
  16. regions = NULL;
  17. }

run_regions

  1. /*
  2. * This function is called every turn.
  3. * It makes the regions age, if necessary and calls the appropriate
  4. * callbacks when needed.
  5. */
  6. void
  7. run_regions()
  8. {
  9. register int i, j, k;
  10. int f_indx;
  11.  
  12. /* End of life ? */
  13. /* Do it backward because the array will be modified */
  14. for (i = n_regions - 1; i >= 0; i--) {
  15. 	if (regions[i]->ttl == 0) {
  16. 	    if ((f_indx = regions[i]->expire_f) == NO_CALLBACK ||
  17. 		(*callbacks[f_indx])(regions[i], (genericptr_t) 0))
  18. 		remove_region(regions[i]);
  19. 	}
  20. }
  21.  
  22. /* Process remaining regions */
  23. for (i = 0; i < n_regions; i++) {
  24. 	/* Make the region age */
  25. 	if (regions[i]->ttl > 0)
  26. 	    regions[i]->ttl--;
  27. 	/* Check if player is inside region */
  28. 	f_indx = regions[i]->inside_f;
  29. 	if (f_indx != NO_CALLBACK && hero_inside(regions[i]))
  30. 	    (void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0);
  31. 	/* Check if any monster is inside region */
  32. 	if (f_indx != NO_CALLBACK) {
  33. 	    for (j = 0; j < regions[i]->n_monst; j++) {
  34. 		struct monst *mtmp = find_mid(regions[i]->monsters[j], FM_FMON);
  35.  
  36. 		if (!mtmp || mtmp->mhp <= 0 ||
  37. 				(*callbacks[f_indx])(regions[i], mtmp)) {
  38. 		    /* The monster died, remove it from list */
  39. 		    k = (regions[i]->n_monst -= 1);
  40. 		    regions[i]->monsters[j] = regions[i]->monsters[k];
  41. 		    regions[i]->monsters[k] = 0;
  42. 		    --j;    /* current slot has been reused; recheck it next */
  43. 		}
  44. 	    }
  45. 	}
  46. }
  47. }

in_out_region

  1. /*
  2. * check whether player enters/leaves one or more regions.
  3. */
  4. boolean
  5. in_out_region(x, y)
  6. xchar
  7. x, y;
  8. {
  9. int i, f_indx;
  10.  
  11. /* First check if we can do the move */
  12. for (i = 0; i < n_regions; i++) {
  13. 	if (inside_region(regions[i], x, y)
  14. 	    && !hero_inside(regions[i]) && !regions[i]->attach_2_u) {
  15. 	    if ((f_indx = regions[i]->can_enter_f) != NO_CALLBACK)
  16. 		if (!(*callbacks[f_indx])(regions[i], (genericptr_t) 0))
  17. 		    return FALSE;
  18. 	} else
  19. 	    if (hero_inside(regions[i])
  20. 		&& !inside_region(regions[i], x, y)
  21. 		&& !regions[i]->attach_2_u) {
  22. 	    if ((f_indx = regions[i]->can_leave_f) != NO_CALLBACK)
  23. 		if (!(*callbacks[f_indx])(regions[i], (genericptr_t) 0))
  24. 		    return FALSE;
  25. 	}
  26. }
  27.  
  28. /* Callbacks for the regions we do leave */
  29. for (i = 0; i < n_regions; i++)
  30. 	if (hero_inside(regions[i]) &&
  31. 		!regions[i]->attach_2_u && !inside_region(regions[i], x, y)) {
  32. 	    clear_hero_inside(regions[i]);
  33. 	    if (regions[i]->leave_msg != NULL)
  34. 		pline(regions[i]->leave_msg);
  35. 	    if ((f_indx = regions[i]->leave_f) != NO_CALLBACK)
  36. 		(void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0);
  37. 	}
  38.  
  39. /* Callbacks for the regions we do enter */
  40. for (i = 0; i < n_regions; i++)
  41. 	if (!hero_inside(regions[i]) &&
  42. 		!regions[i]->attach_2_u && inside_region(regions[i], x, y)) {
  43. 	    set_hero_inside(regions[i]);
  44. 	    if (regions[i]->enter_msg != NULL)
  45. 		pline(regions[i]->enter_msg);
  46. 	    if ((f_indx = regions[i]->enter_f) != NO_CALLBACK)
  47. 		(void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0);
  48. 	}
  49. return TRUE;
  50. }

m_in_out_region

  1. /*
  2. * check wether a monster enters/leaves one or more region.
  3. */
  4. boolean
  5. m_in_out_region(mon, x, y)
  6. struct monst *mon;
  7. xchar x, y;
  8. {
  9. int i, f_indx;
  10.  
  11. /* First check if we can do the move */
  12. for (i = 0; i < n_regions; i++) {
  13. 	if (inside_region(regions[i], x, y) &&
  14. 		!mon_in_region(regions[i], mon) &&
  15. 		regions[i]->attach_2_m != mon->m_id) {
  16. 	    if ((f_indx = regions[i]->can_enter_f) != NO_CALLBACK)
  17. 		if (!(*callbacks[f_indx])(regions[i], mon))
  18. 		    return FALSE;
  19. 	} else if (mon_in_region(regions[i], mon) &&
  20. 		!inside_region(regions[i], x, y) &&
  21. 		regions[i]->attach_2_m != mon->m_id) {
  22. 	    if ((f_indx = regions[i]->can_leave_f) != NO_CALLBACK)
  23. 		if (!(*callbacks[f_indx])(regions[i], mon))
  24. 		    return FALSE;
  25. 	}
  26. }
  27.  
  28. /* Callbacks for the regions we do leave */
  29. for (i = 0; i < n_regions; i++)
  30. 	if (mon_in_region(regions[i], mon) &&
  31. 		regions[i]->attach_2_m != mon->m_id &&
  32. 		!inside_region(regions[i], x, y)) {
  33. 	    remove_mon_from_reg(regions[i], mon);
  34. 	    if ((f_indx = regions[i]->leave_f) != NO_CALLBACK)
  35. 		(void) (*callbacks[f_indx])(regions[i], mon);
  36. 	}
  37.  
  38. /* Callbacks for the regions we do enter */
  39. for (i = 0; i < n_regions; i++)
  40. 	if (!hero_inside(regions[i]) &&
  41. 		!regions[i]->attach_2_u && inside_region(regions[i], x, y)) {
  42. 	    add_mon_to_reg(regions[i], mon);
  43. 	    if ((f_indx = regions[i]->enter_f) != NO_CALLBACK)
  44. 		(void) (*callbacks[f_indx])(regions[i], mon);
  45. 	}
  46. return TRUE;
  47. }

update_player_regions

  1. /*
  2. * Checks player's regions after a teleport for instance.
  3. */
  4. void
  5. update_player_regions()
  6. {
  7. register int i;
  8.  
  9. for (i = 0; i < n_regions; i++)
  10. 	if (!regions[i]->attach_2_u && inside_region(regions[i], u.ux, u.uy))
  11. 	    set_hero_inside(regions[i]);
  12. 	else
  13. 	    clear_hero_inside(regions[i]);
  14. }

update_monster_region

  1. /*
  2. * Ditto for a specified monster.
  3. */
  4. void
  5. update_monster_region(mon)
  6. struct monst *mon;
  7. {
  8. register int i;
  9.  
  10. for (i = 0; i < n_regions; i++) {
  11. 	if (inside_region(regions[i], mon->mx, mon->my)) {
  12. 	    if (!mon_in_region(regions[i], mon))
  13. 		add_mon_to_reg(regions[i], mon);
  14. 	} else {
  15. 	    if (mon_in_region(regions[i], mon))
  16. 		remove_mon_from_reg(regions[i], mon);
  17. 	}
  18. }
  19. }
  20.  
  21. #if 0

replace_mon_regions

  1. /* not yet used */
  2.  
  3. /*
  4. * Change monster pointer in regions
  5. * This happens, for instance, when a monster grows and
  6. * need a new structure (internally that is).
  7. */
  8. void
  9. replace_mon_regions(monold, monnew)
  10. struct monst *monold, *monnew;
  11. {
  12. register int i;
  13.  
  14. for (i = 0; i < n_regions; i++)
  15. 	if (mon_in_region(regions[i], monold)) {
  16. 	    remove_mon_from_reg(regions[i], monold);
  17. 	    add_mon_to_reg(regions[i], monnew);
  18. 	}
  19. }

remove_mon_from_regions

  1. /*
  2. * Remove monster from all regions it was in (ie monster just died)
  3. */
  4. void
  5. remove_mon_from_regions(mon)
  6. struct monst *mon;
  7. {
  8. register int i;
  9.  
  10. for (i = 0; i < n_regions; i++)
  11. 	if (mon_in_region(regions[i], mon))
  12. 	    remove_mon_from_reg(regions[i], mon);
  13. }
  14.  
  15. #endif	/*0*/

visible_region_at

  1. /*
  2. * Check if a spot is under a visible region (eg: gas cloud).
  3. * Returns NULL if not, otherwise returns region.
  4. */
  5. NhRegion *
  6. visible_region_at(x, y)
  7. xchar x, y;
  8. {
  9. register int i;
  10.  
  11. for (i = 0; i < n_regions; i++)
  12. 	if (inside_region(regions[i], x, y) && regions[i]->visible &&
  13. 		regions[i]->ttl != 0)
  14. 	    return regions[i];
  15. return (NhRegion *) 0;
  16. }

show_region

  1. void
  2. show_region(reg, x, y)
  3. NhRegion *reg;
  4. xchar x, y;
  5. {
  6. show_glyph(x, y, reg->glyph);
  7. }

save_regions

  1. /**
  2. * save_regions :
  3. */
  4. void
  5. save_regions(fd, mode)
  6. int fd;
  7. int mode;
  8. {
  9. int i, j;
  10. unsigned n;
  11.  
  12. if (!perform_bwrite(mode)) goto skip_lots;
  13.  
  14. bwrite(fd, (genericptr_t) &moves, sizeof (moves));	/* timestamp */
  15. bwrite(fd, (genericptr_t) &n_regions, sizeof (n_regions));
  16. for (i = 0; i < n_regions; i++) {
  17. 	bwrite(fd, (genericptr_t) &regions[i]->bounding_box, sizeof (NhRect));
  18. 	bwrite(fd, (genericptr_t) &regions[i]->nrects, sizeof (short));
  19. 	for (j = 0; j < regions[i]->nrects; j++)
  20. 	    bwrite(fd, (genericptr_t) &regions[i]->rects[j], sizeof (NhRect));
  21. 	bwrite(fd, (genericptr_t) &regions[i]->attach_2_u, sizeof (boolean));
  22. 	n = 0;
  23. 	bwrite(fd, (genericptr_t) &regions[i]->attach_2_m, sizeof (unsigned));
  24. 	n = regions[i]->enter_msg != NULL ? strlen(regions[i]->enter_msg) : 0;
  25. 	bwrite(fd, (genericptr_t) &n, sizeof n);
  26. 	if (n > 0)
  27. 	    bwrite(fd, (genericptr_t) regions[i]->enter_msg, n);
  28. 	n = regions[i]->leave_msg != NULL ? strlen(regions[i]->leave_msg) : 0;
  29. 	bwrite(fd, (genericptr_t) &n, sizeof n);
  30. 	if (n > 0)
  31. 	    bwrite(fd, (genericptr_t) regions[i]->leave_msg, n);
  32. 	bwrite(fd, (genericptr_t) &regions[i]->ttl, sizeof (short));
  33. 	bwrite(fd, (genericptr_t) &regions[i]->expire_f, sizeof (short));
  34. 	bwrite(fd, (genericptr_t) &regions[i]->can_enter_f, sizeof (short));
  35. 	bwrite(fd, (genericptr_t) &regions[i]->enter_f, sizeof (short));
  36. 	bwrite(fd, (genericptr_t) &regions[i]->can_leave_f, sizeof (short));
  37. 	bwrite(fd, (genericptr_t) &regions[i]->leave_f, sizeof (short));
  38. 	bwrite(fd, (genericptr_t) &regions[i]->inside_f, sizeof (short));
  39. 	bwrite(fd, (genericptr_t) &regions[i]->player_flags, sizeof (boolean));
  40. 	bwrite(fd, (genericptr_t) &regions[i]->n_monst, sizeof (short));
  41. 	for (j = 0; j < regions[i]->n_monst; j++)
  42. 	    bwrite(fd, (genericptr_t) &regions[i]->monsters[j],
  43. 	     sizeof (unsigned));
  44. 	bwrite(fd, (genericptr_t) &regions[i]->visible, sizeof (boolean));
  45. 	bwrite(fd, (genericptr_t) &regions[i]->glyph, sizeof (int));
  46. 	bwrite(fd, (genericptr_t) &regions[i]->arg, sizeof (genericptr_t));
  47. }
  48.  
  49. skip_lots:
  50. if (release_data(mode))
  51. 	clear_regions();
  52. }

rest_regions

  1. void
  2. rest_regions(fd, ghostly)
  3. int fd;
  4. boolean ghostly; /* If a bones file restore */
  5. {
  6. int i, j;
  7. unsigned n;
  8. long tmstamp;
  9. char *msg_buf;
  10.  
  11. clear_regions();		/* Just for security */
  12. mread(fd, (genericptr_t) &tmstamp, sizeof (tmstamp));
  13. if (ghostly) tmstamp = 0;
  14. else tmstamp = (moves - tmstamp);
  15. mread(fd, (genericptr_t) &n_regions, sizeof (n_regions));
  16. max_regions = n_regions;
  17. if (n_regions > 0)
  18. 	regions = (NhRegion **) alloc(sizeof (NhRegion *) * n_regions);
  19. for (i = 0; i < n_regions; i++) {
  20. 	regions[i] = (NhRegion *) alloc(sizeof (NhRegion));
  21. 	mread(fd, (genericptr_t) &regions[i]->bounding_box, sizeof (NhRect));
  22. 	mread(fd, (genericptr_t) &regions[i]->nrects, sizeof (short));
  23.  
  24. 	if (regions[i]->nrects > 0)
  25. 	    regions[i]->rects = (NhRect *)
  26. 				  alloc(sizeof (NhRect) * regions[i]->nrects);
  27. 	for (j = 0; j < regions[i]->nrects; j++)
  28. 	    mread(fd, (genericptr_t) &regions[i]->rects[j], sizeof (NhRect));
  29. 	mread(fd, (genericptr_t) &regions[i]->attach_2_u, sizeof (boolean));
  30. 	mread(fd, (genericptr_t) &regions[i]->attach_2_m, sizeof (unsigned));
  31.  
  32. 	mread(fd, (genericptr_t) &n, sizeof n);
  33. 	if (n > 0) {
  34. 	    msg_buf = (char *) alloc(n + 1);
  35. 	    mread(fd, (genericptr_t) msg_buf, n);
  36. 	    msg_buf[n] = '\0';
  37. 	    regions[i]->enter_msg = (const char *) msg_buf;
  38. 	} else
  39. 	    regions[i]->enter_msg = NULL;
  40.  
  41. 	mread(fd, (genericptr_t) &n, sizeof n);
  42. 	if (n > 0) {
  43. 	    msg_buf = (char *) alloc(n + 1);
  44. 	    mread(fd, (genericptr_t) msg_buf, n);
  45. 	    msg_buf[n] = '\0';
  46. 	    regions[i]->leave_msg = (const char *) msg_buf;
  47. 	} else
  48. 	    regions[i]->leave_msg = NULL;
  49.  
  50. 	mread(fd, (genericptr_t) &regions[i]->ttl, sizeof (short));
  51. 	/* check for expired region */
  52. 	if (regions[i]->ttl >= 0)
  53. 	    regions[i]->ttl =
  54. 		(regions[i]->ttl > tmstamp) ? regions[i]->ttl - tmstamp : 0;
  55. 	mread(fd, (genericptr_t) &regions[i]->expire_f, sizeof (short));
  56. 	mread(fd, (genericptr_t) &regions[i]->can_enter_f, sizeof (short));
  57. 	mread(fd, (genericptr_t) &regions[i]->enter_f, sizeof (short));
  58. 	mread(fd, (genericptr_t) &regions[i]->can_leave_f, sizeof (short));
  59. 	mread(fd, (genericptr_t) &regions[i]->leave_f, sizeof (short));
  60. 	mread(fd, (genericptr_t) &regions[i]->inside_f, sizeof (short));
  61. 	mread(fd, (genericptr_t) &regions[i]->player_flags, sizeof (boolean));
  62. 	if (ghostly) {	/* settings pertained to old player */
  63. 	    clear_hero_inside(regions[i]);
  64. 	    clear_heros_fault(regions[i]);
  65. 	}
  66. 	mread(fd, (genericptr_t) &regions[i]->n_monst, sizeof (short));
  67. 	if (regions[i]->n_monst > 0)
  68. 	    regions[i]->monsters =
  69. 		(unsigned *) alloc(sizeof (unsigned) * regions[i]->n_monst);
  70. 	else
  71. 	    regions[i]->monsters = NULL;
  72. 	regions[i]->max_monst = regions[i]->n_monst;
  73. 	for (j = 0; j < regions[i]->n_monst; j++)
  74. 	    mread(fd, (genericptr_t) &regions[i]->monsters[j],
  75. 		  sizeof (unsigned));
  76. 	mread(fd, (genericptr_t) &regions[i]->visible, sizeof (boolean));
  77. 	mread(fd, (genericptr_t) &regions[i]->glyph, sizeof (int));
  78. 	mread(fd, (genericptr_t) &regions[i]->arg, sizeof (genericptr_t));
  79. }
  80. /* remove expired regions, do not trigger the expire_f callback (yet!);
  81. also update monster lists if this data is coming from a bones file */
  82. for (i = n_regions - 1; i >= 0; i--)
  83. 	if (regions[i]->ttl == 0)
  84. 	    remove_region(regions[i]);
  85. 	else if (ghostly && regions[i]->n_monst > 0)
  86. 	    reset_region_mids(regions[i]);
  87. }

reset_region_mids

  1. /* update monster IDs for region being loaded from bones; `ghostly' implied */
  2. static void
  3. reset_region_mids(reg)
  4. NhRegion *reg;
  5. {
  6. int i = 0, n = reg->n_monst;
  7. unsigned *mid_list = reg->monsters;
  8.  
  9. while (i < n)
  10. 	if (!lookup_id_mapping(mid_list[i], &mid_list[i])) {
  11. 	    /* shrink list to remove missing monster; order doesn't matter */
  12. 	    mid_list[i] = mid_list[--n];
  13. 	} else {
  14. 	    /* move on to next monster */
  15. 	    ++i;
  16. 	}
  17. reg->n_monst = n;
  18. return;
  19. }
  20.  
  21. #if 0

create_msg_region

  1. /* not yet used */
  2.  
  3. /*--------------------------------------------------------------*
  4. *								*
  5. *			Create Region with just a message	*
  6. *								*
  7. *--------------------------------------------------------------*/
  8.  
  9. NhRegion *
  10. create_msg_region(x, y, w, h, msg_enter, msg_leave)
  11. xchar x, y;
  12. xchar w, h;
  13. const char *msg_enter;
  14. const char *msg_leave;
  15. {
  16. NhRect tmprect;
  17. NhRegion *reg = create_region((NhRect *) 0, 0);
  18.  
  19. reg->enter_msg = msg_enter;
  20. reg->leave_msg = msg_leave;
  21. tmprect.lx = x;
  22. tmprect.ly = y;
  23. tmprect.hx = x + w;
  24. tmprect.hy = y + h;
  25. add_rect_to_reg(reg, &tmprect);
  26. reg->ttl = -1;
  27. return reg;
  28. }

enter_force_field

  1. /*--------------------------------------------------------------*
  2. *								*
  3. *			Force Field Related Code		*
  4. *			(unused yet)				*
  5. *--------------------------------------------------------------*/
  6.  
  7. boolean
  8. enter_force_field(p1, p2)
  9. genericptr_t p1;
  10. genericptr_t p2;
  11. {
  12. struct monst *mtmp;
  13.  
  14. if (p2 == NULL) {		/* That means the player */
  15. 	if (!Blind)
  16. 		You("bump into %s. Ouch!",
  17. 		    Hallucination ? "an invisible tree" :
  18. 			"some kind of invisible wall");
  19. 	else
  20. 	    pline("Ouch!");
  21. } else {
  22. 	mtmp = (struct monst *) p2;
  23. 	if (canseemon(mtmp))
  24. 	    pline("%s bumps into %s!", Monnam(mtmp), something);
  25. }
  26. return FALSE;
  27. }

create_force_field

  1. NhRegion *
  2. create_force_field(x, y, radius, ttl)
  3. xchar x, y;
  4. int radius, ttl;
  5. {
  6. int i;
  7. NhRegion *ff;
  8. int nrect;
  9. NhRect tmprect;
  10.  
  11. ff = create_region((NhRect *) 0, 0);
  12. nrect = radius;
  13. tmprect.lx = x;
  14. tmprect.hx = x;
  15. tmprect.ly = y - (radius - 1);
  16. tmprect.hy = y + (radius - 1);
  17. for (i = 0; i < nrect; i++) {
  18. 	add_rect_to_reg(ff, &tmprect);
  19. 	tmprect.lx--;
  20. 	tmprect.hx++;
  21. 	tmprect.ly++;
  22. 	tmprect.hy--;
  23. }
  24. ff->ttl = ttl;
  25. if (!in_mklev && !flags.mon_moving)
  26. 	set_heros_fault(ff);		/* assume player has created it */
  27. /* ff->can_enter_f = enter_force_field; */
  28. /* ff->can_leave_f = enter_force_field; */
  29. add_region(ff);
  30. return ff;
  31. }
  32.  
  33. #endif	/*0*/

expire_gas_cloud

  1. /*--------------------------------------------------------------*
  2. *								*
  3. *			Gas cloud related code			*
  4. *								*
  5. *--------------------------------------------------------------*/
  6.  
  7. /*
  8. * Here is an example of an expire function that may prolong
  9. * region life after some mods...
  10. */
  11. boolean
  12. expire_gas_cloud(p1, p2)
  13. genericptr_t p1;
  14. genericptr_t p2;
  15. {
  16. NhRegion *reg;
  17. int damage;
  18.  
  19. reg = (NhRegion *) p1;
  20. damage = (int) reg->arg;
  21.  
  22. /* If it was a thick cloud, it dissipates a little first */
  23. if (damage >= 5) {
  24. 	damage /= 2;		/* It dissipates, let's do less damage */
  25. 	reg->arg = (genericptr_t) damage;
  26. 	reg->ttl = 2;		/* Here's the trick : reset ttl */
  27. 	return FALSE;		/* THEN return FALSE, means "still there" */
  28. }
  29. return TRUE;		/* OK, it's gone, you can free it! */
  30. }

inside_gas_cloud

  1. boolean
  2. inside_gas_cloud(p1, p2)
  3. genericptr_t p1;
  4. genericptr_t p2;
  5. {
  6. NhRegion *reg;
  7. struct monst *mtmp;
  8. int dam;
  9.  
  10. reg = (NhRegion *) p1;
  11. dam = (int) reg->arg;
  12. if (p2 == NULL) {		/* This means *YOU* Bozo! */
  13. 	if (nonliving(youmonst.data) || Breathless)
  14. 	    return FALSE;
  15. 	if (!Blind)
  16. 	    make_blinded(1L, FALSE);
  17. 	if (!Poison_resistance) {
  18. 	    pline("%s is burning your %s!", Something, makeplural(body_part(LUNG)));
  19. 	    You("cough and spit blood!");
  20. 	    losehp(rnd(dam) + 5, "gas cloud", KILLED_BY_AN);
  21. 	    return FALSE;
  22. 	} else {
  23. 	    You("cough!");
  24. 	    return FALSE;
  25. 	}
  26. } else {			/* A monster is inside the cloud */
  27. 	mtmp = (struct monst *) p2;
  28.  
  29. 	/* Non living and non breathing monsters are not concerned */
  30. 	if (!nonliving(mtmp->data) && !breathless(mtmp->data)) {
  31. 	    if (cansee(mtmp->mx, mtmp->my))
  32. 		pline("%s coughs!", Monnam(mtmp));
  33. 	    setmangry(mtmp);
  34. 	    if (haseyes(mtmp->data) && mtmp->mcansee) {
  35. 		mtmp->mblinded = 1;
  36. 		mtmp->mcansee = 0;
  37. 	    }
  38. 	    if (resists_poison(mtmp))
  39. 		return FALSE;
  40. 	    mtmp->mhp -= rnd(dam) + 5;
  41. 	    if (mtmp->mhp <= 0) {
  42. 		if (heros_fault(reg))
  43. 		    killed(mtmp);
  44. 		else
  45. 		    monkilled(mtmp, "gas cloud", AD_DRST);
  46. 		if (mtmp->mhp <= 0) {	/* not lifesaved */
  47. 		    return TRUE;
  48. 		}
  49. 	    }
  50. 	}
  51. }
  52. return FALSE;		/* Monster is still alive */
  53. }

create_gas_cloud

  1. NhRegion *
  2. create_gas_cloud(x, y, radius, damage)
  3. xchar x, y;
  4. int radius;
  5. int damage;
  6. {
  7. NhRegion *cloud;
  8. int i, nrect;
  9. NhRect tmprect;
  10.  
  11. cloud = create_region((NhRect *) 0, 0);
  12. nrect = radius;
  13. tmprect.lx = x;
  14. tmprect.hx = x;
  15. tmprect.ly = y - (radius - 1);
  16. tmprect.hy = y + (radius - 1);
  17. for (i = 0; i < nrect; i++) {
  18. 	add_rect_to_reg(cloud, &tmprect);
  19. 	tmprect.lx--;
  20. 	tmprect.hx++;
  21. 	tmprect.ly++;
  22. 	tmprect.hy--;
  23. }
  24. cloud->ttl = rn1(3,4);
  25. if (!in_mklev && !flags.mon_moving)
  26. 	set_heros_fault(cloud);		/* assume player has created it */
  27. cloud->inside_f = INSIDE_GAS_CLOUD;
  28. cloud->expire_f = EXPIRE_GAS_CLOUD;
  29. cloud->arg = (genericptr_t) damage;
  30. cloud->visible = TRUE;
  31. cloud->glyph = cmap_to_glyph(S_cloud);
  32. add_region(cloud);
  33. return cloud;
  34. }
  35.  
  36. /*region.c*/