/***********************************************************************
 * 
 * Individual module for the cc65 compiler.
 *
 * The template for this file was created by TempC Module Creator by
 * Joseph Rose.  This template can be used in your programs, provided
 * you mention TempC in your software's documentation.  If this source
 * code is distributed, this copyright must be included in the file.
 *
 ***********************************************************************/

//Replace with the name of your copy of CBMSimpleIO's header file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <errno.h>
#include <ctype.h>

#ifdef __APPLE2ENH__
#include "a2simpleio.h"
#include "pd_disk.h"
unsigned char __fastcall__ loadstub (char* filename, void* buffer);
#pragma charmap (10, 13)
#else
#include "simpleio.h"
#endif

#if defined __APPLE2ENH__ || defined __PLUS4__
#define __NOHIDE__
#endif

//This is needed, as the routine calls itself.
//#pragma static-locals (off)

static unsigned char e;
// #pragma zpsym ("e")

extern unsigned char *s;
#pragma zpsym ("s")

//Insert your tokens here.
static char * tokens[]=
{
	"the ",		//0x80
	" map",		//0x81
	"you ",		//0x82
	"of ",		//0x83
	"and ",		//0x84
	"are",		//0x85
	"enter",	//0x86
	"teleport",	//0x87
	"phone",	//0x88
	"canner",	//0x89
	"look",		//0x8A
	"room",		//0x8B
	"north",	//0x8C
	"house",	//0x8D
	"south",	//0x8E
	"is ",		//0x8F
	"east",		//0x90
	"nter",		//0x91
	"Malgon",	//0x92
	"St.",		//0x93
	"Guian",	//0x94
	"   ",		//0x95
	"type",		//0x96
	"map",		//0x97
	"your",		//0x98
	"have",		//0x99
	"say",		//0x9A
	"read",		//0x9B
	"letter",	//0x9C
	"with ",	//0x9D
	"the",		//0x9E
	"ing"		//0x9F
}, * tokens2a[]= {
	"answer",	//0xE0
	"You ",		//0xE1
	"room",		//0xE2
	"friend",	//0xE3
	"portal",	//0xE4
	"out",		//0xE5
	"commands",	//0xE6
	"game",		//0xE7
	"all",		//0xE8
	"Press ",	//0xE9
	"object",	//0xEA
	"Talnon",	//0xEB
	"Special ",	//0xEC
	"west",		//0xED
	"some",		//0xEE
	"appe",		//0xEF
	"here",		//0xF0
	"Ave.",		//0xF1
	"computer ",	//0xF2
	"weapons",	//0xF3
	"Symba",	//0xF4
	"exit",		//0xF5
	"upplies",	//0xF6
	"section",	//0xF7
	"your ",	//0xF8
	"from ",	//0xF9
	" for",		//0xFA
	"This ",	//0xFB
	"more ",	//0xFC
	"venue ",	//0xFD
	" not ",	//0xFE
	"know"		//0xFF
},
//Even more tokens.  Use if needed.  Refer with an uppercase letter.
* tokens2b[]=
{
	"hostages",	//%A
	"need",		//%B
	"ransom",	//%C
	"tell ",	//%D
	"will ",	//%E
	"can ",		//%F
	"location",	//%G
	"<item>",	//%H
	"runs ",	//%I
	"to ",		//%J
	"books",	//%K
	" sale",	//%L
	"another",	//%M
	"direct",	//%N
	"<person>",	//%O
	"eturn",	//%P
	"pictures",	//%Q
	"Gantron",	//%R
	"you",		//%S
	"living ",	//%T
	"home",		//%U
	"can't ",	//%V
	"item",		//%W
	"don't ",	//%X
	"available",	//%Y
	"full"		//%Z
},
* tokens2c[]= {
	"that",		//%0
	"inventory",	//%1
	"video",	//%2
	"The ",		//%3
	" how ",	//%4
	" and",		//%5
	"store"		//%6
}
;

#ifdef __NOHIDE__
#ifdef __CBM__
#pragma code-name (push, "TAPECODE")
#endif
unsigned char gettokchr (void)
//{return e=hidereadb(s);}
{e=*s; ++s; return e;}
#ifdef __CBM__
#pragma code-name (pop)
#endif
#endif


#ifdef __C64__
#pragma code-name (push, "ZP2CODE")

unsigned char gettokchr (void)
//{return e=hidereadb(s);}
{e=hidereadb(s); ++s; return e;}

#pragma code-name (pop)
#endif

// unsigned char islower2 (void)
// {return e>='a' && e<'z'+1;}

// unsigned char isupper2 (void)
// {return e>='A' && e<'Z'+1;}

// unsigned char isdigit2 (void)
// {return e>='0' && e<'9'+1;}

//This function prints the string given in str to the screen
//with one difference: if a string literal between 0x80 and 0x9F
//is found, it will print the token # that literal-0x80.
//If it finds one, it will recursively call itself, so
//you can embed a token within another token string.
//
//BTW, you need to include this prototype in your
//program's modules.
void __fastcall__ printtok (char* str)
{
	//static unsigned char i;
	//char* 
	s=str;
//printscr (s);
	while (gettokchr ()) {
		//e=*s;
		if (e>=0x80 && e<0xA0) prints (tokens[e&0x1F]);
		else if (e>=0xE0) prints (tokens2a[e&0x1F]);
		else if (e=='%') {
			//gettokchr ();
			//printtok (tokens[*s-'a']);
			if (gettokchr ()=='%') {
				printc ('%');
			}
			// else if (e>='a' && e<'z'+1) {
				// prints (tokens2a[e-'a']);
			// }
			//Kill this condition if you need no
			//more than 58 tokens:
			else if (e>='A' && e<'Z'+1) {
				prints (tokens2b[e-'A']);
			} else if (e>='0' && e<'9'+1) {
				prints (tokens2c[e&0xF]);
			}
		// } else if (e==5) {
			// gettokchr ()-'0'+4;
			// while (--e) printc (' ');
		}
		else printc (e);
		//++s;
	}
}

// void __fastcall__ printtokcr (char* str)
// {
	// printtok (str);
	// printcr();
// }