gtn.l 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. (
  2. (def cursor 0)
  3. (def screen-w 80)
  4. (def screen-h 32)
  5. (def screen (alloc (* screen-w screen-h)))
  6. (def tilemap (alloc (* screen-w screen-h)))
  7. (def solidmap (alloc (* screen-w screen-h)))
  8. (def fill (fn buf from to c (do
  9. (print (list "fill" (size buf) from to c))
  10. (let i from)
  11. (while (lt i (+ to 1)) (do
  12. (put buf i c)
  13. (let i (+ i 1))
  14. ))
  15. )))
  16. (def buf-render (fn b bx by (do
  17. (let y 0)
  18. (while (lt y screen-h) (do
  19. (blit-str (substr b (* y screen-w) screen-w) bx (+ by (* y 16)))
  20. (let y (+ y 1))
  21. ))
  22. 0
  23. )))
  24. ; symbols ------------------------------------------------------
  25. (def sym-block (get "#" 0))
  26. (def sym-umbrella (get "m" 0))
  27. (def sym-coffee (get "c" 0))
  28. (def sym-scissors (get "x" 0))
  29. (def sym-zigzag (get "z" 0))
  30. (def sym-dog (get "D" 0))
  31. (def sym-pot (get "p" 0))
  32. (def sym-pill (get "." 0))
  33. (def sym-egg (get "o" 0))
  34. (def sym-ear (get "e" 0))
  35. (def sym-rabbit (get "R" 0))
  36. (def sym-table (get "T" 0))
  37. (def sym-salad (get "s" 0))
  38. (def sym-hot (get "~" 0))
  39. (def sym-fish (get "<" 0))
  40. (def sym-disc (get "0" 0))
  41. (def sym-potb (get "p" 0))
  42. (def sym-person (get "@" 0))
  43. (def sym-blockb (get "=" 0))
  44. ; structures -----------------------------------------------------------
  45. ; rune color solid
  46. (def tiles (list))
  47. (def num-tiles 0)
  48. (def make-tile (fn definition (do
  49. (let new-tile (cons num-tiles definition))
  50. (def tiles (cons new-tile tiles))
  51. (def num-tiles (+ num-tiles 1))
  52. (print (list "added tile" new-tile))
  53. new-tile
  54. )))
  55. (def tile-space (make-tile (list 32 0 0 "space")))
  56. (def tile-wall (make-tile (list sym-block 12 1 "wall")))
  57. (def tile-asphalt (make-tile (list (get "." 0) 11 0 "asphalt")))
  58. (def tile-floor-yellow (make-tile (list (get "_" 0) 7 0 "yellow floor")))
  59. (def tile-floor-wood (make-tile (list (get "_" 0) 8 0 "wooden floor")))
  60. (def tile-floor-woodl (make-tile (list (get "_" 0) 9 0)))
  61. (def tile-dog (make-tile (list sym-dog 1 0 "dog")))
  62. (def tile-pot (make-tile (list sym-pot 1 0 "pot")))
  63. (def tile-pill (make-tile (list sym-pill 1 0 "pill")))
  64. (def tile-coffee (make-tile (list sym-coffee 1 0 "coffee")))
  65. (def tile-window (make-tile (list sym-blockb 12 0 "window")))
  66. (def tile-player (make-tile (list sym-person 10 1)))
  67. ; rat
  68. ; pills
  69. ; money
  70. ; coffee
  71. (def put-tile (fn x y tile (do
  72. (put tilemap (+ x (* screen-w y)) (car tile))
  73. (put screen (+ x (* screen-w y)) (car (cdr tile)))
  74. (put solidmap (+ x (* screen-w y)) (car (cdr (cdr (cdr tile)))))
  75. )))
  76. (def is-tile-solid (fn tile (do
  77. (eq 1 (car (cdr (cdr tile))))
  78. )))
  79. (def get-tile-rune (fn x y (do
  80. (get screen (+ x (* screen-w y)))
  81. )))
  82. (def get-tile-solid (fn x y (do
  83. (get solidmap (+ x (* screen-w y)))
  84. )))
  85. (def get-tile (fn x y (do
  86. (let tile-id (get tilemap (+ x (* screen-w y))))
  87. (item tiles (- (- num-tiles tile-id) 1))
  88. )))
  89. (def tile-rect (fn x y xx yy tile (do
  90. (let i y)
  91. (while (lt y (+ yy 1)) (do
  92. (put-tile x y tile)
  93. (put-tile xx y tile)
  94. (let y (+ y 1))
  95. ))
  96. (let y i)
  97. (while (lt x (+ xx 1)) (do
  98. (put-tile x y tile)
  99. (put-tile x yy tile)
  100. (let x (+ x 1))
  101. ))
  102. )))
  103. (def fill-tile-rect (fn x y xx yy tile (do
  104. (print (cons "fill-tile-rect" tile))
  105. (print (list x y xx yy))
  106. (while (lt y (+ yy 1)) (do
  107. (let i x)
  108. (while (lt i (+ xx 1)) (do
  109. (put-tile i y tile)
  110. (let i (+ i 1))
  111. ))
  112. (let y (+ y 1))
  113. ))
  114. )))
  115. ; game ------------------------------------------------------------------------
  116. (def make-room (fn x y xx yy (do
  117. (fill-tile-rect (+ x 1) (+ y 1) xx yy tile-floor-yellow)
  118. (tile-rect x y xx yy tile-wall)
  119. (put-tile (/ (+ x xx) 2) y tile-floor-yellow)
  120. )))
  121. (def state-init 1)
  122. (def state-playing 2)
  123. (def state state-init)
  124. (def screen-size (* screen-w screen-h))
  125. (def actors (quote ()))
  126. (def make-actor (fn tile x y
  127. (cons tile (cons x (cons y nil)))
  128. ))
  129. (def add-actor (fn a (do
  130. (def actors (cons a actors))
  131. )))
  132. (def actor-tile (fn a (car a)))
  133. (def actor-rune (fn a (car (cdr (actor-tile a)))))
  134. (def actor-color (fn a (car (cdr (cdr (actor-tile a))))))
  135. (def actor-x (fn a (car (cdr a))))
  136. (def actor-y (fn a (car (cdr (cdr a)))))
  137. (def player (make-actor tile-player 13 13))
  138. ; keyboard -----------------------------------------------------------
  139. (def move-player (fn nx ny (do
  140. (if (get-tile-solid nx ny)
  141. 0
  142. (do
  143. (def player (make-actor (actor-tile player) nx ny))
  144. (def actors (quote ()))
  145. (add-actor player)))
  146. )))
  147. (def player-west (fn (do
  148. (let nx (- (actor-x player) 1))
  149. (let ny (actor-y player))
  150. (move-player nx ny)
  151. )))
  152. (def player-east (fn (do
  153. (let nx (+ (actor-x player) 1))
  154. (let ny (actor-y player))
  155. (move-player nx ny)
  156. )))
  157. (def player-north (fn (do
  158. (let nx (actor-x player))
  159. (let ny (- (actor-y player) 1))
  160. (move-player nx ny)
  161. )))
  162. (def player-south (fn (do
  163. (let nx (actor-x player))
  164. (let ny (+ (actor-y player) 1))
  165. (move-player nx ny)
  166. )))
  167. (def px 0)
  168. (def py 0)
  169. (def player-tile (list))
  170. (def inventory (list))
  171. (def update-player-tile (fn (do
  172. (def px (actor-x player))
  173. (def py (actor-y player))
  174. (def player-tile (get-tile px py))
  175. )))
  176. (def handle-game-key (fn k (do
  177. (if (eq k 20) (player-east)
  178. (if (eq k 19) (player-west)
  179. (if (eq k 17) (player-north)
  180. (if (eq k 18) (player-south) 0
  181. ))))
  182. (update-player-tile)
  183. (if (eq k (get "t" 0)) (do
  184. ; take item
  185. (def inventory (cons player-tile inventory))
  186. (put-tile px py tile-asphalt)
  187. ) 0)
  188. )))
  189. ; --------------------------------------------------------------
  190. (def ox 0)
  191. (def oy 0)
  192. (def actors-render (fn ox oy (do
  193. (let ad actors)
  194. (let aa (car ad))
  195. (while aa (do
  196. (blit-char (actor-rune aa) (* 8 (actor-x aa)) (* 16 (actor-y aa)))
  197. (let ad (cdr ad))
  198. (let aa (car ad))
  199. 0
  200. ))
  201. )))
  202. (def main (fn
  203. (while 1 (do
  204. (if (eq state state-init) (do
  205. ;(fill colormap 0 (- screen-size 1) 0)
  206. (fill screen 0 (- screen-size 1) 32)
  207. ;(fill screen 0 (- screen-size 1) sym-block)
  208. (fill-tile-rect 0 0 screen-w 11 tile-asphalt)
  209. (make-room 10 10 25 22)
  210. (put-tile 15 15 tile-dog)
  211. (put-tile 17 17 tile-pill)
  212. (put-tile 18 17 tile-coffee)
  213. (make-room 25 15 38 31)
  214. (put-tile 27 18 tile-pill)
  215. (put-tile 27 19 tile-pill)
  216. (print (cons "state" state))
  217. (print (cons "screen-size" screen-size))
  218. (print (cons "sym-block" sym-block))
  219. (def player (make-actor tile-player 13 13))
  220. (add-actor player)
  221. (def state state-playing)
  222. ) 0)
  223. (let str (recv keyboard))
  224. (let c (get str 0))
  225. (handle-game-key c)
  226. (buf-render screen 0 0)
  227. (actors-render 0 0)
  228. (p (list px py player-tile inventory) 0 0)
  229. (send scr 0)
  230. (gc)
  231. ))
  232. ))
  233. (main)
  234. )