0     +S
b)T袈yB!6vf  '} M
          2TvͫgE#2Tv2Tv͛W# "Cv 1ewVDuެh43d CTUfvww   pwww                                 """"""""""""""""""""""""""""""""33333333333333333333333333333333UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUffffffffffffffffffffffffffffffffwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww                                                      	 	                 	                                                                                                                                                                                                                                                                                                                                                                                                                                                          	 	 	   	  	  	                                                                                                                       	    	                  	                                                                                                                                                          	  	  	                                            	                                                                                                                                                                                  	                                                                                                                                                     	   	                                                                 	   	                                                                                                                                                                  	              	                                                                                                     	  	 	 	 	 	 		 	                                                                                                                                                                                                                                                                                                                     	                	      	                                  	 	      	                                                                                                                                                                                                                                                                                                                                                       	      	      	       	                                                                                                                                                                                                                                                                                                                                                                                  	                                                                     	   	 	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                """"""""""""""""""""""""""""""""DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD3"""C4"CDC""""""""4"""DD4"CDDDCDDCD3""""""""""""""""4"""DD4"DDDD3333      "  "  "" "" 4""333   0   C  0D  CD 0DD CDD0D4U3VVVDDDDDDDDDDDDDDDDDD4U4UUUUUUUVVVVDDDDDDDDDD4U4UUUUUUeUUeUefDD4e4UeUee""""""""""""""""""""""""                            """"""""""""""""""""""""                         "  "3 "33"333   "  "3 "33"3333333333333333333"   3"  33" 333"3333333333333333                "   3"  33" 333"3333333333333333333"33"3""333"33"3"""333"33"3"3333333333333333"333"33"3"                     " """""     " """""""""""""""""""""!""""""!""""!""!!""""""!""""!" "  !   """"""""""""""""""""" "     """"" "                           "  "  "" "" """"""3   D3  DD3 DDD3DDDDDDDDDDDDDDDD                3   D3  DD3 DDD3DDD4DDDDD4DDD4D4DDD4DDDDD4DDD4D 4     3DDD 3DD  3D   3                DDDDDDDDDDDDDDDD3DDD 3DD  3D   3   0   C  0D  CD 0DD CDD0DDDCDDD   V  VU VUUVUUUUUUUUUUUUUUUVVVVVVVVUUUUUUUUUUUUUUUeUUeUeeVVVfUUeUee                   V  VU VUUfeeeUUUUUDDUDUUUUUDDDDDDDDDDDDUDDDUDDUDUU   DU  DDU DDDUDDDDDDDDDDDDUUUU                U   DU  DDU UUUU""""""""""""""""""""""""""""      "  "  "" "" """"""""33"3"""""333333333333"333"33"3"""""333333333333333"33"3""""""33"3""   0   C  0D  CD 0DD CDD0DDD3333DDDDDDDDDDDDDDDDDDDDDDDDDDDD3333                   "  "3 "33""""   "  "3 "33"333333333333333"""""   3"  33" 333"333333333333""""                "   3"  33" """"   P   E  PD  ED PDD EDDPDDDEDDDUUUUDDDDDDDDDDDDDDDDDDDDDDDDDDDDUUUUDDDDDTDDDTDTDDDTDDDDDTDDDTDT   P   E  PD  ED PDD EDDPDDDUUUUDDDDDDDDDDDDDDDDDDDDDDDDDDDDUUUU"""!""""!""!""""""""""""""""""""""""""""      !  "  !" "" !"""""!"""                                                                UUUUUU UwUwwUpUUUU PUUwPUwwUxUxPxPxPUUUUUUU UU xUxwUwUUpUpUUUU  UUw UPPPPa
