/*                              U3 Funktion library                   /
/           Allmost all copyrights to Samuli Jomppanen 2002-2010      /
/                   I dont recomend this buggy code to anyone.        /
/                         Contact: zimppa666@hotmail.com             */

int r=110,g=50,b=60,lightnes=170,fq=0,sound_vol=255,music_vol=64;
int times[6];
char buff[1024];
struct tm the_time;

void release_draw_for_NPCs()
{
int i=0;
for(i=0;i<99;i++) {
    NPC[i].drawed=0;    
    }
}

void do_ai()
{
int i=0;
for(i=0;i<NPCS-1;i++) {

    if((NPC[i].ai==1) && (i!=talkwith)) {
        if((NPC[i].animi==0)&&(rand ()%100==5)) {
            NPC[i].walkmode=1+rand ()%8;
            if(NPC[i].animi==0) {
                if((NPC[i].walkmode==1)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,SOUTH)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==2)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,NORTH)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==3)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,WEST)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==4)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,EAST)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==5)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,SOUTHEAST)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==6)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,NORTHEAST)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==7)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,NORTHWEST)==1))NPC[i].walkmode=0;
                if((NPC[i].walkmode==8)&&(voidaanko_astua(NPC[i].mx,NPC[i].my,SOUTHWEST)==1))NPC[i].walkmode=0;
                if(NPC[i].walkmode!=0)NPC[i].history=NPC[i].walkmode;
                }
            }
        if(NPC[i].walkmode!=0)NPC[i].animi++;
        if((NPC[i].animi==1)
            ||(NPC[i].animi==3)
                ||(NPC[i].animi==5)
                    ||(NPC[i].animi==7)
                        ||(NPC[i].animi==9)
                            ||(NPC[i].animi==11)
                                ||(NPC[i].animi==12)
                                    ||(NPC[i].animi==14)) {
                if(NPC[i].walkmode==1)move_npc(i,SOUTH);
                if(NPC[i].walkmode==2)move_npc(i,NORTH);
                if(NPC[i].walkmode==3)move_npc(i,WEST);
                if(NPC[i].walkmode==4)move_npc(i,EAST);
                }
        if(NPC[i].walkmode==5)move_npc(i,SOUTHEAST);
        if(NPC[i].walkmode==6)move_npc(i,NORTHEAST);
        if(NPC[i].walkmode==7)move_npc(i,NORTHWEST);
        if(NPC[i].walkmode==8)move_npc(i,SOUTHWEST);
        if((NPC[i].animi==16)
            &&(NPC[i].walkmode<=4)) {
            NPC[i].walkmode=0,NPC[i].animi=0;
//            if(sounds==0)sample(tile[get_tile(player.mx+20,player.my+20)].step_sound,200);
            }
        if((NPC[i].animi==16)
            &&(NPC[i].walkmode>=5)) {
            NPC[i].walkmode=0,NPC[i].animi=0;
//            if(sounds==0)sample(tile[get_tile(player.mx+20,player.my+20)].step_sound,200);
            }
        } //AI 1 end

    }
}


//VIGILANTE 2 SFX
void load_vigilante(int which, char *fname) {
VIGILANTE[which].the_sample=load_sample(fname);
VIGILANTE[which].is_playing=0;
VIGILANTE[which].looper=0;
reservoir++;
}

void handle_vigilante(int which, int x)
{
if (sounds == 0) {
    if(VIGILANTE[which].is_playing==1) {
        adjust_sample(VIGILANTE[which].the_sample,255,128 + ((-320 + x) / 2),1000,1);
        VIGILANTE[which].looper++;
        }
    if(VIGILANTE[which].is_playing==0) {
        play_sample(VIGILANTE[which].the_sample,255,128 + ((-320 + x) / 2),1000,1);
        VIGILANTE[which].is_playing=1;
        VIGILANTE[which].looper++;
        }
    }
}

void prepare_vigilante()
{
int i=0;
for(i=0;i<reservoir;i++)VIGILANTE[i].looper=0;
}

void free_playing_vigilante()
{
int i=0;
for(i=0;i<reservoir;i++)stop_sample(VIGILANTE[i].the_sample);
}


