OSDN Git Service

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