PUUUUUUUUUUUwUUpwUxwUwwUwwUUUU   UwwwPwfUUUUUUU UU xUxUUUpwwUpwwUUUU  UUwPUwUwUwwPwwwPww QUUUUUU UwUwwUpwxwwwwwUUUU PUUwPUwwUwUzPwwPwwwPUUUUUU  UxwUUpxwwww wwUUUU UUU UUzPUwPUzwUwwUwwUUUUUU   wwwwfUUUUPUUUPUUwUUwUUwPUwwPUwwPUUUUUUU  U xwxUUUU UUU UUwPUxwPUxUxUxUafjpU όUfqPjqPP U̻PPPUUpwUwUwUUUUUUaUUͻP̼PPPU݈UUxUxUUUUUUUU UUUUjfPPUUUPPUpwwUpwwUwwU xwU  U˻UUw`QwU UPUPUPUUUwwwxwwpwwUww x UwwwPwwPwwPwwU P PP˻UpwUwU xUUUUU˻wwUwwUwwUw U  UUUUafU̼UU޻xPUxPUxUUUUPUU UUUUUUUaU͈UЎU݁wUwUwPUUUPUPUPUPUUЌUU  U hU  UUUP P P U  PUUUU̼U hUU  UU̼UP hP   P   U  UUUUUUU    U `fUU  UUU UPfP  P  U  UUUUU      U   UU  UPU UPPP` U  UUUUU  U hU  UUUP P P U  PUU̸UU `U `U  UUU P h P   P   U  UUÜUUU f  U  `UU  UUUU U P̌ P̋ Pf U  UUUU`    U   UU  PUPUUPPlP` U  UUUUUUUUUUUUUUUUUUUU UwUwwUpUUUUUUUUUUUUUUUU PUUwPUwwUxUUUUUUUUUUUUUUUUUUUU UU xUxwUwUUUUUUUUUUUUUUUU  UUw UPPUUUUUUUUUUUUUUUUUUU UU xUxwUwUUUUUUUUUUUUUUUU  PUwwUXXUUUUUUUUUUUUUUUUUUU UU xUxUUUUUUUUUUUUUUUUU  UUwPUwUwUUUUUUUUUUUUUUUUUUU UwUwwUpwUUUUUUUUUUUUUUUU PUUwPUwwUwUUUUUUUUUUUUUUUUUUU  UxwUUpUUUUUUUUUUUUUUUU UUU UUzPUwPUUUUUUUUUUUUUUUUUU  UpwwxUUUUUUUUUUUUUUUU UUU UUwPUxwPUUUUUUUUUUUUUUUUUUU  U xwxUUUUUUUUUUUUUUUU UUU UUwPUxwPUafjpU ̚xPxPxPfqPjqPP U̻PUUpUpUpwUwUwUUUPPa
PaUUͻP̌PPUwUpwUpwUwUwUxUU˚XQaQffQUUPUPUUUpwwUpwwUpwwUpwwUwwU xwU  wwPwwwPww Qw`QwU UPUPUxwwwwwwwwxwwpwwUww xzPwwPwwwPwwwPwwPwwPwwU Pxwwww wwpwUwU xUUzwUwwUwwUwwUwwUwwUw U  UjffUUUUxwUxwUxwUwUwUPUPUUUUaU͈xUxUxUwUwUwPUUUPUόUUЌU  U hU  PUUU P P U  PUUUUU̼U hUU  U݈UUU hP   P   U  UUUUUUU  U `fUU  UUȌPUUUfP  P  U  UUUUUU     U   UU  UUUPUPP` U  UU UU  U hU   PPUU P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3333333c33cj33fj3ff3Sff3cfffff:333333V533f:33Z33f33f535333333333333333333333333333333f3333333S33jf3Sffjffjfffffffffffjffffffffffffffffffffffffffffffffffffffffffffff fff fff fff fff 3jjjfjffffffffffffff3333333jjfffjfffffffffff333333335333:33:3Z5::33333333333333333333""""""""33f33f33f3333""33""""""ffffffffffffj3"#33""""fffffffffff"233""""fff ff jfjfff333#"""""ffffjfffffffff2333""""fffffjff9"33""""":3233""""""""33333333333333333333""""""""""""""!""!""!""!""!"""""""""""""""""""""""""""""""""""""""""""""""!"""!"""""""""!"""!"""!""""""""""""""""""""""""""3"""3"""2"""3"""3""""""""3#""3"""3"""3"""3#3"333#""""""""""""""""""""2"""2#""!!""!"!"!"!""""2"""3"""3""2#""3"""3#""332333"""""""""""""""2""2323332#""3#"""""#"""2"""2"""""""""""2"""2""#3""""""""3"""3"""3"""3"""3"3#333#"""3"""2""""""""3"""#"3##23#"23#3333"333"333"233"3332233233323333#"#3#233#2"33""33""3#""3#""3"2"""""""""""""""""""            !"!""!""!""!"""""""""""333323332333"333""23""23""""""""3"3"333"333#3332333#3#3"""#""23""233"233"2333333333333#3"333"233333#3333333333333333333333333333"23#2"333"333333333S333S3U33353"233333333333US355U33533333"#3#""3#3"333#333#333"3333335333333333"""""""""""""""##""##"2##"2##3#3                                            """""""""!"""!"""1"""!"""!"""""""""""""""""""""#"""#"""3""23""23""23""33#"33#"333"333"3332333"333#33333333333"3333333#33333333333333333333333333333333S533333U33#323#333"3"2"2"""#""""""""""33#""2"""3"""#"""#""""""""#""3"""33333333"233""23""2#""2""""""2333#S33#S3#""""1"""2#"#!32#23223333!3!"233"333""33#"3332"#33#"33333333#2"2#"23#"2#2"""""""#"""#23"323#3333323333333#33#"#3""""""""""""333333333333333#33#""3""23""23""333333333#23"""#"""#"""#"""#""""3""23"""#"""""""""""#"""#"""3"""""2""""""""""2#"2#"2"3                  !333!33!3323232"1"!#333333#333323332#333"#"3"""3"""2##""##""33""33""33""33""#3""#"""2#""3#""2#""2#""2#""2#"""#"""#"""""2"""2"""2"""3"""3"""""""""""#3"""3"""3"""#"""#""2"""3"""3""""""""2"""3"""3"""3"""3#233#232333"3"3233#33 3                    !" # 3  2  1  1  !   3""""2"""33"333#33333"""3"#23#"2#"""#"""""""""""3"""3"""3"""#"""""23"""#"""#"""#"""#""""""""""""""""""""""""""33""33"233"3##"2#"233323333#333"33#"3#"3""3"2"S 3                                                33"2Q#"2Q3""13"2S"23## Q33 153"""3#""23""""#"#"2"32#"33#"3"#2"""""""""""3""#"""3#"23#23333333"#"""3"""2#""3"2"233"33#333"3S33""#"3#"332333333"3332333233333332333333333 33 33     5  3   1   1              #"""3"#"3#33333232331313Q  333S333333##33#2333#333#US33533333323332333"23#23353 333333 S                                                                                                                                                                       Ӹ޹           !"# !"#   !"# N !"# !"#N !"# !"# !                                                                                                                        ۷      C   -./N123-./0123N -./0123-./N^0123-./012N-./-./^0123N0123-./01                                                                                                                                    =>?^ABN=>?@ABN^ =>?NABC=>?^ @ABN=>?@AB^=>?=>? @ABC^@ABC=>?@A                                                                                                                        иӸ           N  N ^ N  N ^   N ^N   N    N ^ N  N   N  N   N    N   N  N                                                                                                                        ȷ           ^  ^   ^  ^     ^  ^   ^    ^   ^  ^   ^  ^   ^    ^   ^  ^                                                                                                                                                                                                ƴ                                                                                                                                                   ƴŴ                                                                                                                           δ        '()*+, %&'()*+, %&'()*+  %&'()*+  %&'()*+ %&'()*+   %&'()*+   ƴǶϴ                                                                                                                                               789:;< 56789:;< 56789:;< 56789:;< 56789:;<56789:;<< 56789:;<  ƴӶڴ                                                                                                                                       GHIJKLMEFGHIJKLMEFGHIJKLMEFGHIJKLMEFGHIJKLEFGHIJKLLMEFGHIJKLM                                                                                                                            ƴӸ        WXYZ[\TUVWXYZ[\TUVWXYZ[\TUVWXYZ[\TUVWXYZ[\UVWXYZ[\\TUVWXYZ[\]                                                                                                                             ӸǴ                                                                                                                                                                                                      ȷ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    



                                                                                                                                                                                                                  				                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               	





                                                                                                                                                                                                                           
                                                                                                                                                                                                                           
                                                                                                                                                                                                                   
                                                                                                                                                                                                                  
                                                                                                                                                                                                                  	
                                                                                                                                                                                                                  

                                                                                                                                                                                                                  

                                                                                                                                                                                                                  

                                                                                                                                                                                                                  

                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          		                                                                                                                                                                                                                                    		

                                                                                                                                                                                                                             			





                                                                                                                                                                                                                            	






                                                                                                                                                                                                                             	




		

                                                                                                                                                                                                                                 	


		                                                                                                                                                                                                                                         			

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   C   C     C   CC  C   C     C   C  C   C     C   CC  C   C     C   C                                                                                                                                                                                                                                                                                                                                                         	$ B A @ A B D E F G G H H I I J J J KKK KKK K KK KK KK    S P P T U V W W X Y Y Y Z Z Z Z Z [ [ [ [ \ \ \ \ \ \ \ \ ^                                        4 6 : = > > ? ? 8 : < > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?                                   	                                      s p p p p p q s t t v w w x x x y y z z z { { { { { { { { {                                                                                                      D                                  D    w u t s r q q q q r r s t t u v v w x y y z { { | | } ~ ~  s                                                                                                                                                                                                                                                                                                                                               ~||{zzyXXWWGFH55%%ӽҽ¾        !12"r         


   		 
 
 



s        	 	                           / '                 	                        	                                        	                        	                                           	                              	                                                  	                              	                                                                                                                                                                                                 `        `   	     `        `        `        `        `        `   	     `        `        `        `        `        `   	     `        `                      	                                                            	                                                            	                         `          `          `          `          `          `          `          `          `          `          `          `          `          `          `          `        `        `   	     `        `        `        `        `    `    `        `        `   	     `        `        `        `        `    `    `                      	                                                   `                             	                                                   `              `          `          `          `          `          `          `          `          `          `          `          `          `          `          `          `        `    `    `        `        `        `        `        `        `        `    `    `        `        `   
     `        `        `        `             `                                                                                `                             
                                             `          `          `          `          `          `          `          `          `          `          `          `          `          `          `          `        `    `    `        `        `   	     `        `        `    `    `        `    `    `        `        `   	     `        `        `    `    `             `                             	                               `                    `                             	                               `              `          `          `          `          `          `          `          `          `          `          `          `          `          `          `          `    `    `        `        `        `        `        `        `        `        `        `        `        `                                   `                                                                                                                                                                    `          `          `          `          `          `          `          `          `          `          `          `                                                                                                                                                                                                                                                 f              f               V                      væ      f              f               V                      væ                                                                                                                                                                                                      f        d      f               V         d            væ      f        d      f               V         d            væ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ' b IA   7   I   W   i   w            w   i   W   I   7   &   ' b IA   7   I   W   i   w            w   i   W   I   7   &   % b FA   5   F   U   f   u            u   f   U   F   8   &   % b FA   5   F   U   f   u            u   f   U   F   8 `  @ F                                                                 F    F    F          F                F          F                                                  F          &                                           	 F                F        F F F F                                                                           &                      &                                                                          &               &                                F    F   	 F    F    F               Of    @          f    &                       
                                     &                                &       F                &                           F                &         &    F          F          &              &         f    F          &          &    F          F    &               F         f    F          &                                                                                            F                                           f                                                                                                                                                                                                                                                                                                                                                                                                                                           T                                                                                                                                                                                                                                                                                                                                                                                      8                                             	                                                                                                                                  (                                                                                                                                                            	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         *                                                                                                                                                                                            J                                                                                                                                                                                             j                                                                                                                                                                                            
   0P^aPPPP_$Q0XaaX"XXdXe$Y                                                             2                                                                                                   LK4 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333:3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333cjV53333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333U333fjf:333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333cf:3ffZ333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333ff3Sfff33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333fff3cfff53333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333cffffff53333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333jfffffff333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333Sffffffff333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333jfffffffffjjjjj53333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333Sfffffffff fjfff:33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333jfffffffff fffjfff:333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333jffffffffff fffffffZ533333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333fffffffffff fffff:3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333ffffffffffff fffffff:333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333ffffffffffff fffffff:33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333fffffffffff jfffffj33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333ffffffffjffffff33333333333333333333    3333333333333333333333333333333333333333333333333333333333333333333333jjffffff33333333333333333333    333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333  033333333333333333333""""""""""""""""""""""""""""""""""""""""""""""""""333393233"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""#33"23333#"2333"33""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""!"!""""""""""""""""""""""""""""""!""""""""""""""""""""""""""""!!""""""""""""""""""""""""""""!""""""""""""""""""""""""""""!""""""""""""""""""""""""""""    """""""""""""""""""""""""""""    """"""""""""""""""""""""""""""     """""""""""""""""""""""3#"""""      !""""""""""""""""""""""33"""""""       !""""""""""""""""""""""33"""""""        !""""""""""""""""""""""23"""""""         """""""""""""""""""""""33#3"2"""          """""""""""""""""""""""3333#2#""                       !"""""""2"""""""#"""""""333333#"#"                                !"""""""3"""""""2"""""""2"3333#23"                                 """"""""3"""""""23""""""""3333#2"""                                  """""""2#"""2""""3""""""""23333""""                                   !"""""""3"""23""""3"""3""""33333""""                                    !"""""""3#2333"""23"""#"3#22333#"""""                                     !"""""""332#"""""23"3##23#23333#"""""                                      !"""""23333#""""#3333#"23#23333"2"""""                                               !"""""33333"3""233333#"23#23333#3"""""                                                        !""""""2333333""23333332"333333333#""""                                                         !""""""2333333#"23333333"333333333#""""                                                          !"""""""3333332333333333333US35333""""#                                                           !""""""""23333#33333333333S5U333333#""#                                                            """""""""233#3"33#33333333S53333353#"2#                                                             """""""""""""#""33333333U3333"#3333#"2#                                                              """"""""""""23""2333333353"3#""3333#3#3                                                               """"""""""""23""3333333U33#3#""3333333#                                                                """"""""""""23"#3333333323#2"""3333S33#                                                                 """"""""""""33#33333333333"3""""233S3#                                                                  !""""""""""""33#333333333"2"#"""""23"""                                                                   !"""""#""""""333"33333332"""#"""""2#"                                                                    1"""""#""""""33333333333#"""""""""2"                                                                     !"""""3""""""333#3333S53"""""#"""""                                                                      !""""23"""""233333333333"""33"""""2                                                                       1""""233#2"23333333333333""2""""""2                                                                        2#"#"333#"233233333333333""""""""""                                                                         !32#""33#"2#333333333#23#""""""""""                                                                          232#"332"""3#33333#"""#"""""""""""                                                                           23332"#""""#"#333#""""#"""""""""2#                                                                            3333#"#""""""""3"""""##""""""""2#                                                                             !33333#23"""""23"""""##""""""""2                                                                              !3333323#""""23""""""3""""""""3                                                                               !3333333##""2#"""""23""""""""3                                                                                !3333#3##""3#"""""23"""2""""3                                                                                 !33333233""2#"""""23"""3"""23                                                                                   23333233""2#"""""3#"""3"""3                                                                                     23#33333""2#"""""3#""23"""#                                                                                       2""#"333""2#"""""""""33#233                                                                                        1""""3#3"""#"""""""""33#233                                                                                         !#"""2#""""#"""""#""""23333                                                                                          !"3"""#"""""""""23""""2333S                                                                                          #"2""#""""""""""#""""23333                                                                                          3"33""""""""""""#""""3#33                                                                                            2333#"""""""""""#""333"33                                                                                             133333""""""""""#""33#"3                                                                                            13"""3""""""""""""233#"3                                                                                             !3"#23""""""""""""3##""3                                                                                               3#"2#""""""""""""2#""2"                                                                                               33"23""""""""#""""#"23                                                                                               Q#"23#"""""""3""3#"333                                                                                               Q3""23"""""3"2#"323333                                                                                                13"2""#"""#""3"2333333                                                                                                S"2#"2"""3#"233"33333                                                                                                 3##32#""23#"33#233333                                                                                                  Q3333#"2333333"233333                                                                                                  153"#233333S333333                                                                                                    5#"""333S3533333                                                                                                      33"#"3333333333                                                                                                       13#3333##23333                                                                                                        1333233#22333S                                                                                                        3233333#"23#                                                                                                          1313333#233                                                                                                           QUS353                                                                                                              ,0     +S
b)T袈yB!6vf  '} M! UUUUUU UwUwwUpUUUU PUUwPUwwUxUxPxPxPUUUUUUU UU xUxwUwUUpUpUUUU  UUw UPPPPa
PUUUUUUUUUUUwUUpwUxwUwwUwwUUUU   UwwwPwfUUUUUUU UU xUxUUUpwwUpwwUUUU  UUwPUwUwUwwPwwwPww QUUUUUU UwUwwUpwxwwwwwUUUU PUUwPUwwUwUzPwwPwwwPUUUUUU  UxwUUpxwwww wwUUUU UUU UUzPUwPUzwUwwUwwUUUUUU   wwwwfUUUUPUUUPUUwUUwUUwPUwwPUwwPUUUUUUU  U xwxUUUU UUU UUwPUxwPUxUxUxUafjpU όUfqPjqPP U̻PPPUUpwUwUwUUUUUUaUUͻP̼PPPU݈UUxUxUUUUUUUU UUUUjfPPUUUPPUpwwUpwwUwwU xwU  U˻UUw`QwU UPUPUPUUUwwwxwwpwwUww x UwwwPwwPwwPwwU P PP˻UpwUwU xUUUUU˻wwUwwUwwUw U  UUUUafU̼UU޻xPUxPUxUUUUPUU UUUUUUUaU͈UЎU݁wUwUwPUUUPUPUPUPUUЌUU  U hU  UUUP P P U  PUUUU̼U hUU  UU̼UP hP   P   U  UUUUUUU    U `fUU  UUU UPfP  P  U  UUUUU      U   UU  UPU UPPP` U  UUUUU  U hU  UUUP P P U  PUU̸UU `U `U  UUU P h P   P   U  UUÜUUU f  U  `UU  UUUU U P̌ P̋ Pf U  UUUU`    U   UU  PUPUUPPlP` U  UUUUUUUUUUUUUUUUUUUU UwUwwUpUUUUUUUUUUUUUUUU PUUwPUwwUxUUUUUUUUUUUUUUUUUUUU UU xUxwUwUUUUUUUUUUUUUUUU  UUw UPPUUUUUUUUUUUUUUUUUUU UU xUxwUwUUUUUUUUUUUUUUUU  PUwwUXXUUUUUUUUUUUUUUUUUUU UU xUxUUUUUUUUUUUUUUUUU  UUwPUwUwUUUUUUUUUUUUUUUUUUU UwUwwUpwUUUUUUUUUUUUUUUU PUUwPUwwUwUUUUUUUUUUUUUUUUUUU  UxwUUpUUUUUUUUUUUUUUUU UUU UUzPUwPU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UUUUUUUUUUU3333                            3333          0""""""""UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU3333333333333333333333333333333333333333333333U33cf:3fffffcfff3333333c33cj33fj3ff3Sff3cfffff:333333V533f:33Z33f33f535333333333333333333333333333333f3333333S33jf3Sffjffjfffffffffffjffffffffffffffffffffffffffffffffffffffffffffff fff fff fff fff 3jjjfjffffffffffffff3333333jjfffjfffffffffff333333335333:33:3Z5::33333333333333333333""""""""33f33f33f3333""33""""""ffffffffffffj3"#33""""fffffffffff"233""""fff ff jfjfff333#"""""ffffjfffffffff2333""""fffffjff9"33""""":3233""""""""33333333333333333333""""""""""""""!""!""!""!""!"""""""""""""""""""""""""""""""""""""""""""""""!"""!"""""""""!"""!"""!""""""""""""""""""""""""""3"""3"""2"""3"""3""""""""3#""3"""3"""3"""3#3"333#""""""""""""""""""""2"""2#""!!""!"!"!"!""""2"""3"""3""2#""3"""3#""332333"""""""""""""""2""2323332#""3#"""""#"""2"""2"""""""""""2"""2""#3""""""""3"""3"""3"""3"""3"3#333#"""3"""2""""""""3"""#"3##23#"23#3333"333"333"233"3332233233323333#"#3#233#2"33""33""3#""3#""3"2"""""""""""""""""""            !"!""!""!""!"""""""""""333323332333"333""23""23""""""""3"3"333"333#3332333#3#3"""#""23""233"233"2333333333333#3"333"233333#3333333333333333333333333333"23#2"333"333333333S333S3U33353"233333333333US355U33533333"#3#""3#3"333#333#333"3333335333333333"""""""""""""""##""##"2##"2##3#3                                            """""""""!"""!"""1"""!"""!"""""""""""""""""""""#"""#"""3""23""23""23""33#"33#"333"333"3332333"333#33333333333"3333333#33333333333333333333333333333333S533333U33#323#333"3"2"2"""#""""""""""33#""2"""3"""#"""#""""""""#""3"""33333333"233""23""2#""2""""""2333#S33#S3#""""1"""2#"#!32#23223333!3!"233"333""33#"3332"#33#"33333333#2"2#"23#"2#2"""""""#"""#23"323#3333323333333#33#"#3""""""""""""333333333333333#33#""3""23""23""333333333#23"""#"""#"""#"""#""""3""23"""#"""""""""""#"""#"""3"""""2""""""""""2#"2#"2"3                  !333!33!3323232"1"!#333333#333323332#333"#"3"""3"""2##""##""33""33""33""33""#3""#"""2#""3#""2#""2#""2#""2#"""#"""#"""""2"""2"""2"""3"""3"""""""""""#3"""3"""3"""#"""#""2"""3"""3""""""""2"""3"""3"""3"""3#233#232333"3"3233#33 3                    !" # 3  2  1  1  !   3""""2"""33"333#33333"""3"#23#"2#"""#"""""""""""3"""3"""3"""#"""""23"""#"""#"""#"""#""""""""""""""""""""""""""33""33"233"3##"2#"233323333#333"33#"3#"3""3"2"S 3                                                33"2Q#"2Q3""13"2S"23## Q33 153"""3#""23""""#"#"2"32#"33#"3"#2"""""""""""3""#"""3#"23#23333333"#"""3"""2#""3"2"233"33#333"3S33""#"3#"332333333"3332333233333332333333333 33 33     5  3   1   1              #"""3"#"3#33333232331313Q  333S333333##33#2333#333#US33533333323332333"23#23353 333333 S    $ ѠѠѫѴƴƴŴƴǶϴƴӶڴѠѠw # title: BUMM0CK
# author: Alec Troemel
# desc: The brave explorer with skates prepared | Drifts & dashes with increasing speed | Decending the icy depths of BUMM0CK
# site: alectroemel.com
# license: MIT License
# version: 1.0
# script: janet
#
# ls | entr -r bin/tic80 bummock.janet --skip --volume 0  --fs .
# bin/tic80 --skip --cli --fs . --cmd="load bummock.janet & import tiles tiles.png & save & exit"
#
# The brave explorer with skates prepared
# drifts & dashes with increasing speed
# Decending the icy depths of BUMM0CK