void check_if_playing(int which)
{
if(VIGILANTE[which].looper==0) {
    stop_sample(VIGILANTE[which].the_sample);
    VIGILANTE[which].is_playing=0;
    }
}

void move_npc(int who,int suunta)
{
if(suunta==0)//NORTH
        {
        NPC[who].sx=NPC[who].sx+2;
        NPC[who].sy++;
        if((NPC[who].sy>=8)&&(NPC[who].sx>=16))NPC[who].sy=0,NPC[who].sx=0,NPC[who].my--;
        }
if(suunta==1)//SOUTH
        {
        NPC[who].sx=NPC[who].sx-2;
        NPC[who].sy--;
        if((NPC[who].sy<=-8)&&(NPC[who].sx<=-16))NPC[who].sy=0,NPC[who].sx=0,NPC[who].my++;
        }
if(suunta==2)//EAST
        {
        NPC[who].sy++;
        NPC[who].sx=NPC[who].sx-2;
        if((NPC[who].sy>=8)&&(NPC[who].sx<=-16))NPC[who].sy=0,NPC[who].sx=0,NPC[who].mx++;
        }
if(suunta==3)//WEST
        {
        NPC[who].sy--;
        NPC[who].sx=NPC[who].sx+2;
        if((NPC[who].sy<=-8)&&(NPC[who].sx>=16))NPC[who].sy=0,NPC[who].sx=0,NPC[who].mx--;
        }
if(suunta==4)//NORTHEAST
        {
        NPC[who].sy++;
        if(NPC[who].sy>=16)NPC[who].my--,NPC[who].mx++,NPC[who].sy=0;
        }
if(suunta==5)//SOUTHEAST
        {
        NPC[who].sx=NPC[who].sx-2;
        if(NPC[who].sx<=-32)NPC[who].sx=0,NPC[who].mx++,NPC[who].my++;
        }
if(suunta==6)//NORTHWEST
        {
        NPC[who].sx=NPC[who].sx+2;
        if(NPC[who].sx>=32)NPC[who].sx=0,NPC[who].mx--,NPC[who].my--;
        }
if(suunta==7)//SOUTHWEST
        {
        NPC[who].sy--;
        if(NPC[who].sy<=-16)NPC[who].my++,NPC[who].mx--,NPC[who].sy=0;
        }
}

void read_line(char *file_name,int line, char *output)
{
FILE *f;
char buf[1024];
int num=0;
f=fopen(file_name,"r");
while((fgets(buf,1024,f))!=0) {
    if(num==line) {
        buf[strlen(buf)-1]=0;
        strcpy(output,buf);
        //memcpy(output,buf,strlen(buf)-1);
		break;
        }
    num++;
    }
fclose(f);
free(buf);
}

int onkopaalla(int x,int y,int x2,int y2)
{
int paluu=0;
if((mouse_x>x)&&(mouse_x<x2))
        {
        if((mouse_y>y)&&(mouse_y<y2))paluu=1;
        }
return paluu;
}

void cross_fade(BITMAP *what,BITMAP *where,int x,int y,int x2,int y2,int xs,int ys, int speed)
{
int alpha=0;
BITMAP *sprite,*buffer;
sprite=create_bitmap(xs,ys);
blit(what,sprite,x,y,0,0,xs,ys);
if((speed<0)||(speed>255))speed=5;
buffer=where;
while(alpha!=255)
        {
        fblend_trans(sprite,buffer,x2,y2,alpha);
        alpha=alpha+speed;
        if(alpha>255)alpha=255;
        blit(buffer,screen,0,0,0,0,640,480);
        }
destroy_bitmap(sprite);
}

void mess_up(BITMAP *what,BITMAP *where,int ammo) { fblend_trans(where,what,0,0,ammo); }

void mix_multip(BITMAP *what, BITMAP *where, int x, int y, int x2, int y2, int xs, int ys, int ammo)
{
BITMAP *sprite;
sprite=create_bitmap(xs,ys);
blit(what,sprite,x,y,0,0,xs,ys);
set_multiply_blender(0, 0, 0,ammo);
draw_trans_sprite(where,sprite,x2,y2);
destroy_bitmap(sprite);
}

