Source:NetHack 3.4.3/src/rect.c

From NetHackWiki
(Redirected from Source:NetHack 3.4.3/rect.c)
Jump to: navigation, search

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

Top of file

  1. /*	SCCS Id: @(#)rect.c	3.4	1990/02/22	*/
  2. /* Copyright (c) 1990 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.  
  3. int FDECL(get_rect_ind, (NhRect *));
  4.  
  5. static boolean FDECL(intersect, (NhRect *,NhRect *,NhRect *));
  6.  
  7. /*
  8. * In this file, we will handle the various rectangle functions we
  9. * need for room generation.
  10. */
  11.  
  12. #define MAXRECT	50
  13. #define XLIM	4
  14. #define YLIM	3
  15.  
  16. static NhRect rect[MAXRECT+1];
  17. static int rect_cnt;
  18.  
  19. /*
  20. * Initialisation of internal structures. Should be called for every
  21. * new level to be build...
  22. */

init_rect

  1. void
  2. init_rect()
  3. {
  4. 	rect_cnt = 1;
  5. 	rect[0].lx = rect[0].ly = 0;
  6. 	rect[0].hx = COLNO - 1;
  7. 	rect[0].hy = ROWNO - 1;
  8. }

get_rect_ind

  1. /*
  2. * Search Index of one precise NhRect.
  3. *
  4. */
  5.  
  6. int
  7. get_rect_ind(r)
  8. NhRect *r;
  9. {
  10. 	register NhRect *rectp;
  11. 	register int lx, ly, hx, hy;
  12. 	register int i;
  13.  
  14. 	lx = r->lx; ly = r->ly;
  15. 	hx = r->hx; hy = r->hy;
  16. 	for (i=0,rectp = &rect[0];i<rect_cnt;i++,rectp++)
  17. 	    if ( lx == rectp->lx && ly == rectp->ly &&
  18. 		 hx == rectp->hx && hy == rectp->hy)
  19. 		return i;
  20. 	return -1;
  21. }

get_rect

  1. /*
  2. * Search a free rectangle that include the one given in arg
  3. */
  4.  
  5. NhRect *
  6. get_rect(r)
  7. NhRect *r;
  8. {
  9. 	register NhRect *rectp;
  10. 	register int lx, ly, hx, hy;
  11. 	register int i;
  12.  
  13. 	lx = r->lx; ly = r->ly;
  14. 	hx = r->hx; hy = r->hy;
  15. 	for (i=0,rectp = &rect[0];i<rect_cnt;i++,rectp++)
  16. 	    if ( lx >= rectp->lx && ly >= rectp->ly &&
  17. 		 hx <= rectp->hx && hy <= rectp->hy)
  18. 		return rectp;
  19. 	return 0;
  20. }

rnd_rect

  1. /*
  2. * Get some random NhRect from the list.
  3. */
  4.  
  5. NhRect *
  6. rnd_rect()
  7. {
  8. 	    return rect_cnt > 0 ? &rect[rn2(rect_cnt)] : 0;
  9. }

intersect

  1. /*
  2. * Search intersection between two rectangles (r1 & r2).
  3. * return TRUE if intersection exist and put it in r3.
  4. * otherwise returns FALSE
  5. */
  6.  
  7. static boolean
  8. intersect(r1, r2, r3)
  9. NhRect *r1, *r2, *r3;
  10. {
  11. 	if (r2->lx > r1->hx || r2->ly > r1->hy ||
  12. 	    r2->hx < r1->lx || r2->hy < r1->ly)
  13. 	    return FALSE;
  14.  
  15. 	r3->lx = (r2->lx > r1->lx ? r2->lx : r1->lx);
  16. 	r3->ly = (r2->ly > r1->ly ? r2->ly : r1->ly);
  17. 	r3->hx = (r2->hx > r1->hx ? r1->hx : r2->hx);
  18. 	r3->hy = (r2->hy > r1->hy ? r1->hy : r2->hy);
  19.  
  20. 	if (r3->lx > r3->hx || r3->ly > r3->hy)
  21. 	    return FALSE;
  22. 	return TRUE;
  23. }

remove_rect

  1. /*
  2. * Remove a rectangle from the list of free NhRect.
  3. */
  4.  
  5. void
  6. remove_rect(r)
  7. NhRect *r;
  8. {
  9. 	int ind;
  10.  
  11. 	ind = get_rect_ind(r);
  12. 	if ( ind >=0 )
  13. 	    rect[ind] = rect[--rect_cnt];
  14. }

add_rect

  1. /*
  2. * Add a NhRect to the list.
  3. */
  4.  
  5. void
  6. add_rect(r)
  7. NhRect *r;
  8. {
  9. 	if (rect_cnt >= MAXRECT) {
  10. #ifdef WIZARD
  11. 		if (wizard) pline("MAXRECT may be too small.");
  12. #endif
  13. 		return;
  14. 	}
  15. 	/* Check that this NhRect is not included in another one */
  16. 	if (get_rect(r))
  17. 	    return;
  18. 	rect[rect_cnt] = *r;
  19. 	rect_cnt++;
  20. }

split_rects

  1. /*
  2. * Okay, here we have two rectangles (r1 & r2).
  3. * r1 was already in the list and r2 is included in r1.
  4. * What we want is to allocate r2, that is split r1 into smaller rectangles
  5. * then remove it.
  6. */
  7.  
  8. void
  9. split_rects(r1, r2)
  10. NhRect *r1, *r2;
  11. {
  12. 	NhRect r, old_r;
  13. 	int i;
  14.  
  15. 	old_r = *r1;
  16. 	remove_rect(r1);
  17.  
  18. 	/* Walk down since rect_cnt & rect[] will change... */
  19. 	for (i=rect_cnt-1; i>=0; i--)
  20. 	    if (intersect(&rect[i], r2, &r))
  21. 		split_rects(&rect[i], &r);
  22.  
  23. 	if (r2->ly - old_r.ly-1 > (old_r.hy < ROWNO - 1 ? 2*YLIM : YLIM+1)+4) {
  24. 		r = old_r;
  25. 		r.hy = r2->ly - 2;
  26. 		add_rect(&r);
  27. 	}
  28. 	if (r2->lx - old_r.lx-1 > (old_r.hx < COLNO - 1 ? 2*XLIM : XLIM+1)+4) {
  29. 		r = old_r;
  30. 		r.hx = r2->lx - 2;
  31. 		add_rect(&r);
  32. 	}
  33. 	if (old_r.hy - r2->hy-1 > (old_r.ly > 0 ? 2*YLIM : YLIM+1)+4) {
  34. 		r = old_r;
  35. 		r.ly = r2->hy + 2;
  36. 		add_rect(&r);
  37. 	}
  38. 	if (old_r.hx - r2->hx-1 > (old_r.lx > 0 ? 2*XLIM : XLIM+1)+4) {
  39. 		r = old_r;
  40. 		r.lx = r2->hx + 2;
  41. 		add_rect(&r);
  42. 	}
  43. }
  44.  
  45. /*rect.c*/