program GraveIntentions;
var  


// Change to VIC-20, C64
@projectsettings "system" "C64"


@define roomID 239
@define cnl 240
@define ccol 241
@define cexit 242
@define ppos 238

@define vicTempScreen $3000

@define c1 14
@define c2 1
@define c3 0
@define c4 6
@define c5 5
@define dir_N  %00001000
@define dir_S  %00000100
@define dir_E  %00000010
@define dir_W  %00000001
@define dir_SW %00000101
@define dir_NE %00001010
@define dir_NS %00001100

@define cursorChar $a0
@define clearChar $20


@define startx 1
@define starty 12
@define commandY 20

@define item_branch 1
@define animPos $4000

//@export "screens/croom1.flf" "screens/croom1.trm" 256
//@export "screens/croom3.flf" "screens/croom3.trm" 256
//
//@export "screens/croom4.flf" "screens/croom4.trm" 256

//@export "screens/croom5.flf" "screens/croom5.trm" 256

@define specialRoomFire 0

@donotremove room1
@donotremove room2
@donotremove room3
@donotremove room4
@donotremove room5






    i,j,k,time,key,prevKey: byte = 0;  
    specialRoom : byte =255;
   	// Player start position
	player_x:byte = 16;
	player_y:byte = 16;   
	cx, cy, curCol,curExits,row,col,bgCol, fgCol, decrunchIndex,inputColor: byte = 0;
	clearMessage : byte = 1;

	commandNotOK, redraw : byte=true;
	roomP,zp : pointer;
	command : array[41] of byte;
	sSplit : array[41] of byte;
 	roomColor : byte = 0;
	dataScale : byte = 1;
	dataXStart : byte = 0;
	dataUseColors : byte = true;

	maxFrames, compressionType, hasTimeStamps, endChar, skipChar, curFrame : byte = 0;
	roomTest: incbin("screens/room1.trm",@animPos);
@include "packed_resources1.ras"

//@export "charset/charset.flf" "charset/charset.bin" 256   
/*@ifdef C64
@define charsetPos $2000
@endif
*/
//	charset:incbin("charset/charset.bin",@charsetPos);

@include "rooms.ras"
@include "trm.ras"

@ifdef C64
@include "c64_methods.ras"
@endif

//@include "fire.ras"

procedure FindRoom();
var
	notDone : byte = true;
begin
	notDone:=true;
	roomP := #room1;
//	inc(^$D020);
	while (notDone) do
	begin
		if (roomP[0]=@roomID) do
		begin
			bgCol:=roomP[3];
			fgCol:=roomP[4];
			inputColor:=roomP[6];
			roomColor:=roomP[7];
			decrunchIndex:=roomP[5];
			if (roomP[1]=player_x and roomP[2]=player_y) then
				return();
			roomP:=roomP+6;
		end;
		roomP:=roomP+1;
	end;
	roomP:=0;
end;

procedure NewLine(i:byte);
begin
	cx:=@startx;
	cy:=cy+i;
end;



procedure PrintLine(tp:pointer);
var 
   i:byte;
begin
	while (tp[0]<>0) do
	begin
		i:=tp[0];
		if (i>96 and i<123) then
		  i:=i-96;
		moveto(cx,cy,hi(screen_char_loc));
	 	screenmemory[0]:=i;
	 	@ifdef C64
		moveto(cx,cy,hi(screen_col_loc));
	 	screenmemory[0]:=curCol;
	 	@endif
		inc(cx);
		tp:=tp+1;
	end;
end;


procedure PrintRoomText();
var
	i,j,print : byte;
	zp:pointer;