void book_backround(char *book, BITMAP *war)
{
BITMAP *sivu;
PALLETE pal;
sivu=load_pcx(book,pal);
blit(sivu,war,0,0,0,0,320,480);
draw_sprite_h_flip(war,sivu,320,0);
destroy_bitmap(sivu);
}

void credits(BITMAP *war)
{
DATAFILE *filu;
BITMAP *picto,*picto2;
filu=load_datafile("font.dat");
picto=create_bitmap(640,480);
book_backround("manuska/blancob.pcx",picto);
picto2=load_pcx("manuska/phil.pcx",NULL);
mix_multip(picto2,picto,0,0,30,30,250,430,200);
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,40,-1,"The Legend of Jou (tm)");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,60,-1,"Coding:");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,70,-1,"Samuli Jomppanen");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,80,-1,"Tero Laitinen");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,90,-1,"Joonatan Kaartinen");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,110,-1,"Graphics:");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,120,-1,"Samuli Jomppanen");
textprintf_centre(picto,(FONT*)filu[bookf].dat,480,130,-1,"Janne Vanska");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,140,-1,"Matias Kilpinen");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,160,-1,"Story by:");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,170,-1,"Samuli Jomppanen");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,180,-1,"Janne Vanska");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,190,-1,"Kristiina Luomala");
                textprintf_centre(picto,(FONT*)filu[bookf].dat,480,200,-1,"'Areor'");
                cross_fade(picto,war,0,0,0,0,640,480,5);
                rest(3000);
                book_backround("manuska/burned.pcx",picto);
				destroy_bitmap(picto2);
                picto2=load_pcx("manuska/marion.pcx",NULL);
                mix_multip(picto2,picto,0,0,30,30,250,430,192);
                cross_fade(picto,war,0,0,0,0,640,480,5);
                rest(3000);
                book_backround("manuska/note.pcx",picto);
				destroy_bitmap(picto2);
                picto2=load_pcx("manuska/henry.pcx",NULL);
                mix_multip(picto2,picto,0,0,30,30,250,430,192);
                cross_fade(picto,war,0,0,0,0,640,480,5);
                rest(3000);
                book_backround("manuska/blancob.pcx",picto);
				destroy_bitmap(picto2);
                picto2=load_pcx("manuska/sceledar.pcx",NULL);
                mix_multip(picto2,picto,0,0,30,30,250,430,192);
                cross_fade(picto,war,0,0,0,0,640,480,5);
                rest(3000);
                book_backround("manuska/blancob.pcx",picto);
				destroy_bitmap(picto2);
                picto2=load_pcx("manuska/archoroid.pcx",NULL);
                mix_multip(picto2,picto,0,0,30,30,250,430,192);
                cross_fade(picto,war,0,0,0,0,640,480,5);
                rest(3000);
				destroy_bitmap(picto2);
}

int get_tile(int x,int y)
{
unsigned int taili=map->line[y][x];
if(taili>99)return 0;
return taili;
}

void put_tile(int x,int y,int da)
{
map->line[y][x]=da;
}

int get_sprite(int x,int y)
{
unsigned int taili=map->line[y+MAP_Y][x];
if(taili>99)return 0;
return taili;
}

void put_sprite(int x,int y,int da)
{
map->line[y+MAP_Y][x]=da;
}

int get_roof(int x,int y)
{
unsigned int taili=map->line[y+(2*MAP_Y)][x];
if(taili>99)return 0;
return taili;
}

void put_roof(int x,int y,int da)
{
map->line[y+(2*MAP_Y)][x]=da;
}

int get_wall(int x,int y)
{
unsigned int taili=map->line[y+(3*MAP_Y)][x];
if(taili>99)return 0;
return taili;
}

void put_wall(int x,int y,int da)
{
map->line[y+(3*MAP_Y)][x]=da;
}

int get_second(int x,int y)
{
unsigned int taili=map->line[y+(4*MAP_Y)][x];
if(taili>99)return 0;
return taili;
}

void put_second(int x,int y,int da)
{
map->line[y+(4*MAP_Y)][x]=da;
}

void save_map(char *fname)      { save_pcx(fname,map,NULL); }