# Title Screen:
# - DONE dramatically zoom in on a big iceberg with a big omonous black door like in "Below"
# - DONE Game Title: BUMM0CK (underpart of the iceberg)
# - Music: SUPER mellow at the actual title. cresendo chords on each zoom hit.

# STRETCH GOAL
# Intro Level: on top of the iceberg, get a feel for the controls then enter the Doungeon "Cooler maybe?"
# - no music

# Main Levels
# - DONE Music
# - DONE load different levels,
#   - DONE add support for multiple
#   - DONE finish the level a big hole to roll into to. Fall into it
# - TODO Build as many as possible
# - DONE Timer, PER level.
# - DONE Marker to show how deep you are into the iceberg. display the time you got on that level as you go deeper
# - DONE background

# Player
# - SFX of ice skating
# - DONE SFX boost
# - DONE Tweak numbers for friction, boost, maxspeed to not feel horrible
# - DONE Respawn when fall off cliff
# - DONE ice skate trail, list of where you used to be!

# DONE Final "You win!" scene
# - Maybe youre just falling and falling


# Constants
# lets rename tic80s map to stamp, so we dont override janets map function
(var temp-map map)
(use tic80)
(def stamp map)
(def map temp-map)

(def RNG (math/rng (tstamp)))

# Input
(def UP 0)
(def DOWN 1)
(def LEFT 2)
(def RIGHT 3)
(def A 4)
(def B 5)
(def X 6)
(def Y 7)

