Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active December 19, 2019 20:30
Show Gist options
  • Save snipsnipsnip/1217262 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/1217262 to your computer and use it in GitHub Desktop.
ants.hsp
#define INITW 400
#define INITH 250
#packopt name "ants"
#packopt hide 1
#define trackbar_onscroll(%1) oncmd %1, 0x114
#module mod_trackbar self
#modinit int min, int max, int ifreq, int ipos
winobj "msctls_trackbar32", "", , 0x50000000 | 1, 200, 30
self = objinfo(stat, 2)
sendmsg self, 0x407, 1, min
sendmsg self, 0x408, 1, max
sendmsg self, 0x405, 1, ipos
sendmsg self, 0x414, 1, freq
return
#modcfunc trackbar_is_selected
return lparam == self
#modcfunc trackbar_get
sendmsg self, 0x400
return stat
#global
#module
#defcfunc wrap int x, int from, int to
if x < from { return to }
if x > to { return from }
return x
#global
#define ctype ant_x_at(%1) ants(%1, 0)
#define ctype ant_y_at(%1) ants(%1, 1)
#define ctype ant_dir_at(%1) ants(%1, 2)
#define ctype ant_r_at(%1) ants(%1, 3)
#define ctype ant_g_at(%1) ants(%1, 4)
#define ctype ant_b_at(%1) ants(%1, 5)
#define ant_x ant_x_at(cnt)
#define ant_y ant_y_at(cnt)
#define ant_dir ant_dir_at(cnt)
#define ant_r ant_r_at(cnt)
#define ant_g ant_g_at(cnt)
#define ant_b ant_b_at(cnt)
*defaults
gsel 0, -1
randomize
ANTS_COUNT = 5
RANDOM_TILES = 1
FULLSCREEN = 0
ZOOM = 10
SHRINK_TILES = 1
gosub *init_config_window
goto *config
*init_config_window
screen 2, ginfo_dispx, ginfo_dispy, 64, (ginfo_dispx - INITW) / 2, (ginfo_dispy - INITH) / 2, INITW, INITH
return
*config
if FULLSCREEN {
gosub *init_config_window
}
title "ants"
trackbar_onscroll *scroll
gosub *draw_labels
pos 10, 10
objsize 300, 30, 20
chkbox "フルスクリーン", FULLSCREEN
chkbox "床のタイルをランダムに初期化", RANDOM_TILES
chkbox "床のタイルをふちどり", SHRINK_TILES
objsize 100, 30, 30
newmod antbar, mod_trackbar, 1, 100, 1, ANTS_COUNT
objsize 100, 30, 40
newmod zoombar, mod_trackbar, 1, 20, 1, ZOOM
button "start", *start
stop
*start
clrobj
delmod antbar
delmod zoombar
goto *main
*scroll
if trackbar_is_selected(antbar) {
ANTS_COUNT = trackbar_get(antbar)
}
if trackbar_is_selected(zoombar) {
ZOOM = trackbar_get(zoombar)
}
gosub *draw_labels
stop
*draw_labels
objsize
redraw 0
syscolor 15
boxf
color
sysfont 17
pos 220, 110
mes "アリの数: " + ANTS_COUNT
pos 220, 140
mes "タイルのサイズ: " + ZOOM
pos 10, 205
mes "実行中スペースでアリを非表示"
mes "タブで1000倍速 リターンでポーズ"
mes "エスケープでこの画面に戻る"
redraw 1
return
*main
gosub *init
repeat
gosub *tick
redraw 0
gosub *draw
redraw 1
if esc {
goto *config
}
await 0
loop
*init
if FULLSCREEN {
bgscr 2, ginfo_dispx, ginfo_dispy, 0, 0, 0
}
c = ZOOM
w = ginfo_winx / c
h = ginfo_winy / c
speed = 1
dim world, h, w
if RANDOM_TILES {
repeat length(world)
y = cnt
repeat length2(world)
x = cnt
world(y, x) = rnd(2)
loop
loop
}
dim ants, ANTS_COUNT, 6
foreach ants
ant_x = rnd(w)
ant_y = rnd(h)
ant_dir = rnd(4)
ant_r = 50 + rnd(100)
ant_g = 50 + rnd(100)
ant_b = 50 + rnd(100)
loop
speed = 1
return
*tick
stick keys
esc = keys & 128
ret = keys & 32
getkey tab, 9
if speed = 0 {
if ret {
speed = 1
}
} else : if tab {
speed = 1000
} else : if ret {
speed = 0
} else {
speed = 1
}
repeat speed
gosub *step_ants
loop
return
*step_ants
step++
foreach ants
if world(ant_y, ant_x) \ 2 {
ant_dir = (ant_dir + 1) \ 4
world(ant_y, ant_x) = cnt * 2 + 2
} else {
ant_dir--
if ant_dir < 0 {
ant_dir = 3
}
world(ant_y, ant_x) = cnt * 2 + 3
}
if ant_dir = 0 {
ant_x--
} else : if ant_dir = 1 {
ant_y--
} else : if ant_dir = 2 {
ant_x++
} else {
ant_y++
}
ant_x = wrap(ant_x, 0, w - 1)
ant_y = wrap(ant_y, 0, h - 1)
loop
return
*draw
title "" + step
color 255, 255, 255
boxf
color
repeat length(world)
y = cnt
repeat length2(world)
x = cnt
if world(y, x) = 0 { continue }
if world(y, x) = 1 {
color 200, 255, 200
} else {
antid = world(y, x) / 2 - 1
if world(y, x) \ 2 = 0 {
color ant_r_at(antid) + 100, ant_g_at(antid) + 100, ant_b_at(antid) + 100
} else {
color ant_r_at(antid) + 50, ant_g_at(antid) + 50, ant_b_at(antid) + 50
}
}
if SHRINK_TILES {
boxf x * c + 1, y * c + 1, x * c + c - 1, y * c + c - 1
} else {
boxf x * c, y * c, x * c + c, y * c + c
}
loop
loop
getkey space, 32
if space { return }
foreach ants
color ant_r, ant_g, ant_b
boxf ant_x * c, ant_y * c, ant_x * c + c, ant_y * c + c
if ant_dir = 0 {
boxf ant_x * c - 5, ant_y * c + 2, ant_x * c, ant_y * c + c - 2
} else : if ant_dir = 1 {
boxf ant_x * c + 2, ant_y * c - 5, ant_x * c + c - 2, ant_y * c
} else : if ant_dir = 2 {
boxf ant_x * c + c, ant_y * c + 2, ant_x * c + c + 5, ant_y * c + c - 2
} else {
boxf ant_x * c + 2, ant_y * c + c, ant_x * c + c - 2, ant_y * c + c + 5
}
loop
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment