Difference between revisions of "Source:NetHack 3.6.0/src/rect.c"

From NetHackWiki
Jump to navigation Jump to search
 
Line 28: Line 28:
 
  <span id="line21">21. static int rect_cnt;</span>
 
  <span id="line21">21. static int rect_cnt;</span>
 
  <span id="line22">22. </span>
 
  <span id="line22">22. </span>
 +
 +
== init_rect ==
 +
   
 
  <span id="line23">23. /*</span>
 
  <span id="line23">23. /*</span>
 
  <span id="line24">24.  * Initialisation of internal structures. Should be called for every</span>
 
  <span id="line24">24.  * Initialisation of internal structures. Should be called for every</span>
Line 42: Line 45:
 
  <span id="line35">35. }</span>
 
  <span id="line35">35. }</span>
 
  <span id="line36">36. </span>
 
  <span id="line36">36. </span>
 +
 +
== get_rect_ind ==
 +
   
 
  <span id="line37">37. /*</span>
 
  <span id="line37">37. /*</span>
 
  <span id="line38">38.  * Search Index of one precise NhRect.</span>
 
  <span id="line38">38.  * Search Index of one precise NhRect.</span>
Line 66: Line 72:
 
  <span id="line59">59. }</span>
 
  <span id="line59">59. }</span>
 
  <span id="line60">60. </span>
 
  <span id="line60">60. </span>
 +
 +
== get_rect ==
 +
   
 
  <span id="line61">61. /*</span>
 
  <span id="line61">61. /*</span>
 
  <span id="line62">62.  * Search a free rectangle that include the one given in arg</span>
 
  <span id="line62">62.  * Search a free rectangle that include the one given in arg</span>
Line 89: Line 98:
 
  <span id="line82">82. }</span>
 
  <span id="line82">82. }</span>
 
  <span id="line83">83. </span>
 
  <span id="line83">83. </span>
 +
 +
== rnd_rect ==
 +
   
 
  <span id="line84">84. /*</span>
 
  <span id="line84">84. /*</span>
 
  <span id="line85">85.  * Get some random NhRect from the list.</span>
 
  <span id="line85">85.  * Get some random NhRect from the list.</span>
Line 99: Line 111:
 
  <span id="line92">92. }</span>
 
  <span id="line92">92. }</span>
 
  <span id="line93">93. </span>
 
  <span id="line93">93. </span>
 +
 +
== intersect ==
 +
   
 
  <span id="line94">94. /*</span>
 
  <span id="line94">94. /*</span>
 
  <span id="line95">95.  * Search intersection between two rectangles (r1 & r2).</span>
 
  <span id="line95">95.  * Search intersection between two rectangles (r1 & r2).</span>
Line 123: Line 138:
 
  <span id="line116">116. }</span>
 
  <span id="line116">116. }</span>
 
  <span id="line117">117. </span>
 
  <span id="line117">117. </span>
 +
 +
== remove_rect ==
 +
   
 
  <span id="line118">118. /*</span>
 
  <span id="line118">118. /*</span>
 
  <span id="line119">119.  * Remove a rectangle from the list of free NhRect.</span>
 
  <span id="line119">119.  * Remove a rectangle from the list of free NhRect.</span>
Line 138: Line 156:
 
  <span id="line131">131. }</span>
 
  <span id="line131">131. }</span>
 
  <span id="line132">132. </span>
 
  <span id="line132">132. </span>
 +
 +
== add_rect ==
 +
   
 
  <span id="line133">133. /*</span>
 
  <span id="line133">133. /*</span>
 
  <span id="line134">134.  * Add a NhRect to the list.</span>
 
  <span id="line134">134.  * Add a NhRect to the list.</span>
Line 158: Line 179:
 
  <span id="line151">151. }</span>
 
  <span id="line151">151. }</span>
 
  <span id="line152">152. </span>
 
  <span id="line152">152. </span>
 +
 +
== split_rects ==
 +
   
 
  <span id="line153">153. /*</span>
 
  <span id="line153">153. /*</span>
 
  <span id="line154">154.  * Okay, here we have two rectangles (r1 & r2).</span>
 
  <span id="line154">154.  * Okay, here we have two rectangles (r1 & r2).</span>

Latest revision as of 22:10, 3 January 2016

Below is the full text to rect.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/rect.c#line123]], for example.

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.

Top of file

 /* NetHack 3.6	rect.c	$NHDT-Date: 1432512774 2015/05/25 00:12:54 $  $NHDT-Branch: master $:$NHDT-Revision: 1.11 $ */
 /* Copyright (c) 1990 by Jean-Christophe Collet	 */
 /* NetHack may be freely redistributed.  See license for details. */
 
 #include "hack.h"
 
 int FDECL(get_rect_ind, (NhRect *));
 
 STATIC_DCL boolean FDECL(intersect, (NhRect *, NhRect *, NhRect *));
 
 /*
  * In this file, we will handle the various rectangle functions we
  * need for room generation.
  */
 
 #define MAXRECT 50
 #define XLIM 4
 #define YLIM 3
 
 static NhRect rect[MAXRECT + 1];
 static int rect_cnt;
 