# PI
(def PI_OVER_6 (/ math/pi 6)) # 0.523598775598299
(def PI_OVER_4 (/ math/pi 4)) # 0.785398163397448
(def PI_OVER_2 (/ math/pi 2)) # 1.5707963267949
(def THREE_PI_OVER_4 (/ (* 3 math/pi) 4)) # 2.35619449019234
(def PI math/pi) # 3.14159265358979
(def FIVE_PI_OVER_4 (/ (* 5 math/pi) 4)) # 3.92699081698724
(def THREE_PI_OVER_2 (/ (* 3 math/pi) 2))  # 4.71238898038469
(def SEVEN_PI_OVER_4 (/ (* 7 math/pi) 4)) # 5.49778714378214
(def TWO_PI (* 2 math/pi)) 6.28318530717959
(def TWOTWO_POINT_FIVE_DEGREES 0.3926991) # 22.5 degrees in radians

# Player
(var PLAYER nil) # we'll set this later
(def PLAYER_FRICTION_HIGH 0.945)
(def PLAYER_FRICTION_MID 0.96)
(def PLAYER_FRICTION_LOW 0.97)
(def PLAYER_BOOST_FACTOR 0.2)
(def PLAYER_BOOST_DURATION 0.15)

(def PLAYER_BOOST_ACC 0.003)
(def PLAYER_ACC 0.003)
(def PLAYER_SLOPE_ACC 0.003)

(def PLAYER_MAX_SPEED_IDLE 0.5)
(def PLAYER_MAX_SPEED_BOOST 0.5)
(def PLAYER_GRAVITY 9.81)
(def PLAYER_IDLE_SPRITES   [256 258 260 262 264 266 268 270])
(def PLAYER_CHARGE_SPRITES [304 306 308 310 312 314 316 318])

# Level
(var *CURRENT_LVL* 0)
(def LEVELS
  [{:map-coords [0 17 30 17]
    :spawn [5 4]
    :goal [26 4]}

   # half pipe
   {:map-coords [0 34 30 17]
    :spawn [2 2]
    :goal [15 3]}

   # drifting
   {:map-coords [0 51 30 17]
    :spawn [18 4]
    :goal [4 13]}

   # bowl
   {:map-coords [0 68 30 17]
    :spawn [1 9]
    :goal [13 9]}

   # stairs
   {:map-coords [0 86 30 17]
    :spawn [12 1]
    :goal [9 12]}

   # bump
   {:map-coords [0 103 30 17]
    :spawn [24 5]
    :goal [10 2]}

   ])

(def LEVEL_CLIP_X 6)
(def LEVEL_CLIP_Y 6)

(def SFX_BOOST 16)
(def SFX_CHARGE 17)
(def SFX_GOAL_REACHED 18)
(def SFX_GOAL_FALL 19)


# Small Helper Functions
(defn map-scales [a-min a-max b-min b-max val]
  "Map value from number scale a -> b"
  (+ b-min (* (- b-max b-min)
	      (/ (- val a-min)
		 (- a-max a-min)))))

(defn drange [start stop step]
  "Decimal Range"
  (var cur start)
  (var acc @[])

  (while (<= cur stop)
    (array/push acc cur)
    (+= cur step))

  acc)

(defn between [val start end]
  (and (> val start) (< val end)))

(defn print-centered [text x y color &opt fixed scale smallfont]
  """
  Prints text where x is the center of text.
  """
  (default fixed false)
  (default scale 1)
  (default smallfont false)

  (let [width (print text 0 -100 color fixed scale smallfont)
	x (- x (/ width 2))]
    (print text (math/round x) (math/round y)
	   color fixed scale smallfont)))

# Class Macro
(defn first-as-keyword [[form-symbol & rest]]
  [(keyword form-symbol) ;rest])

(defn to-class-prototype [forms &opt state-name]
  (->> forms
       (map |(match (first-as-keyword $)
	       [:defn name & body]
	       (tuple (keyword name)
		      ~(fn ,(if state-name
			      (symbol state-name "/" name)
			      name)
			 ,;body))

	       [:def-state state-name & state-forms]
	       (tuple (keyword state-name)
		      (to-class-prototype state-forms state-name))

	       _ []))
       (from-pairs)))

(defn to-default-values [forms]
  (->> forms
       (map |(match (first-as-keyword $)
	       [:field name val]
	       (tuple name val)

	       [:def-state state-name & state-forms]
	       (tuple (keyword state-name)
		      (to-default-values state-forms))

	       _ []))
       (from-pairs)))

(defmacro def-class [name & forms]
  ~(upscope
     (def ,name ,(to-class-prototype forms))

     (defn ,(symbol name "/init") [& overrides]
       (default overrides [])
       (var tbl
	 (table/setproto
	   (merge ,(to-default-values forms)
		  (table ;overrides))
	   ,name))
       (when-let [init-fn (get tbl :init)]
	 (:init tbl))
       tbl)))

