' NekoCat.ibas
{CREATORID "LDNE"}
{VERSION "4.4"}
{RESOURCEFILE "NekoCat.rsrc"}

' Variables assignation:
' A	temp
' B	temp
' C	Draw Food, 0=No, 1=Yes
' D	Small Images 1=No, 2=Yes
' E	Event
' F	Food
'	Images Rsrc are at #F for LowRes
'	and at #F+50 for HighRes
'	201=Bird
'	202=Mouse
'	203=Fish
' G	Duration of current Behaviour
' H	temp
' I	Food Pos X
' J	Food Pos Y
' K	Background Color
' L	Neko Behaviour
'	Images Rsrc are at #L+300 for 
'	LowRes and at #L+350 for 
'	HighRes
'	1=Wake up, 2=Yawn
'	3=Eat1, 4=Eat2
'	5=Scratch1, 6=Scratch2
'	7=Sleep1, 8=Sleep2
'	9=Up1, 10=Up2
'	11=UpRight1, 12=UpRight2
'	13=UpLeft1, 14=UpLeft2
'	15=Down1, 16=Down2
'	17=DownRight1, 18=DownRight2
'	19=DownLeft1, 20=DownLeft2
'	21=Right1, 22=Right2
'	23=Left1, 24=Left2	
' M	Previous Neko Pos X
' N	Previous Neko Pos Y
' O	[not used]
' P	[not used]
' Q	Tick Seed
' R	Relative speed
' S	Wake up duration
' T	Eat duration
' U	Scratch duration
' V	Yawn duration
' W	Relative sleep
' X	Neko Pos X
' Y	Neko Pos Y
' Z	Form, 0=Main, 1=Options

' A$	temp
' B$-Z$  [not used]


BEGIN
   GOSUB _InitNeko
   GOSUB _InitMain
   GOSUB _EventLoop
   GOSUB _NekoSave
END