init_rect

 /*
  * Initialisation of internal structures. Should be called for every
  * new level to be build...
  */
 
 void
 init_rect()
 {
     rect_cnt = 1;
     rect[0].lx = rect[0].ly = 0;
     rect[0].hx = COLNO - 1;
     rect[0].hy = ROWNO - 1;
 }
 

get_rect_ind

 /*
  * Search Index of one precise NhRect.
  *
  */
 
 int
 get_rect_ind(r)
 NhRect *r;
 {
     register NhRect *rectp;
     register int lx, ly, hx, hy;
     register int i;
 
     lx = r->lx;
     ly = r->ly;
     hx = r->hx;
     hy = r->hy;
     for (i = 0, rectp = &rect[0]; i < rect_cnt; i++, rectp++)
         if (lx == rectp->lx && ly == rectp->ly && hx == rectp->hx
             && hy == rectp->hy)
             return i;
     return -1;
 }
 

get_rect

 /*
  * Search a free rectangle that include the one given in arg
  */
 
 NhRect *
 get_rect(r)
 NhRect *r;
 {
     register NhRect *rectp;
     register int lx, ly, hx, hy;
     register int i;
 
     lx = r->lx;
     ly = r->ly;
     hx = r->hx;
     hy = r->hy;
     for (i = 0, rectp = &rect[0]; i < rect_cnt; i++, rectp++)
         if (lx >= rectp->lx && ly >= rectp->ly && hx <= rectp->hx
             && hy <= rectp->hy)
             return rectp;
     return 0;
 }
 

rnd_rect

 /*
  * Get some random NhRect from the list.
  */
 
 NhRect *
 rnd_rect()
 {
     return rect_cnt > 0 ? &rect[rn2(rect_cnt)] : 0;
 }
 

intersect

 /*
  * Search intersection between two rectangles (r1 & r2).
  * return TRUE if intersection exist and put it in r3.
  * otherwise returns FALSE
  */
 
 STATIC_OVL boolean
 intersect(r1, r2, r3)
 NhRect *r1, *r2, *r3;
 {
     if (r2->lx > r1->hx || r2->ly > r1->hy || r2->hx < r1->lx
         || r2->hy < r1->ly)
         return FALSE;
 
     r3->lx = (r2->lx > r1->lx ? r2->lx : r1->lx);
     r3->ly = (r2->ly > r1->ly ? r2->ly : r1->ly);
     r3->hx = (r2->hx > r1->hx ? r1->hx : r2->hx);
     r3->hy = (r2->hy > r1->hy ? r1->hy : r2->hy);
 
     if (r3->lx > r3->hx || r3->ly > r3->hy)
         return FALSE;
     return TRUE;
 }
 

remove_rect

 /*
  * Remove a rectangle from the list of free NhRect.
  */
 
 void
 remove_rect(r)
 NhRect *r;
 {
     int ind;
 
     ind = get_rect_ind(r);
     if (ind >= 0)
         rect[ind] = rect[--rect_cnt];
 }
 

add_rect

 /*
  * Add a NhRect to the list.
  */
 
 void
 add_rect(r)
 NhRect *r;
 {
     if (rect_cnt >= MAXRECT) {
         if (wizard)
             pline("MAXRECT may be too small.");
         return;
     }
     /* Check that this NhRect is not included in another one */
     if (get_rect(r))
         return;
     rect[rect_cnt] = *r;
     rect_cnt++;
 }
 

split_rects

 /*
  * Okay, here we have two rectangles (r1 & r2).
  * r1 was already in the list and r2 is included in r1.
  * What we want is to allocate r2, that is split r1 into smaller rectangles
  * then remove it.
  */
 
 void
 split_rects(r1, r2)
 NhRect *r1, *r2;
 {
     NhRect r, old_r;
     int i;
 
     old_r = *r1;
     remove_rect(r1);
 
     /* Walk down since rect_cnt & rect[] will change... */
     for (i = rect_cnt - 1; i >= 0; i--)
         if (intersect(&rect[i], r2, &r))
             split_rects(&rect[i], &r);
 
     if (r2->ly - old_r.ly - 1
         > (old_r.hy < ROWNO - 1 ? 2 * YLIM : YLIM + 1) + 4) {
         r = old_r;
         r.hy = r2->ly - 2;
         add_rect(&r);
     }
     if (r2->lx - old_r.lx - 1
         > (old_r.hx < COLNO - 1 ? 2 * XLIM : XLIM + 1) + 4) {
         r = old_r;
         r.hx = r2->lx - 2;
         add_rect(&r);
     }
     if (old_r.hy - r2->hy - 1 > (old_r.ly > 0 ? 2 * YLIM : YLIM + 1) + 4) {
         r = old_r;
         r.ly = r2->hy + 2;
         add_rect(&r);
     }
     if (old_r.hx - r2->hx - 1 > (old_r.lx > 0 ? 2 * XLIM : XLIM + 1) + 4) {
         r = old_r;
         r.lx = r2->hx + 2;
         add_rect(&r);
     }
 }
 
 /*rect.c*/