begin
	zp:=roomP;
	zp:=zp+8;
	cx:=@startx;
	curCol:=@c1;
	cy:=@starty;
	clearscreen(key_space,screen_char_loc);
	NewLine(1);
	while (zp[0]<>0) do 
	begin
		print:=true;
		i:=zp[0];
		if (i=@cnl) then
		begin
			cx:=@startx;
			inc(cy);
			print:=false;
			zp:=zp+1;			
		end;
		if (i=@ccol) then
		begin
			curCol:=zp[1];
			zp:=zp+2;
			print:=false;			
		end;
		if (i=@cexit) then
		begin
			print:=false;
			curExits:=zp[1];
			zp:=zp+2;	
		end;
		if (print) then
		begin
			moveto(cx,cy,hi(screen_char_loc));
			if (i>96 and i<123) then
			  i:=i-96;
			  

			screenmemory[0]:=i;
			@ifdef C64
			moveto(cx,cy,hi(screen_col_loc));
		 	screenmemory[0]:=curCol;
			@endif		 	
			inc(cx);
			zp:=zp+1;
		end;
	end;
	NewLine(1);
	curCol:=@c1;
	PrintLine("Exits ");
	curCol:=@c2;
	
	if (curExits & @dir_N=@dir_N) then
		PrintLine("N ");
	if (curExits & @dir_S=@dir_S) then
		PrintLine("S ");
	if (curExits & @dir_E=@dir_E) then
		PrintLine("E ");
	if (curExits & @dir_W=@dir_W) then
		PrintLine("W");
		
end;	




procedure InitializeRoom();
begin
	specialRoom:=255;
	FindRoom();
	
