0  D$404mNJNL04e$FHuqaY}},m,Ҫm^
   !2DevxfE# UUUUUUUUUUUUUUUU   1111111333DDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUÜ陙                                                                                                                                                                                                   ̪̪̪̪̪̪̪DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUÜ   @@DD@DD@DD@DD@DD@DD؍؈݈؍؍݈݈؍؈݈؍؍݈݈""""""""""""""""""""""""(""""""""""""""""""""""""""""                                                                                                                                ˻˻˻˻DDDDDDDDDDDDDDDDDDDDNNNDDDDNNDDDDjjfjjjʦjfljʬjc3ccAfFfdAdcdd[[[[UUUU[[[[UUUUޮޮޮޮ        DwAyDwyAwDyAwDAy""("""""("""""("""("؈ݍ؈؈݈؈ݍݍݍ݈ݍݍݍݍ݈                                                                                                tDtDDtDttDtDDtDttDtDDtDttDtDDtDtIIIIIIIIIIIIIIII        DDDDDDDDDDDDJDDJDDJDDJDDJDDJDDJ                                N~N~N~N~~NN~~~fwwffwwfwwwwwwwww33wws7ww37ww37www7www7w""""""""""""؈݈݈݈݈ݍ݈ݍ݈ݍ݈                                                                                                                                               UU[UUU[U[[UU[UUUUUUUUU[UU[[                                zzzzwwzzzwwzzzwwzzzw                                AIAIAIAIAIAIAIAIs33szzzzzzz7337off633c6cc66oc6cc6o6c633coffDAA"""""""""                                                                                                                                                                                                                                   0   S  0U  SU 0UU SUU0UUUSUUUjfjffjfjffffffffDDDDffffffffDDDDffffffff                                DDDDDDDDDDDDDDDDDtGDDGtDDGtDDtGDfff6633qwwqwwDDDDDDDDjfjfffffffffffffffƪlƬʪj̪jjʪfffffƪf̪fffffFdffDdfftdffDdffFgffFfffDgffffffFfffFffvFdfvDdfvDdfvGfffGdfffffffvffdvdfdvdfdvDfdvDfdvDfdvD                                                                                                                                 			   		 	                                                                                                 3333qwwfwfFFFFfDFF                                jʪjjfʬjfʪjfffjfffffffffffffffffffjffƪff̪jfDgffFgffDgffDgffDgffDgffDfffDdffGdffGdffGdffGdffGDffFDffFDffDGffdfDfdftfdfdfgfdFffdfffdFffgvfff                                                                                                                                                                                                                                333333333333fffffflflʪff̪fƪfƪjjjƪfjfffffƬffff̪fFfffFfffFfffDffftffftffftfffdfffDgffDgffDfffDfffDfffDfffdffffffvfffffffFffFFffFFffGFffGvffgfffg                                                                                                                                                                                                                                3333qwwqwwqwwqww3333333333333333wwwwwwwwwwwwwwww3333wwwwwwww                                                                                                                                                                                                                                                                                                                                                                                                                                qwwqwwwwwwwwwwwwww                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     p      w  w  w               w  w                                                                                                                                                                                                                                                                                                                                                                                                                                                                          @    N  D  D  D               D  D                                                                                                                                                                                                                                                                                                                                                                                                           lf @lf ff n3 6s<s7     nfF FDfD4d33Cdss377          -  "  "  "               "  "                                                                                                                                                                                                                                                                                                                                                                                                     s3D63w6s7`3q            47  
    ?           `  f  Ff  f  `  f  d  @d           a      a  F                               0                                                p   w   7  ps  p        
  w  s  ww :sw 33sw P PPU UP PP U    U U UP PP    UU U U U PUUUUUP  PU  P UUU                                                                   P_[     P   P P [    P    [  U       [                  P3 [P5;P; U;P;5  [  [;  5;  S[    [  U         [  [           P   UP PP P       [      U     @ @1D @D@DADCDADD0D4  1  q DDC DC CC DDD 0DD    w: p3 wpwwwwwwwwwwpwwwpwww333s333s333s333s733sw33ww7swws UPUP U  P  PU PPUUU UP PPU P UPU UU  UP  P U P P  UU     [P      P    P PP [ _[ P             P                  [   P[Q[PPP   P      Q[ Q U  [       [   ;[[5[PP3             [ ; ;U [P 5P   U   U  PU                                                              "                      )             " "                                                                                                                                                                                                 UU[UU[UU[U[UUUUUUUUUU[UUUUU[UU[[UUUU[UUU[UU[UUUUUUU[UUUU[UUU[UU[UUUUU[UU[UUUUU[UU                                                                                                                                       	                  f bf/"/f" i&b ffb "                                                                                                           U PU PU                   UUUUU\                              U[U[UUU[UUUUUUUUUUUUUUUUUUU[UUUUUUU[U[U[U[UUUUUUUUU[UUU[[U[UUUUUU[U[U[UUUUUUUUUUUU[UU[                                                                                                                                                               o    /.//.                                                         U  U  U\ P P\ P\PU\ U PU\  P  P   P U\PUUUU\\\\\\\\\\\\\\\U   U  \  U \\ \\\\\\\UU[[UU[UUUUU[UUUUUUUUU[UUUUUUUUUUUUUUUUUUUUUUUUU[UU[UUU[UUU[UUU[UUU[UUUUUUUU[                                                                                                                                                                                                                                                       P\ PUPP\U U P  PU        PUUU\UUUUUPU        \\U\U UU        U\\UUUPU                                                                                                                                                                                                            DDDDDDDDDwwwDwwwDwwwDwwwDwwwDwDDDDDDDDwwwwwwwwwwwwwwwwwwww}wDDDDDDDDwDfDfDfDfDfD                                DDDJDDDJDDDJDDDJDDDJDDDJDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                DwDwDDDDDDDDDDDDDwwwDwwwDwww}w}wDDDDDDDDDDDDwwwwwwwwwwwwfDfDDDDDDDDDDDDDwwwDw'rDw""D                                JDDDJDDDJDDDJDDDJDDDJDDDJDDDJDDDDDDDDDDDDDDDDDDDDDDDDD:333DD3DDDDDDDDDDDDDDDDDDDDDDDD                                    ݍw݈r-"3"1"    23DwwwDwwwDwwwDwwwDwwwDDDDDDDDDDDDwwwwwwwwwww'www'wwwwDDDDDDDDDDDD'"rD""wD"rwD"wwDrwwDDDDDDDDDDDDD                                JDDJDD4JDD4JDD4JDDDJDDDDDDCDD4CDD4CDD4CDD4DD:33D33DJDDCDDCDDCDDDDDDDDDDD                                rrwr'wrr"r'w-w"-'w"r"w"w!r'*"'w""rwwr"w'"*'""""""""!22*"322322#-"!#3:-31!123311233333"333311ݻ˻̻컻˻˼̻̻̼̻ϼϼμ̻                                DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                ""݂rݍ(-""-݂'""r""" "2  " " 2 *2 *p2"҈/" # *" *3" "3 *3 q
3 3 " /11 0#  ! !~ 0*0ݻ˻̻̼̼̻μ̼˼̻̻̻                                DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                --""-"-"=---  "# "3 (22 ""  "3 ""#"#*#"3""' "0/" 	  101:3811" 2"*#!# !"
"*""0!!"ݻ̻켻̻̻̻̻컻̻컻̻̻λ̼̻̻̼                                DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                -݂ -8 (  -" - """*"/""b":zw  

 w 0  3" *
 **!**"   " "!DDDDDDDDDDDDDDDDDDD$DDD"DD$D$"DDDDDDDDDDDDDDD$DDD$BDDD"DDD"DDDDDDDDDDD"BDD("DD.BD.BD"(BDD"DD                                DDDDDDDDDDDDDDDDDDDdDDDfDDdDdfDDDDDDDDDDDDDDDdDDDdFDDDfDDDfDDDDDDDDDDDfFDDlfDDnFDnFDflFDDfDD                                ""݂-ݍݍ=ݍݨ "***"*"ݨ"ݨ# "ݧ373--ݪ*"**-""
--!!!D$D"D"D""D$"$DDDDDDDDDDDD""DD""BD"BD""DD(BDD"BDD$BDDDDD"DDDDDDDDDDDDDDDDDDDDDDDDD"DD$"DD                                DdDfDfDffDdfdDDDDDDDDDDDDffDDffFDfFDffDDlFDDfFDDdFDDDDDfDDDDDDDDDDDDDDDDDDDDDDDDDfDDdfDD                                DDDDD"DD$BD$BD""D"("DDB"DDDDDDDD"DD$DD$"DD$DD$"DDD"DDDDDDDD"(DD"BD"D"""D""BDBDDDDDDDDDDD                                DDDDDfDDdFDdFDffDflfDDFfDDDDDDDDfDDdDDdfDDdDDdfDDDfDDDDDDDDflDDfFDfDfffDffFDFDDDDDDDDDDD                                DDDDD4D4D4DDDDDDDDDDDDDCDDDCDDDADADADADCDADADADD4ADD4ADC4ADA4ADA4ADA4wwwwwwwwwwww33wswwswwswwwwwwwwwwwww3333wwwwwwwwwwwwwwwwzwwwwwwwzz33zww7zww7zww7zw                                                                U[UU[U[UUUU[U[                                                                                                                                                                D4DDDDDDDDDJDDtD4wDD3CADADADADADADADADADADADCDADADDDADADC4ADA4ADA4ADC4ADA4ADA4ADA4ADA4wswwswwswwswwwswwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww7zww7zww7zww7zww7zww7zzw7zww7zw                                                                [[[[[UUU                                                                                                                                                                DD4DDDDDADDADDDCDDDDCDDDADDDADDDADADADADDDADA4ADA4ADA4ADA4ADA4ADA4wswwswwswwswwswwswswwswwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww7zww7zww7zww7zww7zww7zww7zzw7zw                                                                [[[[U[[U[[                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ((       (((((          ((((( ( (((((((((  ((((                                                                                                                                                                                                                                      ((((   ((((                                                                                                                                                                                                                                             ((((                                     F                                                                                                                                                                                                      ((((                             GWFFFFGF6FG                                                                                                                                                                                                     ( ((                            G66666G666G                                                                                                                                                                                                                                     GGGVGGGGGGG                                                                                                                                                                                                                                       GGG                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ))))))))))))))))))))))))))))))                                                                                                                                                                                                                  )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                                                                                                                                                                                                  )IIIIIIIIIIIIIIIIIII)IIIIIIII)                                                                                                                                                                                                                  )IIIIIIIIIIIIIIIIIII)IIIIIIII)                                                                                                                                                                                                                  )IIIIIII)II)IIIII)II)))IIIIII)                                                     3                                                                                                                                                            )IIIIII)IIII)))))IIIIIIIIIIII)                                                     3                                                                                                                                                            )IIIII)IIIIIIIIIIIIIIIIIIIIII)                                                     3                                                                                                                                                            )IIIII)IIIIIIIIIIIIIIII)IIIII)                                                     3                                                                                                                                                            )IIIII)IIIIIIIIIIIIIII))IIIII)                                                     3                                                                                                                                                            )IIIIII)IIII)))))IIII)IIIIIII)                                                     3                                                              0                                                                                             )IIIIIII))))IIIII))))IIIIIIII)                                                     3                                                            0000000                                                                                         )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                                     3                                                           000000000                                                                                        )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                                     3                                                          000000000000                                                                                      )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                                     3                                                         00000000000000                                                                                     )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                   333333  333333  333                                                       00000000000000000                                                                                    )IIIIIIIIIIIIIIIIIIIIIIIIIIII)                                                                                                         0000000000000000000000                                                                                 ))))))))))))))))))))))))))))))                                                                                          FFFWWFFF00000000000000000000000                                                                                ++++++++++                                                                                              (##(0000000000000000000000000                                                                                                                      &&&&&&&&  &&&                                                (##(000000   00000000000000000 0000                                                                          ++++++++                                     & &     &    &&&&                                             (##( 000      0000000000000000000000                                                                         +++++++++                                    &   &&&&  &  && &                                             (##(          0000000000000000000000                                                                         +++++++                                  &&       & &  &&&& &                                 (           (##(         000         00000000000                                                                         ++                                 &&    &&&   & &&  &&  &                                 (           (###(       000              00000000                                                                        ++++++++++                                 &    && &&  & &    &   &                                (           (###(      00               00000000                                                                        ++++++                                     &&   &    & &&      &                               (         (##(                       00000000                                                                        ++++                                     &  &  && &&  & &    &                               (      (##(                       000000000                                                                       ++++++                                       &&&  &&&  && &&   &                               (          (##(                       00000000                                                                        ++++++++++                                        &       &&   &   &                               (      #                      0000000                                                                         ++++                                          &  && &   &&   &                               (   "                           0000                                                                           +++++++++++++++                                          &&&&     &&    &                               (!!!!!!!!!                          00000                                                                          ++                                                 &&&     &                               (!!!!!!!!!!!!                    000000                                                                         ++++++++                                                         &                        (((((((((!!!!!!                   000                                                                                                                                                             (((((((((     !!                                                                                                                                                                                    (   ((((((                                                                                                                                                                                                                 (  (((((2                                                                                                                                                                                                                        (                                                                                                                                                                                                                          (((((((                                                                                                                                                                                                                         ((((((((                                                                                                                                                                                                                             (((((((                                                                                                                                                                                                                    (((((((((                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   (((((((((                                                                                                                                                                                                                                (                                                                                                                                                                                                                                (                                                                                                                                                                                                                                     (        !                                                                                                                                                                                                                            (        !!!                                                                                                                                                                                                                               (        !!!!!                                                                                                                                                                                                                               (        !!                                                                                                                                                                                                                                                                                                               ((                                                                                                                                                                                                                                             ((                                                                                                                                                                               (                                                              ((                                                                                                                                                                  ((        (((((                                                            ((                                                                                                                                      BBBB                  (((((((((       ((  (((                                                          (((                                                                                                                               BBBBBBBBBBBBBBBB         (((((((          (((   ((((((                                                        ((                                                                                                                            BBBBBBBBBBBBBBBBBBBBB       (( (          ((( ((       (((((((((((                                                 ((                                                                                                                        BBBBBBBBBBRRBBBBBBBBBBBBBB                  (( (((          ((((                                                      (                                                                                                                        BBBBBBbbBBBRRRRBBBBBBBBBBBBBB                (              %%%%                                      ((               ((                                                                                                                      BBBBBBbbbbbbRRRRRRBBBBBBBBBBBBB              ((             %%%%                                       ((               (((                                                                                                                    BBBBbbbBBBbbbBBRRRRRRBBBBBBBBBBBB  (          (             %%%%%                                       ((                ((                                                                                                                    BBBbbbbBBBBbBBBBBRRRRRRRRRBBBBBBBB((    ((    (             %%%                                         (                 (((                                                                                                                  BBBBbbbbbBBbbBBBBRRRRRRRRRRRRBBBBBB(   ((((((((((                                                       ((                  ((((                                                                                                               BBBBBBBBBbbBbBBBBBRRRRRRRRRRRRRRRBB((   (       (                                                      ((((                   ((((                                                                                                              BBBBBBBBBBbBBBBBBBBRRRRRRRRRRRRRRB((   ((                                                             (((                      (((                                                                                                              BBBBBBBBBBBBBBBBBBBBBBRbBRRRRBBBBB(    (                                  $  %                       (((                        (((                                                                                                            BBBBBBBBBBBBBBBBBBBBBBBRRRRBRRRBBB((     (                               $$$$%%%%           ((       (((                         (((                                                                                                            BBBBBBBBBBBRRRBBbBBBBBBBBRRRRRBBBB((     (                               $$%%%%%%%((((((((((((((((  (((                           (((                                                                                                            BBBBBBBBBBRBRRRBBBbBBBBBBBRBBRRBB(       (                          $$$$$$%%%%%%%((((((((((((((((((((                            (((                                                                                                            BBBBBBBBBRRRRRRRRRBBBBBBBRBRRRRBB(       (       $$$$$$$$$$      $$$$$$%%$$%%%%%%%              (((((                            (((                                                                                                           BBBBBBBBBRRRRRRRRRBRBBBBRRRBBRBRBB(       ((   $$$$$$$$$$$$$  $$ $$$%%%%%%$$%%%%%%%              (((((                             ((                                                                                                           BBBBBBBBBBBBRRRRRRBRRBBRRRRRRRRBB((        (   $$$$$$$$$$$$$  $$$$$%%%%%%%$$$$%%%%                                                 ((                                                                                                           BBBBBBBBBBBBBBBBRbBBRRBBRRBBBBBBB((        (  $$$$$$$$$$$$$$$#$$$$%%%%%%$$$$$$$%%%                                                  ((                                                                                                            BBBBBBBGBBBBBBBBBBBBBBBBBbBBBG ((        (   $$$$$$$$$$$$$$#$$$$%%$%%$$$$$$%%%%%                                                  ((                                                                                                             BBBBBBGGGBBBBBBBbBBBBBbBBBGG   (        ((     $$$$$$$$$$$#$$$$$$$$$$$$$$$%%%%                               !!!!!               (                                                                                                                BB  GGGGGGBBBBBbbBBBBBGGGG   (((       (     $$$$$$$$$$$#$$$$$$$$$$$$$$$%%%%                               !TTT!              ((                                                                                                                     GGGGGGBBBBBBBBGGGGG      (((       (        $$$$$$$################%%%%                               4!VT!               (((                                                                                                                     GGGGGGBBGGGGGGG        (((       ((             $$$$$$$$$#$$$$$$$%%%%                               !TTT!               (((                                                                                                                         GGGGGGGG            ((         (             $$$$$$$$#$$$$$$$%%%%                               !!!!!               (((                                                                                                                                             ((          (             $$$$$$$#$$$$$$%%%         4                                         (((                                                                                                                                           ((           ((            $$%%$$#$%%$$%%%                                             (((                                                                                                                                           ((            (            $%%%$$#%%%%$%%   !!!   V                                         (((                                                                                                                                            (             ((             $%%$#$$$$    !! 2                                         (((                                                                                                                                          (              (                 #                                                     ((                                                                                                                                           (            ((                  #                                                        ((                                                                                                                                           (            (                   #######################                                      (                                                                                                                                             (((           (                              00   #                  ((                  (                                                                                                                                             (((           ((                            0 #                 (( ((               ((                                                                                                                                              ((            (                        00#                 (    (              (                                                                                                                                                ((           ((                V4####004#                (     ((            ((                                                                                                                                                ((           (                 0                 (                   ((                                !!!!!!!!                                                                                                ((          (                   000V                 (                    (                                         !      !                                                                                                ((         ((         ####                   (                   ((                                         !    !                                                                                                 (         (         V#         V  &                 ((                   ((                                         !    !                                                                                                 (         (          #       &                 (                   ((                                         !!! !                                                                                                  (        ((        &  &     &                ((                   ((                                                 !!                                                                                                ((        ((           &  &     !!!!&              (((                   (((                                          !!!                                                                                                ((        (               D!!!!!!D &            ((                      ((                                                                                                                                                  ((        (                &     !!!!    &            (                    ( ((((                                                                                                                                                  ((      (                    &&&&&&&"!&&&&&           ((                   ((((((                                                                                                                                                     ((      ((                                                     (                  ((((((                                                                                                                                                      (((      (                                                       (                (((((((                                                                                                                                                      ((      ((                                                      (                (((( (                                                                                                                                                   ((      ((                                                     (                (((                                          					    						                                                                                                   (       (((                                                   ((               (((                                          	   	    	    	                                                                                               (          (((((                                               (                ((                                          	   	    	    	                                                                                               (             ((                                              ((              (((                                         		        	   	                                                                                               ((              (        (((((((((                            ((               ((                                         	    		   		  	                                                                                                  ((              (    (((((   (( (((((                       (                 (((                                        	     	                                                                                                           (((        (( (  (((( ((           (((((((             (((((                   ((                                        	    		                                                                                                          ((        (((  ((((((                (( (((         (((((((                   ((                                        			 		     	 		                                                                                                        (((         (((((                         ((      ((                          (                                           		     	   	                                                                                               (((                                         (    ((                           ((                                                 	   	                                                                                                (((((                                     (   (                              (                                              	   	                                                                                                           ((((                                     ( ((                               ((                                          					                                                                                                          ((  ((        ((((((((((                ((                                 ((                                                                                                                                                                   (((((((((((((((((((((  ((((( (        ((((( (((                             ((                                                                                                                                                                                             ((((((((((((((((((((((((((((((((((((((((((((     ((                                                            	                                                                                                                                                                 ;                       `                            `                 `               `                                                     ```   `   `   ```   `   `   ```   `   `   @@@   @   @   ```   `   `   ```   `   `   ```   `   `   `   @   @   @                                                                                       m=                          -               -                   -  =  -                      -  -a-        -a-  -a  -        -  --           -  4  4        444  4  4    4  4        9=4 4 9=9=       4  44a4a  a   a       $        4   4a  4   4a  4  44a                                                                                                 @         `        @         `        @         `        `        `        @         `        @         `        @         `        @        `   d  0                                                                                            =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDD                                                        DDDDDDDD                                                        DDDDDDDD                                                                                                                                                                                                                                                                                        D                                                        D                                                        D                                                        D                                                        DDD                                                        DDD                                                        DDD                                                        DDD                                                        DDDD                                                        DDDD                                                        DDD                                                        DDD                                                        D                                                        D                                                        wDfD                                                        wDfD                                                        wwfDDDD                                                        wwfDDDD                                                        wfDD                                                        wfDD                                                        wwwwwwDDUUUUUUUUUUUU                                                        wwwwwwDDUUUUUUUUUUUU                                                        wwwwwww    UUUU3UUUUUUUUUUUUUUUUUUUU                                                        wwwwwww    UUUU3UUUUUUUUUUUUUUUUUUUU                                                        wwwwwwww    w                                                        wwwwwwww    w                                                        wwwwwww    w                                                        wwwwwww    w                                                        wwwwwwww   UUUUUU3UUUUUUUUUUUUUUUUUUUU                                                        wwwwwwww   UUUUUU3UUUUUUUUUUUUUUUUUUUU                                                        wwwwDwwww""""""wwww""""""̪̪UUUUUUUU                                                        wwwwDwwww""""""wwww""""""̪̪UUUUUUUU                                                        wwwwfDDDDUUUUUUUUUUU̪̪www""""""""                                                        wwwwfDDDDUUUUUUUUUUU̪̪www""""""""                                                        wwwfDDDDUUUU̪"""wwwwwww                                                        wwwfDDDDUUUU̪"""wwwwwww                                                        wDDDfUUUUU̪̪UUUUUUUU                                                        wDDDfUUUUU̪̪UUUUUUUU                                                        DDD̪̪UUUUUUU                                                        DDD̪̪UUUUUUU                                                        DDDDUUUUU̪                                                        DDDDUUUUU̪                                                        DDD̪UUUUU                                                        DDD̪UUUUU                                                        DDDDUU̪̪                                                        DDDDUU̪̪                                                        DDU̪̪UU                                                        DDU̪̪UU                                                        DU̪̪U                                                        DU̪̪U                                                        U̪̪                                                        U̪̪                                                        U̪̪                                                        U̪̪                                                        ̪̪                                                        ̪̪                                                        DDDD̪                                                        DDDD̪                                                        DDDDDDDDDDD̪                                                        DDDDDDDDDDD̪                                                        DDDDDDDDDDDDDDDDD̪                                                        DDDDDDDDDDDDDDDDD̪                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD                                                        DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDd -- title:  Sunray Valley
-- author: https://verysoftwares.itch.io
-- desc:   raycast game for LOWREZJAM'17
-- script: lua
-- input:  gamepad
-- saveid: verysoft

sqrt=math.sqrt

cos=math.cos
sin=math.sin
tan=math.tan
atan=math.atan2

abs=math.abs
floor=math.floor

pi=math.pi

-- Helpers --
function PrintToCell(text,cx,cy,color)
 print(text,CELL_W*cx,CELL_H*cy+2,color)
end

function DrawToCell(sprId,cx,cy)
 spr(sprId,CELL_W*cx,CELL_H*cy)
end

p={
	--mx=103,my=59,
	mx=107,my=40,
	w=3,h=3,
	ox=4,oy=4,	
	dx=0,dy=0,
	a=-math.pi/2-math.pi/4,
	turn=math.pi/54,
	fwd=0.2,
	cone=60*pi/180,
	sight=8*5,
}

function coll() 
end

function ctrl()
	local sidestep=false
	
	-- world coordinates.
	p.x= p.x or p.mx*8+p.ox
	p.y= p.y or p.my*8+p.oy
	
	p.dx=0; p.dy=0
	
	if btn(0) then p.dy= p.fwd end
	if btn(1) then p.dy=-p.fwd end
	if btn(4) and db_flags["run"] then p.dy=p.dy*3 end

	if btn(5) and db_flags["sidestep"]then sidestep=true; end

	if not sidestep and btn(2) then p.a=p.a-p.turn end
	if not sidestep and btn(3) then p.a=p.a+p.turn end


	coll()

	local cross=false	
	local pdx=cos(p.a)*p.dy
	local pdy=sin(p.a)*p.dy
	
	local opix=p.ix
	local opiy=p.iy

	p.ix=p.x+p.w/2+cos(p.a)*12
	p.iy=p.y+p.h/2+sin(p.a)*12
	p.ix=floor(p.ix/8)
	p.iy=floor(p.iy/8)

	if p.ix~=opix or p.iy ~= opiy then
		msg=nil
	end
		
	if sidestep then
		if btn(2) then
			pdx=cos(p.a-math.pi/2)*p.fwd
			pdy=sin(p.a-math.pi/2)*p.fwd
		end
		if btn(3) then
			pdx=cos(p.a+math.pi/2)*p.fwd
			pdy=sin(p.a+math.pi/2)*p.fwd
		end
	end
	
	--[[if p.ox<0 then p.mx=p.mx-1; p.ox=p.ox+8; cross=true end
	if p.ox>8 then p.mx=p.mx+1; p.ox=p.ox-8; cross=true end
	
	if cross and mget(p.mx,p.my)~=0 then
		if pdx>0 then p.mx=p.mx-1; p.ox=8 end
		if pdx<0 then p.mx=p.mx+1; p.ox=0 end
	end
	--]]
	
	p.x=p.x+pdx
	local pmx=floor(p.x/8)
	local pmxw=floor((p.x+p.w)/8)
	local pmy=floor(p.y/8)
	local pmyh=floor((p.y+p.h)/8)
	cx=nil; cy=nil
	
	local odx=dx; local ody=dy
	
	if pdx>0 then
		if solid(mget(pmxw,pmy)) then
			p.x=(pmxw)*8-p.w-0.02 
			--trace("pmxw pmy")
			cx=pmxw;cy=pmy
		elseif solid(mget(pmxw,pmyh)) then
			p.x=(pmxw)*8-p.w-0.02  
			--trace("pmxw pmyh")
			cx=pmxw;cy=pmyh
		end
	elseif pdx<0 then
		if solid(mget(pmx,pmy)) then
			p.x=(pmx+1)*8
			cx=pmx;cy=pmy
		elseif solid(mget(pmx,pmyh)) then
			p.x=(pmx+1)*8
			cx=pmx;cy=pmyh
		end
	end
	
	if cx~=nil then
		if deco(mget(cx,cy)) and not walkable(mget(cx,cy)) then
			dx,dy=cx,cy
			warn_step(mget(cx,cy))
		end
	end
	
	p.y=p.y+pdy
	local pmx=floor(p.x/8)
	local pmxw=floor((p.x+p.w)/8)
	local pmy=floor(p.y/8)
	local pmyh=floor((p.y+p.h)/8)
	cx=nil; cy=nil
	if pdy>0 then
		if solid(mget(pmx,pmyh)) then
			p.y=(pmyh)*8-p.h-0.02 
			--trace("pmx pmyh")
			cx=pmx;cy=pmyh
		elseif solid(mget(pmxw,pmyh)) then
		 p.y=(pmyh)*8-p.h-0.02 
			--trace("pmxw pmyh")
			cx=pmxw;cy=pmyh
		end
	elseif pdy<0 then
		if solid(mget(pmx,pmy)) then
			p.y=(pmy+1)*8
			cx=pmx;cy=pmy
		elseif solid(mget(pmxw,pmy)) then
			p.y=(pmy+1)*8 
			cx=pmxw;cy=pmy
		end
	end

--'''''''''	
	if cx~=nil then
		if deco(mget(cx,cy)) and not walkable(mget(cx,cy)) then
			dx,dy=cx,cy
			warn_step(mget(cx,cy))
		end
	end
	
	if odx~=dx or ody~=dy then
		msg=nil
	end

end

function walkable(tid)
	return deco(tid) and in_db(tid) and walk_lv(tid)
end

function walk_lv(tid)
	local lv=db[tid]
	if lv==nil then return false end
	if tid==16 or tid==17 or tid==33 then return true end
	if tid==0 then
			return lv>=1 
	end
	if tid==35 then
			return lv>=1 
	end
	if tid>160 then
			return false
	end
	if tid==48 then return true end
	return lv==4
end
annoy={}
function warn_step(tid)
	if not msg and annoy[tid]==nil then--or oldmsg=="STEP on @:\n>Need\n>research!") then
		msg="STEP on @:\n>"
		if tid<=160 then
			if not in_db(tid) then msg=msg.."Inspect\n>first!" 
			elseif not walk_lv(tid) then msg=msg.."Need\n>LV. up!"; atid=tid end
		else
			msg=msg.."Who would\n>step on\n>flowers?!"
		end
	end
end

function tex_tgt(tx,ty)
	if (tx<0.1 and (ty<0.4 or ty>=0.6)) or (ty<0.1 and (tx<0.4 or tx>=0.6)) then return 1 end
	if (tx>=0.9 and (ty<0.4 or ty>=0.6)) or (ty>=0.9 and (tx<0.4 or tx>=0.6)) then return 1 end
	return 0
end
function tex_water(tx,ty)
	--if 1 then return floor(sin((tx*4+ty*2+t/14/2))*2) end 
	if (tx*4+ty*2+t/14/2)%2>=0.8 then return 0 end
	return 1 
end
function tex_plank(tx,ty)
	if (tx*4)%2>=1.5 then return 0 end
	return 1 
end
function tex_stone(tx,ty)
	if xor(tx>=0.5, ty>=0.5) then return 1 end
	return 0
end

function tex_stripe(tx,ty)
	return ((tx+ty)*4)%2
end

function tex_multistripe(tx,ty)
	return ((tx+ty))%1
end

function tex_dia(tx,ty)
	local tc=(tx+ty-0.5)%2
	
	if xor(tx>0.5, ty>0.5) then
		tc=2-(tx-ty-0.5)%2
	end
	
	return 2-tc
end
function tex_blu(tx,ty)
	--if (ty*2)%1>0.2 and (ty*2)%1<0.8 and (tx*2)%1>0.4 and (tx*2)%1<0.6 then return 1 end
	--if (tx*2)%1>0.2 and (tx*2)%1<0.8 and (ty*2)%1>0.4 and (ty*2)%1<0.6 then return 1 end
	local w=0.4
	if tx<w and ty<w/2 then return 1 end
	if ty<w and tx<w/2 then return 1 end
	if tx>1-w and ty>(1-w)/2 then return 1 end
	if ty>1-w and tx>(1-w)/2 then return 1 end
	return 0--(ty+(t/16)%1)
end

function tex_bluflo(tx,ty)
	return tex_shine(tx,ty)
end

function tex_wood(tx,ty)
	if (ty*4)%4<1 or (ty*4)%4>=3 then return 0 end
	return 1 
end


function shade(c)
	if c==15 then return 10 end
	if c==14 then return 9 end
	if c==13 then return 8 end 
	if c==12 then return 9 end 
	if c==11 then return 5 end 
	if c==10 then return 7 end 
	if c==9 then return 4 end 
	if c==8 then return 2 end 
	if c==7 then return 3 end 
	if c==6 then return 4 end 
	if c==5 then return 3 end 
	if c==4 then return 2 end 
	if c==3 then return 2 end 
	if c==2 then return 1 end 
	return 0
end

function xor(a,b)
	return ((a or b) and not (a and b))
end

function deco(tid)
	--#deco
	return tid==0 or tid==16 or tid==17 or tid==25 or tid==33 or tid==36 or tid==35 or tid==27 or tid==73 or tid==54 or tid==86 or tid==82 or tid==32 or tid==48 or tid==248 or tid==102 or tid>160 or tid==66
end
function solid(tid)
	--and wall(tid) or not in_db(tid)
	return not deco(tid) or not in_db(tid) or not walkable(tid)
end

function unit(dir,rox,roy,rdx,rdy)
	if dir=="r" then
		d=(8-rox)/rdx
		m=8-rox
	end 
	if dir=="l" then
		d=(-rox)/rdx
		m=-rox
	end
	if dir=="d" then
		d=(8-roy)/rdy
		m=8-roy
	end 
	if dir=="u" then
		d=(-roy)/rdy
		m=-roy
	end
	return {dir=dir, d=d, m=m}
end

function raycast2(rx,ry)
	if skytest then
		sky=((t+4)/16)%16
		gnd=(t/16)%16
	 print("SKY"..tostring(floor(sky)),0,64)
	 print("GND"..tostring(floor(gnd)),32,64)	
	end
	rect(rx,ry,   64,32,sky)
	rect(rx,ry+32,64,32,gnd)
	
	local sw = 64
	local pp_dist = sw/2/tan(p.cone/2)
	local ra_diff = p.cone/sw
	
	for s=0,63 do

		local rx=floor((p.x+p.w/2)/8); local ry=floor((p.y+p.h/2)/8)
		local rox=(p.x+p.w/2)%8; local roy=(p.y+p.h/2)%8
		local ra=p.a-p.cone/2+p.cone/sw*s
		
		-- get the ray to closest edge
		local rdx=cos(ra)
		local rdy=sin(ra)
		local rlx=0
		local rly=0
		local rlf=0
		local linestop=32
		
		cared={}
		if rdx>0 then table.insert(cared,"r") end
		if rdx<0 then table.insert(cared,"l") end
		if rdy>0 then table.insert(cared,"d") end
		if rdy<0 then table.insert(cared,"u") end
		
		-- if there are two cared walls,
		-- check for closer one.
		
		k=0
		while #cared==2 and rlf<8*22 do
			k=k+1
			-- get dists to both with this angle
			-- move to the closer one
			one=unit(cared[1],rox,roy,rdx,rdy)
			other=unit(cared[2],rox,roy,rdx,rdy)
			
			-- which one's edge is closer 
			-- with this angle?
			if abs(one.d)<abs(other.d) then
				win=one
			else
				win=other
			end
			
			-- now, move to the winner's edge.
			
			if win.dir=="r" then
				rx=rx+1
				rox=0
				roy=roy+win.d*rdy

				rlx=rlx+win.m
				rly=rly+win.d*rdy
			end
			if win.dir=="l" then
				rx=rx-1
				rox=8
				roy=roy+win.d*rdy

				rlx=rlx+win.m
				rly=rly+win.d*rdy
			end
			if win.dir=="d" then
				ry=ry+1
				roy=0
				rox=rox+win.d*rdx

				rlx=rlx+win.d*rdx
				rly=rly+win.m
			end
			if win.dir=="u" then
				ry=ry-1
				roy=8
				rox=rox+win.d*rdx

				rlx=rlx+win.d*rdx
				rly=rly+win.m
			end
			
			rlf=sqrt(rlx*rlx+rly*rly)
			
			local tid=mget(rx,ry)
			
			if not deco(tid) then
			
				local corr=cos(-p.cone/2+p.cone/sw*s)
				rlf=rlf*corr
				
				local maxvis=pp_dist
				local c=12
				if rlf>26 then c=4 end
				if rlf>32 then c=1 end
				if win.dir=="r" or win.dir=="l" then
					c=10
					if rlf>26 then c=3 end
					if rlf>32 then c=2 end
				end
				
				--lh=(1-rlf/maxvis)*maxvis
				--if lh<0.5 then lh=0.5 end
				if rlf<1 then break end
				lh=8/rlf*pp_dist
				linestop=math.floor(32+lh/2)
				
				local tx=0
				local tc=0
				if win.dir=="u" or win.dir=="d" then
					tx=rox/8	--0..1
				else
					tx=roy/8	--0..1
				end
				--local sh=lh
				--if sh>63 then sh=63 end
				
				for l=0,lh do
					local sx=s; local sy=32-lh/2+l
					if sy>=0 and sy<=64 then
						local ty=l/lh	--0..1
						tc=tex(tid,tx,ty)
						
						if rlf>(24) or rlf>(18) and xor(sx%2>=1, sy%2>=1) then
									
								if tid==8 then
									 if tc==14 then tc=9
										elseif tc==15 then tc=12 
										end
									
								elseif tid==33 then
										if tc==7 then tc=1
										elseif tc==9 then tc=4 
										end
									

								elseif tid==4 then
											if tc==15 then tc=10
												elseif tc==11 then tc=5 
												end
			
								elseif tid==20 then
												if tc==15 then tc=10
												elseif tc==11 then tc=5 
												end
		
		
		
								elseif tid==5 then
											if tc==15 then tc=10
												elseif tc==14 then tc=9 
												end
			
								elseif tid==21 then
											if tc==15 then tc=10
												elseif tc==14 then tc=9 
												end
				
		
								
								elseif tid==6 then
											if tc==15 then tc=10
												elseif tc==12 then tc=6 
												end
		
								elseif tid==22 then
											if tc==15 then tc=10
												elseif tc==12 then tc=6 
												end
		
											
								elseif tid==2 then
											if tc==15 then tc=10
												elseif tc==10 then tc=7 
												end
		
								elseif tid==18 then
											if tc==15 then tc=10
												elseif tc==10 then tc=7 
												end
			
								
								elseif tid==7 then
											if tc==15 then tc=10
												elseif tc==8 then tc=2 
												end
		
								elseif tid==23 then
											if tc==15 then tc=10
												elseif tc==8 then tc=2 
												end
				
								elseif tid==37 then
											if tc==6 then tc=1
												else tc=4 end
				
								elseif tid==43 then
											if tc==13 then tc=8
												elseif tc==8 then tc=2 
												--elseif tc==2 then tc=0
												
												end
								
								else 
										tc=shade(tc)
								end
						
						end 
						
						if win.dir=="l" or win.dir=="r" then
							if tid~=43 and tid~=41 and tid~=238 and tid~=70 and tid~=71 and tid~=52 and tid~=68 then
								tc=shade(tc)
							else
								if tid==43 then
										if tc==2 then tc=0
										elseif tc==8 then tc=2 
										elseif tc==13 then tc=8 end
								end
								if tid==41 then
										if tc==1 then tc=0
										elseif tc==4 then tc=1 
										elseif tc==9 then tc=4 
										elseif tc==7 then tc=3 
										end
								end
							end
						end
						
						pix(sx,sy,tc)
						
						target(rx,ry,tx,ty,sx,sy)

						linestop=floor(sy+1)
					end
						
				end
				break
				
			end
		end
		
		--if linestop>63 then linestop=63 end

		--floorcasting
		--rlf=rlf/cos(-p.cone/2+p.cone/sw*s)
		for l=linestop,63 do
			if 64-linestop<1 then break end
			
			local dist_per_px= rlf/(64-linestop)
			--trace("px")
			--trace(rp)
			local dist_from_stp= (l-linestop)*dist_per_px
			
			local scan_stx= rx*8+rox
			local scan_sty= ry*8+roy
			--trace("l-linestop")
			--trace(l-linestop)
			--trace("dist")
			--trace(lp)
			--local rp=sqrt(rlx*rlx+rly*rly)/(64-linestop)
			--local lp=(l-linestop)*rp
						
			--#floor
			cD=64/(2*(l)-64)*(8-p.w/2+0.25)
			weight=(cD/rlf)
			cFx=weight*scan_stx+(1-weight)*(p.x+p.w/2)
			cFy=weight*scan_sty+(1-weight)*(p.y+p.h/2)
			
			--map pos at distance lp from player
			--local lpx=(floor((scan_stx-rdx*dist_from_stp)/8))
			--local lpy=(floor((scan_sty-rdy*dist_from_stp)/8))
			local lpx=(floor(cFx/8))
			local lpy=(floor(cFy/8))

			--if floor((cFx*2/8)%2)==1 then c=0
			--else c=0 end
			
			c=-1
			
			local tid=mget(lpx,lpy)
			local tx=(cFx%8)/8
			local ty=(cFy%8)/8
			local tc=0
			
			if deco(tid) then
				c=0
				
				c=tex(tid,tx,ty)

				if tid==25 then 
					if weight>(0.32) then
					if c==13 then c=7 end
					if c==8 then c=2 end
						--c=shade(c)
					end

				end
				if tid==27 then 
					if weight>(0.32) then
						if c==15 then c=13
					 else c=8 end
					end
					if weight>(0.32) then
					if c==8 then c=2 end
					if c==13 then c=8 end
						--c=shade(c)
					end
				end
				
				-- #FLOOR
				
			end
			--c=lpx%2+lpy%2
			--trace("->mapx")
			--trace(lpx)
			--trace("->mapy")
			--trace(lpy)
			--if lpx%2==0 then c=2	end
			
			local dsds=0
			--sqrt((cFx-p.x)*(cFx-p.x)+(cFy-p.y)*(cFy-p.y))
			
			if dsds>(24) or dsds>(18) and xor(s%2>=1, l%2>=1) then
				c=shade(c)
			end
									
			if l<64 and c~=-1 then 
					pix(s,l,c)
					if tid==17 or tid==33 then
						pix(s,63-l,c)
					end 
					--if tid==248 and not ghbl then
					--	drawsprtex(232,16,16, s-8,l-16,16,16)
					--	ghbl=true
					--end
			end--(5+l+t/2)%16/4

			target(lpx,lpy,tx,ty,s,l)			

		end
	end
	--ghbl=nil
end

virtualscr=0x8000
spmem=0x4000
function sp8x8(sp,sx,sy,off)
		off=off or 0
		local c=0
		local cy=0
		local i=0
		while cy<8 do
			tst=peek(spmem+32*sp+i)
			poke(virtualscr+off+i,tst)

			--local hi=(tst/16)%16
			--local lo=tst%16
			--pix(sx+c+1, sy+cy, hi)
			--pix(sx+c,   sy+cy, lo)

			c=c+2
			if c>=8 then
				c=0
				cy=cy+1
			end
			--out=out..string.format("%02x")
			i=i+1
		end
end

function init_sprite(_sp,_sw,_sh)
		--copy sprite batch into virtual mem
		--for use as texture.
		
		--already loaded.
		if _sp==spl then return end
		
		--globals.
		spl=_sp
		spw=_sw; sph=_sh

		local sp_=spmem
		local out=""
		clip()
		--spr(363,64,64)
		local off=0
		--for sy=0,sph/8-1 do
			--for sx=0,spw/8-1 do
				--sp8x8(spl+sx+sy*16,sx*8,sy*8,off)
				--sp_line()
				
				--copy line of width spw ()
				--off=off+32
			--end
		--end
		
		local rd=0
		for sy=0,sph/8-1 do
			for col=0,8-1 do 			
			
				for sx=0,spw/8-1 do
					for i=0,4-1 do
						local byte= peek(spmem+32*(spl+sy*16+sx)+col*4+i)
						poke(virtualscr+rd, byte)
						rd=rd+1
					end
				end
	
			end

		end
		--trace("rd")
		--trace(rd)
				
		clip(0,0,64,64)
end

-- get spritesheet pixel
-- from TIC wiki
function sget(x,y)
    local addr=0x4000+(x//8+(y//8)*16)*32 -- get sprite address
    return peek4(addr*2+x%8+y%8*8) -- get sprite pixel
end

function sprite_tex_fast(_sp,_sw,_sh,tx,ty)
		--idonno just output straight from mem
		
		local sx=(_sp&0xf)
		sx=sx//1
		sx=sx<<3
		sx=sx+tx*_sw
		--sx=(floor(_sp%16)*8+tx*_sw)
		local sy=_sp//1
		sy=sy>>4
		sy=sy<<3
		sy=sy+ty*_sh
		--sy=(floor(_sp/16)*8+ty*_sh)
		
		return sget(sx//1,sy//1)
		
end

function sprite_tex(tx,ty)
		--use after sprite has been init.
		--tx,ty from the currently loaded sprite.
		--returns pixel.
		
		sx=(tx*spw); sy=(ty*sph)
		--trace("s")
		--trace(sx)
		--trace(sy)
		--trace(string.format("%04x", floor(virtualscr+sy*spw+sx/2)))

		local byte= peek(virtualscr+floor(sy)*spw/2+sx/2)
			--local hi=(tst/16)%16
			--local lo=tst%16
		if sx%2<1 then
			local lo=byte%16
			return lo
		else
			local hi=(byte>>4)%16
			return hi
		end

end

function tex_meadow(tx,ty)
	--if (tx*4+ty%4)%2<0.5 then return 1
	--else return 0 end
	init_sprite(32,8,8)
	return sprite_tex(tx,ty)
end
function tex_shelf(tx,ty)
	init_sprite(320,24,24)
	return sprite_tex(tx,ty)
end
function tex_mud(tx,ty)
	if (tx*8+ty%8)%2<0.5 then return 1
	else return 0 end
end

function tex_swamp(tx,ty)
	if (tx+ty)>1 then return 1
	else return 0 end
end 

function tex_roxwall(tx,ty)
	if tx>0.5 then return 1
	else return 0 end
end
function tex_rox(tx,ty)
	if xor(tx>0.5,(ty>0.2 and ty<0.8)) then return 1
	else return 0 end
end

function selcolor()
		if t%16<8 then
			return (1+t/2)%16
		end
		return 14
end
function selcolor2()
		if t%16<8 then
			return (1+t/2)%16
		end
		return 10
end
function selcolor3()
		if t%16<8 then
			return (1+t/2)%16
		end
		return 11
end

function target(gx,gy,tx,ty,sx,sy)
		if gx==p.ix and gy==p.iy then
			tc=tex_tgt(tx,ty)
			if tc==1 then
					local c=0
				 if verb=="INSPECT" then c=selcolor() end
				 if verb=="OPEN" then c=selcolor2() end
				 if verb=="PLANT" then c=selcolor3() end
					pix(sx,sy,c)
			end

			if not ablock and btn(5) and btn(4) and (not msg or (msg and string.sub(msg,1,4)=="STEP") or (msg and string.sub(msg,1,5)=="PLANT")) then 
					action(gx,gy)
					ablock=true
			end
			if not (btn(5) and btn(4)) then
					ablock=false
			end

		end
			
end

--verbs={"INSPECT"}

verb="INSPECT"
opened=0
neveraction=true
hatches={}
function action(gx,gy)
	neveraction=false
	local tid=mget(gx,gy)
	if tid==50 then
					--verb change time.
					tostate("change")
					if not in_db(tid) then
						discover(tid)
					end
					return
	elseif tid==34 then
					--research time.
					tostate("research")
					if not in_db(tid) then
						discover(tid)
					end
					return
	elseif tid==102 then
	
								local pass=true
							for i,c in ipairs(hatches) do
									if c.x==gx and c.y==gy then
											pass=false
											break
									end
							end
							
							if pass then 
									table.insert(hatches,{x=gx,y=gy})
							end
	
					msg="ENTER =:\n>Not in\n>demo, but\n>good find.\n>("..tostring(#hatches).." of 5.)"
					if not in_db(tid) then
						discover(tid)
					end
					return
	end

	--'	
	if verb=="OPEN" then
			msg="OPEN =:\n>How'd I\n>open that?"
			if tid==52 then
					mset(gx,gy,0)
					opened=opened+1					
					msg="OPEN &:\n>Door open.\n(Total "..tostring(opened)..".)"
			end
			if tid==68 then
					if db_flags["LV.2 door"] and db_flags["LV.1 door2"] then
						mset(gx,gy,0)
						opened=opened+1					
						msg="OPEN +:\n>Door open.\n(Total "..tostring(opened)..".)"
					else
						msg="OPEN +:\n>Need\n>LV. up!"
					end
			end
			if tid==102 then
					msg="OPEN =:\n>It already\n>is, yo."
			end
			if tid==86 then
					mset(gx,gy,102)
					--opened=opened+1					
					msg="OPEN =:\n>Hatch\n>open."
			end
	end
	
	if verb=="PLANT" then
			--'
			msg="PLANT =:\n>Nothing\n>there to\n>plant."

			if plantx==nil then
					if tid>160 then
							msg="PLANT {:\n>Choose\n>target."
							plantx=gx; planty=gy; plantid=tid
					end
					if tid==38 then
							if db_flags["LV.1 hedge"] then
									msg="PLANT {:\n>Choose\n>target."
									plantx=gx; planty=gy; plantid=tid
							else
									msg="PLANT =:\n>Need\n>LV. up!"
							end
					end
			else
					if tid==0 then
							if db_flags["LV.2 grass"] then
									mset(plantx,planty,0)
									mset(gx,gy,plantid)
									msg="PLANT on {:\n>It's just\n>basic\n>gardening."
									plantx=nil;planty=nil;plantid=0
							else					
									msg="PLANT on =:\n>Need\n>LV. up!"
									plantx=nil;planty=nil;plantid=nil
							end
					elseif tid==48 then
							mset(plantx,planty,48)
							mset(gx,gy,plantid)
							--'
							msg="PLANT on {:\n>It's the\n>miracle\n>of life."
							plantx=nil;planty=nil;plantid=48
					--elseif tid>160 then
					--		--'fusion!
					else
							msg="PLANT on =:\n>Nevermind."
							plantx=nil;planty=nil;plantid=nil
					end
			end
	end
				
	if verb=="INSPECT" then
					
				--else
	
					local dc=desc(tid)
					msg="INSPECT =:\n>"..string.upper(string.sub(dc,1,1))..string.sub(dc,2).."."
					if not in_db(tid) then
						discover(tid)
					end
					if annoy[tid] then msg=msg.."\n>Research\n>to travel." end
					if wall(tid) then
							local pass=true
							for i,c in ipairs(wallc[tid]) do
									if c.x==gx and c.y==gy then
											pass=false
											break
									end
							end
							
							if pass then 
									table.insert(wallc[tid],{x=gx,y=gy})
							end
							
							msg=msg.."\n>("..tostring(#wallc[tid]).." found)"
					end
				--end
	end
end
wallc={
--list of inspected positions
[2]={},
[4]={},
[5]={},
[6]={},
[7]={},
}
grid=8
function coll_xy(a,b)
    aw= a.w or grid
    bw= b.w or grid
    ah= a.h or grid
    bh= b.h or grid
    return a.x < b.x+bw and
           b.x < a.x+aw and
           a.y < b.y+bh and
           b.y < a.y+ah
end

function wall(tid)
	return tid==6 or tid==7 or tid==2 or tid==4 or tid==5
	--return not deco(tid)
end

function discover(tid)
	db[tid]=0
	if tid==102 then return end
	if msg then msg=msg.."\n*NEW*" end
	if deco(tid) then
			--'
			if walkable(tid) then msg=msg..":\n>Ah, it's\n>safe."
			else 
				if tid~=25 then msg=msg..":\n>Better not\n>step on it\n>just yet."
				else msg=msg..":\n>Shiny, but\n>I can't\n>swim." end
			end
	end
	--if walkable(tid) then
	--	msg=msg.."\n    Safe!"
	--end
end

function desc(tid)
	local out="dunno"
	
	if db_reqs[tid]~=nil then
			if db_reqs[tid].desc then out=db_reqs[tid].desc end
			return out
	end
	
	if tid==0 then 
			out="grass"
	end
	if tid==32 then return "meadow" end
	if tid==34 then return "shelf" end
	if tid==71 then return "fog" end
	if wall(tid) then
	 out="wall"
		
		if tid==2 or tid==18 then out=out.."(gray)" end
		if tid==7 or tid==23 then out=out.."(blue)" end
		if tid==21 or tid==5 then out=out.."(gold)" end
		if tid==6 or tid==22 then out=out.."(pink)" end
		if tid==4 or tid==20 then out=out.."(leaf)" end
	 return out  
	end
	--if deco(tid) then
	--	out="just a deco"
		if tid==16 then
			out="walkway" 
		end
		if tid==17 or tid==33 then out="planks" end
		if tid==25 then out="water" end
	--	return out
	--end
	return out
end

function batch(sp,sx,sy,sq)
	sq=sq or 4
	for i=0,sq-1 do
		for j=0,sq-1 do
			spr(sp+j*16+i,sx+i*8,sy+j*8,0)
		end
	end
end


db={			--database of known blocks
	[17]=0,		--both planks
	[33]=0,		--start at lv.0.
} 

function in_db(tid) 
	--for i,v in ipairs(db) do
	--	if v==tid then return true end
	--end
	return db[tid]~=nil
end

function tex_grass(tx,ty)
			if (tx*2)%2>=1 then return 5
			else return 11 end
end

function tex_tree(tx,ty)
	if abs(ty-0.5+tx-0.5+1)<0.8 then return 0 end
	
	if abs(tx-0.5)<0.1 then return 0
	else return 1 end
end

function tex_shine(tx,ty)
local tc=0
	
	if xor(tx>0.8, ty>0.8) then
		tc=(t/16+tx-ty-0.8)%2
	end
	
	return tc
end

function tex_spr24(sp,tx,ty)
	init_sprite(sp,24,24)
	return sprite_tex(tx,ty)
end

function tex_spr_any(sp,sw,sh,tx,ty)
		return sprite_tex_fast(sp,sw,sh,tx,ty)
end

function tex_hatch(tx,ty)
	init_sprite(324,24,24)
	return sprite_tex(tx,ty)
end

function tex(tid,tx,ty)
	local tc=0
	if tid==0 then
		return tex_grass(ty,tx)
	end

	if tid==34 then return tex_spr_any(320,24,24,tx,ty) end
	if tid==86 then return tex_spr_any(324,24,24,tx,ty) end
	if tid==32 then return tex_spr_any(368,24,24,tx,ty) end

	if tid==208 then return tex_spr_any(416,24,24,tx,ty) end
	if tid==224 then return tex_spr_any(420,24,24,tx,ty) end
	if tid==38 then return tex_spr_any(464,24,24,tx,ty) end

	if tid==52 then return tex_spr_any(456,24,24,tx,ty) end
	if tid==68 then return tex_spr_any(459,24,24,tx,ty) end

	if tid==36 then return tex_spr_any(88,24,24,tx,ty) end
	if tid==37 then return 6 end--tex_spr24(91,tx,ty) end

	if tid==102 then return tex_spr_any(117,24,24,tx,ty) end--tex_spr24(91,tx,ty) end
	if tid==66 then return tex_spr_any(266,24,24,tx,ty) end--tex_spr24(91,tx,ty) end

	--#tex

	if tid==25 then 
		tc=tex_water(ty,tx) 
		if tc>=1 then tc=13 else tc=8 end
		return tc
	end
	if tid==25 then 
		tc=tex_water(ty,tx) 
		if tc>=1 then tc=13 else tc=8 end
		return tc
	end
	if tid==16 then
		tc= tex_stone(tx,ty)
		if tc>=1 then tc=10 else tc=12 end
		return tc
	end
	if tid==8 then
		tc=tex_dia(tx,ty)
		if tc>=1 then tc=14 else tc=15 end
		return tc
	end
	if tid==17 then 
		tc=tex_plank(tx,ty) 
		if tc>=1 then tc=4 else tc=1 end
		return tc
	end
	if tid==33 then 
		tc=tex_plank(ty,tx) 
		if tc>=1 then tc=4 else tc=1 end
		return tc
	end
	if tid==4 then
		tc=tex_wood(tx,ty)
		if tc>=1 then tc=15 else tc=11 end
		return tc
	end
	if tid==20 then
		tc=tex_wood(ty,tx)
		if tc>=1 then tc=15 else tc=11 end
		return tc
	end
	if tid==5 then
		tc=tex_wood(tx,ty)
		if tc>=1 then tc=15 else tc=14 end
		return tc
	end
	if tid==21 then
		tc=tex_wood(ty,tx)
		if tc>=1 then tc=15 else tc=14 end
		return tc
	end
	if tid==6 then
		tc=tex_wood(tx,ty)
		if tc>=1 then tc=15 else tc=12 end
		return tc
	end
	if tid==22 then
		tc=tex_wood(ty,tx)
		if tc>=1 then tc=15 else tc=12 end
		return tc
	end
	if tid==2 then
		tc=tex_wood(tx,ty)
		if tc>=1 then tc=15 else tc=10 end
		return tc
	end
	if tid==18 then
		tc=tex_wood(ty,tx)
		if tc>=1 then tc=15 else tc=10 end
		return tc
	end
	if tid==7 then
		tc=tex_wood(tx,ty)
		if tc>=1 then tc=15 else tc=8 end
		return tc
	end
	if tid==23 then
		tc=tex_wood(ty,tx)
		if tc>=1 then tc=15 else tc=8 end
		return tc
	end
 if tid==37 then
			tc=tex_roxwall(tx,ty)
			if tc>=1 then tc=6 else tc=3 end
			return tc
	end
	if tid==40 then
			tc=tex_tree(tx,ty)
			if tc>=1 then tc=1 else tc=5 end
			return tc
	end
	if tid==43 then
			tc=tex_blu(tx,ty)
			if tc>=1 then tc=13 else tc=8 end
			return tc
	end
	if tid==41 then
			tc=tex_multistripe(tx,ty)
			if tc>=0.75 then tc=7 
			elseif tc>=0.5 then tc=9 
			elseif tc>=0.25 then tc=4 
			elseif tc<0.25 then tc=1 end
			return tc
	end
	if tid==70 then return 4 end			
	if tid==71 then return 10 end			
	if tid==98 then
			tc=tex_tree(tx,ty)
			if tc>=1 then tc=9 else tc=11 end
			return tc
	end


				if tid==27 then 
					tc=tex_bluflo(tx,ty) 
					if tc>=1 then tc=15
					else tc=13 end
					return tc
				end

				if tid==35 then 
					tc=10
					return tc
				end
				if tid==36 then
					tc=tex_rox(ty,tx)
					if tc>=1 then tc=6
					else tc=10 end
					return tc
				end
				if tid==73 then
					tc=1
					return tc
				end
				
				if tid==82 then
					tc=tex_swamp(ty,tx)
					if tc>=1 then tc=0
					else tc=5 end
					return tc
				end
				if tid==32 then
						tc=tex_meadow(ty,tx)
					if tc>=1 then tc=12
					else tc=11 end
					return tc
						
				end
				if tid==48 then
						tc=tex_mud(ty,tx)
					if tc>=1 then tc=7
					else tc=4 end
					return tc
				end


 	return (-t/8+tx-ty)%16
end

function tostate(st)
	state=st
	res=nil
	plantx=nil; planty=nil; plantid=nil
	if st=="research" then
		if steps4 and stepslv.steps==0 then
				state="rsrs2"
				return
		end	
		if stepslv and stepslv.opens~=nil and (stepslv.opens-opened)<=0 then
				state="rsrs2"
				return
		end
		if stepslv and stepslv.inspects~=nil and (stepslv.inspects-#wallc[inspects4])<=0 then
				state="rsrs2"
				return
		end
		res_tbl={}
		rx=0;ry=0
		sel=1
		for k,v in pairs(db) do 
				if k~=17 then
					--for now
						table.insert(res_tbl, k) end
		end
  table.sort(res_tbl)
	end
	if st=="change" then
		verbs={"INSPECT"}
		vsel=1
		if db_flags["PLANT verb"] then table.insert(verbs,"PLANT") end
		if db_flags["OPEN verb"] then table.insert(verbs,"OPEN") end
	end
end

function printreqs(tid,sx,sy)
	local out="???"
	local rq=db_reqs[tid]
	if rq==nil then print(out,sx,sy,(t/8)%16); return end
	local lv=rq[db[tid]+1]
	if lv==nil then print(out,sx,sy,(t/8)%16); return  end
	
	if lv.steps then stw=print(tostring(lv.steps).." steps",sx,sy,15) end
	if lv.stepmatch then
					stw=stw+print(" @",stw,sy)
					drawtex(stw,sy,6,6,lv.stepmatch)
	end
	if lv.opens then
			local oppa=lv.opens-opened
			if oppa<=0 then oppa=0 end
			print("OPEN "..tostring(oppa).." doors",sx,sy)
	end
	if lv.inspects then
			local oppa=lv.inspects-#wallc[tid]
			if oppa<=0 then oppa=0 end
			stw=print("INSPECT "..tostring(oppa).."x",sx,sy) 
			drawtex(stw,sy,6,6,tid)
	end
	local sc=15
	for i,f in ipairs(lv) do
			sc=15
			if not db_flags[f] then 
					sc=6
			end
			print(f,sx,sy+i*6,sc)
	end
end

function vchange()
		msg="SET CURSOR"
		for i,v in ipairs(verbs) do
			if i==vsel then msg=msg.."\n>"..v
			else msg=msg.."\n"..v end
		end
		
	if res then			
		if btnp(4) then
				verb=verbs[vsel]
				tostate(nil)
				msg=nil
		end
		if btnp(5) then
				tostate(nil)
				msg=nil
		end
		if btnp(0) then
			vsel=vsel-1
			if vsel<1 then vsel=#verbs end
		end
		if btnp(1) then
			vsel=vsel+1
			if vsel>#verbs then vsel=1 end
		end
	end		
	if not res then res=true end
end

function research2()
	msg="LV. up!"
	local tid=steps4 or opens4 or inspects4
	local rq=db_reqs[tid]
	local lv=rq[db[tid]]
	--[[if steps4==0 and db[tid]+1==1 then
			msg=msg.."\nCan STEP\non grass."
	end
	if steps4==33 and db[tid]+1==1 then
			msg=msg.."\nThere are\ntwo types\nof planks."
	end--]]
	if stepslv.rwd then
			msg=msg.."\n"..stepslv.rwd
	end
	--print(msg)

	if res then			
		if btnp(4) or btnp(5) then
				db[tid]=db[tid]+1
				if stepslv.setflag then db_flags[stepslv.setflag]=true end
				steps4=nil
				opens4=nil
				inspects4=nil
				stepslv=nil
				msg=nil
				db_flags["LV."..tostring(db[tid]).." "..desc(tid)]=true
				trace("added flag LV."..tostring(db[tid]).." "..desc(tid))
				tostate("research")
		end	
	end
	if not res then res=true end
end

function research()
				rect(0,0,64,6,12+(t/8)%3)


					print("RESEARCH:", 0,0, 2+(selcolor()+6)%13, true)

				rect(0,6*3-2-10,64,6*4+4,15)
			local wx=rx+64/2-24/2
			for i,tid in ipairs(res_tbl) do
				local w=8
				if i==sel then 
					w=24 
					rect(0,6*7+2-10,64,6,12+(t/8)%3)
					print("LV."..tostring(db[tid]+1).." "..desc(tid),0,6*7+2-10,selcolor()+6%13)
					printreqs(tid,0,6*9+2-10-4,15)
				end
				if wx>=0 and wx<64 then drawtex(wx,6*3+6*2-w/2-10,w,w,tid) end
				wx=wx+w+2
			end
			
				line(0,6*7+2-10+6,64,6*7+2-10+6,1)
				line(0,6*7+2-10+6+1,64,6*7+2-10+6+1,4)

			if res then			
				if btnp(2) and sel>1 then
					rx=rx+10
					sel=sel-1
				end
				if btnp(3) and sel<#res_tbl then
					rx=rx-10
					sel=sel+1
				end
		
				if btnp(4) then upgrade(res_tbl[sel]) end
				if btnp(5) then tostate(nil) end
			end
			if not res then res=true end

end

function upgrade(tid)
	-- day-=1 if all other flags exist
	-- lv+=1 if day 0
	
	local rq=db_reqs[tid]
	if rq==nil then return end
	local lv=rq[db[tid]+1]
	if lv==nil then return end
	
	local flags=true
	for i,f in ipairs(lv) do
			if not db_flags[f] then 
			-- indicate that it is because of this specifically?
					flags=false
					break
			end
	end

	if not flags then return end
	
	--launch a step counter.
	--with other reqs too.
	if lv.steps~= nil then steps4=tid end
	if lv.opens~= nil then opens4=tid end
	if lv.inspects~= nil then inspects4=tid end
	stepslv=lv
	tostate(nil)	
	--if flags then
	--		lv.days=lv.days-1
	--		if lv.days<=0 then
	--				db[tid]=db[tid]+1
	--				-- output msg/reward
	--		end
	--end
end

db_reqs={
	[0]={ --#grass
			desc="grass",
			{steps=5, rwd="Can STEP\non grass."},
			{steps=50, "PLANT verb", rwd="Can PLANT\non grass."},
			{"Not in demo."},
	},
	[2]={ 
			desc="wall(gray)",
			{inspects=5,setflag="a LV.1 wall",rwd="5th of 5\nwall types."},
			{inspects=10,rwd="Gray walls\nare shared\nproperty."},
			{inspects=16,rwd="Collection\ncomplete."},
			{"Not in demo."},
	},
	[4]={ 
			desc="wall(leaf)",
			{inspects=5,setflag="a LV.1 wall",rwd="4th of 5\nwall types."},
			{inspects=10,rwd="Leaf walls\nbelong to\nsomeone\neccentric."},
			{inspects=14,rwd="Collection\ncomplete.\nThe owner\nwould be\nsatisfied."},
			{"Not in demo."},
	},
	[5]={ 
			desc="wall(gold)",
			{inspects=5,setflag="a LV.1 wall",rwd="1st of 5\nwall types."},
			{inspects=10,rwd="Gold walls\nbelong to\nsomeone\nsoft."},
			{inspects=12,rwd="Collection\ncomplete.\nThe owner\nwould be\nhappy for\nyou."},
			{"Not in demo."},
	},
	[6]={ 
			desc="wall(pink)",
			{inspects=5,setflag="a LV.1 wall",rwd="3rd of 5\nwall types."},
			{inspects=15,rwd="Pink walls\nbelong to\nsomeone\ndiplomatic."},
			{inspects=24,rwd="Collection\ncomplete.\nThe owner\nwould be\nwriting\na report."},
			{"Not in demo."},
	},
	[7]={ 
			desc="wall(blue)",
			{inspects=5,setflag="a LV.1 wall",rwd="2nd of 5\nwall types."},
			{inspects=15,rwd="Blue walls\nbelong to\nsomeone\nperceptive."},
			{inspects=20,rwd="Collection\ncomplete.\nThe owner\nwould be\nwondering\nwhy."},
			{"Not in demo."},
	},
	[16]={ --#walkway
			desc="walkway",
			{steps=20,stepmatch=16,setflag="run",rwd="Hold Z to\nrun."},
			{steps=60,stepmatch=35,setflag="sidestep",rwd="Hold X to\nsidestep."},
			{"Not in demo."},
	},
	[32]={ --#meadow
			desc="meadow",
   {"Not in demo."},
	},
	[33]={	--#planks (other)
			desc="planks",
			{steps=15,rwd="There are\ntwo types\nof planks."},
			{"Not in demo."},
	},
	[35]={	--#road
			desc="road",
			{steps=42, "LV.1 walkway", rwd="Hit the\nroad!"},
   {"Not in demo."},
	},
	[36]={	
			desc="ore",
   {"Not in demo."},
	},
	[37]={	
			desc="quarry",
   {"Not in demo."},
	},
	[52]={	
			desc="door",
			{steps=0, "a LV.1 wall", setflag="OPEN verb", rwd="Learned\nOPEN verb!"},
			{opens=3, rwd="Getting\ngood with\ndoors."},
			{"Not in demo."},
	},
	[68]={	
			desc="door2",
			{steps=0, "LV.2 door", rwd="Can OPEN\nnew doors\nnow."},
			{"Not in demo."},
	},
	[48]={	
			desc="soil",
			{steps=50, rwd="Learned\nPLANT verb!", setflag="PLANT verb"},
			{"Not in demo."},
	},
	[38]={	
			desc="hedge",
			{steps=260, rwd="A mystery\nunfolds."},
			{"Not in demo."},
	},
	[224]={
			desc="flora(pink)",
			{"Not in demo."},
	},
	[208]={
			desc="flora(blue)",
			{"Not in demo."},
	},
	[25]={
			desc="water",
			{"Not in demo."},
	},
	
	[34]={
			desc="shelf",
			{"Not in demo."},			
	},
	[50]={
			desc="drobe",
			{"Not in demo."},			
	},
	[102]={
			desc="stairs",
			{"Not in demo."},			
	},
	[86]={
			desc="hatch",
			{"Not in demo."},			
	},

	--#reqs
}
db_flags={
	--[0]
}

function drawtex(sx,sy,sw,sh,tid)
	for ty=0,sh-1 do
		for tx=0,sw-1 do
			local tc=tex(tid,tx/sw,ty/sh)
			pix(sx+(sw-1-tx),sy+ty,tc)
		end
	end
end
function drawsprtex(_sp,_sw,_sh,sx,sy,sw,sh)
	init_sprite(_sp,_sw,_sh)
	
	for ty=0,sh-1 do
		for tx=0,sw-1 do
			local tc=sprite_tex(tx/sw,ty/sh,sw,sh)
			if tc>0 then pix(sx+tx,sy+ty,tc) end
		end
	end
end

sprites={{id=248,x=105,y=52}}
function spritehandle()
		for i,s in ipairs(sprites) do
				local a=atan(s.y-p.y, s.x-p.x)
				while a<0 do a=a+2*pi end
				while a>2*pi do a=a-2*pi end

				local a2=p.a-p.cone/2
				local a3=p.a+p.cone/2
				while a2<0 or a3<0 do a2=a2+2*pi; a3=a3+2*pi end
				while a2>2*pi or a3>2*pi do a2=a2-2*pi; a3=a3-2*pi end

				--trace("p.a")
				--trace(a2)
				--trace(a3)
				--trace("a")
				--trace(a)
		end
end

function TIC()
	--Init()
	--Update()
	--Draw()
	--trace(mget(101,75))
	if t%8==0 then i=i+1; if i>15 then i=0 end end

	cls(0)
	clip(0,0,64,64)

	stepped=false
	if p.x~=nil then
		opx=floor(p.x/8)
		opy=floor(p.y/8)
	end
	if state==nil then ctrl() end
	if p.x and (floor(p.x/8)~=opx or floor(p.y/8)~=opy) then stepped=true end

	--rect(0,0,64*2+16,64+8,0)
	

	--minimap(64+8,0)

	--raycast(0,0)	
	raycast2(0,0)	
	
	if state=="research" then
		research()
	end
	if state=="change" then
		vchange()
	end
	if state=="rsrs2" then
		research2()
	end
	
	--batch(257,0,16+8)
	--batch(262,0+32,16+8)
	--batch(384,0,0,8)
	
	spritehandle()

	--if atid and not msg then
	--	annoy[atid]=true
	--	atid=nil
	--end	
	stepcount()
	
	if plantx~=nil then
			msg="PLANT {:\n>Choose\n>target."
	end

	if msg then
				oldmsg=msg
				local _, count = string.gsub(msg, "\n", "")

				rect(0,0,64,6+count*6,15)
				rect(0,0,64,6,12+(t/8)%3)
				if count>=2 and string.find(msg,"*NEW*") then
					rect(0,12,64,6,12+(t/8)%3)
				end
-- character line color
--				line(0,6+count*6,64,6+count*6,14)
				line(0,6+count*6,64,6+count*6,1)
				line(0,6+count*6+1,64,6+count*6+1,4)
				local mx=0; local my=0
				for i=1,#msg do
					local char=string.sub(msg,i,i)
					if char=="=" or char=="@" or char=="&" or char=="+" or char=="{" then 

						local tid=0
						if char=="=" then tid=mget(p.ix,p.iy) end
						--if string.sub(msg,1,4)=="STEP" then
						if char=="@" then tid=mget(dx,dy) end
						if char=="&" then tid=52 end
						if char=="+" then tid=68 end
						if char=="{" then tid=plantid end
						--end

						drawtex(mx,my,6,6,tid)

					else
					print(char, mx,my, 2+(selcolor()+6)%13, true)
					end
					mx=mx+6
					if char=="\n" then mx=0; my=my+6 end
				end
				--print(msg, 0,0, (selcolor()+6)%16, true)

	end

	
	--drawsprtex(206,16,16, 0,0,16,16)
	--drawsprtex(206,16,16, 32,0,16+math.sin(t/8)*8,16+math.cos(t/8)*8)
	
	t=t+1
	
	--trace(string.format("%02x ",peek(0x13FFC))..string.format("%02x ",peek(0x13FFC+1))..string.format("%02x ",peek(0x13FFC+2))..string.format("%02x ",peek(0x13FFC+3)))
	
	--trace(string.format("%02x ",peek(0xFF9C+72-16-1)))
	--trace(string.format("%02x ",peek(0xFF9C+1)))
	--poke(0xFF9C+1,peek(0xFF9C+1)&0x2f)
	--trace(string.format("%02x ",0x0f|0x2<<4))
	
	dj()
	--clip()	
	--rect(0,64,240,32,1)
	--clip(0,0,64,64)	
	if neveraction then print("Welcome!\n Move with \n    arrows,\n  Z+X to use\n cursor.") end

	clip()
	for sx=64-1,0,-1 do
	for sy=64-1,0,-1 do
			local p=pix(sx,sy)
			rect(240/2-64+sx*2,136/2-64+sy*2,2,2,p)
	end
	end
	rect(0,0,240/2-64,64,0)
	rect(0,0,64,136/2-64,0)
	
end

function stepcount()
		if not stepslv or state=="research" then return end

		if stepslv.steps~=nil then		
				if stepped and stepslv.steps>0 then
						--trace(mget(dx,dy))
						if (stepslv.stepmatch==nil) or (stepslv.stepmatch==mget(floor(p.x/8),floor(p.y/8))) then
								stepslv.steps=stepslv.steps-1
						end
				end
				if stepslv.steps>0 then	
					stw= print(tostring(stepslv.steps).." steps")
					if stepslv.stepmatch~=nil then
							stw=stw+print(" @",stw,0)
							drawtex(stw,0,6,6,stepslv.stepmatch)
					end 
				else
					print("Done!",0,0,(t/4)%15)
				end
		end
		if stepslv.opens~=nil then
					local oppa=stepslv.opens-opened
					if oppa<=0 then print("Done!",0,0,(t/4)%15)
					else print("OPEN "..tostring(oppa).." doors") end
		end

		if stepslv.inspects~=nil then
					local oppa=stepslv.inspects-#wallc[inspects4]
					if oppa<=0 then print("Done!",0,0,(t/4)%15)
					else 
							stw=print("INSPECT "..tostring(oppa).."x") 
							drawtex(stw,0,6,6,inspects4)
					end
		end
		
end

--ruutu ch0 93 53
--pata ch1 99 59
--hertta ch2 101 66
--risti ch3 115 51

--lowrez
--gold 135 115
--blue 142 107
--pink 129 107
--leaf 135 100

song=0
function dj()
		--local ppos=mget(,floor(p.y/8)))
		if song~=0 and floor(p.x/8)==122 and floor(p.y/8)==100 then
				song=0
				music(0)
		end
		if song~=1 and floor(p.x/8)==122 and floor(p.y/8)==96 then
				song=1
				music(1)
		end

		if song==0 then
				local dist=dist2(135,115)
				setvol(0,mixervol(dist))
		
				local dist=dist2(142,107)
				setvol(1,mixervol(dist))
		
				local dist=dist2(129,107)*1.2
				setvol(2,mixervol(dist))
		
				local dist=dist2(135,100)
				--local vol=15
				--if vol<
				if dist>7 then
						setvol(3,0)
				end
		end
end
function mixervol(dist)
		--if dist<1 then dist=1 end
		local vol=floor(15-dist*1.2)
		if vol>15 then vol=15 end
		if vol<0 then vol=0 end
		return vol
end
function dist2(px,py)
		return sqrt(math.pow(abs(p.x/8-px),2)+math.pow(abs(p.y/8-py),2))
end

function setvol(ch,vol)
	-- channel `ch` 0..3 
	-- volume `vol` 0..15
	local val=peek(0xFF9C+18*ch+1)
	poke(0xFF9C+18*ch+1, val&(0x0f|vol<<4))
end


t=0
i=0

sky=15; gnd=8

function spawn(loc)
	if loc=="diamtest" then
		p.mx=97; p.my=40; p.a=-math.pi/2
		sky=13; gnd=12
	end
	if loc=="stripetest" then
		p.mx=107; p.my=40; p.a=-math.pi/2-math.pi/4
		gnd=1; sky=3
	end
	if loc=="woodtest" then
		p.mx=80; p.my=37; p.a=math.pi/2
		--skytest=true
		gnd=11; sky=12
	end
	if loc=="village" then
		p.mx=98; p.my=76-17; p.a=0
		--skytest=true
		gnd=11; sky=15
	end
	if loc=="village_yellow" then
		p.mx=92; p.my=70-17; p.a=0.02
		--skytest=true
		gnd=11; sky=15
	end
	if loc=="library" then
		p.mx=101; p.my=65-17; p.a=0.02-math.pi/2
		--skytest=true
		gnd=11; sky=15
	end
	if loc=="indust" then
		p.mx=114; p.my=100-17; p.a=0.02+math.pi/2
		gnd=11; sky=15
		for i=0,256 do
			db[i]=4
		end
	end
	if loc=="trees" then
		p.mx=92; p.my=61; p.a=0.02-math.pi
		gnd=11; sky=15
		for i=0,256 do
			db[i]=4
		end
	end
	if loc=="bluecave" then
		p.mx=224; p.my=42; p.a=0.02+math.pi/2
		--skytest=true
		gnd=2; sky=13--2
		for i=0,256 do
			db[i]=4
		end
	end
	if loc=="minecave" then
		p.mx=224; p.my=25; p.a=0.02-math.pi/2
		gnd=1; sky=7
		
	end
	if loc=="mountain" then
		p.mx=216; p.my=4; p.a=0.02-math.pi/2
		gnd=14; sky=10
	end
	if loc=="swamp" then
		p.mx=64; p.my=79; p.a=0.02+math.pi/2
		gnd=5; sky=9
	end
	if loc=="village_smol" then
		p.mx=136; p.my=117; p.a=0.02-math.pi/2
		gnd=5; sky=9
	end
	
	if debug then
	  for i=0,256 do
	  		db[i]=4
  	end
			db_flags["run"]=true
			db_flags["sidestep"]=true
			db_flags["PLANT verb"]=true
			db_flags["OPEN verb"]=true
	end
end

--#debug
--debug=true

--spawn("stripetest")
--spawn("diamtest")
--spawn("woodtest")
--spawn("village_yellow")

debug=false
spawn("village_smol")
if not debug then
		music(0)
end
--spawn("library")
--spawn("indust")
--spawn("trees")
--spawn("bluecave")
--spawn("minecave")
--spawn("mountain")
--spawn("swamp")
