-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseg_handler.py
More file actions
352 lines (294 loc) · 14.7 KB
/
seg_handler.py
File metadata and controls
352 lines (294 loc) · 14.7 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from settings import *
class SegHandler:
MAX_SCALE = 64.0
MIN_SCALE = 0.00390625
def __init__(self, engine):
self.engine = engine
self.wad_data = engine.wad_data
self.player = engine.player
self.framebuffer = self.engine.framebuffer
self.textures = self.wad_data.asset_data.textures
self.sky_id = self.wad_data.asset_data.sky_id
self.seg = None
self.rw_angle1 = None
self.screen_range: set = None
self.x_to_angle = self.get_x_to_angle_table()
self.upper_clip, self.lower_clip = [], []
def update(self):
self.init_floor_ceil_clip_height()
self.init_screen_range()
def init_floor_ceil_clip_height(self):
self.upper_clip = [-1 for _ in range(WIDTH)]
self.lower_clip = [HEIGHT for _ in range(WIDTH)]
@staticmethod
def get_x_to_angle_table():
x_to_angle = []
for i in range(0, WIDTH + 1):
angle = math.degrees(math.atan((H_WIDTH - i) / SCREEN_DIST))
x_to_angle.append(angle)
return x_to_angle
def scale_from_global_angle(self, x, rw_normal_angle, rw_distance):
x_angle = self.x_to_angle[x]
num = SCREEN_DIST * math.cos(math.radians(rw_normal_angle - x_angle - self.player.angle))
den = rw_distance * math.cos(math.radians(x_angle))
scale = num / den
scale = min(self.MAX_SCALE, max(self.MIN_SCALE, scale))
return scale
def init_screen_range(self):
self.screen_range = set(range(WIDTH))
def draw_solid_wall_range(self, x1, x2):
seg = self.seg
front_sector = seg.front_sector
line = seg.linedef
side = seg.linedef.front_sidedef
renderer = self.engine.view_renderer
upper_clip = self.upper_clip
lower_clip = self.lower_clip
framebuffer = self.framebuffer
wall_texture_id = side.middle_texture
ceil_texture_id = front_sector.ceil_texture
floor_texture_id = front_sector.floor_texture
light_level = front_sector.light_level
world_front_z1 = front_sector.ceil_height - self.player.height
world_front_z2 = front_sector.floor_height - self.player.height
b_draw_wall = side.middle_texture != '-'
b_draw_ceil = world_front_z1 > 0 or front_sector.ceil_texture == self.sky_id
b_draw_floor = world_front_z2 < 0
rw_normal_angle = seg.angle + 90
offset_angle = rw_normal_angle - self.rw_angle1
hypotenuse = math.dist(self.player.pos, seg.start_vertex)
rw_distance = hypotenuse * math.cos(math.radians(offset_angle))
rw_scale1 = self.scale_from_global_angle(x1, rw_normal_angle, rw_distance)
if math.isclose(offset_angle % 360, 90, abs_tol=1):
rw_scale1 *= 0.01
if x1 < x2:
scale2 = self.scale_from_global_angle(x2, rw_normal_angle, rw_distance)
rw_scale_step = (scale2 - rw_scale1) / (x2 - x1)
else:
rw_scale_step = 0
wall_texture = self.textures[wall_texture_id]
if line.flags & self.wad_data.LINEDEF_FLAGS['DONT_PEG_BOTTOM']:
v_top = front_sector.floor_height + wall_texture.shape[1]
middle_tex_alt = v_top - self.player.height
else:
middle_tex_alt = world_front_z1
middle_tex_alt += side.y_offset
rw_offset = hypotenuse * math.sin(math.radians(offset_angle))
rw_offset += seg.offset + side.x_offset
rw_center_angle = rw_normal_angle - self.player.angle
wall_y1 = H_HEIGHT - world_front_z1 * rw_scale1
wall_y1_step = -rw_scale_step * world_front_z1
wall_y2 = H_HEIGHT - world_front_z2 * rw_scale1
wall_y2_step = -rw_scale_step * world_front_z2
for x in range(x1, x2 + 1):
draw_wall_y1 = wall_y1 - 1
draw_wall_y2 = wall_y2
if b_draw_ceil:
cy1 = upper_clip[x] + 1
cy2 = int(min(draw_wall_y1 - 1, lower_clip[x] - 1))
renderer.draw_flat(ceil_texture_id, light_level, x, cy1, cy2, world_front_z1)
if b_draw_wall:
wy1 = int(max(draw_wall_y1, upper_clip[x] + 1))
wy2 = int(min(draw_wall_y2, lower_clip[x] - 1))
if wy1 < wy2:
angle = rw_center_angle - self.x_to_angle[x]
texture_column = rw_distance * math.tan(math.radians(angle)) - rw_offset
inv_scale = 1.0 / rw_scale1
renderer.draw_wall_col(framebuffer, wall_texture, texture_column, x, wy1, wy2,
middle_tex_alt, inv_scale, light_level)
if b_draw_floor:
fy1 = int(max(draw_wall_y2 + 1, upper_clip[x] + 1))
fy2 = lower_clip[x] - 1
renderer.draw_flat(floor_texture_id, light_level, x, fy1, fy2, world_front_z2)
rw_scale1 += rw_scale_step
wall_y1 += wall_y1_step
wall_y2 += wall_y2_step
def draw_portal_wall_range(self, x1, x2):
seg = self.seg
front_sector = seg.front_sector
back_sector = seg.back_sector
line = seg.linedef
side = seg.linedef.front_sidedef
renderer = self.engine.view_renderer
upper_clip = self.upper_clip
lower_clip = self.lower_clip
framebuffer = self.framebuffer
upper_wall_texture = side.upper_texture
lower_wall_texture = side.lower_texture
tex_ceil_id = front_sector.ceil_texture
tex_floor_id = front_sector.floor_texture
light_level = front_sector.light_level
world_front_z1 = front_sector.ceil_height - self.player.height
world_back_z1 = back_sector.ceil_height - self.player.height
world_front_z2 = front_sector.floor_height - self.player.height
world_back_z2 = back_sector.floor_height - self.player.height
if front_sector.ceil_texture == back_sector.ceil_texture == self.sky_id:
world_front_z1 = world_back_z1
if (world_front_z1 != world_back_z1 or
front_sector.light_level != back_sector.light_level or
front_sector.ceil_texture != back_sector.ceil_texture):
b_draw_upper_wall = side.upper_texture != '-' and world_back_z1 < world_front_z1
b_draw_ceil = world_front_z1 >= 0 or front_sector.ceil_texture == self.sky_id
else:
b_draw_upper_wall = False
b_draw_ceil = False
if (world_front_z2 != world_back_z2 or
front_sector.floor_texture != back_sector.floor_texture or
front_sector.light_level != back_sector.light_level):
b_draw_lower_wall = side.lower_texture != '-' and world_back_z2 > world_front_z2
b_draw_floor = world_front_z2 <= 0
else:
b_draw_lower_wall = False
b_draw_floor = False
if (not b_draw_upper_wall and not b_draw_ceil and not b_draw_lower_wall and
not b_draw_floor):
return None
rw_normal_angle = seg.angle + 90
offset_angle = rw_normal_angle - self.rw_angle1
hypotenuse = math.dist(self.player.pos, seg.start_vertex)
rw_distance = hypotenuse * math.cos(math.radians(offset_angle))
rw_scale1 = self.scale_from_global_angle(x1, rw_normal_angle, rw_distance)
if x2 > x1:
scale2 = self.scale_from_global_angle(x2, rw_normal_angle, rw_distance)
rw_scale_step = (scale2 - rw_scale1) / (x2 - x1)
else:
rw_scale_step = 0
if b_draw_upper_wall:
upper_wall_texture = self.textures[side.upper_texture]
if line.flags & self.wad_data.LINEDEF_FLAGS['DONT_PEG_TOP']:
upper_tex_alt = world_front_z1
else:
v_top = back_sector.ceil_height + upper_wall_texture.shape[1]
upper_tex_alt = v_top - self.player.height
upper_tex_alt += side.y_offset
if b_draw_lower_wall:
lower_wall_texture = self.textures[side.lower_texture]
if line.flags & self.wad_data.LINEDEF_FLAGS['DONT_PEG_BOTTOM']:
lower_tex_alt = world_front_z1
else:
lower_tex_alt = world_back_z2
lower_tex_alt += side.y_offset
if seg_textured:= b_draw_upper_wall or b_draw_lower_wall:
rw_offset = hypotenuse * math.sin(math.radians(offset_angle))
rw_offset += seg.offset + side.x_offset
#
rw_center_angle = rw_normal_angle - self.player.angle
wall_y1 = H_HEIGHT - world_front_z1 * rw_scale1
wall_y1_step = -rw_scale_step * world_front_z1
wall_y2 = H_HEIGHT - world_front_z2 * rw_scale1
wall_y2_step = -rw_scale_step * world_front_z2
if b_draw_upper_wall:
if world_back_z1 > world_front_z2:
portal_y1 = H_HEIGHT - world_back_z1 * rw_scale1
portal_y1_step = -rw_scale_step * world_back_z1
else:
portal_y1 = wall_y2
portal_y1_step = wall_y2_step
if b_draw_lower_wall:
if world_back_z2 < world_front_z1:
portal_y2 = H_HEIGHT - world_back_z2 * rw_scale1
portal_y2_step = -rw_scale_step * world_back_z2
else:
portal_y2 = wall_y1
portal_y2_step = wall_y1_step
for x in range(x1, x2 + 1):
draw_wall_y1 = wall_y1 - 1
draw_wall_y2 = wall_y2
if seg_textured:
angle = rw_center_angle - self.x_to_angle[x]
texture_column = rw_distance * math.tan(math.radians(angle)) - rw_offset
inv_scale = 1.0 / rw_scale1
if b_draw_upper_wall:
draw_upper_wall_y1 = wall_y1 - 1
draw_upper_wall_y2 = portal_y1
if b_draw_ceil:
cy1 = upper_clip[x] + 1
cy2 = int(min(draw_wall_y1 - 1, lower_clip[x] - 1))
renderer.draw_flat(tex_ceil_id, light_level, x, cy1, cy2, world_front_z1)
wy1 = int(max(draw_upper_wall_y1, upper_clip[x] + 1))
wy2 = int(min(draw_upper_wall_y2, lower_clip[x] - 1))
renderer.draw_wall_col(framebuffer, upper_wall_texture, texture_column, x, wy1, wy2,
upper_tex_alt, inv_scale, light_level)
if upper_clip[x] < wy2:
upper_clip[x] = wy2
portal_y1 += portal_y1_step
if b_draw_ceil:
cy1 = upper_clip[x] + 1
cy2 = int(min(draw_wall_y1 - 1, lower_clip[x] - 1))
renderer.draw_flat(tex_ceil_id, light_level, x, cy1, cy2, world_front_z1)
if upper_clip[x] < cy2:
upper_clip[x] = cy2
if b_draw_lower_wall:
if b_draw_floor:
fy1 = int(max(draw_wall_y2 + 1, upper_clip[x] + 1))
fy2 = lower_clip[x] - 1
renderer.draw_flat(tex_floor_id, light_level, x, fy1, fy2, world_front_z2)
draw_lower_wall_y1 = portal_y2 - 1
draw_lower_wall_y2 = wall_y2
wy1 = int(max(draw_lower_wall_y1, upper_clip[x] + 1))
wy2 = int(min(draw_lower_wall_y2, lower_clip[x] - 1))
renderer.draw_wall_col(framebuffer, lower_wall_texture, texture_column, x, wy1, wy2,
lower_tex_alt, inv_scale, light_level)
if lower_clip[x] > wy1:
lower_clip[x] = wy1
portal_y2 += portal_y2_step
if b_draw_floor:
fy1 = int(max(draw_wall_y2 + 1, upper_clip[x] + 1))
fy2 = lower_clip[x] - 1
renderer.draw_flat(tex_floor_id, light_level, x, fy1, fy2, world_front_z2)
if lower_clip[x] > draw_wall_y2 + 1:
lower_clip[x] = fy1
rw_scale1 += rw_scale_step
wall_y1 += wall_y1_step
wall_y2 += wall_y2_step
def clip_portal_walls(self, x_start, x_end):
curr_wall = set(range(x_start, x_end))
if intersection := curr_wall & self.screen_range:
if len(intersection) == len(curr_wall):
self.draw_portal_wall_range(x_start, x_end - 1)
else:
arr = sorted(intersection)
x = arr[0]
for x1, x2 in zip(arr, arr[1:]):
if x2 - x1 > 1:
self.draw_portal_wall_range(x, x1)
x = x2
self.draw_portal_wall_range(x, arr[-1])
def clip_solid_walls(self, x_start, x_end):
if self.screen_range:
curr_wall = set(range(x_start, x_end))
if intersection := curr_wall & self.screen_range:
if len(intersection) == len(curr_wall):
self.draw_solid_wall_range(x_start, x_end - 1)
else:
arr = sorted(intersection)
x, x2 = arr[0], arr[-1]
for x1, x2 in zip(arr, arr[1:]):
if x2 - x1 > 1:
self.draw_solid_wall_range(x, x1)
x = x2
self.draw_solid_wall_range(x, x2)
#
self.screen_range -= intersection
else:
self.engine.bsp.is_traverse_bsp = False
def classify_segment(self, segment, x1, x2, rw_angle1):
self.seg = segment
self.rw_angle1 = rw_angle1
if x1 == x2:
return None
back_sector = segment.back_sector
front_sector = segment.front_sector
if back_sector is None:
self.clip_solid_walls(x1, x2)
return None
if (front_sector.ceil_height != back_sector.ceil_height or
front_sector.floor_height != back_sector.floor_height):
self.clip_portal_walls(x1, x2)
return None
if (back_sector.ceil_texture == front_sector.ceil_texture and
back_sector.floor_texture == front_sector.floor_texture and
back_sector.light_level == front_sector.light_level and
self.seg.linedef.front_sidedef.middle_texture == '-'):
return None
self.clip_portal_walls(x1, x2)