/*	if (decrunchIndex=@specialRoomFire) then
	begin
		specialRoom:=@specialRoomFire;
	end;*/
	if (specialRoom=255) then
	begin
		decrunchfromindex(#resources,decrunchIndex);
		ReadAnimationHeader();
	end;
	@ifdef C64
	clearscreen(fgCol, screen_col_loc);	
	screen_fg_col:=bgCol;
	@endif
	@ifdef VIC20
	clearscreen(roomColor, screen_col_loc);
	@endif
	PrintRoomText();
end;




procedure Init();
begin
	
	ClearScreen(key_space,screen_char_loc);
@ifdef C64	
	SetCharsetLocation($1800);
	screen_bg_col:=black;
@endif



@ifdef VIC20
	dataScale :=2;
	screen_bg_color:=8;
	clearscreen(yellow,screen_col_loc);
	aux_color_and_volume:=black;
	SetCharsetLocation($8800);
//	screen_fg_col:=red;
@endif
@ifdef PET
	dataXStart:=20;
	dataUseColors:=false;
@endif


end;

// Clear command stuff
procedure PrepareCommand();
var
	i:byte;
begin

	moveto(0,@commandY,hi(screen_char_loc));
	for i:=0 to screen_height-@commandY-clearMessage do
	begin
		fill(screenmemory,@clearChar,screen_width);
		screenmemory:=screenmemory+screen_width;
	end;
	clearMessage:=0;
	
@ifdef C64
	moveto(@startx,@commandY,hi(screen_col_loc));
	fill(screenmemory,inputColor,40);
@endif
	moveto(@startx,@commandY,hi(screen_char_loc));
	cx:=@startx;
	cy:=@commandY;
end;

procedure Message(p:pointer);
var 
	i:byte;
begin
	moveto(0,24,hi(screen_char_loc));
	i:=cy;
	cy:=24;
	fill(screenmemory,key_space,screen_width);
	PrintLine(p);
	cy:=i;
	clearMessage:=1;
end;


procedure CommandGO(arg1:pointer);
var
   ok,unknown:byte;
//	cmdN : string="n";
begin
	unknown:=true;
	ok:=false;
	moveto(30,24,04);
	printstring(arg1,0,40);
	if (strcmp(arg1,"n")) then
	begin
		if (curExits & @dir_N=@dir_N) then
		begin
			player_y:=player_y-1;
			ok:=true;
			redraw:=true;
		end
		else
		begin
			Message("Can't go north.");
			ok:=false;
			unknown:=false;
			redraw:=true;
		end;
	end;
	if (strcmp(arg1,"s")) then
	begin
		if (curExits & @dir_S=@dir_S) then
		begin
			player_y:=player_y+1;
			ok:=true;
			redraw:=true;
		end
		else
		begin
			Message("Can't go south.");
			ok:=false;
			unknown:=false;
			redraw:=true;
		end;
	end;
	if (strcmp(arg1,"e")) then
	begin
		if (curExits & @dir_E=@dir_E) then
		begin
			player_x:=player_x+1;
			ok:=true;
			redraw:=true;
		end
		else
		begin
			Message("Can't go east.");
			ok:=false;
			unknown:=false;
			redraw:=true;
		end;
	end;
	if (strcmp(arg1,"w")) then
	begin
		if (curExits & @dir_W=@dir_W) then
		begin
			player_x:=player_x-1;
			ok:=true;
			redraw:=true;
		end
		else
		begin
			Message("Can't go west.");
			ok:=false;
			unknown:=false;
			redraw:=true;
		end;
	end;
	
	if (ok=false and unknown) then
	begin
		Message("I don't know where to go");			
	end;
	returnvalue(ok);
end;


procedure ExecuteCommand();
var
	cmd, arg1 : pointer;
	cmdGo : string="go";
	ok : byte=false;
begin
	ok:=false;
	if (command[0]=0) then return();
	StrToLower(#command);
	strSplit(#command, #sSplit, key_space);
	// Command
	cmd:=strgetfromindex(#sSplit,0);
	arg1:=strgetfromindex(#sSplit,1);

	if (strcmp(#cmd,#cmdGo)) then
		ok:=CommandGO(arg1);
	
	commandNotOK:=1-ok;
end;



procedure WaitForCommand();
var
	isNotDone, renderChar : byte=true;
	cur,reset,time, curPos, firstKey, blink: byte = 0;
begin
	isNotDone:=true;
	while (isNotDone) do
	begin
		@ifdef C64
		waitforraster(0);
		@endif
		@ifdef PET
		for i:=0 to 8 do
			wait(200);
		@endif
		@ifdef VIC20
			waitforraster(0);
		@endif
			
		renderChar:=true;
		@ifdef C64
		key:=getKey();
		@endif
		@ifdef PET
		key:=$FF;
		@endif
		@ifdef VIC20
		key:=$FF;
		@endif
		if (key=$FF) then
			firstKey:=0;
		prevKey:=key;
		
		if (key<>$FF and reset=0) then
		begin
			if (firstKey=0) then
				reset:=16
			else
				reset:=8;
				
			firstKey:=1;

			if (key=$F7) then // backspace
			begin
				screenmemory[cur]:=key_space;
				if (cur<>0) then
					cur:=cur-1;
				screenmemory[cur]:=key_space;
			
				renderChar:=false;		
			end;
			if (key=$8E) then // return 
			begin
				renderChar:=false;
				memcpy(#screenmemory,0,#command,40);
				command[cur]:=0;
				cur:=0;
				ExecuteCommand();
				isNotDone:=false;							
			end;

			if (renderChar) then
			begin
				screenmemory[cur]:=key;
				inc(cur);
			end;

		end;
		inc(blink);
		if (blink=16) then screenmemory[cur]:=key_space+$80;
		if (blink=32) then begin screenmemory[cur]:=key_space; blink:=0; end;
		
		if (reset<>0) then dec(reset);
		if (time=0) then
		begin
//			RenderNextPackedFrameCompressionType2(22,8,0);
			if (specialRoom=255) then
				RenderNextPackedFrameCompressionType2(40,dataXStart,0);
			time := 16;
		end;

		dec(time);

	end;
end;



procedure MainLoop();
begin
	while (true) do
	begin
		commandNotOK:=true;
		InitializeRoom();
		while (commandNotOK) do
		begin
			PrepareCommand();
			WaitForCommand();
		end;
	end;
end;


begin
	Init();
	
	MainLoop();
end.