_EventLoop:
   REPEAT
      E=DOEVENTS
      ' Manage events available in all forms
      IF E>=1 IF E<=3 LET F=200+E : GOSUB _ChooseFood : IF C=1 GOSUB _EraseFood : GOSUB _PutNeko : H=D-1*50+F : IMAGE H,I,J
      IF E=4 THEN
         E=NOTICEBOX(1)
         IF E=2 IF Z=0 LET Z=1 : GOSUB _InitOptions
         IF E=3 LET E=NOTICEBOX(2)
      ENDIF
      ' Manage events in main form
      IF Z=0 THEN
         IF E=0 THEN 
            IF L=7 THEN
               B=TICKSPERSEC\W
            ELSE
               B=TICKSPERSEC\R
            ENDIF
            A=TICKS-Q
            IF A>B LET Q=TICKS : GOSUB _NekoAction
         ENDIF
         IF E=1000 LET B=PENDOWN : IF B=0 GOSUB _PutFood
      ' Manage events in options form
      ELSE
         IF E=13 DEC R : R=MAX(R,1)
         IF E=14 INC R : R=MIN(R,20)
         IF E=17 DEC S : S=MAX(S,1)
         IF E=18 INC S : S=MIN(S,20)
         IF E=21 DEC T : T=MAX(T,1)
         IF E=22 INC T : T=MIN(T,20)
         IF E=25 DEC U : U=MAX(U,1)
         IF E=26 INC U : U=MIN(U,20)
         IF E=29 DEC V : V=MAX(V,1)
         IF E=30 INC V : V=MIN(V,20)
         IF E=33 DEC W : W=MAX(W,1)
         IF E=34 INC W : W=MIN(W,20)
         IF E>=13 IF E<=34 GOSUB _RefreshOptions
         IF E=35 GOSUB _DownloadOptions : GOSUB _FinishOptions : Z=0
         IF E=36 GOSUB _UploadOptions : GOSUB _FinishOptions : Z=0
         IF E=37 GOSUB _ResetOptions : GOSUB _RefreshOptions
         IF E=39 LET D=CHECKBOX(#39)+1
      ENDIF
   UNTIL E=-1
RETURN

_InitNeko:
   F=LOADPREF(#1)
   IF F=0 THEN
      F=201 : X=64 : Y=64 : L=7
   ELSE
      X=LOADPREF(#2)
      Y=LOADPREF(#3)
      L=LOADPREF(#4)
      C=LOADPREF(#5)
      I=LOADPREF(#6)
      J=LOADPREF(#7)
   ENDIF
   M=X : N=Y
   GOSUB _UploadOptions
   Q=TICKS
RETURN

_NekoSave:
   SAVEPREF #1,F
   SAVEPREF #2,X
   SAVEPREF #3,Y
   SAVEPREF #4,L
   SAVEPREF #5,C
   SAVEPREF #6,I
   SAVEPREF #7,J
RETURN

_InitMain:
   K=COLOR(0)
   B=F=201 : CHECKBOX #1,"",B,50,0,10,10
   IMAGE 201,62,0
   B=F=202 : CHECKBOX #2,"",B,82,0,10,10
   IMAGE 202,94,0
   B=F=203 : CHECKBOX #3,"",B,114,0,10,10
   IMAGE 203,126,0
   SETFONT 1
   BUTTON #4,"i",149,1,10,10
   SETFONT 0
RETURN

_ChooseFood:
   B=F=201 : UPDATEVALUE #1,B
   B=F=202 : UPDATEVALUE #2,B
   B=F=203 : UPDATEVALUE #3,B
RETURN

_PutFood:
   A=PENY
   IF A>16 THEN
      IF C=1 GOSUB _EraseFood : GOSUB _PutNeko
      IF L=3 LET L=1
      I=-8\D+PENX : H=-16\D+160 : I=MAX(I,0) : I=MIN(I,H)
      J=-6\2+PENY : H=-14\D+160 : J=MAX(J,16) : J=MIN(J,H)
      H=D-1*50+F : IMAGE H,I,J
      C=1
   ENDIF
RETURN

_EraseFood:
   A=16\D+I : B=14\D+J
   COLOR K
   BOXFILLED I,J TO A,B
RETURN

_InitOptions:
   GOSUB _UploadOptions
   COLOR K
   BOXFILLED 0,17 TO 160,160
   SETFONT 0
   LABEL #11,"NekoCat Speed",10,20
   SETFONT 1
   A$=STR$(R,0) : LABEL #12,A$,105,20
   SETFONT 0
   BUTTON #13,"-",125,20,10,10
   BUTTON #14,"+",136,20,10,10
   LABEL #15,"Wake up Duration",10,36
   SETFONT 1
   A$=STR$(S,0) : LABEL #16,A$,105,36
   SETFONT 0
   BUTTON #17,"-",125,36,10,10
   BUTTON #18,"+",136,36,10,10
   LABEL #19,"Eat Duration",10,52
   SETFONT 1
   A$=STR$(T,0) : LABEL #20,A$,105,52
   SETFONT 0
   BUTTON #21,"-",125,52,10,10
   BUTTON #22,"+",136,52,10,10
   LABEL #23,"Scratch Duration",10,68
   SETFONT 1
   A$=STR$(U,0) : LABEL #24,A$,105,68
   SETFONT 0
   BUTTON #25,"-",125,68,10,10
   BUTTON #26,"+",136,68,10,10
   LABEL #27,"Yawn Duration",10,84
   SETFONT 1
   A$=STR$(V,0) : LABEL #28,A$,105,84
   SETFONT 0
   BUTTON #29,"-",125,84,10,10
   BUTTON #30,"+",136,84,10,10
   LABEL #31,"Sleep Speed",10,100
   SETFONT 1
   A$=STR$(W,0) : LABEL #32,A$,105,100
   SETFONT 0
   BUTTON #33,"-",125,100,10,10
   BUTTON #34,"+",136,100,10,10
   H=HIGHRES(1)
   IF H=1 THEN
       H=HIGHRES(0)
       H=D-1
       LABEL #38,"Small Images",10,116
       CHECKBOX #39,"",H,102,116,10,10
   ENDIF
   BUTTON #35,"OK",4,140,30,14
   BUTTON #36,"Cancel",40,140,38,14
   BUTTON #37,"Default values",84,140,68,14
RETURN

_UploadOptions:
   R=LOADPREF(#8)
   IF R=0 THEN
      GOSUB _ResetOptions
   ELSE
      S=LOADPREF(#9)
      T=LOADPREF(#10)
      U=LOADPREF(#11)
      V=LOADPREF(#12)
      W=LOADPREF(#13)
      D=LOADPREF(#14)
   ENDIF
RETURN

_ResetOptions:
   R=6 : S=2 : T=4 : U=4 : V=6 : W=2 : D=1
RETURN

_DownloadOptions:
   SAVEPREF #8,R
   SAVEPREF #9,S
   SAVEPREF #10,T
   SAVEPREF #11,U
   SAVEPREF #12,V
   SAVEPREF #13,W
   SAVEPREF #14,D
RETURN

_RefreshOptions:
   A$=STR$(R,0) : UPDATELABEL #12,A$
   A$=STR$(S,0) : UPDATELABEL #16,A$
   A$=STR$(T,0) : UPDATELABEL #20,A$
   A$=STR$(U,0) : UPDATELABEL #24,A$
   A$=STR$(V,0) : UPDATELABEL #28,A$
   A$=STR$(W,0) : UPDATELABEL #32,A$
   H=HIGHRES(1)
   IF H=1 THEN
       H=HIGHRES(0)
       H=D-1
       UPDATEVALUE #39,H
   ENDIF
RETURN

_FinishOptions:
   FOR B=11 TO 37 : DESTROY #B : NEXT
   H=HIGHRES(1)
   IF H=1 THEN
      H=HIGHRES(0)
      DESTROY #38 : DESTROY #39
   ENDIF
   COLOR K
   BOXFILLED 1,17 TO 160,160
   IF C=1 LET H=D-1*50+F : IMAGE H,I,J
RETURN

_NekoAction:
   INC G
   IF C=1 THEN
      IF L>=9 GOSUB _CalcNekoDir
      IF L=3 IF G>T LET C=0 : GOSUB _EraseFood
      IF L=1 IF G>S LET G=0 : GOSUB _CalcNekoDir
      IF L<9 IF L<>3 IF L<>1 LET L=1 : G=0
   ELSE
      IF L=2 IF G>V LET G=0 : L=7
      IF L=5 IF G>U LET G=0 : L=2
      IF L=3 IF G>T LET G=0 : L=5
   ENDIF
   IF G>21 LET G=0
   GOSUB _EraseNeko
   GOSUB _PutNeko
   IF C=1 LET H=D-1*50+F : IMAGE H,I,J
   M=X : N=Y
RETURN

_CalcNekoDir:
   A=-8\D-X+I : H=ABS(A)*D
   IF H<12 LET A=0
   B=-22\D-Y+J : H=ABS(B)*D
   IF Y=16 IF B<0 LET B=0
   IF H<12 LET B=0
   IF A<0 IF B<0 LET L=13
   IF A<0 IF B>0 LET L=19
   IF A>0 IF B<0 LET L=11
   IF A>0 IF B>0 LET L=17
   IF A<0 IF B=0 LET L=23
   IF A>0 IF B=0 LET L=21
   IF A=0 IF B<0 LET L=9
   IF A=0 IF B>0 LET L=15
   IF A=0 IF B=0 LET L=3 : G=0
   IF A<0 LET X=-12\D+X
   IF A>0 LET X=12\D+X
   IF A=0 LET X=-8\D+I
   IF B<0 LET Y=-12\D+Y
   IF B>0 LET Y=12\D+Y
   IF B=0 LET Y=-22\D+J
   H=-32\D+160
   X=MAX(X,0) : X=MIN(X,H)
   Y=MAX(Y,16) : Y=MIN(Y,H)
RETURN

_EraseNeko:
   A=32\D+M : B=32\D+N
   COLOR K
   BOXFILLED M,N TO A,B
RETURN

_PutNeko:
   B=L>2*G MOD 2 +300+L
   H=D-1*50+B : IMAGE H,X,Y
RETURN
