Advertisement
ekgame

BlockCad v1

Dec 13th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 9.48 KB | None | 0 0
  1. ;;;;;;;;;;;;;;
  2. ;;; Levels ;;;
  3. ;;;;;;;;;;;;;;
  4.  
  5. (setq levels (list         
  6.   (list
  7.     1 1 1 1 1 1 1 1 1 1
  8.     1 0 0 0 1 3 0 0 0 1
  9.     1 0 2 0 1 1 1 0 0 1
  10.     1 0 0 0 0 0 0 0 0 1
  11.     1 0 0 0 0 0 0 0 0 1
  12.     1 0 0 0 0 0 0 0 0 1
  13.     1 0 0 0 0 0 0 0 0 1
  14.     1 0 0 0 0 0 0 0 0 1
  15.     1 0 0 0 0 0 0 0 0 1
  16.     1 1 1 1 1 1 1 1 1 1
  17.   )
  18.  
  19.   (list
  20.     1 1 1 1 1 1 1 1 1 1
  21.     1 0 0 0 0 0 0 0 0 1
  22.     1 0 2 0 0 0 0 0 0 1
  23.     1 0 0 2 0 0 0 0 0 1
  24.     1 0 0 0 0 0 0 0 0 1
  25.     1 0 0 0 0 0 0 0 0 1
  26.     1 0 0 0 0 0 0 0 0 1
  27.     1 0 0 3 0 0 0 0 0 1
  28.     1 0 0 3 0 0 0 0 0 1
  29.     1 1 1 1 1 1 1 1 1 1
  30.   )
  31.  
  32.   (list
  33.     1 1 1 1 1 1 1 1 1 1
  34.     1 0 0 0 0 0 0 0 0 1
  35.     1 0 0 0 0 0 0 0 0 1
  36.     1 0 0 0 0 0 0 0 0 1
  37.     1 0 0 0 0 2 0 0 0 1
  38.     1 0 0 0 0 0 2 0 0 1
  39.     1 0 0 0 0 0 0 0 0 1
  40.     1 0 0 0 0 2 0 1 3 1
  41.     1 0 0 0 0 0 3 1 3 1
  42.     1 1 1 1 1 1 1 1 1 1
  43.   )
  44.  
  45.   (list
  46.     1 1 1 1 1 1 1 1 1 1
  47.     1 0 0 0 0 1 0 0 0 1
  48.     1 0 0 0 0 1 0 0 0 1
  49.     1 0 0 2 0 1 0 0 0 1
  50.     1 0 3 2 0 0 0 0 0 1
  51.     1 0 0 2 3 0 0 0 0 1
  52.     1 0 0 0 0 1 3 0 0 1
  53.     1 0 0 0 0 1 0 0 0 1
  54.     1 0 0 0 0 1 0 0 0 1
  55.     1 1 1 1 1 1 1 1 1 1
  56.   )
  57. ))
  58.  
  59. ;;;;;;;;;;;;;;;;;;;;;;;;
  60. ;;; Action constants ;;;
  61. ;;;;;;;;;;;;;;;;;;;;;;;;
  62.  
  63. (setq
  64.   action_none  0
  65.   action_up    1
  66.   action_down  2
  67.   action_left  3
  68.   action_right 4
  69.   action_reset 5
  70. )
  71.  
  72. ;;;;;;;;;;;;;
  73. ;;; UTILS ;;;
  74. ;;;;;;;;;;;;;
  75.  
  76. (defun nth-replace ( pos new-item lst )
  77.   (if (null lst)
  78.     nil
  79.     (cons
  80.       (if (eq pos 0) new-item (car lst))
  81.       (nth-replace (1- pos) new-item (cdr lst))
  82.     )
  83.   )
  84. )
  85.  
  86. (DEFUN SETUP ()
  87.   (COMMAND "OSNAP" "OFF")
  88. )
  89.  
  90. ;;;;;;;;;;;;;;;;;;;;;;;;;
  91. ;;; Level control API ;;;
  92. ;;;;;;;;;;;;;;;;;;;;;;;;;
  93.  
  94. (DEFUN GET_LEVEL_STATIC (id)
  95.   (subst 0 2 (nth id levels))
  96. )
  97.  
  98. (DEFUN GET_LEVEL (id)
  99.   (subst 0 3 (nth id levels))
  100. )
  101.  
  102. (DEFUN GET_BLOCK_AT (scene x y size)
  103.   (nth (+ x (* y size)) scene)
  104. )
  105.  
  106. (DEFUN SET_BLOCK_AT (scene x y size new-block)
  107.   (nth-replace (+ x (* y size)) new-block scene)
  108. )
  109.  
  110. (DEFUN RESET_SCENE (level)
  111.   (GET_LEVEL level)
  112. )
  113.  
  114. (DEFUN VALIDATE_POS (x y size)
  115.   (and
  116.     (and (>= x 0) (< x size))
  117.     (and (>= y 0) (< y size))
  118.   )
  119. )
  120.  
  121. (DEFUN COMPARE_SCENES (scene_dynamic scene_static / i block_static block_dynamic)
  122.   (setq i 0)
  123.   (setq result T)
  124.   (while (< i (length scene_dynamic))
  125.     (setq block_dynamic (nth i scene_dynamic))
  126.     (setq block_static (nth i scene_static))
  127.  
  128.     (if (= block_dynamic 2)
  129.       (if (/= block_static 3)
  130.         (progn
  131.       (setq result nil)
  132.       (setq i (length scene_dynamic))
  133.     )
  134.       )
  135.     )
  136.    
  137.     (setq i (1+ i))
  138.   )
  139.  
  140.   result
  141. )
  142.  
  143. ;;;;;;;;;;;;;;;
  144. ;;; Drawing ;;;
  145. ;;;;;;;;;;;;;;;
  146.  
  147. (DEFUN GRID (count spacing color / x len)
  148.   (COMMAND "COLOR" color)
  149.   (setq len (* count spacing))
  150.  
  151.   (setq x 0)
  152.  
  153.   (while (>= count 0)
  154.     (setq count (1- count))
  155.  
  156.     (COMMAND "LINE" (list x 0) (list x len) "")
  157.     (COMMAND "LINE" (list 0 x) (list len x) "")
  158.  
  159.     (setq x (+ x spacing))
  160.   )
  161. )
  162.  
  163. (DEFUN DRAW_BLOCK (x y size spacing color / drawx drawy p1 p2 p3 p4)
  164.   (setq drawx (* x spacing))
  165.   (setq drawy (* (- (1- size) y) spacing))
  166.  
  167.   (setq p1 (list drawx drawy))
  168.   (setq p2 (list (+ drawx spacing) drawy))
  169.   (setq p3 (list drawx (+ drawy spacing)))
  170.   (setq p4 (list (+ drawx spacing) (+ drawy spacing)))
  171.  
  172.   (COMMAND "COLOR" color)
  173.   (COMMAND "SOLID" p1 p2 p3 p4 "")
  174. )
  175.  
  176. (DEFUN RENDER_BUTTON (x y size action)
  177.   (COMMAND "RECTANG" (list x y) (list (+ x size) (+ y size)))
  178.  
  179.   (if (= action action_down)
  180.       (COMMAND "LINE" (list x (+ y size)) (list (+ x (/ size 2)) y) (list (+ x size) (+ y size)) "")
  181.   )
  182.  
  183.   (if (= action action_up)
  184.       (COMMAND "LINE" (list x y) (list (+ x (/ size 2)) (+ y size)) (list (+ x size) y) "")
  185.   )
  186.  
  187.   (if (= action action_left)
  188.       (COMMAND "LINE" (list (+ x size) (+ y size)) (list x (+ y (/ size 2))) (list (+ x size) y) "")
  189.   )
  190.  
  191.   (if (= action action_right)
  192.       (COMMAND "LINE" (list x y) (list (+ x size) (+ y (/ size 2))) (list x (+ y size)) "")
  193.   )
  194.  
  195.   (if (= action action_reset)
  196.       (COMMAND "CIRCLE" "2P" (list x (+ y (/ size 2))) (list (+ x size) (+ y (/ size 2))))
  197.   )
  198. )
  199.  
  200. (DEFUN POINT_IN_RECT (point x y w h / px py)
  201.   (setq px (nth 0 point)
  202.     py (nth 1 point))
  203.  
  204.   (and
  205.     (and (> px x) (> py y) )
  206.     (and (< px (+ x w)) (< py (+ y h)) )
  207.   )
  208. )
  209.  
  210. (DEFUN RENDER_CONTROLS ()
  211.   (RENDER_BUTTON -30 50 10 action_up)
  212.   (RENDER_BUTTON -30 30 10 action_down)
  213.   (RENDER_BUTTON -40 40 10 action_left)
  214.   (RENDER_BUTTON -20 40 10 action_right)
  215.   (RENDER_BUTTON -30 10 10 action_reset)
  216. )
  217.  
  218. (DEFUN RENDER_SCENE (scene scene_static size spacing / x y current total block)
  219.   (GRID size spacing "White")
  220.  
  221.   (setq current 0)
  222.   (setq total (* size size))
  223.   (setq x 0 y 0)
  224.  
  225.   (while (< current total)
  226.     (setq block (GET_BLOCK_AT scene x y size))
  227.     (setq block_static (nth current scene_static))
  228.  
  229.     (if (= block_static 3)
  230.       (DRAW_BLOCK x y size spacing "Cyan")
  231.     )
  232.  
  233.     (if (= block 1)
  234.       (DRAW_BLOCK x y size spacing "White")
  235.     )
  236.  
  237.     (if (= block 2)
  238.       (DRAW_BLOCK x y size spacing "Red")
  239.     )
  240.  
  241.     (setq x (1+ x))
  242.     (if (>= x size)
  243.     (setq x 0 y (1+ y))
  244.     )
  245.  
  246.     (setq current (1+ current))
  247.   )
  248. )
  249.  
  250. (DEFUN RESET ()
  251.   (COMMAND "ERASE" "ALL" "")
  252. )
  253.  
  254. ;;;;;;;;;;;;;;;;;;;;;;;;
  255. ;;; Game interaction ;;;
  256. ;;;;;;;;;;;;;;;;;;;;;;;;
  257.  
  258. (DEFUN PARSE_CLICK (point)
  259.   (cond ((POINT_IN_RECT point -30 50 10 10) action_up)
  260.     ((POINT_IN_RECT point -30 30 10 10) action_down)
  261.     ((POINT_IN_RECT point -40 40 10 10) action_left)
  262.     ((POINT_IN_RECT point -20 40 10 10) action_right)
  263.     ((POINT_IN_RECT point -30 10 10 10) action_reset)
  264.     (t action_none)
  265.   )
  266. )
  267.  
  268. (DEFUN HANDLE_ACTION (action scene size level)
  269.   (cond
  270.     ((= action action_up)    (MOVE_UP scene size))
  271.     ((= action action_down)  (MOVE_DOWN scene size))
  272.     ((= action action_left)  (MOVE_LEFt scene size))
  273.     ((= action action_right) (MOVE_RIGHT scene size))
  274.     ((= action action_reset) (RESET_SCENE level))
  275.     (t scene)
  276.   )
  277. )
  278.  
  279. (DEFUN MOVE_UP (scene size / x y new_scene block block_destination)
  280.   (setq x 0 y 0)
  281.   (setq new_scene scene)
  282.  
  283.   (while (< y size)
  284.     (setq block (GET_BLOCK_AT new_scene x y size))
  285.  
  286.     (cond
  287.       ((and (VALIDATE_POS x (1- y) size) (= block 2))
  288.        (setq block_destination (GET_BLOCK_AT new_scene x (1- y) size))
  289.        
  290.        (cond ((= block_destination 0)
  291.      (setq new_scene (SET_BLOCK_AT new_scene x (1- y) size 2))
  292.      (setq new_scene (SET_BLOCK_AT new_scene x y size 0))
  293.        ))
  294.       )
  295.     )
  296.  
  297.     (setq x (1+ x))
  298.     (cond ((>= x size)
  299.       (setq y (1+ y))
  300.       (setq x 0)
  301.     ))
  302.   )
  303.  
  304.   new_scene
  305. )
  306.  
  307. (DEFUN MOVE_DOWN (scene size / x y new_scene block block_destination)
  308.   (setq x 0 y (1- size))
  309.   (setq new_scene scene)
  310.  
  311.   (while (>= y 0)
  312.     (setq block (GET_BLOCK_AT new_scene x y size))
  313.  
  314.     (cond
  315.       ((and (VALIDATE_POS x (1+ y) size) (= block 2))
  316.        (setq block_destination (GET_BLOCK_AT new_scene x (1+ y) size))
  317.        
  318.        (cond ((= block_destination 0)
  319.      (setq new_scene (SET_BLOCK_AT new_scene x (1+ y) size 2))
  320.      (setq new_scene (SET_BLOCK_AT new_scene x y size 0))
  321.        ))
  322.       )
  323.     )
  324.  
  325.     (setq x (1+ x))
  326.     (cond ((>= x size)
  327.       (setq y (1- y))
  328.       (setq x 0)
  329.     ))
  330.   )
  331.  
  332.   new_scene
  333. )
  334.  
  335. (DEFUN MOVE_LEFT (scene size / x y new_scene block block_destination)
  336.   (setq x 0 y 0)
  337.   (setq new_scene scene)
  338.  
  339.   (while (< x size)
  340.     (setq block (GET_BLOCK_AT new_scene x y size))
  341.  
  342.     (cond
  343.       ((and (VALIDATE_POS (1- x) y size) (= block 2))
  344.        (setq block_destination (GET_BLOCK_AT new_scene (1- x) y size))
  345.        
  346.        (cond ((= block_destination 0)
  347.      (setq new_scene (SET_BLOCK_AT new_scene (1- x) y size 2))
  348.      (setq new_scene (SET_BLOCK_AT new_scene x y size 0))
  349.        ))
  350.       )
  351.     )
  352.  
  353.     (setq y (1+ y))
  354.     (cond ((>= y size)
  355.       (setq x (1+ x))
  356.       (setq y 0)
  357.     ))
  358.   )
  359.  
  360.   new_scene
  361. )
  362.  
  363. (DEFUN MOVE_RIGHT (scene size / x y new_scene block block_destination)
  364.   (setq x size y 0)
  365.   (setq new_scene scene)
  366.  
  367.   (while (>= x 0)
  368.     (setq block (GET_BLOCK_AT new_scene x y size))
  369.  
  370.     (cond
  371.       ((and (VALIDATE_POS (1+ x) y size) (= block 2))
  372.        (setq block_destination (GET_BLOCK_AT new_scene (1+ x) y size))
  373.        
  374.        (cond ((= block_destination 0)
  375.      (setq new_scene (SET_BLOCK_AT new_scene (1+ x) y size 2))
  376.      (setq new_scene (SET_BLOCK_AT new_scene x y size 0))
  377.        ))
  378.       )
  379.     )
  380.  
  381.     (setq y (1+ y))
  382.     (cond ((>= y size)
  383.       (setq x (1- x))
  384.       (setq y 0)
  385.     ))
  386.   )
  387.  
  388.   new_scene
  389. )
  390.  
  391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  392. ;;; Program loop          ;;;
  393. ;;; Usage (command): game ;;;
  394. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  395.  
  396. (DEFUN c:game ()
  397.   (setq current_level 0)
  398.   (setq current_scene (GET_LEVEL current_level))
  399.   (setq current_scene_static (GET_LEVEL_STATIC current_level))
  400.  
  401.   (SETUP)
  402.  
  403.   (setq loop 1)
  404.   (while (= loop 1)
  405.     (RESET)
  406.     (RENDER_SCENE current_scene current_scene_static 10 10)
  407.     (RENDER_CONTROLS)
  408.    
  409.     (setq point (getpoint "\n click a button: "))
  410.     (setq action (PARSE_CLICK point))
  411.     (prompt (strcat (itoa action)))
  412.     (setq current_scene (HANDLE_ACTION action current_scene 10 current_level))
  413.     (if (COMPARE_SCENES current_scene current_scene_static)
  414.       (progn
  415.     (setq current_level (1+ current_level))
  416.     (setq current_scene (GET_LEVEL current_level))
  417.     (setq current_scene_static (GET_LEVEL_STATIC current_level))
  418.       )
  419.     )
  420.  
  421.     (if (>= current_level (length levels))
  422.       (progn
  423.     (setq loop 0)
  424.     (alert "Well done! You've completed the game.\n\nBlockCad v1 by ekgame\n2014-12-13")
  425.       )
  426.     )
  427.   )
  428. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement