Skip to content

Commit 84c6567

Browse files
authored
clean up & rearrange code (#24)
* rename units.jl to utils.jl * clean up files * remove navigation.jl
1 parent 6ba7e34 commit 84c6567

File tree

4 files changed

+83
-67
lines changed

4 files changed

+83
-67
lines changed

src/RayCastWorlds.jl

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,19 @@ module RayCastWorlds
22

33
import ReinforcementLearningBase as RLBase
44

5-
reset!(world) = error("Method not implemented")
6-
act!(world) = error("Method not implemented")
7-
cast_rays!(world) = error("Method not implemented")
8-
get_action_keys(env) = error("Method not implemented")
9-
get_action_names(env) = error("Method not implemented")
10-
play!(env) = error("Method not implemented")
11-
update_top_view!(env) = error("Method not implemented")
12-
update_camera_view!(env) = error("Method not implemented")
13-
14-
function copy_image_to_frame_buffer!(frame_buffer, image)
15-
height_image, width_image = size(image)
16-
for j in 1:width_image
17-
for i in 1:height_image
18-
frame_buffer[j, i] = image[i, j]
19-
end
20-
end
21-
22-
return nothing
23-
end
24-
25-
function sample_empty_position(rng, tile_map, region, max_tries)
26-
position = rand(rng, region)
27-
28-
for i in 1:max_tries
29-
if any(@view tile_map[:, position])
30-
position = rand(rng, region)
31-
else
32-
return position
33-
end
34-
end
35-
36-
@warn "Could not sample an empty position in max_tries = $(max_tries). Returning non-empty position: $(position)"
37-
38-
return position
39-
end
40-
41-
function sample_empty_position(rng, tile_map, region)
42-
max_tries = 1024 * length(region)
43-
position = sample_empty_position(rng, tile_map, region, max_tries)
44-
return position
45-
end
46-
47-
function sample_empty_position(rng, tile_map, max_tries::Integer)
48-
_, height, width = size(tile_map)
49-
region = CartesianIndices((1 : height, 1 : width))
50-
position = sample_empty_position(rng, tile_map, region, max_tries)
51-
return position
52-
end
53-
54-
function sample_empty_position(rng, tile_map)
55-
_, height, width = size(tile_map)
56-
region = CartesianIndices((1 : height, 1 : width))
57-
max_tries = 1024 * height * width
58-
position = sample_empty_position(rng, tile_map, region, max_tries)
59-
return position
60-
end
61-
625
abstract type AbstractGame end
636

64-
include("units.jl")
7+
function reset! end
8+
function act! end
9+
function cast_rays! end
10+
function get_action_keys end
11+
function get_action_names end
12+
function play! end
13+
function update_top_view! end
14+
function update_camera_view! end
15+
16+
include("utils.jl")
6517
include("collision_detection.jl")
66-
include("navigation.jl")
6718
include("rlbase.jl")
6819
include("single_room.jl")
6920

src/navigation.jl

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/units.jl

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#####
2+
##### unit conversions
3+
#####
4+
5+
wu_to_tu(x_wu) = floor(Int, x_wu) + 1
6+
wu_to_pu(x_wu, pu_per_wu) = floor(Int, x_wu * pu_per_wu) + 1
7+
pu_to_tu(i_pu, pu_per_tu) = (i_pu - 1) ÷ pu_per_tu + 1
8+
9+
#####
10+
##### navigation
11+
#####
12+
13+
turn_left(direction_au, num_directions) = mod(direction_au + 1, num_directions)
14+
turn_right(direction_au, num_directions) = mod(direction_au - 1, num_directions)
15+
16+
move_forward(position_wu, direction_wu, position_increment_wu) = position_wu + position_increment_wu * direction_wu
17+
move_backward(position_wu, direction_wu, position_increment_wu) = position_wu - position_increment_wu * direction_wu
18+
19+
#####
20+
##### sampling empty tiles on the tile map
21+
#####
22+
23+
function sample_empty_position(rng, tile_map, region, max_tries)
24+
position = rand(rng, region)
25+
26+
for i in 1:max_tries
27+
if any(@view tile_map[:, position])
28+
position = rand(rng, region)
29+
else
30+
return position
31+
end
32+
end
33+
34+
@warn "Could not sample an empty position in max_tries = $(max_tries). Returning non-empty position: $(position)"
35+
36+
return position
37+
end
38+
39+
function sample_empty_position(rng, tile_map, region)
40+
max_tries = 1024 * length(region)
41+
position = sample_empty_position(rng, tile_map, region, max_tries)
42+
return position
43+
end
44+
45+
function sample_empty_position(rng, tile_map, max_tries::Integer)
46+
_, height, width = size(tile_map)
47+
region = CartesianIndices((1 : height, 1 : width))
48+
position = sample_empty_position(rng, tile_map, region, max_tries)
49+
return position
50+
end
51+
52+
function sample_empty_position(rng, tile_map)
53+
_, height, width = size(tile_map)
54+
region = CartesianIndices((1 : height, 1 : width))
55+
max_tries = 1024 * height * width
56+
position = sample_empty_position(rng, tile_map, region, max_tries)
57+
return position
58+
end
59+
60+
#####
61+
##### transpose and copy image to minifb frame buffer
62+
#####
63+
64+
function copy_image_to_frame_buffer!(frame_buffer, image)
65+
height_image, width_image = size(image)
66+
for j in 1:width_image
67+
for i in 1:height_image
68+
frame_buffer[j, i] = image[i, j]
69+
end
70+
end
71+
72+
return nothing
73+
end

0 commit comments

Comments
 (0)