void load_map(char *fname)
{
int x=0,y=0;
map=load_pcx(fname,pal);
if(!map) {
        clear_to_color(screen,makecol16(255,0,0));
        textprintf_centre(screen,font,320,240,makecol16(255,255,0),"ERROR CODE: KRT404");
        readkey();
        }
}

void move_to(int suunta)
{
if(suunta==0)//NORTH
        {
        player.sx=player.sx+2;
        player.sy++;
        if((player.sy>=8)&&(player.sx>=16))player.sy=0,player.sx=0,player.my--;
        }
if(suunta==1)//SOUTH
        {
        player.sx=player.sx-2;
        player.sy--;
        if((player.sy<=-8)&&(player.sx<=-16))player.sy=0,player.sx=0,player.my++;
        }
if(suunta==2)//EAST
        {
        player.sy++;
        player.sx=player.sx-2;
        if((player.sy>=8)&&(player.sx<=-16))player.sy=0,player.sx=0,player.mx++;
        }
if(suunta==3)//WEST
        {
        player.sy--;
        player.sx=player.sx+2;
        if((player.sy<=-8)&&(player.sx>=16))player.sy=0,player.sx=0,player.mx--;
        }
if(suunta==4)//NORTHEAST
        {
        player.sy++;
        if(player.sy>=16)player.my--,player.mx++,player.sy=0;
        }
if(suunta==5)//SOUTHEAST
        {
        player.sx=player.sx-2;
        if(player.sx<=-32)player.sx=0,player.mx++,player.my++;
        }
if(suunta==6)//NORTHWEST
        {
        player.sx=player.sx+2;
        if(player.sx>=32)player.sx=0,player.mx--,player.my--;
        }
if(suunta==7)//SOUTHWEST
        {
        player.sy--;
        if(player.sy<=-16)player.my++,player.mx--,player.sy=0;
        }
}

void load_fwcbmp(char *filename, BITMAP *target)
{
BITMAP *bufferi,*fixup;
bufferi=load_bitmap(filename,NULL);
fixup=load_bitmap(filename,NULL);
clear_to_color(fixup,makecol16(255,0,255));
set_trans_blender(r,g,b,lightnes);
draw_lit_sprite(fixup,bufferi,0,0,lightnes);
blit(fixup,target,0,0,0,0,fixup->w,fixup->h);
destroy_bitmap(bufferi);
destroy_bitmap(fixup);
}

void insert_to_string (char **string, char *from, int *size)
{
if (strlen (*string) + strlen (from) >= (*size) - 2)
        {
        (*size) <<= 1;
        *string = (char *)realloc (*string, (*size));
        memset (&((*string)[(*size) >> 1]), 0, (*size) >> 1);
        }
strcat (*string, from);
}

void evolute_player()
{

//Regeneraatio
if((player.mana < player.mana_max) && (rand ()%501>=500))player.mana++;
if((player.hp < player.hp_max) && (rand ()%501>=500))player.hp++;

//Expotus
if(player.exp>=player.exp_needet)
        {
        player.level++;
        player.exp_needet=player.exp_needet*2;
        sample("sfx/expotus.wav",0);
        }
player.dexterity=5;
player.damage=2;
player.defense=1;
}


void update_time()
{
time_t current_time;
struct tm *t;
current_time = time(NULL);
t = localtime(&current_time);
the_time = *t;
times[0]=the_time.tm_sec;
times[1]=the_time.tm_min;
times[2]=the_time.tm_hour;
times[3]=the_time.tm_mday;
times[4]=the_time.tm_mon+1;
times[5]=the_time.tm_year;
}
END_OF_FUNCTION(update_time);

int give_time(int mode)
{
int paluu;
paluu=times[mode];
return paluu;
}