(defn GOTO [tab state-name & enter-args]
  (var old-state-name (tab :state))
  (put tab :state state-name)

  # Clear out old fields
  (each key (get tab :__state_keys__ [])
    (put tab key nil))

  # Copy new state keys out to tab
  (let [proto-tab (get (table/getproto tab) state-name)
	inst-tab (get tab state-name)
	merged-tab (merge @{} proto-tab inst-tab)]
    (put tab :__state_keys__ (keys merged-tab))
    (each [key val] (pairs merged-tab)
      (put tab key val)))

  # call the "on-enter" fn if present on new state
  (when-let [on-enter-fn (get tab :on-enter)]
    (on-enter-fn tab old-state-name ;enter-args))

  # return table for chaining
  tab)

# Vector: represented as array or tuple
(defn vector? [v] (and (indexed? v) (all number? v)))

# Redefine a function to handle vectors
(defmacro- vectorize [fn]
  (let [original-fn (symbol :original/ fn)]
    ~(upscope
       (def ,original-fn ,fn)
       (defn ,(symbol fn) [& args]
	 (reduce2
	   |(cond (and (vector? $0) (vector? $1)) (map ,original-fn $0 $1)
		  (and (vector? $0) (number? $1)) (map (fn [v] (,original-fn v $1)) $0)
		  (and (number? $0) (vector? $1)) (map (fn [v] (,original-fn v $0)) $1)
		  (and (number? $0) (number? $1)) (,original-fn $0 $1))
	   args)))))

# Make common operators work on tuples
(vectorize +) (vectorize -) (vectorize /) (vectorize *)

# Re-define the += type macros, so they use our vectorized functions
(defmacro += [x & ns] ~(set ,x (,+ ,x ,;ns)))
(defmacro -= [x & ns] ~(set ,x (,- ,x ,;ns)))
(defmacro *= [x & ns] ~(set ,x (,* ,x ,;ns)))
(defmacro /= [x & ns] ~(set ,x (,/ ,x ,;ns)))

# bunch of functions that work on both vectors
(defn clone [v] @[;v])
(defn magnitude2 [v] (sum (map |(math/pow $ 2) v)))
(defn magnitude [v] (math/sqrt (magnitude2 v)))
(defn distance2 [v1 v2] (magnitude2 (- v1 v2)))
(defn distance [v1 v2] (math/sqrt (distance2 v1 v2)))
(defn dot [v1 v2] (+ ;(* v1 v2)))
(defn round [v] (tuple ;(map math/round v)))
(defn floor [v] (tuple ;(map math/floor v)))
(defn ceil [v] (tuple ;(map math/ceil v)))
(defn trim [v max-length]
  (if-let [len2 (magnitude2 v)
	   non-zero? (> len2 0)
	   s (* max-length (/ max-length len2))
	   s (if (>= s 1) 1 (math/sqrt s))]
    (* v s)
    v))
(defn normalize [v]
  (if-let [v-len (magnitude v)
	   greater-then-zero? (> v-len 0)]
    (/ v v-len)
    v))
(defn project [v1 v2] (* (/ (dot v1 v2) (dot v2 v2)) v2))
(defn angle [[x y]] (math/atan2 x y))
(defn angle-to [v1 v2] (angle (- v1 v2)))
(defn perpendicular [[x y]] @[y (* -1 x)])
(defn normal [v] (normalize (perpendicular v)))
(defn rotated [[x y] theta]
  (let [cos-t (math/cos theta)
	sin-t (math/sin theta)]
    @[(- (* x cos-t) (* y sin-t))
      (+ (* y cos-t) (* x sin-t))]))
(defn from-polar [angle &opt radius]
  (default radius 1)
  @[(* (math/cos angle) radius)
    (* (math/sin angle) radius)])
(defn cross [[ax ay] [bx by]] (- (* ax by) (* ay bx)))
(defn to-mag [v mag] (* (normalize v) mag))

# TIMELINE SEQUENCER
(defn wait [seconds]
  (repeat (* 60 seconds) (yield)))

(def *TIMELINES* @[])

(defn push-timeline [fb]
  (array/push *TIMELINES* fb))

(defn update-timelines []
  # iterate backwards for stable deletion
  (loop [i :in (range (length *TIMELINES*) 0 -1)
	 :let [fb (*TIMELINES* (dec i))]]
    (if (fiber/can-resume? fb)
      (resume fb)
      (array/remove *TIMELINES* (dec i)))))

(def-class Shaker
  (field :duration 0)
  (field :power 0)

  (defn shake-it [self duration power]
    (put self :duration duration)
    (put self :power power))

  (defn update [self]
    (let [{:duration dur :power pow} self
	  x-offset (math/round (- (math/rng-int RNG pow)
				  (/ pow 2)))
	  y-offset (math/round (- (math/rng-int RNG pow)
				  (/ pow 2)))]
      # time to shake
      (when (pos? dur)
	(vbank 0)
	(poke 0x3FF9 x-offset)
	(poke 0x3FFA y-offset)
	(vbank 1)
	(poke 0x3FF9 x-offset)
	(poke 0x3FFA y-offset)
	(vbank 0)
	(-- (self :duration)))

      # shake all done, reset everything
      (when (zero? dur)
	(vbank 0)
	(memset 0x3FF9 0 2)
	(vbank 1)
	(memset 0x3FF9 0 2)
	(vbank 0)))))

(def SHAKER (Shaker/init))

# Tweens
(defn- flip [f]
    (fn [s & args]
      (- 1 (f (- 1 s) ;args))))
  (defn- chain [f1 f2]
    (fn [s & args]
      (* 0.5
	 (if (< s 0.5)
	   (f1 (* 2 s) ;args)
	   (+ 1 (f2 (- (* 2 s) 1) ;args))))))
  (defmacro- def-tween
    "define the in, out, in-out, and out-in versions of a tween"
    [name & body]
    (with-syms [$in $out $in-out $out-in]
      (let [$in (symbol "in-" name)
	    $out (symbol "out-" name)
	    $in-out (symbol "in-out-" name)
	    $out-in (symbol "out-in-" name)]
	~(upscope
	  (defn ,$in [s] ,;body)
	  (def ,$out (flip ,$in))
	  (def ,$in-out (chain ,$in ,$out))
	  (def ,$out-in (chain ,$out ,$in))))))
  (def-tween linear s)
  (def-tween quad (* s s))
  (def-tween quart (* s s s s))
  (def-tween bounce
    (let [a 7.5625 b (/ 1 2.75)]
      (min (* a (math/pow s 2))
	   (+ 0.75 (* a (math/pow (- s (* b (- 1.5))) 2)))
	   (+ 0.9375 (* a (math/pow (- s (* b (- 2.25))) 2)))
	   (+ 0.984375 (* a (math/pow (- s (* b (- 2.625))) 2))))))

  (defn lerp [start end percent func]
    (if (array? start)
      (map |(+ $0 (* (- $1 $0) (func percent))) start end)
      (+ start (* (- end start) (func percent)))))


(defmacro interpolate [accessor start end seconds tween-fn]
    (def end-i (* 60 seconds))
    (with-syms [$start $end]
      ~(let [,$start ,start ,$end ,end]
	 (for i 0 ,end-i
	   (set ,accessor (lerp ,$start ,$end (/ i ,end-i) ,tween-fn))
	   (yield)))))



# ISOMETRIC tiles
# https://peterellisjones.com/posts/generating-transport-tycoon-terrain/
# https://gamedev.stackexchange.com/questions/104271/isometric-terrain-editor
(def SCREEN_WIDTH 240)
(def SCREEN_HEIGHT 136)
(def SCREEN_CENTER [(/ SCREEN_WIDTH 2) (/ SCREEN_HEIGHT 2)])
(def BASE_HEIGHT 0)
(def TILE_WIDTH (* 8 4))
(def TILE_HEIGHT (* 8 2))
(def HALF_TILE_WIDTH (/ TILE_WIDTH 2))
(def HALF_TILE_HEIGHT (/ TILE_HEIGHT 2))

# All the tile  configurations
(def TILE_EMPTY (partial stamp 0 5 4 3))
(def TILE_FLAT (partial stamp 0 0 4 3))
(def TILE_WALL (partial stamp 0 3 4 2))
(def TILE_RAMP_SE (partial stamp 4 0 4 3))
(def TILE_RAMP_SW (partial stamp 8 0 4 3))
(def TILE_RAMP_NW (partial stamp 12 0 4 2))
(def TILE_RAMP_NE (partial stamp 16 0 4 2))

