OSDN Git Service

ダンジョンの部屋生成コードを変更。SAngから大幅に変更しつつ移植。
[hengband/hengband.git] / src / grid.c
1 /*
2  * File: grid.c
3  * Purpose: low-level dungeon creation primitives
4  */
5
6 /*
7  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
8  *
9  * This software may be copied and distributed for educational, research,
10  * and not for profit purposes provided that this copyright and statement
11  * are included in all such copies.  Other copyrights may also apply.
12  */
13
14 #include "angband.h"
15 #include "generate.h"
16 #include "grid.h"
17
18
19 /*
20  * Returns random co-ordinates for player/monster/object
21  */
22 bool new_player_spot(void)
23 {
24         int     y, x;
25         int max_attempts = 5000;
26
27         cave_type *c_ptr;
28
29         /* Place the player */
30         while (max_attempts--)
31         {
32                 /* Pick a legal spot */
33                 y = rand_range(1, cur_hgt - 2);
34                 x = rand_range(1, cur_wid - 2);
35
36                 c_ptr = &cave[y][x];
37
38                 /* Must be a "naked" floor grid */
39                 if (!cave_clean_bold(y, x) || c_ptr->m_idx) continue;
40                 if (!in_bounds(y,x)) continue;
41
42                 /* Refuse to start on anti-teleport grids */
43                 if (c_ptr->info & (CAVE_ICKY)) continue;
44
45                 /* Done */
46                 break;
47         }
48
49         if (max_attempts < 1) /* Should be -1, actually if we failed... */
50                 return FALSE;
51
52         /* Save the new player grid */
53         py = y;
54         px = x;
55
56         return TRUE;
57 }
58
59
60 /*
61  * Place an up/down staircase at given location
62  */
63 void place_random_stairs(int y, int x)
64 {
65         bool up_stairs = TRUE;
66         bool down_stairs = TRUE;
67         cave_type *c_ptr;
68
69         /* Paranoia */
70         c_ptr = &cave[y][x];
71         if (!is_floor_grid(c_ptr) || c_ptr->o_idx) return;
72
73         /* Town */
74         if (!dun_level)
75                 up_stairs = FALSE;
76
77         /* Ironman */
78         if (ironman_downward)
79                 up_stairs = FALSE;
80
81         /* Bottom */
82         if (dun_level >= d_info[dungeon_type].maxdepth)
83                 down_stairs = FALSE;
84
85         /* Quest-level */
86         if (quest_number(dun_level) && (dun_level > 1))
87                 down_stairs = FALSE;
88
89         /* We can't place both */
90         if (down_stairs && up_stairs)
91         {
92                 /* Choose a staircase randomly */
93                 if (randint0(100) < 50)
94                         up_stairs = FALSE;
95                 else
96                         down_stairs = FALSE;
97         }
98
99         /* Place the stairs */
100         if (up_stairs)
101                 place_up_stairs(y, x);
102         else if (down_stairs)
103                 place_down_stairs(y, x);
104 }
105
106
107 /*
108  * Place a random type of door at the given location
109  */
110 void place_random_door(int y, int x, bool room)
111 {
112         int tmp;
113         cave_type *c_ptr = &cave[y][x];
114
115         /* Initialize mimic info */
116         c_ptr->mimic = 0;
117         
118         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
119         {
120                 place_floor_bold(y, x);
121                 return;
122         }
123
124         /* Choose an object */
125         tmp = randint0(1000);
126
127         /* Open doors (300/1000) */
128         if (tmp < 300)
129         {
130                 /* Create open door */
131                 set_cave_feat(y, x, FEAT_OPEN);
132         }
133
134         /* Broken doors (100/1000) */
135         else if (tmp < 400)
136         {
137                 /* Create broken door */
138                 set_cave_feat(y, x, FEAT_BROKEN);
139         }
140
141         /* Secret doors (200/1000) */
142         else if (tmp < 600)
143         {
144                 /* Create secret door */
145                 place_closed_door(y, x);
146
147                 /* Hide. If on the edge of room, use outer wall. */
148                 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
149
150                 /* Floor type terrain cannot hide a door */
151                 if (feat_floor(c_ptr->mimic))
152                 {
153                         c_ptr->feat = c_ptr->mimic;
154                         c_ptr->mimic = 0;
155                 }
156         }
157
158         /* Closed, locked, or stuck doors (400/1000) */
159         else place_closed_door(y, x);
160 }
161
162
163 /*
164  * Place a random type of normal door at the given location.
165  */
166 void place_closed_door(int y, int x)
167 {
168         int tmp;
169
170         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
171         {
172                 place_floor_bold(y, x);
173                 return;
174         }
175
176         /* Choose an object */
177         tmp = randint0(400);
178
179         /* Closed doors (300/400) */
180         if (tmp < 300)
181         {
182                 /* Create closed door */
183                 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
184         }
185
186         /* Locked doors (99/400) */
187         else if (tmp < 399)
188         {
189                 /* Create locked door */
190                 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
191         }
192
193         /* Stuck doors (1/400) */
194         else
195         {
196                 /* Create jammed door */
197                 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
198         }
199
200         /* Now it is not floor */
201         cave[y][x].info &= ~(CAVE_MASK);
202 }
203
204
205 /*
206  * Make an empty square floor, for the middle of rooms
207  */
208 void place_floor(int x1, int x2, int y1, int y2, bool light)
209 {
210         int x, y;
211
212         /* Place a full floor under the room */
213         for (y = y1 - 1; y <= y2 + 1; y++)
214         {
215                 for (x = x1 - 1; x <= x2 + 1; x++)
216                 {
217                         place_floor_bold(y, x);
218                         add_cave_info(y, x, CAVE_ROOM);
219                         if (light) add_cave_info(y, x, CAVE_GLOW);
220                 }
221         }
222 }
223
224
225 /*
226  * Make an empty square room, only floor and wall grids
227  */
228 void place_room(int x1, int x2, int y1, int y2, bool light)
229 {
230         int y, x;
231
232         place_floor(x1, x2, y1, y2, light);
233
234         /* Walls around the room */
235         for (y = y1 - 1; y <= y2 + 1; y++)
236         {
237                 place_outer_bold(y, x1 - 1);
238                 place_outer_bold(y, x2 + 1);
239         }
240         for (x = x1 - 1; x <= x2 + 1; x++)
241         {
242                 place_outer_bold(y1 - 1, x);
243                 place_outer_bold(y2 + 1, x);
244         }
245 }
246
247
248 /*
249  * Create up to "num" objects near the given coordinates
250  * Only really called by some of the "vault" routines.
251  */
252 void vault_objects(int y, int x, int num)
253 {
254         int dummy = 0;
255         int i = 0, j = y, k = x;
256
257         cave_type *c_ptr;
258
259
260         /* Attempt to place 'num' objects */
261         for (; num > 0; --num)
262         {
263                 /* Try up to 11 spots looking for empty space */
264                 for (i = 0; i < 11; ++i)
265                 {
266                         /* Pick a random location */
267                         while (dummy < SAFE_MAX_ATTEMPTS)
268                         {
269                                 j = rand_spread(y, 2);
270                                 k = rand_spread(x, 3);
271                                 dummy++;
272                                 if (!in_bounds(j, k)) continue;
273                                 break;
274                         }
275
276
277                         if (dummy >= SAFE_MAX_ATTEMPTS)
278                         {
279                                 if (cheat_room)
280                                 {
281 #ifdef JP
282 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
283 #else
284                                         msg_print("Warning! Could not place vault object!");
285 #endif
286
287                                 }
288                         }
289
290
291                         /* Require "clean" floor space */
292                         c_ptr = &cave[j][k];
293                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
294
295                         /* Place an item */
296                         if (randint0(100) < 75)
297                         {
298                                 place_object(j, k, 0L);
299                         }
300
301                         /* Place gold */
302                         else
303                         {
304                                 place_gold(j, k);
305                         }
306
307                         /* Placement accomplished */
308                         break;
309                 }
310         }
311 }
312
313
314 /*
315  * Place a trap with a given displacement of point
316  */
317 void vault_trap_aux(int y, int x, int yd, int xd)
318 {
319         int count = 0, y1 = y, x1 = x;
320         int dummy = 0;
321
322         cave_type *c_ptr;
323
324         /* Place traps */
325         for (count = 0; count <= 5; count++)
326         {
327                 /* Get a location */
328                 while (dummy < SAFE_MAX_ATTEMPTS)
329                 {
330                         y1 = rand_spread(y, yd);
331                         x1 = rand_spread(x, xd);
332                         dummy++;
333                         if (!in_bounds(y1, x1)) continue;
334                         break;
335                 }
336
337                 if (dummy >= SAFE_MAX_ATTEMPTS)
338                 {
339                         if (cheat_room)
340                         {
341 #ifdef JP
342 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
343 #else
344                                 msg_print("Warning! Could not place vault trap!");
345 #endif
346
347                         }
348                 }
349
350                 /* Require "naked" floor grids */
351                 c_ptr = &cave[y1][x1];
352                 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
353
354                 /* Place the trap */
355                 place_trap(y1, x1);
356
357                 /* Done */
358                 break;
359         }
360 }
361
362
363 /*
364  * Place some traps with a given displacement of given location
365  */
366 void vault_traps(int y, int x, int yd, int xd, int num)
367 {
368         int i;
369
370         for (i = 0; i < num; i++)
371         {
372                 vault_trap_aux(y, x, yd, xd);
373         }
374 }
375
376
377 /*
378  * Hack -- Place some sleeping monsters near the given location
379  */
380 void vault_monsters(int y1, int x1, int num)
381 {
382         int k, i, y, x;
383         cave_type *c_ptr;
384
385         /* Try to summon "num" monsters "near" the given location */
386         for (k = 0; k < num; k++)
387         {
388                 /* Try nine locations */
389                 for (i = 0; i < 9; i++)
390                 {
391                         int d = 1;
392
393                         /* Pick a nearby location */
394                         scatter(&y, &x, y1, x1, d, 0);
395
396                         /* Require "empty" floor grids */
397                         c_ptr = &cave[y][x];
398                         if (!cave_empty_grid(c_ptr)) continue;
399
400                         /* Place the monster (allow groups) */
401                         monster_level = base_level + 2;
402                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
403                         monster_level = base_level;
404                 }
405         }
406 }
407
408
409 /*
410  * Count the number of walls adjacent to the given grid.
411  *
412  * Note -- Assumes "in_bounds(y, x)"
413  *
414  * We count only granite walls and permanent walls.
415  */
416 int next_to_walls(int y, int x)
417 {
418         int     k = 0;
419
420         if (cave_floor_grid(&cave[y + 1][x])) k++;
421         if (cave_floor_grid(&cave[y - 1][x])) k++;
422         if (cave_floor_grid(&cave[y][x + 1])) k++;
423         if (cave_floor_grid(&cave[y][x - 1])) k++;
424
425         return (k);
426 }
427
428
429 /*
430  * Always picks a correct direction
431  */
432 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
433 {
434         /* Extract vertical and horizontal directions */
435         *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
436         *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
437
438         /* Never move diagonally */
439         if (*rdir && *cdir)
440         {
441                 if (randint0(100) < 50)
442                         *rdir = 0;
443                 else
444                         *cdir = 0;
445         }
446 }
447
448
449 /*
450  * Pick a random direction
451  */
452 void rand_dir(int *rdir, int *cdir)
453 {
454         /* Pick a random direction */
455         int i = randint0(4);
456
457         /* Extract the dy/dx components */
458         *rdir = ddy_ddd[i];
459         *cdir = ddx_ddd[i];
460 }
461
462
463 /* Function that sees if a square is a floor.  (Includes range checking.) */
464 bool get_is_floor(int x, int y)
465 {
466         if (!in_bounds(y, x))
467         {
468                 /* Out of bounds */
469                 return (FALSE);
470         }
471
472         /* Do the real check */
473         if (is_floor_bold(y, x)) return (TRUE);
474
475         return (FALSE);
476 }
477
478
479 /* Set a square to be floor.  (Includes range checking.) */
480 void set_floor(int x, int y)
481 {
482         if (!in_bounds(y, x))
483         {
484                 /* Out of bounds */
485                 return;
486         }
487
488         if (cave[y][x].info & CAVE_ROOM)
489         {
490                 /* A room border don't touch. */
491                 return;
492         }
493
494         /* Set to be floor if is a wall (don't touch lakes). */
495         if (is_extra_bold(y, x))
496                 place_floor_bold(y, x);
497 }
498
499
500
501 /*
502  * Constructs a tunnel between two points
503  *
504  * This function must be called BEFORE any streamers are created,
505  * since we use the special "granite wall" sub-types to keep track
506  * of legal places for corridors to pierce rooms.
507  *
508  * We use "door_flag" to prevent excessive construction of doors
509  * along overlapping corridors.
510  *
511  * We queue the tunnel grids to prevent door creation along a corridor
512  * which intersects itself.
513  *
514  * We queue the wall piercing grids to prevent a corridor from leaving
515  * a room and then coming back in through the same entrance.
516  *
517  * We "pierce" grids which are "outer" walls of rooms, and when we
518  * do so, we change all adjacent "outer" walls of rooms into "solid"
519  * walls so that no two corridors may use adjacent grids for exits.
520  *
521  * The "solid" wall check prevents corridors from "chopping" the
522  * corners of rooms off, as well as "silly" door placement, and
523  * "excessively wide" room entrances.
524  *
525  * Useful "feat" values:
526  *   FEAT_WALL_EXTRA -- granite walls
527  *   FEAT_WALL_INNER -- inner room walls
528  *   FEAT_WALL_OUTER -- outer room walls
529  *   FEAT_WALL_SOLID -- solid room walls
530  *   FEAT_PERM_EXTRA -- shop walls (perma)
531  *   FEAT_PERM_INNER -- inner room walls (perma)
532  *   FEAT_PERM_OUTER -- outer room walls (perma)
533  *   FEAT_PERM_SOLID -- dungeon border (perma)
534  */
535 void build_tunnel(int row1, int col1, int row2, int col2)
536 {
537         int y, x;
538         int tmp_row, tmp_col;
539         int row_dir, col_dir;
540         int start_row, start_col;
541         int main_loop_count = 0;
542
543         bool door_flag = FALSE;
544
545         cave_type *c_ptr;
546
547         /* Save the starting location */
548         start_row = row1;
549         start_col = col1;
550
551         /* Start out in the correct direction */
552         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
553
554         /* Keep going until done (or bored) */
555         while ((row1 != row2) || (col1 != col2))
556         {
557                 /* Mega-Hack -- Paranoia -- prevent infinite loops */
558                 if (main_loop_count++ > 2000) break;
559
560                 /* Allow bends in the tunnel */
561                 if (randint0(100) < dun_tun_chg)
562                 {
563                         /* Acquire the correct direction */
564                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
565
566                         /* Random direction */
567                         if (randint0(100) < dun_tun_rnd)
568                         {
569                                 rand_dir(&row_dir, &col_dir);
570                         }
571                 }
572
573                 /* Get the next location */
574                 tmp_row = row1 + row_dir;
575                 tmp_col = col1 + col_dir;
576
577
578                 /* Extremely Important -- do not leave the dungeon */
579                 while (!in_bounds(tmp_row, tmp_col))
580                 {
581                         /* Acquire the correct direction */
582                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
583
584                         /* Random direction */
585                         if (randint0(100) < dun_tun_rnd)
586                         {
587                                 rand_dir(&row_dir, &col_dir);
588                         }
589
590                         /* Get the next location */
591                         tmp_row = row1 + row_dir;
592                         tmp_col = col1 + col_dir;
593                 }
594
595
596                 /* Access the location */
597                 c_ptr = &cave[tmp_row][tmp_col];
598
599
600                 /* Avoid the edge of the dungeon */
601                 if (c_ptr->feat == FEAT_PERM_SOLID) continue;
602
603                 /* Avoid the edge of vaults */
604                 if (c_ptr->feat == FEAT_PERM_OUTER) continue;
605
606                 /* Avoid "solid" granite walls */
607                 if (is_solid_grid(c_ptr)) continue;
608
609                 /* Pierce "outer" walls of rooms */
610                 if (is_outer_grid(c_ptr))
611                 {
612                         /* Acquire the "next" location */
613                         y = tmp_row + row_dir;
614                         x = tmp_col + col_dir;
615
616                         /* Hack -- Avoid outer/solid permanent walls */
617                         if (cave[y][x].feat == FEAT_PERM_SOLID) continue;
618                         if (cave[y][x].feat == FEAT_PERM_OUTER) continue;
619
620                         /* Hack -- Avoid outer/solid granite walls */
621                         if (is_outer_bold(y, x)) continue;
622                         if (is_solid_bold(y, x)) continue;
623
624                         /* Accept this location */
625                         row1 = tmp_row;
626                         col1 = tmp_col;
627
628                         /* Save the wall location */
629                         if (dun->wall_n < WALL_MAX)
630                         {
631                                 dun->wall[dun->wall_n].y = row1;
632                                 dun->wall[dun->wall_n].x = col1;
633                                 dun->wall_n++;
634                         }
635
636                         /* Forbid re-entry near this piercing */
637                         for (y = row1 - 1; y <= row1 + 1; y++)
638                         {
639                                 for (x = col1 - 1; x <= col1 + 1; x++)
640                                 {
641                                         /* Convert adjacent "outer" walls as "solid" walls */
642                                         if (is_outer_bold(y, x))
643                                         {
644                                                 /* Change the wall to a "solid" wall */
645                                                 place_solid_noperm_bold(y, x);
646                                         }
647                                 }
648                         }
649                 }
650
651                 /* Travel quickly through rooms */
652                 else if (c_ptr->info & (CAVE_ROOM))
653                 {
654                         /* Accept the location */
655                         row1 = tmp_row;
656                         col1 = tmp_col;
657                 }
658
659                 /* Tunnel through all other walls */
660                 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
661                 {
662                         /* Accept this location */
663                         row1 = tmp_row;
664                         col1 = tmp_col;
665
666                         /* Save the tunnel location */
667                         if (dun->tunn_n < TUNN_MAX)
668                         {
669                                 dun->tunn[dun->tunn_n].y = row1;
670                                 dun->tunn[dun->tunn_n].x = col1;
671                                 dun->tunn_n++;
672                         }
673
674                         /* Allow door in next grid */
675                         door_flag = FALSE;
676                 }
677
678                 /* Handle corridor intersections or overlaps */
679                 else
680                 {
681                         /* Accept the location */
682                         row1 = tmp_row;
683                         col1 = tmp_col;
684
685                         /* Collect legal door locations */
686                         if (!door_flag)
687                         {
688                                 /* Save the door location */
689                                 if (dun->door_n < DOOR_MAX)
690                                 {
691                                         dun->door[dun->door_n].y = row1;
692                                         dun->door[dun->door_n].x = col1;
693                                         dun->door_n++;
694                                 }
695
696                                 /* No door in next grid */
697                                 door_flag = TRUE;
698                         }
699
700                         /* Hack -- allow pre-emptive tunnel termination */
701                         if (randint0(100) >= dun_tun_con)
702                         {
703                                 /* Distance between row1 and start_row */
704                                 tmp_row = row1 - start_row;
705                                 if (tmp_row < 0) tmp_row = (-tmp_row);
706
707                                 /* Distance between col1 and start_col */
708                                 tmp_col = col1 - start_col;
709                                 if (tmp_col < 0) tmp_col = (-tmp_col);
710
711                                 /* Terminate the tunnel */
712                                 if ((tmp_row > 10) || (tmp_col > 10)) break;
713                         }
714                 }
715         }
716 }
717
718
719 /*
720  * This routine adds the square to the tunnel
721  * It also checks for SOLID walls - and returns a nearby
722  * non-SOLID square in (x,y) so that a simple avoiding
723  * routine can be used. The returned boolean value reflects
724  * whether or not this routine hit a SOLID wall.
725  *
726  * "affectwall" toggles whether or not this new square affects
727  * the boundaries of rooms. - This is used by the catacomb
728  * routine.
729  */
730 static bool set_tunnel(int *x, int *y, bool affectwall)
731 {
732         int feat, i, j, dx, dy;
733
734         cave_type *c_ptr = &cave[*y][*x];
735
736         if (!in_bounds(*y, *x)) return TRUE;
737
738         feat = c_ptr->feat;
739
740         if ((feat == FEAT_PERM_OUTER) ||
741             (feat == FEAT_PERM_INNER) ||
742             is_inner_grid(c_ptr))
743         {
744                 /*
745                  * Ignore permanent walls - sometimes cannot tunnel around them anyway
746                  * so don't try - it just complicates things unnecessarily.
747                  */
748                 return TRUE;
749         }
750
751         if (is_extra_bold(*y,*x))
752         {
753                 /* Save the tunnel location */
754                 if (dun->tunn_n < TUNN_MAX)
755                 {
756                                 dun->tunn[dun->tunn_n].y = *y;
757                                 dun->tunn[dun->tunn_n].x = *x;
758                                 dun->tunn_n++;
759                 }
760
761                 return TRUE;
762         }
763
764         if (is_floor_bold(*y, *x))
765         {
766                 /* Don't do anything */
767                 return TRUE;
768         }
769
770         if (is_outer_grid(c_ptr) && affectwall)
771         {
772                 /* Save the wall location */
773                 if (dun->wall_n < WALL_MAX)
774                 {
775                         dun->wall[dun->wall_n].y = *y;
776                         dun->wall[dun->wall_n].x = *x;
777                         dun->wall_n++;
778                 }
779
780                 /* Forbid re-entry near this piercing */
781                 for (j = *y - 1; j <= *y + 1; j++)
782                 {
783                         for (i = *x - 1; i <= *x + 1; i++)
784                         {
785                                 /* Convert adjacent "outer" walls as "solid" walls */
786                                 if (is_outer_bold(j, i))
787                                 {
788                                         /* Change the wall to a "solid" wall */
789                                         place_solid_noperm_bold(j, i);
790                                 }
791                         }
792                 }
793
794                 /* Clear mimic type */
795                 cave[*y][*x].mimic = 0;
796
797                 place_floor_bold(*y, *x);
798
799                 return TRUE;
800         }
801
802         if (is_solid_grid(c_ptr) && affectwall)
803         {
804                 /* cannot place tunnel here - use a square to the side */
805
806                 /* find usable square and return value in (x,y) */
807
808                 i = 50;
809
810                 dy = 0;
811                 dx = 0;
812                 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
813                 {
814                         dy = randint0(3) - 1;
815                         dx = randint0(3) - 1;
816
817                         if (!in_bounds(*y + dy, *x + dx))
818                         {
819                                 dx = 0;
820                                 dy = 0;
821                         }
822
823                         i--;
824                 }
825
826                 if (i == 0)
827                 {
828                         /* Failed for some reason: hack - ignore the solidness */
829                         place_outer_grid(c_ptr);
830                         dx = 0;
831                         dy = 0;
832                 }
833
834                 /* Give new, acceptable coordinate. */
835                 *x = *x + dx;
836                 *y = *y + dy;
837
838                 return FALSE;
839         }
840
841         return TRUE;
842 }
843
844
845 /*
846  * This routine creates the catacomb-like tunnels by removing extra rock.
847  * Note that this routine is only called on "even" squares - so it gives
848  * a natural checkerboard pattern.
849  */
850 static void create_cata_tunnel(int x, int y)
851 {
852         int x1, y1;
853
854         /* Build tunnel */
855         x1 = x - 1;
856         y1 = y;
857         set_tunnel(&x1, &y1, FALSE);
858
859         x1 = x + 1;
860         y1 = y;
861         set_tunnel(&x1, &y1, FALSE);
862
863         x1 = x;
864         y1 = y - 1;
865         set_tunnel(&x1, &y1, FALSE);
866
867         x1 = x;
868         y1 = y + 1;
869         set_tunnel(&x1, &y1, FALSE);
870 }
871
872
873 /*
874  * This routine does the bulk of the work in creating the new types of tunnels.
875  * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
876  * It doesn't need to add any complexity - straight lines are fine.
877  * The SOLID walls are avoided by a recursive algorithm which tries random ways
878  * around the obstical until it works.  The number of itterations is counted, and it
879  * this gets too large the routine exits. This should stop any crashes - but may leave
880  * small gaps in the tunnel where there are too many SOLID walls.
881  *
882  * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used
883  * as a part of the dodge SOLID walls algorithm.
884  *
885  * Type 2 tunnels are made of two straight lines at right angles. When this is used with
886  * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
887  *
888  * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
889  * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
890  * the surface.
891  */
892 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
893 {
894         int i, x, y;
895         int length;
896
897         /* Check for early exit */
898         if (!(*fail)) return;
899
900         length = distance(x1, y1, x2, y2);
901
902         count++;
903
904         if ((type == 1) && (length != 0))
905         {
906
907                 for (i = 0; i <= length; i++)
908                 {
909                         x = x1 + i * (x2 - x1) / length;
910                         y = y1 + i * (y2 - y1) / length;
911                         if (!set_tunnel(&x, &y, TRUE))
912                         {
913                                 if (count > 50)
914                                 {
915                                         /* This isn't working - probably have an infinite loop */
916                                         *fail = FALSE;
917                                         return;
918                                 }
919
920                                 /* solid wall - so try to go around */
921                                 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
922                                 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
923                         }
924                 }
925         }
926         else if ((type == 2) || (type == 3))
927         {
928                 if (x1 < x2)
929                 {
930                         for (i = x1; i <= x2; i++)
931                         {
932                                 x = i;
933                                 y = y1;
934                                 if (!set_tunnel(&x, &y, TRUE))
935                                 {
936                                         /* solid wall - so try to go around */
937                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
938                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
939                                 }
940                                 if ((type == 3) && ((x + y) % 2))
941                                 {
942                                         create_cata_tunnel(i, y1);
943                                 }
944                         }
945                 }
946                 else
947                 {
948                         for (i = x2; i <= x1; i++)
949                         {
950                                 x = i;
951                                 y = y1;
952                                 if (!set_tunnel(&x, &y, TRUE))
953                                 {
954                                         /* solid wall - so try to go around */
955                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
956                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
957                                 }
958                                 if ((type == 3) && ((x + y) % 2))
959                                 {
960                                         create_cata_tunnel(i, y1);
961                                 }
962                         }
963
964                 }
965                 if (y1 < y2)
966                 {
967                         for (i = y1; i <= y2; i++)
968                         {
969                                 x = x2;
970                                 y = i;
971                                 if (!set_tunnel(&x, &y, TRUE))
972                                 {
973                                         /* solid wall - so try to go around */
974                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
975                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
976                                 }
977                                 if ((type == 3) && ((x + y) % 2))
978                                 {
979                                         create_cata_tunnel(x2, i);
980                                 }
981                         }
982                 }
983                 else
984                 {
985                         for (i = y2; i <= y1; i++)
986                         {
987                                 x = x2;
988                                 y = i;
989                                 if (!set_tunnel(&x, &y, TRUE))
990                                 {
991                                         /* solid wall - so try to go around */
992                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
993                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
994                                 }
995                                 if ((type == 3) && ((x + y) % 2))
996                                 {
997                                         create_cata_tunnel(x2, i);
998                                 }
999                         }
1000                 }
1001         }
1002 }
1003
1004
1005 /*
1006  * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1007  * Permanent rock is ignored in this path finding- sometimes there is no
1008  * path around anyway -so there will be a crash if we try to find one.
1009  * This routine is much like the river creation routine in Zangband.
1010  * It works by dividing a line segment into two.  The segments are divided
1011  * until they are less than "cutoff" - when the corresponding routine from
1012  * "short_seg_hack" is called.
1013  * Note it is VERY important that the "stop if hit another passage" logic
1014  * stays as is.  Without this the dungeon turns into Swiss Cheese...
1015  */
1016 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1017 {
1018         int x3, y3, dx, dy;
1019         int changex, changey;
1020         int length;
1021         int i;
1022         bool retval, firstsuccede;
1023         cave_type *c_ptr;
1024
1025         length = distance(x1, y1, x2, y2);
1026
1027         if (length > cutoff)
1028         {
1029                 /*
1030                 * Divide path in half and call routine twice.
1031                  */
1032                 dx = (x2 - x1) / 2;
1033                 dy = (y2 - y1) / 2;
1034
1035                 /* perturbation perpendicular to path */
1036                 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1037
1038                 /* perturbation perpendicular to path */
1039                 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1040
1041                 /* Work out "mid" ponit */
1042                 x3 = x1 + dx + changex;
1043                 y3 = y1 + dy + changey;
1044
1045                 /* See if in bounds - if not - do not perturb point */
1046                 if (!in_bounds(y3, x3))
1047                 {
1048                         x3 = (x1 + x2) / 2;
1049                         y3 = (y1 + y2) / 2;
1050                 }
1051                 /* cache c_ptr */
1052                 c_ptr = &cave[y3][x3];
1053                 if (is_solid_grid(c_ptr))
1054                 {
1055                         /* move midpoint a bit to avoid problem. */
1056
1057                         i = 50;
1058
1059                         dy = 0;
1060                         dx = 0;
1061                         while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1062                         {
1063                                 dy = randint0(3) - 1;
1064                                 dx = randint0(3) - 1;
1065                                 if (!in_bounds(y3 + dy, x3 + dx))
1066                                 {
1067                                         dx = 0;
1068                                         dy = 0;
1069                                 }
1070                                 i--;
1071                         }
1072
1073                         if (i == 0)
1074                         {
1075                                 /* Failed for some reason: hack - ignore the solidness */
1076                                 place_outer_bold(y3, x3);
1077                                 dx = 0;
1078                                 dy = 0;
1079                         }
1080                         y3 += dy;
1081                         x3 += dx;
1082                         c_ptr = &cave[y3][x3];
1083                 }
1084
1085                 if (is_floor_grid(c_ptr))
1086                 {
1087                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1088                         {
1089                                 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1090                                 {
1091                                         /* do second half only if works + if have hit a room */
1092                                         retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1093                                 }
1094                                 else
1095                                 {
1096                                         /* have hit another tunnel - make a set of doors here */
1097                                         retval = FALSE;
1098
1099                                         /* Save the door location */
1100                                         if (dun->door_n < DOOR_MAX)
1101                                         {
1102                                                 dun->door[dun->door_n].y = y3;
1103                                                 dun->door[dun->door_n].x = x3;
1104                                                 dun->door_n++;
1105                                         }
1106                                 }
1107                                 firstsuccede = TRUE;
1108                         }
1109                         else
1110                         {
1111                                 /* false- didn't work all the way */
1112                                 retval = FALSE;
1113                                 firstsuccede = FALSE;
1114                         }
1115                 }
1116                 else
1117                 {
1118                         /* tunnel through walls */
1119                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1120                         {
1121                                 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1122                                 firstsuccede = TRUE;
1123                         }
1124                         else
1125                         {
1126                                 /* false- didn't work all the way */
1127                                 retval = FALSE;
1128                                 firstsuccede = FALSE;
1129                         }
1130                 }
1131                 if (firstsuccede)
1132                 {
1133                         /* only do this if the first half has worked */
1134                         set_tunnel(&x3, &y3, TRUE);
1135                 }
1136                 /* return value calculated above */
1137                 return retval;
1138         }
1139         else
1140         {
1141                 /* Do a short segment */
1142                 retval = TRUE;
1143                 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1144
1145                 /* Hack - ignore return value so avoid infinite loops */
1146                 return TRUE;
1147         }
1148 }
1149