void set_world_color()
{
int hour=give_time(2);
if(hour==0)  r=0,g=0,b=0,lightnes=165;
if(hour==1)  r=0,g=0,b=0,lightnes=200;
if(hour==2)  r=0,g=0,b=0,lightnes=165;
if(hour==3)  r=0,g=0,b=0,lightnes=150;
if(hour==4)  r=0,g=0,b=0,lightnes=85;
if(hour==5)  r=0,g=0,b=0,lightnes=40;
if(hour==6)  r=0,g=0,b=0,lightnes=35;
if(hour==7)  r=0,g=0,b=0,lightnes=30;
if(hour==8)  r=0,g=0,b=0,lightnes=25;
if(hour==9)  r=0,g=0,b=0,lightnes=20;
if(hour==10) r=0,g=0,b=0,lightnes=15;
if(hour==11) r=0,g=0,b=0,lightnes=10;
if(hour==12) r=0,g=0,b=0,lightnes=5;
if(hour==13) r=0,g=0,b=0,lightnes=5;
if(hour==14) r=0,g=0,b=0,lightnes=10;
if(hour==15) r=0,g=0,b=0,lightnes=15;
if(hour==16) r=0,g=0,b=0,lightnes=20;
if(hour==17) r=0,g=0,b=0,lightnes=25;
if(hour==18) r=0,g=0,b=0,lightnes=30;
if(hour==19) r=0,g=0,b=0,lightnes=35;
if(hour==20) r=0,g=0,b=0,lightnes=40;
if(hour==21) r=0,g=0,b=0,lightnes=45;
if(hour==22) r=0,g=0,b=0,lightnes=100;
if(hour==23) r=0,g=0,b=0,lightnes=155;
}

void load_tilet(char *fname)
{
BITMAP *tile,*rite,*wall,*roof;
BITMAP *tiilix;
int i;

tiilix=load_bitmap(fname,NULL);
load_fwcbmp("gfx/tilet2.lbm",tiilix);

tile=create_bitmap(32,40);
rite=create_bitmap(32,40);
wall=create_bitmap(32,80);
roof=create_bitmap(32,16);

for(i=0; i<99;i++)
        {
        blit(tiilix,tile,i<<5,0,0,0,32,40);
        blit(tiilix,wall,i<<5,40,0,0,32,80);
        blit(tiilix,rite,i<<5,120,0,0,32,40);
        blit(tiilix,roof,i<<5,160,0,0,32,16);
        tiilit[i]=get_compiled_sprite(tile,0);
        spraitit[i]=get_compiled_sprite(rite,0);
        seinat[i]=get_compiled_sprite(wall,0);
        katot[i]=get_compiled_sprite(roof,0);
        }
destroy_bitmap(tile);
destroy_bitmap(rite);
destroy_bitmap(wall);
destroy_bitmap(roof);
destroy_bitmap(tiilix);
}

void show_story(int wa)
{
BITMAP *introduce;
BITMAP *varux;
varux=create_bitmap(640,480);
//clear_to_color(screen,makecol16(255,255,255));
book_backround("manuska/neatscrl.pcx",varux);
blit(bar,varux,0,0,0,360,640,120);
blit(varux,screen,0,0,0,0,640,480);
if(wa==1)
        {
        introduce=load_lbm("manuska/1.lbm",NULL);
        mix_multip(introduce,varux,0,0,0,0,240,240,192);
        cross_fade(varux,screen,0,0,0,0,640,480,5);
//        blit(screen,varux,0,0,0,0,640,480);
        scroll_txt_file("manuska/1a.txt",110,420,varux);

        mix_multip(introduce,varux,240,0,240,0,400,235,192);
        cross_fade(varux,screen,0,0,0,0,640,480,5);
        scroll_txt_file("manuska/1b.txt",110,420,varux);

        mix_multip(introduce,varux,240,240,95,120,400,240,192);
        cross_fade(varux,screen,0,0,0,0,640,480,5);
        scroll_txt_file("manuska/1c.txt",110,420,varux);
        }

if(wa==2)
        {
        introduce=load_lbm("manuska/2.lbm",NULL);
        cross_fade(introduce,screen,0,0,0,0,212,480,5);
        blit(screen,varux,0,0,0,0,640,480);
        scroll_txt_file("manuska/2a.txt",110,420,varux);
        cross_fade(introduce,screen,212,0,212,0,211,480,5);
        blit(screen,varux,0,0,0,0,640,480);
        scroll_txt_file("manuska/2b.txt",110,420,varux);
        cross_fade(introduce,screen,420,0,420,0,220,480,5);
        blit(screen,varux,0,0,0,0,640,480);
        scroll_txt_file("manuska/2c.txt",110,420,varux);
        }
clear(introduce);
cross_fade(introduce,screen,0,0,0,0,640,480,15);
destroy_bitmap(introduce);
}