(def TILE_3_HIGH_E (partial stamp 4  3 4 3))
(def TILE_3_HIGH_S (partial stamp 8  3 4 3))
(def TILE_3_HIGH_W (partial stamp 12 3 4 3))
(def TILE_3_HIGH_N (partial stamp 16 3 4 2))

(def TILE_1_HIGH_N (partial stamp 4  6 4 4))
(def TILE_1_HIGH_E (partial stamp 8  6 4 3))
(def TILE_1_HIGH_S (partial stamp 12 6 4 3))
(def TILE_1_HIGH_W (partial stamp 16 6 4 3))

(def TILE_STEEP_S (partial stamp 4  10 4 4))
(def TILE_STEEP_W (partial stamp 8  10 4 3))
(def TILE_STEEP_N (partial stamp 12 10 4 2))
(def TILE_STEEP_E (partial stamp 16 10 4 3))

# (def X_START (- (/ SCREEN_WIDTH 2) (/ TILE_WIDTH 2)))
# (def Y_START 10)
(def X_START 0)
(def Y_START 0)

(defn to-isometric [vec]
  (let [[x y] vec]
    [(* (- x y) HALF_TILE_WIDTH)
     (* (+ x y) (* HALF_TILE_HEIGHT))]))

# So final actual commands are:
(defn from-isometric [vec]
  (let [[x y] vec]
    [(/ (+ (/ x HALF_TILE_WIDTH) (/ y HALF_TILE_HEIGHT)) 2)
     (/ (- (/ y HALF_TILE_HEIGHT) (/ x HALF_TILE_WIDTH)) 2)]))

(defn get-tile-height [x y]
  (+ BASE_HEIGHT (mget x y)))

(defn slope [[ax ay] [bx by]]
  (let [a-height (get-tile-height ax ay)
	b-height (get-tile-height bx by)]
    (max -1 (min 1 (- b-height a-height)))))

(defn calc-slopes [x y]
  (let [NW [x y]
	NE [(+ x 1) y]
	SE [(+ x 1) (+ y 1)]
	SW [x (+ y 1)]]
    [(slope NW NE)
     (slope NE SE)
     (slope SE SW)
     (slope SW NW)]))

(defn find-tile [slopes]
  (match slopes
    [0 0 0 0] TILE_FLAT

    [0 1 0 -1] TILE_RAMP_NE
    [0 -1 0 1] TILE_RAMP_SW
    [1 0 -1 0] TILE_RAMP_NW
    [-1 0 1 0] TILE_RAMP_SE

    [1 0 0 -1] TILE_3_HIGH_N
    [-1 1 0 0] TILE_3_HIGH_E
    [0 -1 1 0] TILE_3_HIGH_S
    [0 0 -1 1] TILE_3_HIGH_W

    [0 1 -1 0] TILE_1_HIGH_S
    [0 0 1 -1] TILE_1_HIGH_W
    [-1 0 0 1] TILE_1_HIGH_N
    [1 -1 0 0] TILE_1_HIGH_E

    [1 1 -1 -1] TILE_STEEP_N
    [-1 1 1 -1] TILE_STEEP_E
    [-1 -1 1 1] TILE_STEEP_S
    [1 -1 -1 1] TILE_STEEP_W

    _ TILE_FLAT))

(defn tile-force [tile-fn mx my x-offset y-offset]
  (var dist2topleft  (distance [mx my] [x-offset y-offset]))
  (var dist2topright (distance [(+ mx 1) my] [x-offset y-offset]))

  (match tile-fn
    (@ TILE_FLAT) [0 0]

    (@ TILE_RAMP_NE) [0 -1]
    (@ TILE_RAMP_SW) [0 1]
    (@ TILE_RAMP_NW) [-1 0]
    (@ TILE_RAMP_SE) [1 0]

    (@ TILE_3_HIGH_N) (if (< dist2topleft 0.5) [-1 -1] [0 0])
    (@ TILE_3_HIGH_E) (if (< dist2topright 0.5) [1 -1] [0 0])
    (@ TILE_3_HIGH_S) (if (> dist2topleft 0.5) [1 1] [0 0])
    (@ TILE_3_HIGH_W) (if (> dist2topright 0.5) [-1 1] [0 0])

    (@ TILE_1_HIGH_S) (if (> dist2topleft 0.5) [-1 -1] [0 0])
    (@ TILE_1_HIGH_N) (if (< dist2topleft 0.5) [1 1] [0 0])
    (@ TILE_1_HIGH_W) (if (> dist2topright 0.5) [1 -1] [0 0])
    (@ TILE_1_HIGH_E) (if (< dist2topright 0.5) [-1 1] [0 0])

    (@ TILE_STEEP_W) [-1 1]
    (@ TILE_STEEP_E) [1 -1]
    (@ TILE_STEEP_N) [-1 -1]
    (@ TILE_STEEP_S) [1 1]

    _ [0 0]))

(defn tile-height-offset [tile-fn mx my x-offset y-offset]
  (var xdiff (- x-offset mx))
  (var ydiff (- my y-offset))
  (var dist2topleft  (distance [mx my] [x-offset y-offset]))
  (var dist2topright (distance [(+ mx 1) my] [x-offset y-offset]))
  (var dist2bottomleft  (distance [mx (+ my 1)] [x-offset y-offset]))
  (var dist2bottomright (distance [(+ mx 1) (+ my 1)] [x-offset y-offset]))

  (match tile-fn
    (@ TILE_FLAT) 0

    (@ TILE_RAMP_NE) (* -1 ydiff)
    (@ TILE_RAMP_SW) ydiff
    (@ TILE_RAMP_NW) xdiff
    (@ TILE_RAMP_SE) (* -1 xdiff)

    (@ TILE_1_HIGH_N) (if (> dist2topleft 0.3) (* -1 dist2topleft) 0)
    (@ TILE_1_HIGH_E) (if (> dist2topright 0.5) (/ (+ xdiff ydiff) 2) 0)
    (@ TILE_1_HIGH_S) (if (< dist2bottomright 0.3) (* 0.5 dist2topleft) 0)
    (@ TILE_1_HIGH_W) (if (< dist2bottomleft 0.5) dist2bottomleft 0)

    (@ TILE_3_HIGH_N) (if (< dist2topleft 0.7) dist2topleft 1)
    (@ TILE_3_HIGH_S) (if (< dist2bottomright 0.5) (* -1 dist2bottomright) 0)
    (@ TILE_3_HIGH_E) (if (< dist2topright 0.5) dist2topright 0)
    (@ TILE_3_HIGH_W) (if (< dist2bottomleft 0.5) (* -1 dist2bottomleft) 0)

    (@ TILE_STEEP_W) (do
		       (if (< dist2topleft 0.5) (* 1 dist2bottomleft) dist2bottomleft))
    (@ TILE_STEEP_E) (do  (* 0.5 dist2topright))
    (@ TILE_STEEP_S) (do  (* -1 dist2topleft))
    (@ TILE_STEEP_N) (do  dist2topleft)

    _ 0))




# CAMERA
(def-class Camera
  (field :pos [0 0])
  (field :des-pos [0 0])
  (field :target nil)

  (defn set-des-pos [self]
    (put self :des-pos
	 (-> (get-in self [:target :pos])
	     (to-isometric)
	     (- [0 (* 8 (- (get-in self [:target :height]) 1))])
	     (- SCREEN_CENTER))))

  (defn teleport [self]
    (:set-des-pos self )
    (put self :pos (self :des-pos)))

  (defn update [self]
    (:set-des-pos self)
    (put self :pos
	 (-> (self :pos)
	     (+ (self :des-pos))
	      (/ 2)))))

(def CAMERA (Camera/init :pos [2 0]))


# Levels, and drawing the Isometric height map
(defn draw-background []
  (let [camera-pos (CAMERA :pos)
	camera-y (camera-pos 1)]
    (cls 0)
    (stamp 30 0 60 30 ;(-> camera-pos
			   (* [-0.2 -0.1])
			   (+ [-100 -10])
			   (round)))))

(defn draw-tile [tile-fn x y height]
  (let [[x-screen y-screen] (- (to-isometric [x y]) (CAMERA :pos))
	floor-x-screen (math/round x-screen)
	floor-y-screen (math/round y-screen)
	px-height (* 8 height)
	top-y (- floor-y-screen px-height)]
    (if (= BASE_HEIGHT height)
      (TILE_EMPTY floor-x-screen floor-y-screen 0)
      (let [tile-specific-y-offset 0]
	# sides
	(rect floor-x-screen (+ top-y 16 tile-specific-y-offset)
	      16 (- px-height 8 tile-specific-y-offset)
	      9)
	(rect (+ 16 floor-x-screen) (+ top-y 16 tile-specific-y-offset)
	      16 (- px-height 8 tile-specific-y-offset) 1)

	# bottom
	(when (> height 1)
	  (TILE_WALL floor-x-screen (+ floor-y-screen tile-specific-y-offset) 0))

	# top
	(tile-fn floor-x-screen top-y 0)))))

