OSDN Git Service

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