void scroll_txt_file(char *fname,int resti,int height,BITMAP *backround)
{
BITMAP *txt;
BITMAP *buffer;
char puffi[1024];
int lenght=0,done=0;

buffer=create_bitmap(640,480);

while(done!=1)
        {
        read_line(fname,lenght,puffi);
        if(strcmp(puffi,"END_OF_TEXT")==0)done=1;
        lenght++;
        }
txt=create_bitmap(620,lenght*10);
clear_to_color(txt,makecol16(255,0,255));
done=0;
while(done!=lenght-1)
        {
        read_line(fname,done,puffi);
        textprintf_centre(txt,filu[REDFONT].dat,320,done*10,-1,"%s",puffi);
        done++;
        }
done=0;
if(height>360)height=360;
if(height<0)height=0;
while(done!=(txt->h)-40)
        {
        blit(backround,buffer,0,0,0,0,640,480);
        masked_blit(txt,buffer,0,done,10,height+10,620,90);
        done++;
        rest(resti);
        blit(buffer,screen,0,0,0,0,640,480);
        }
blit(backround,screen,0,0,0,0,640,480);
}

short _iio=0; short _iis=2; unsigned short _iip=0; void _it() { if (_iip==0) { _iio=_iio+_iis; if (_iio>=255) _iip=1; } if (_iip==1) { _iio=_iio-_iis; if (_iio<=0) { _iio=255; _iip=2; } } if (_iip==2) { _iio=_iio-_iis; if (_iio<=0) { _iio=255; _iip=3; } } if (_iip==3) { _iio=_iio-_iis; if (_iio<=0) { _iip=4; } } } int _inph_intro(void) { BITMAP *_if = create_bitmap_ex(16,640,480); BITMAP *_id = load_pcx("gfx/cr.pcx", NULL); install_int_ex(_it, BPS_TO_TIMER(80));
do { clear(_if); if (_iip==0) { set_trans_blender(0,0,0,_iio); textprintf_centre(_if,filu[cfont].dat,320,40,-1,"They made Motoko..."); draw_trans_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); set_burn_blender(0,0,0,_iio); draw_trans_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); } if (_iip==1) { textprintf_centre(_if,filu[cfont].dat,320,40,-1,"They made Motoko..."); draw_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); set_add_blender(0,0,0,255); draw_trans_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); } if (_iip==2) {
textprintf_centre(_if,filu[cfont].dat,320,40,-1,"They made Motoko..."); draw_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); set_add_blender(0,0,0,_iio); draw_trans_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); } if (_iip==3) { textprintf_centre(_if,filu[cfont].dat,320,40,-1,"They made Motoko..."); set_trans_blender(0,0,0,_iio); draw_trans_sprite(_if, _id, 320-(_id->w/2), 240-(_id->h/2)); } blit(_if, screen, 0, 0, 0, 0, 640, 480); } while(_iip!=4); remove_int(_it); destroy_bitmap(_if); destroy_bitmap(_id); }

void adjust_fire_quality()
{
char puffi[128];
read_line(CFG_FILE,12,puffi);
if(strcmp(puffi,"HIGH")==0)fq=1;
if(strcmp(puffi,"MEDIUM")==0)fq=2;
if(strcmp(puffi,"LOW")==0)fq=3;
free(puffi);
}

int does_list(char this[1024])
{
int loop=0,end=0,well=NO;
char puffi[1024];
while(end!=1)
        {
        read_line("include.lst",loop,puffi);
        if(strcmp(puffi,this)==0)
                {
                well=YES;
                break;
                }
        if(strcmp(puffi,"END_OF_LIST")==0)end=1;
        loop++;
        }
return well;
}

void generate_tempfilename(char * buf)
{
	int i;
	FILE * f;

	for (i = 0; i < 10000; i++)
	{
		sprintf(buf, "$tmp%d.tmp", i);
		f = fopen(buf, "r");
		if (f)
		{
			fclose(f);
			return;
		}
	}
}