(defn draw-height-map [x y w h sx sy player]
  (loop [:let [[px py] (+  (player :pos) 0)
	       px (math/floor px)
	       py (math/floor py)

	       clipped-start-x (max 0 (- (+ px sx) LEVEL_CLIP_X))
	       clipped-start-y (max 0 (- (+ py sy) LEVEL_CLIP_Y))
	       clipped-end-x (min (- (+ sx w) 1) (+ (+ px sx) LEVEL_CLIP_X))
	       clipped-end-y (min (+ sy h) (+ (+ py sy) LEVEL_CLIP_Y))]
	 i :range [clipped-start-x clipped-end-x]
	 j :range [clipped-start-y clipped-end-y]
	 :let [mx (+ i x)
	       my (+ j y -1)
	       slopes (calc-slopes mx my)
	       height (get-tile-height mx my)
	       tile-fn (find-tile slopes)]
	 :when (function? tile-fn)]

    (draw-tile tile-fn i j height)
    (when (and (= px i) (= py (- j 1)))
      (:draw player))))


# ISOMETRIC tile height map editor
# - Hit "E" to toggle editor mode
# - wasd to move camera
# - left click to raise, right to lower terrain

(def-class TerrainSculper
  (field :editing? false)
  (field :left-down false)
  (field :right-down false)
  (field :map-coord [0 0])
  (field :screen-pos [0 0])
  (field :current-height 0)

  (defn update [self]
    (when (keyp 05)
      (put self :editing? (not (self :editing?))))

    (when (self :editing?)
      (let [[x y left middle right] (mouse)
	    new-map-coord (round (from-isometric (+ [x y] (CAMERA :pos))))
	    new-map-coord new-map-coord
	    [lx ly _ _] (get-in LEVELS [*CURRENT_LVL* :map-coords])
	    [hx hy] new-map-coord
	    mx (+ lx hx)
	    my (+ ly hy)
	    current-height (mget mx my)]

	(put self :map-coord new-map-coord )
	(put self :screen-pos (- (to-isometric new-map-coord) (CAMERA :pos)))
	(put self :current-height current-height)

	(if left
	  (when (not (self :left-down))
	    (mset mx my (inc current-height))
	    (sync 4 0 true)
	    (put self :left-down true))
	  (put self :left-down false))

	(if right
	  (when (not (self :right-down))
	    (mset mx my (dec current-height))
	    (sync 4 0 true)
	    (put self :right-down true))
	  (put self :right-down false)))))

  (defn draw [{:editing? editing?
	       :screen-pos [x y]
	       :current-height h}]
    (when editing?
      (print "Editing" 4 4 15)
      (circ (math/round x) (math/round (- y (* 8 (+ (- h 1) BASE_HEIGHT)))) 2 h))))

(def TERRAIN_SCULPER (TerrainSculper/init))


# Player
(defn solid? [height x y]
  (let [tile-height (get-tile-height (math/round x) (math/round y))
	height-diff (- tile-height height)]
    (> height-diff 2)))

(defn hit? [height [x y] [w h]]
  (or (any? (map |(or (solid? height $ y)
		      (solid? height $ (+ y h)))
		 (drange x (+ x w) (/ w 6))))

      (any? (map |(or (solid? height x $)
		      (solid? height (+ x w) $))
		 (drange y (+ y h) (/ h 6))))))

(def-class Player
  (field :pos [1.4 1.45])
  (field :height 999)
  (field :height-offset 0)
  (field :vel [0 0])
  (field :acc [0 0])
  (field :fric PLAYER_FRICTION_HIGH)
  (field :max-speed PLAYER_MAX_SPEED_IDLE)

  (field :acc-ammount PLAYER_ACC)
  (field :boost-charge 0)
  (field :debug-color 15)

  (field :sprites PLAYER_IDLE_SPRITES)
  (field :trail @[])

  (defn apply-force [self force]
    (+= (self :acc) force))

  (defn integrate [self]
    (put self :vel
	 (-> (+ (self :vel) (self :acc))
	     (trim (self :max-speed))))
    (*= (self :vel) (self :fric))

    # Collision!
    (let [[dx dy] (self :vel)
	  pos (self :pos)
	  height (self :height)
	  col-box [0.5 0.5]
	  lvl-offset (take 2 (get-in LEVELS [*CURRENT_LVL* :map-coords]))]

      # X direction
      (when (not (hit? height (+ pos [dx 0] [-1 -1] lvl-offset) col-box))
	(put self :pos (+ (self :pos) [dx 0])))

      # Y direction
      (when (not (hit? height (+ pos [0 dy] [-1 -1] lvl-offset) col-box))
	(put self :pos (+ (self :pos) [0 dy]))))

    (put self :acc [0 0]))

  (defn update-height [self]
    (let [pos (self :pos)
	  lvl-offset (take 2 (get-in LEVELS [*CURRENT_LVL* :map-coords]))
	  [mx my] (floor (+ lvl-offset pos))
	  tile-height (get-tile-height ;(floor (+ pos lvl-offset)))
	  tile-fn (find-tile (calc-slopes mx my))
	  tile-offset (tile-height-offset tile-fn mx my ;(+ lvl-offset pos))]
      (put self :height tile-height)
      (put self :height-offset tile-offset)))

  (defn handle-movement-input [self]
    (var move-impulse [0 0])
    (when (or (key 01) (btn LEFT)) (+= move-impulse [-1 1]))
    (when (or (key 04)(btn RIGHT)) (+= move-impulse [1 -1]))
    (when (or (key 23)(btn UP)) (+= move-impulse [-1 -1]))
    (when (or (key 19)(btn DOWN)) (+= move-impulse [1 1]))
    (:apply-force self (trim move-impulse (self :acc-ammount))))

  (defn apply-slopes-forces [self]
    (let [lvl-offset (take 2 (get-in LEVELS [*CURRENT_LVL* :map-coords]))
	  pos (self :pos)
	  [mx my] (floor (+ lvl-offset pos))
	  slopes (calc-slopes mx my)
	  tile-fn (find-tile slopes)
	  slope-force (tile-force tile-fn mx my ;(+ lvl-offset pos))]
      (:apply-force self (trim slope-force PLAYER_SLOPE_ACC))

      ))

  (defn update-trail [self]
    (array/push (self :trail)
		(-> (self :pos)
		    (+ [0.25 0])
		    (to-isometric)
		    (- [0 (* 8 (+ (self :height-offset) (self :height)))])
		    (round)))
    (array/push (self :trail)
		(-> (self :pos)
		    (+ [0 0.25])
		    (to-isometric)
		    (- [0 (* 8 (+ (self :height-offset) (self :height)))])
		    (round)))
    (when (> (length (self :trail)) 50)
      (array/remove (self :trail) 0 2)))

  (def-state :idle
    (defn on-enter [self & args]
      (put self :acc-ammount PLAYER_ACC)
      (put self :debug-color 15)
      (put self :boost-charge 0)
      (put self :max-speed PLAYER_MAX_SPEED_IDLE)
      (put self :sprites PLAYER_IDLE_SPRITES)
      (put self :fric PLAYER_FRICTION_LOW))

    (defn handle-input [self]
      (:handle-movement-input self)
      (when (or (btn A) (btn B))
	(GOTO self :charging))))

  (def-state :charging
    (defn on-enter [self & args]
      (sfx SFX_CHARGE)
      (put self :acc-ammount PLAYER_BOOST_ACC)
      (put self :debug-color 11)
      (put self :fric PLAYER_FRICTION_HIGH)
      (put self :sprites PLAYER_CHARGE_SPRITES))

    (defn handle-input [self]
      (:handle-movement-input self)
      (put self :boost-charge (min 30 (+ 1 (self :boost-charge))))
      (when (not (or (btn A) (btn B)))
	(GOTO self :boosting))))

  (def-state :boosting
    (defn on-enter [self & args]
      (sfx SFX_BOOST)
      (put self :acc-ammount PLAYER_ACC)
      (put self :debug-color 14)
      (put self :max-speed PLAYER_MAX_SPEED_BOOST)
      (put self :fric PLAYER_FRICTION_MID)
      (put self :sprites PLAYER_IDLE_SPRITES)

      (:apply-force
	self
	(-> (self :vel)
	    (normalize)
	    (* (map-scales 0 30
			   0 PLAYER_BOOST_FACTOR
			   (self :boost-charge)))))
      (push-timeline
	(coro
	  (wait (map-scales 0 30
			    0 PLAYER_BOOST_DURATION
			    (self :boost-charge)))
	  (GOTO self :idle))))

    (defn handle-input [self]
      (:handle-movement-input self)))

  (defn update [self]
    (:handle-input self)
    (:apply-slopes-forces self)
    (:update-height self)
    (:integrate self)
    (:update-trail self)
    )

  (defn draw [self]
    # trails
    (each pos (self :trail)
      (pix ;(round (- pos (CAMERA :pos))) 4))

    (let [vel-angle (+ (angle (normalize (self :vel)))
		       TWOTWO_POINT_FIVE_DEGREES)
	  sprites (self :sprites)
	  sprite (->> (cond
			(between vel-angle PI_OVER_4 PI_OVER_2)
			0 # DOWN
			(between vel-angle PI_OVER_2 THREE_PI_OVER_4)
			1 # DOWN RIGHT
			(between vel-angle THREE_PI_OVER_4 PI)
			2 # RIGHT
			(or (> vel-angle THREE_PI_OVER_4)
			    (between vel-angle (* -1 PI) (* -1 THREE_PI_OVER_4)))
			3 # UP RIGHT
			(between vel-angle (* -1 THREE_PI_OVER_4) (* -1 PI_OVER_2))
			4 # UP
			(between vel-angle (* -1 PI_OVER_2) (* -1 PI_OVER_4))
			5 # UP LEFT
			(between vel-angle (* -1 PI_OVER_4) 0)
			6 # LEFT
			(between vel-angle 0 PI_OVER_4)
			7)
		      (get sprites))
	  screen-pos (-> (self :pos)
			 (to-isometric)
			 (- [0 (* 8 (+ (self :height-offset) (self :height)))])
			 (- [8 20])
			 (- (CAMERA :pos) )
			 (round))]

      # sprites
      (spr sprite ;screen-pos 5 1 0 0 2 3)

      # charge
      (when (pos? (self :boost-charge))
	(circ ;(+ screen-pos [8 -2])
	      (math/round (map-scales 0 30 5 2 (self :boost-charge)))
	      (cond (> (self :boost-charge) 20) 15
		    (> (self :boost-charge) 10) 12
		    14))))))


