[REBOL] Slow map generator
From: oliva:david:seznam:cz at: 8-Nov-2001 19:39
Slow map generator
==================
I know, that Rebol is not the best tool for things like that, but I cannot help myself...
...Does anybody know how to speed up the script below somehow?
What's so slow? I'm going thru a matrix (image in this example) to create random heights
of the map and then several times to make somethink like erosion of the land. I need
to find adjacent cells in the matrix and that's the slow thing I think:-)
Holger, do you think it would be posible to make some native support for that kind of
operations (matrix neighbours) I would find it useful in more of my scripts (in the RBox2
as well) - it's always the slowest part?
thank you, Oldes
;----------------------------------
rebol [title: "map generator test"]
map_size_x: 200
map_size_y: 200
random/seed 1000
height_map: make block! (map_size_x * map_size_y)
set-pixel: func [
im [image!]
x [integer!]
y [integer!]
color [tuple!]
/local x-siz y-siz siz
][
x-siz: im/size/x
y-siz: im/size/y
poke im (y-siz - y * x-siz + x) color
]
t: now/precise/time
for y 1 map_size_y 1 [
for x 1 map_size_x 1 [
insert tail height_map (random 40 + ((500 - abs (map_size_y / 2 - y)) / 10))
]
]
probe t: now/time/precise - t
map_adjust_y: func[y][either y < 1 [map_size_y][either y > map_size_y [1][y]]]
map_adjust_x: func[x][either x < 1 [map_size_x][either x > map_size_x [1][x]]]
get_hmap: func[x y][pick height_map ((y - 1) * map_size_y + x)]
set_hmap: func[x y val][poke height_map ((y - 1) * map_size_y + x) val]
smooth_map: func[][
for y 1 map_size_y 1 [
my: map_adjust_y (y - 1)
py: map_adjust_y (y + 1)
for x 1 map_size_x 1 [
mx: map_adjust_x (x - 1)
px: map_adjust_x (x + 1)
a: 2 * get_hmap x y
a: a + get_hmap px my
a: a + get_hmap mx my
a: a + get_hmap mx py
a: a + get_hmap px py
a: a + get_hmap x my
a: a + get_hmap mx y
a: a + get_hmap x py
a: a + get_hmap px y
a: max (a - 30 + random 60) 0
set_hmap x y a / 10
]
]
]
peeks: make block! 40
loop 20 [
x: random map_size_x
y: random map_size_y
insert tail peeks reduce [x y]
set_hmap x y 255
x: x - 1
set_hmap x y min (60 + get_hmap x y ) 255
y: y - 1
set_hmap x y min (60 + get_hmap x y ) 255
]
t: now/precise/time
loop 8 [smooth_map] ;really slow loop (map erosion)
probe t: now/time/precise - t
map-img: make image! to-pair reduce [map_size_x map_size_y]
forall height_map [
poke map-img index? height_map 1.1.1 * first height_map
]
foreach [x y] peeks [set-pixel map-img x y 255.100.0]
height_map: head height_map
view/options center-face layout [origin 0x0 image map-img effect [luma 10 contrast]]
[no-border no-title]