int replace_line(char * filename, int linenum, char * string)
{
	FILE * f = fopen(filename, "r");
	FILE * f2;
	char buf[1024];
	char tmpname[128];
	int num=0;

	if (!f)
		return -1;
	generate_tempfilename(tmpname);
	f2 = fopen(tmpname, "w");
	while (fgets(buf, 1024, f))
	{
		if (num == linenum)
			fprintf(f2, "%s\n", string);
		else
			fprintf(f2, "%s", buf);
		num++;
	}
	fclose(f);
	fclose(f2);
	remove(filename);
	rename(tmpname, filename);
	return 0;
}

int voidaanko_astua(int x,int y,int suuntaan)
{
int no_voiko=0;

if(suuntaan==EAST)
        {
        no_voiko=tile[get_tile(x+1,y)].solid;
        if(no_voiko==0)no_voiko=wall[get_wall(x+1,y)].solid;
        if(no_voiko==0)no_voiko=sprite[get_sprite(x+1,y)].solid;
        }
if(suuntaan==NORTH)
        {
        no_voiko=tile[get_tile(x,y-1)].solid;
        if(no_voiko==0)no_voiko=wall[get_wall(x,y-1)].solid;
        if(no_voiko==0)no_voiko=sprite[get_sprite(x,y-1)].solid;
        }
if(suuntaan==WEST)
        {
        no_voiko=tile[get_tile(x-1,y)].solid;
        if(no_voiko==0)no_voiko=wall[get_wall(x-1,y)].solid;
        if(no_voiko==0)no_voiko=sprite[get_sprite(x-1,y)].solid;
        }
if(suuntaan==SOUTH)
        {
        no_voiko=tile[get_tile(x,y+1)].solid;
        if(no_voiko==0)no_voiko=wall[get_wall(x,y+1)].solid;
        if(no_voiko==0)no_voiko=sprite[get_sprite(x,y+1)].solid;
        }
if(suuntaan==NORTHWEST)
        {
        no_voiko=1;
        if((tile[get_tile(x-1,y-1)].solid==0)&&
        (tile[get_tile(x,y-1)].solid==0)&&
        (tile[get_tile(x,y-1)].solid==0)&&
        (wall[get_wall(x-1,y-1)].solid==0)&&
        (wall[get_wall(x,y-1)].solid==0)&&
        (wall[get_wall(x-1,y)].solid==0)&&
        (sprite[get_sprite(x-1,y-1)].solid==0)&&
        (sprite[get_sprite(x,y-1)].solid==0)&&
        (sprite[get_sprite(x-1,y)].solid==0))
                {
                no_voiko=0;
                }
        }
if(suuntaan==SOUTHEAST)
        {
        no_voiko=1;
        if((tile[get_tile(x+1,y+1)].solid==0)&&
        (tile[get_tile(x,y+1)].solid==0)&&
        (tile[get_tile(x,y+1)].solid==0)&&
        (wall[get_wall(x+1,y+1)].solid==0)&&
        (wall[get_wall(x,y+1)].solid==0)&&
        (wall[get_wall(x+1,y)].solid==0)&&
        (sprite[get_sprite(x+1,y+1)].solid==0)&&
        (sprite[get_sprite(x,y+1)].solid==0)&&
        (sprite[get_sprite(x+1,y)].solid==0))
                {
                no_voiko=0;
                }
        }
if(suuntaan==SOUTHWEST)
        {
        no_voiko=1;
        if((tile[get_tile(x-1,y+1)].solid==0)&&
        (tile[get_tile(x,y+1)].solid==0)&&
        (tile[get_tile(x,y+1)].solid==0)&&
        (wall[get_wall(x-1,y+1)].solid==0)&&
        (wall[get_wall(x,y+1)].solid==0)&&
        (wall[get_wall(x-1,y)].solid==0)&&
        (sprite[get_sprite(x-1,y+1)].solid==0)&&
        (sprite[get_sprite(x,y+1)].solid==0)&&
        (sprite[get_sprite(x-1,y)].solid==0))
                {
                no_voiko=0;
                }
        }
if(suuntaan==NORTHEAST)
        {
        no_voiko=1;
        if((tile[get_tile(x+1,y-1)].solid==0)&&
        (tile[get_tile(x,y-1)].solid==0)&&
        (tile[get_tile(x,y-1)].solid==0)&&
        (wall[get_wall(x+1,y-1)].solid==0)&&
        (wall[get_wall(x,y-1)].solid==0)&&
        (wall[get_wall(x+1,y)].solid==0)&&
        (sprite[get_sprite(x+1,y-1)].solid==0)&&
        (sprite[get_sprite(x,y-1)].solid==0)&&
        (sprite[get_sprite(x+1,y)].solid==0))
                {
                no_voiko=0;
                }
        }
return no_voiko;
}