# Main game state machine
(def-class Game
  (field :timers @[])

  (def-state :title
    (field :zoom 1)

    (defn on-enter [self & args]
      (put self :timers @[])
      (set *CURRENT_LVL* 0)
      (sync (bor 1 2 4 ) 1)
      (music 1))

    (defn update [self]
      (when (and (or (btnp A) (btnp B) (btnp X) (btnp Y))
		 (= 1 (self :zoom)))
	(push-timeline
	  (coro
	    (put self :zoom 2)
	    (music 2)
	    (wait 1.5)
	    (put self :zoom 4)
	    (music 3)
	    (wait 1.5)
	    (put self :zoom 8)
	    (music 4)
	    (wait 1.5)
	    (GOTO self :game)))))

    (defn draw [self]
      (cls 0)
      (when (< (self :zoom) 8)
	(stamp 0 0
	       30 17
	       ;(match (self :zoom)
		  1 [0 0]
		  2 [-118 0]
		  4 [-382 -10])
	       -1 (self :zoom)))

      (when (= (self :zoom) 1)
	(print "BUMM0CK" 87 120 2 false 2)
	(print "By Alec Troemel" 100 2 6 false 1 true))

      ))

  (def-state :end
    (field :final-time 0)

    (defn on-enter [self & args]
      (put self :final-time (sum (self :timers)))
      (music 2))

    (defn update [self]
      (when (or (btnp A) (btnp B) (btnp X) (btnp Y))
	(GOTO self :title)))

    (defn draw [self]
      (cls 0)
      (print-centered "You have reached the bottom of the Iceberg." 120 30 2)

      (let [seconds (math/floor (/ (self :final-time) 60))
	    sub-seconds (% (self :final-time) 60 )]
	(print-centered (string "Final Time: " seconds "." sub-seconds) 120 50 2))

      (print-centered "Thanks for playing!" 120 70 2)
      (print-centered "Click anything to restart." 120 120 1)))

  (def-state :game
    (field :goal-radius 16)
    (field :goal-reached? false)
    (field :dead? false)

    (field :timer 0 )

    (defn start-level [self lvl &opt restart-time?]
      (default restart-time? true)

      (set *CURRENT_LVL* lvl)
      (let [current-lvl (get LEVELS lvl)
	    starting-pos (get current-lvl :spawn)]
	(put PLAYER :pos starting-pos)
	(put PLAYER :vel [0 0])
	(put PLAYER :acc [0 0])
	(:update-height PLAYER)
	(:teleport CAMERA)
	(GOTO PLAYER :idle)
	(put self :goal-radius 1000)
	(push-timeline
	  (coro
	    (interpolate (self :goal-radius) 1000 16 0.5 out-quad))))

      (put self :dead? false)
      (when restart-time?
	(put self :timer 0))
      (put self :goal-reached? false))

    (defn on-enter [self & args]
      (sync (bor 1 2 4 ) 0)
      (music 0)
      (set PLAYER (-> (Player/init) (GOTO :idle)))
      (put CAMERA :target PLAYER)
      (:start-level self *CURRENT_LVL*))

    (defn goal-reached [self]
      (put self :goal-reached? true)

      (push-timeline
	(coro
	  (sfx SFX_GOAL_REACHED)
	  (array/push (self :timers) (self :timer))
	  (put self :timer 0)
	  (interpolate (self :goal-radius) 16 300 1 out-quad)

	  (if (< (+ 1 *CURRENT_LVL*) (length LEVELS))
	    (:start-level self (+ 1 *CURRENT_LVL*))
	    (GOTO self :end)))))

    (defn update-goal [self]
      (let [goal-reached? (self :goal-reached?)
	    player-pos (PLAYER :pos)
	    goal-pos (get-in LEVELS [*CURRENT_LVL* :goal])
	    dist (distance player-pos goal-pos)]
	(when (and (not goal-reached?) (< dist 1.5))
	  (:goal-reached self))))

    (defn update [self]
      (:update CAMERA)
      (when (not (self :dead?))
	(:update PLAYER))
      (:update TERRAIN_SCULPER)
      (:update-goal self)

      (when (and (not (self :goal-reached?)))
	(+= (self :timer) 1))

      (when (and (not (self :dead?))
		 (zero? (PLAYER :height)))
	(sfx SFX_GOAL_FALL)
	(put self :dead? true)
	(:shake-it SHAKER 10 4)
	(push-timeline
	  (coro (wait 0.3)
	    (:start-level self *CURRENT_LVL* false)))))

    (defn draw-goal [self]
      (let [current-lvl (get LEVELS *CURRENT_LVL*)
	    lvl-offset (take 2 (current-lvl :map-coords))
	    goal-pos (get current-lvl :goal)
	    height (get-tile-height ;(+ goal-pos lvl-offset))
	    screen-pos (-> goal-pos
			   (to-isometric)
			   (- [0 (* 8 (+ 1 height))])
			   (- (CAMERA :pos))
			   (round))
	    radius (math/round (self :goal-radius))
	    half-radius (math/round (/ radius 2))]
	(elli ;screen-pos radius half-radius 0)
	(ellib ;screen-pos radius half-radius 6)))

    (defn draw-times [self]
      (loop [[i t] :pairs [;(self :timers) (self :timer)]
	     :let [seconds (math/floor (/ t 60))
		   sub-seconds (% t 60 )
		   text (string seconds "." sub-seconds)
		   text-width (print text 0 -50 0)
		   y (+ 2 (* 7 i))
		   c (cond (< i 1) 5
			   (< i 3) 4
			   (< i 5) 3
			   2)]]
	(rect 2 y (+ 2 text-width) 7 c)
	(print text 3 (+ 1 y) 0)))

    (defn draw [self]
      (draw-background)
      (draw-height-map ;(get-in LEVELS [*CURRENT_LVL* :map-coords]) 0 0 PLAYER)
      (:draw TERRAIN_SCULPER)
      (:draw-goal self)
      (:draw-times self))))


(def GAME (-> (Game/init) (GOTO :title)))

(defn TIC []
  (update-timelines)
  (:update SHAKER)
  (:update GAME)
  (:draw GAME))
