/* U3 Script Engine  (U3SE) */

//System Script
void read_system_script(char *fname, int line);
int do_as_told(char *fname, int command, int line);
void funk_1(char *fname, int line);

//NPC script
int read_NPC_talk_script(int whos,int line);
int inside_gametag(int whos,int line);

char buffra[1024];
char ploti[2];

void read_system_script(char *fname, int line) {

int done=0;

while(done!=-1) {

    read_line(fname,line,buffra);
    if(strcmp(buffra,"END_SCRIPT")==0)done=-1;
    if(strcmp(buffra,"CHARACTER_START")==0)done=1;
    if((done!=0) && (done!=-1))done=do_as_told(fname, done, line);
    line++;
    }
}

int do_as_told(char *fname, int command, int line) {

int reply=0;

switch (command) {
    case 1:
    funk_1(fname,line);
    super_console(filu[REDFONT].dat,"NPC : script @ sexy");
    break;
    }
}

void funk_1(char *fname, int line)
{

int editoi=0;
int handlers[6];

int ix[2];
line++;

while(editoi!=1) {
    read_line(fname,line,buffra);
    editoi=1;

    if(strcmp(buffra,"CHARACTER_END")!=0) {
        char name[20] = "Ghost404"; 
        char filez[30] = "Cemetary404"; 
        editoi=0;

        sscanf(buffra,"%d %s %d %d %d %d %d %d %s"
        ,&handlers[0]
            , name
                , &handlers[1]
                    , &handlers[2]
                        , &handlers[3]
                            , &handlers[4]
                                , &handlers[5]
                                    , &handlers[6]
                                        , filez);

        memcpy(NPC[handlers[0]].name,name,20);
        memcpy(NPC[handlers[0]].script_file,filez,20);
        NPC[handlers[0]].mx = handlers[1];
        NPC[handlers[0]].my = handlers[2];
        NPC[handlers[0]].ai = handlers[3];
        NPC[handlers[0]].animset = handlers[4];
        NPC[handlers[0]].healt = handlers[5];
        NPC[handlers[0]].armour = handlers[6];

	//Random talk system init
	ix[1]=0;
        for(ix[0]=0;ix[0]<10;) {
		ix[1]++;
		read_line(NPC[handlers[0]].script_file,ix[1],buffra);
		if(strcmp(buffra,"RANDOM_CHAT")==0) {
			NPC[handlers[0]].sayings[0]=ix[1];
			NPC[handlers[0]].sayings[1]=0;
			NPC[handlers[0]].sayings[2]=0;
			ix[0]=10;
			}
		}
        }
    line++;
    }
}

int read_NPC_talk_script(int whos,int line)
{
int done=0,aid=0;
while(done!=-1) {
    char commando[20];
    read_line(NPC[whos].script_file,line,buffra);
    sscanf(buffra,"%s",commando);

    if(strcmp(commando,"CONSOLE")==0) {
        read_line(NPC[whos].script_file,line+1,buffra);
        super_console(filu[statu_font].dat,"%s:%s",NPC[whos].name , buffra);
        empty_line_to_console();
        }

    if(strcmp(commando,"GAMETAG")==0) {
        sscanf(buffra,"%s %d",ploti, &aid);
        if(aid == gametag) inside_gametag(whos,line+1);
        }

    if(strcmp(commando,"LET_ME_GO")==0)talkwith=-1;
    if(strcmp(commando,"END_SCRIPT")==0)done=-1;
    line++;
    }
return line-1;
}

int inside_gametag(int whos,int line)
{
int done=0,rullaa=line;
while(done!=-1) {
    char commando[20];
    read_line(NPC[whos].script_file,line,buffra);
    sscanf(buffra,"%s",commando);

    if(strcmp(commando,"SAY")==0) {
        int lopea=0;
        line++;
        super_console(filu[REDFONT].dat,"%s says:",NPC[whos].name);

        while(lopea!=-1) {
            read_line(NPC[whos].script_file,line,buffra);
            if(strcmp(buffra,"STOP_TALKING")==0)lopea=-1;
            if(strcmp(buffra,"END_TAG")==0)lopea=-1;
            if(lopea!=-1) {
                super_console(filu[statu_font].dat,"%s",buffra);
                line++;
                }
            }
        empty_line_to_console();
        }//SAY end

    if(strcmp(commando,"QUESTIONS")==0) {
        int lopea=0;
        line++;
        super_console(filu[REDFONT].dat,"You say:");
        while(lopea!=-1) {
            read_line(NPC[whos].script_file,line,buffra);
            if(strcmp(buffra,"WAIT_FOR_ANSWERS")==0)lopea=-1;
            if(strcmp(buffra,"END_TAG")==0)lopea=-1;
            if(lopea!=-1) {
                super_console(filu[REDFONT].dat,"%s",buffra);
                line++;
                }
            }
        empty_line_to_console();
        }//QUEST end
    if(strcmp(commando,"END_TAG")==0) done=-1;
    line++;
    }
}