void operate(int x,int y,int minako)
{
int efecti=0;
SAMPLE *what;
efecti=wall[get_wall(x,y)].type;

switch(efecti)
        {
        case SWICH_UP:
        put_wall(x,y,get_wall(x,y)+1);
        what=load_sample("sfx/swich.wav");
        break;

        case SWICH_DOWN:
        put_wall(x,y,get_wall(x,y)-1);
        what=load_sample("sfx/swich.wav");
        break;

        case OPEN_DOOR:
        put_wall(x,y,get_wall(x,y)+1);
        what=load_sample("sfx/door.wav");
        break;

        case CLOSE_DOOR:
        put_wall(x,y,get_wall(x,y)-1);
        what=load_sample("sfx/door.wav");
        break;

        case OPEN_JAIL:
        put_wall(x,y,get_wall(x,y)+1);
        what=load_sample("sfx/jail.wav");
        break;

        case CLOSE_JAIL:
        put_wall(x,y,get_wall(x,y)-1);
        what=load_sample("sfx/jail.wav");
        break;
        }
if((efecti!=0)&&(what != 0))play_sample(what,sound_vol,128,1000,0);
else destroy_sample(what);

//Talk Initiated with NPC
for(efecti=0;efecti<NPCS-1;efecti++) {
    if((NPC[efecti].mx==x) && (NPC[efecti].my==y)) {
        talkwith=efecti;
        break;
        }
    }

if(strcmp(wall[get_wall(x,y)].name,"sign")==0) {
    read_line("manuska/plating.gpt",get_second(x,y),buff);
    super_console(filu[statu_font].dat,"%s",buff);
    }

if(get_sprite(x,y)!=0) {
    efecti=get_sprite(x,y);
    if(strcmp(sprite[efecti].name,"book")==0) {
        if(get_second(x,y)==0)read_book("manuska/books/book.bok","manuska/blancob.pcx");
        if(get_second(x,y)==1)read_book("manuska/books/glory.bok","manuska/burned.pcx");
        if(get_second(x,y)==2)read_book("manuska/books/gold.bok","manuska/neatscrl.pcx");
        if(get_second(x,y)==3)read_book("manuska/books/gilius1.bok","manuska/note.pcx");
        }
    }
}

void explode_at(int x,int y)
{
int oldest,i=0;
for(i=0;i<10;i++)
        {
        if(kabuum[i].mode==0)
                {
                kabuum[i].mode=1;
                kabuum[i].frame=0;
                kabuum[i].type=rand ()%3;
                kabuum[i].x=x;
                kabuum[i].y=y;
                i=10;
                }
        }
}


void read_book(char *book,char *bookpages)
{
BITMAP *buffer;
DATAFILE *filu;
long int rivi=0;
int done=0,page=0;

filu=load_datafile("font.dat");
buffer=create_bitmap(640,480);
book_backround(bookpages,buffer);
while(done!=1) {
    blit(buffer,screen,0,0,0,0,640,480);
    rivi=page*40;
    while(rivi!=(page*40)+40) {
        read_line(book,rivi,buff);
        if(strcmp(buff,"END_OF_BOOK")==0) {
            done=2;
            break;
            }
        else textprintf(screen,(FONT*)filu[bookf].dat,360,60+(rivi-(page*40))*9,-1,buff);
        textprintf_centre(screen,(FONT*)filu[bookf].dat,480,450,-1,"%d",page);
        rivi++;
        }
    clear_keybuf();
    readkey();
    if((page==0)&&(key[KEY_LEFT]))done=1;
    if((done==2)&&(key[KEY_RIGHT]))done=1;
    if(key[KEY_RIGHT])page++;
    if(key[KEY_LEFT])page--;
    sample("sfx/consolew.wav",0);
    }
}
