File: geoelf_corridors.lua

package info (click to toggle)
crawl 2%3A0.34.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,204 kB
  • sloc: cpp: 363,900; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (190 lines) | stat: -rw-r--r-- 6,792 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
------------------------------------------------------------------------------
-- geoelf.lua:
--
-- Functions that draw the corridors for the geoelf layout.
------------------------------------------------------------------------------

geoelf.corridors = {}    -- Namespace for corridor drawing functions

crawl_require("dlua/layout/geoelf_directions.lua")
crawl_require("dlua/layout/geoelf_glyphs.lua")

----------------
--
-- Corridor states
--
-- These are the possible states for a possible corridor.
--

geoelf.corridors.INACTIVE = 0
geoelf.corridors.ACTIVE   = 1
geoelf.corridors.BLOCKED  = 2

----------------------------------------------------------------
--
-- Drawing corridors
--


function geoelf.corridors.draw (e, room_data, corridor_data, index)
  if (corridor_data == nil) then
    crawl.mpr("Error: No corridor data array")
  end
  if (corridor_data.count == nil) then
    crawl.mpr("Error: Corridor data array does not have count")
  end
  if (index >= corridor_data.count) then
    crawl.mpr("Error: index is past the end of corridor_data")
  end
  if (corridor_data[index].status ~= geoelf.corridors.ACTIVE ) then
    crawl.mpr("Error: Attempting to draw an invalid corridor type")
  end

  if (room_data == nil) then
    crawl.mpr("Error: No room data array")
  end
  if (room_data.count == nil) then
    crawl.mpr("Error: Room data array does not have count")
  end
  local room1_index = corridor_data[index].room1
  local room2_index = corridor_data[index].room2
  if (room1_index >= room_data.count or
      room2_index >= room_data.count) then
    crawl.mpr("Error: Index of room is past the end of room_data")
  end
  if (room_data[room1_index] == nil or
      room_data[room2_index] == nil) then
    crawl.mpr("Error: Room referenced by corridor does not exist")
  end
  local direction = corridor_data[index].direction

  -- flip "backwards" corridors
  if (geoelf.directions.IS_REVERSE[direction]) then
    direction = geoelf.directions.GET_REVERSE[direction]
    local temp  = room1_index
    room1_index = room2_index
    room2_index = temp
  end

  -- I don't think we need fancier corridors or even wider ones
  if (direction == geoelf.directions.S) then
    geoelf.corridors.draw_s (e, room_data, room1_index, room2_index)
  elseif (direction == geoelf.directions.E) then
    geoelf.corridors.draw_e (e, room_data, room1_index, room2_index)
  elseif (direction == geoelf.directions.SE) then
    geoelf.corridors.draw_se(e, room_data, room1_index, room2_index)
  elseif (direction == geoelf.directions.SW) then
    geoelf.corridors.draw_sw(e, room_data, room1_index, room2_index)
  else
    crawl.mpr("Error: Trying to draw corridor with invalid direction " ..
              direction)
  end
end

function geoelf.corridors.draw_s (e, room_data, room1_index, room2_index)
  local x1 = room_data[room1_index].center_x
  local x2 = room_data[room2_index].center_x
  local radius1 = room_data[room1_index].radius
  local radius2 = room_data[room2_index].radius
  local x = geoelf.corridors.calculate_position(x1,      x2,
                                                radius1, radius2)

  local start_y = room_data[room1_index].center_y
  local end_y   = room_data[room2_index].center_y
  if (start_y > end_y) then
    crawl.mpr("Error: Corridor S is being drawn backwards")
  end

  --print("Drawing corridor S from y = " .. start_y .. " to " .. end_y ..
  --      " x = " .. x)
  for y = start_y, end_y do
    e.mapgrd[x][y] = geoelf.glyphs.CORRIDOR
  end
end

function geoelf.corridors.draw_e (e, room_data, room1_index, room2_index)
  local y1 = room_data[room1_index].center_y
  local y2 = room_data[room2_index].center_y
  local radius1 = room_data[room1_index].radius
  local radius2 = room_data[room2_index].radius
  local y = geoelf.corridors.calculate_position(y1,      y2,
                                                radius1, radius2)

  local start_x = room_data[room1_index].center_x
  local end_x   = room_data[room2_index].center_x
  if (start_x > end_x) then
    crawl.mpr("Error: Corridor E is being drawn backwards")
  end

  --print("Drawing corridor E from x = " .. start_x .. " to " .. end_x ..
  --      " y = " .. y)
  for x = start_x, end_x do
    e.mapgrd[x][y] = geoelf.glyphs.CORRIDOR
  end
end

function geoelf.corridors.draw_se (e, room_data, room1_index, room2_index)
  local x_minus_y1 = room_data[room1_index].center_x -
                     room_data[room1_index].center_y
  local x_minus_y2 = room_data[room2_index].center_x -
                     room_data[room2_index].center_y
  local radius1 = math.floor(room_data[room1_index].radius * 3/4)
  local radius2 = math.floor(room_data[room2_index].radius * 3/4)
  local x_minus_y = geoelf.corridors.calculate_position(x_minus_y1, x_minus_y2,
                                                        radius1,    radius2)

  local start_y = room_data[room1_index].center_y
  local end_y   = room_data[room2_index].center_y
  if (start_y > end_y) then
    crawl.mpr("Error: Corridor SE is being drawn backwards")
  end

  --print("Drawing corridor SE from y = " .. start_y .. " to " .. end_y ..
  --      " x_minus_y = " .. x_minus_y)
  for y = start_y, end_y do
    e.mapgrd[x_minus_y + y][y] = geoelf.glyphs.CORRIDOR
  end
end

function geoelf.corridors.draw_sw (e, room_data, room1_index, room2_index)
  local x_plus_y1 = room_data[room1_index].center_x +
                    room_data[room1_index].center_y
  local x_plus_y2 = room_data[room2_index].center_x +
                    room_data[room2_index].center_y
  local radius1 = math.floor(room_data[room1_index].radius * 3/4)
  local radius2 = math.floor(room_data[room2_index].radius * 3/4)
  local x_plus_y = geoelf.corridors.calculate_position(x_plus_y1, x_plus_y2,
                                                       radius1,   radius2)

  local start_y = room_data[room1_index].center_y
  local end_y   = room_data[room2_index].center_y
  if (start_y > end_y) then
    crawl.mpr("Error: Corridor SW is being drawn backwards")
  end

  --print("Drawing corridor SW from y = " .. start_y .. " to " .. end_y ..
  --      " x_plus_y = " .. x_plus_y)
  for y = start_y, end_y do
    e.mapgrd[x_plus_y - y][y] = geoelf.glyphs.CORRIDOR
  end
end

function geoelf.corridors.calculate_position (pos1, pos2, radius1, radius2)
  local pos_min
  local pos_max
  if (pos1 < pos2) then
    pos_min = math.max(pos1, pos2 - radius2)
    pos_max = math.min(pos2, pos1 + radius1)
  else
    pos_min = math.max(pos2, pos1 - radius1)
    pos_max = math.min(pos1, pos2 + radius2)
  end

  if (pos_min < pos_max) then
    -- anything in this range is good
    return crawl.random_range(pos_min, pos_max)
  else
    -- if nothing is good, use the middle of the less bad range
    return math.floor((pos_min + pos_max) / 2)
  end
end