//6.8.2001 - x.x.2004  - Shadows of Gurmanyala
#include <allegro.h>
#include <string.h>
#include <time.h>

#include "fire.h"
#include "fblend.h"
#include "jgmod.h"
#include "fpsemu.h"
#include "font.h"
#include "protot.h"
#include "funk.h"
#include "ground.h"
#include "motoko.h"
#include "people.sex"
#include "sss.h"
#include "pathfind.h"

volatile int last_fps;
volatile int frame_counter;

void count_frames()
{
last_fps = frame_counter;
frame_counter = 0;
}
END_OF_FUNCTION(count_frames);

BITMAP *tilet,*buffer,*mpointer;
BITMAP *kablam, *poks, *trees, *transsu[3];
BITMAP *character[6],*phil,*muu,*electro;
SAMPLE *fire,*fountain,*lava,*kablami;

JGMOD *ex;
PALLETE pal;
char puffi[1024];
int animo=0,fr=0,is_mousewalk=0;
int loopperi[3],vimex=0,console_y=200,ready_wait=0;
int hidaste=20;
    
char version[]= "0.3";

void *fireplace,*torch;
void summon_intro();
void clear_ligt();
void reserve_ligt(int intensity,int x,int y);
void open_chat_terminal(int with_whom);


int main()
{
int alpha=0,mani=0,count=0;
BITMAP *buffer,*kuva,*buttons,*button,*kuva2,*transis;
PALLETE pal;
_motoko_preset *lurrinlur1 = malloc(sizeof(_motoko_preset));
_motoko_preset *lurrinlur2 = malloc(sizeof(_motoko_preset));
srand(time(NULL));

player.dx = player.mx =20;
player.dx = player.my =30;



player.sx=0;
player.sy=0;
player.hp=30;
player.hp_max=60;
player.mana=0;
player.mana_max=10;
player.exp=0;
player.exp_needet=256;
player.level=0;
player.walk_mode=0;

allegro_init();
install_keyboard();
install_mouse();
install_timer();
set_up_npcs();

LOCK_VARIABLE(last_fps);
LOCK_VARIABLE(frame_counter);
LOCK_FUNCTION(count_frames);
install_int_ex(count_frames, BPS_TO_TIMER(1));

fps_init(); // well it makes things happen :p

if(does_list("musics")==YES)music=0;

if(music==0)reserve_voices (32, -1);
sounds=install_sound (DIGI_AUTODETECT, MIDI_NONE, NULL);
if(sounds==0)
        {
        sounds=1;
        if(does_list("sounds")==YES)sounds=0;
        read_line(CFG_FILE,16,puffi);
        sound_vol=atoi(puffi);
        }
if(music==0) {
read_line(CFG_FILE,14,puffi);
music_vol=atoi(puffi);
install_mod (4);
ex = load_mod ("music/main.s3m");
if(ex != 0) {
    set_mod_volume(music_vol);
    play_mod (ex, TRUE);
    }
else music=1;
}

load_vigilante(0,"sfx/notski.wav");
load_vigilante(1,"sfx/fountain.wav");
load_vigilante(2,"sfx/lightmach.wav");
load_vigilante(3,"sfx/lavapour.wav");

set_color_depth(16);
read_line(CFG_FILE,8,puffi);
if(strcmp(puffi,"WINDOWED")==0) {
        if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0)<0) {
                printf("Unicorn 3:%s\n",allegro_error);
                return 0;
                }
        }

else {
        if(set_gfx_mode(GFX_AUTODETECT,640,480,0,0)<0) {
                printf("Unicorn 3:%s\n",allegro_error);
                return 0;
                }
        }

set_color_conversion(COLORCONV_TOTAL);
init_fire_engine();
adjust_fire_quality();
fireplace=new_fire(20/fq,32,40);
torch=new_fire(10/fq,16,20);

_motoko_preset_init(lurrinlur1,500, 310, 240, 0, -.01, 0, 0, 360, 1, 360, .3, .2, 0, 50, 0, 1, 240, 50, 400, 1, 500, 1, 0,3,0,makecol16(205,205,255));
_motoko_preset_init(lurrinlur2,15, 320, 240, 0, -.01, 0, 0, 360, 0, 360, .8, .2, 0, 1, 0, 1, 240, 300, 400, 1, 100, 1, 0,3,0,makecol16(255,255,0));
_motoko.di=2;
_motoko_all_kill();
_motoko_preset_group(0,lurrinlur1);
_motoko_preset_group(1,lurrinlur2);

filu=load_datafile("font.dat");

tilet=load_bitmap("gfx/tilet.pcx",pal);

//FONTTI PALLETI HOMMA
bar=load_lbm("gfx/pallete.lbm",pal);
set_pallete(pal);


bar=load_lbm("gfx/baarit.lbm",pal);

character[0]=load_pcx(PLAYER_ANIM,pal);
character[1]=load_pcx("gfx/ladyb.pcx",pal);
character[2]=load_pcx("gfx/man.pcx",pal);
character[3]=load_pcx("gfx/mage.pcx",pal);
character[4]=load_pcx("gfx/ladyw.pcx",pal);
character[5]=load_pcx("gfx/dtknight.pcx",pal);

electro=load_pcx("gfx/electro.pcx",pal);

trees=load_lbm("gfx/trees.lbm",pal);
phil=create_bitmap(32,60);
muu=create_bitmap(32,60);
buffer=load_pcx("gfx/owet.pcx",pal);

mpointer=load_pcx("gfx/pointer.pcx",pal);
buttons=load_pcx("gfx/napit.pcx",pal);
kablam=load_lbm("gfx/tnt.lbm",pal);

transsu[0]=create_bitmap(50,50);
transsu[1]=create_bitmap(100,100);
transsu[2]=load_lbm("gfx/ligt.lbm",NULL);

stretch_blit(transsu[2],transsu[1],0,0,200,200,0,0,100,100);
stretch_blit(transsu[2],transsu[0],0,0,200,200,0,0,50,50);

button=create_bitmap(200,40);
conbuf=create_bitmap(600,300);
poks=create_bitmap(64,80);
clear(conbuf);
clear(screen);
buffer=create_bitmap(640,600);
clear(buffer);

read_line(CFG_FILE,18,puffi);
if(strcmp(puffi,"ON")==0)is_mousewalk=1;


//SCRIPT
read_system_script("ss/init.u3s",0);

set_tiles();
kuva=load_pcx("gfx/dvstudios.pcx",NULL);
read_line(CFG_FILE,4,puffi);
if(strcmp(puffi,"OFF")==0)goto ohita_intro;
clear(screen);
_inph_intro();
clear(screen);
clear(buffer);
cross_fade(kuva,buffer,0,0,0,0,640,480,1);
rest(2000);
clear(buffer);
cross_fade(buffer,kuva,0,0,0,0,640,480,1);

summon_intro();
rest(1000);
clear(screen);
ohita_intro:

kuva=load_pcx("gfx/menu.pcx",pal);
text_mode(-1);
kuva2=load_pcx("gfx/anim.pcx",NULL);
transis=create_bitmap(510,77);

while(mani!=1000) {
        blit(kuva,buffer,0,0,0,0,640,480);
        blit(kuva2,transis,0,(mani/10)*77,0,0,510,77);
        fblend_add(transis,buffer,65,0,128);
        for(count=0;count<5;count++) {
                masked_blit(buttons,buffer,0,count*30,245,200+(count*30),150,30);
                if(onkopaalla(245,200+(count*30),395,229+(count*30))==1)masked_blit(buttons,buffer,150,count*30,245,200+(count*30),150,30);
                }
        textprintf_centre(buffer,filu[cfont].dat,320,440,-1,"Unicorn:%s",version);
        if(sounds!=0)textprintf_centre(buffer,filu[REDFONT].dat,320,450,-1,"SOUND EFECTS NOT AVAILABLE");
        if(music!=0)textprintf_centre(buffer,filu[REDFONT].dat,320,460,-1,"MUSIC NOT AVAILABLE");
        draw_sprite(buffer,mpointer,mouse_x,mouse_y);
        if(key[KEY_F12])save_pcx("shot.pcx",buffer,pal);
        blit(buffer,screen,0,0,0,0,640,480);
        if(mani==91)mani=0;
        if(mani<91)mani++;
        if((mouse_b & 1)&&(onkopaalla(191,200,449,229)==1))
                {
                masked_blit(buttons,screen,300,0,245,200,150,30);
                if(sounds==0)sample("sfx/click.wav",0);
                stop_mod();

                clear(kuva);
                clear(buffer);

		read_line(CFG_FILE,4,puffi);
		if(strcmp(puffi,"ON")==0) {

                sample("sfx/fortune.wav",0);
                cross_fade(kuva,buffer,0,0,0,0,640,480,1);
                read_line("manuska/fortunes.dat",rand ()%20,puffi);
                textprintf_centre(kuva,filu[REDFONT].dat,320,240,-1,"%s",puffi);
                cross_fade(kuva,buffer,0,0,0,0,640,480,1);
                rest(1000);
                clear(kuva);
                cross_fade(kuva,buffer,0,0,0,0,640,480,1);
		}
                clear_ligt();

                kuva=load_pcx("gfx/menu.pcx",pal);
                game_engine();
                clear_keybuf();
                set_trans_blender(0, 0, 0,255);
                }
        if((mouse_b & 1)&&(onkopaalla(191,230,449,259)==1))
                {
                masked_blit(buttons,screen,300,30,245,230,150,30);
                if(sounds==0)sample("sfx/click.wav",0);
                read_book("manuska/books/exambook.bok","manuska/blancob.pcx");
                //Load Game
                }
        if((mouse_b & 1)&&(onkopaalla(191,260,449,289)==1))
                {
                masked_blit(buttons,screen,300,60,245,260,150,30);
		if(sounds==0)sample("sfx/click.wav",0);
                creduts(CREDITS_FILE,80);
                //Save Game
                }
        if((mouse_b & 1)&&(onkopaalla(191,290,449,319)==1)&&(key[KEY_LSHIFT]))creduts(MOCK_FILE,80);
        if((mouse_b & 1)&&(onkopaalla(191,290,449,319)==1))
                {
                //Credits
                masked_blit(buttons,screen,300,90,245,290,150,30);
                if(sounds==0)sample("sfx/click.wav",0);
                credits(buffer);
                kuva=load_pcx("gfx/menu.pcx",pal);
                cross_fade(kuva,buffer,0,0,0,0,640,480,10);
                }
        if((mouse_b & 1)&&(onkopaalla(191,320,449,349)==1))mani=1000;
        }
stop_mod();
destroy_mod (ex);
destroy_bitmap(tilet);
destroy_bitmap(kuva);
destroy_bitmap(kuva2);
destroy_bitmap(buffer);
destroy_bitmap(bar);
destroy_bitmap(character[0]);
destroy_bitmap(character[1]);
destroy_bitmap(character[2]);
destroy_bitmap(character[3]);
destroy_bitmap(character[4]);
destroy_bitmap(character[5]);
destroy_bitmap(phil);
destroy_bitmap(muu);
destroy_bitmap(transis);
destroy_bitmap(mpointer);
destroy_bitmap(button);
destroy_bitmap(buttons);
destroy_bitmap(conbuf);
destroy_bitmap(map);
destroy_bitmap(kablam);
destroy_sample(kablami);
destroy_sample(lava);
destroy_sample(fountain);
destroy_sample(fire);
destroy_bitmap(transsu[0]);
destroy_bitmap(transsu[1]);
destroy_bitmap(transsu[2]);

//create_saved_map("map/world1.map");
_motoko_preset_kill(lurrinlur1);
_motoko_preset_kill(lurrinlur2);

allegro_exit();
printf("Thanks for running The summoner of Arrakun!\n");
printf("V.%s\n",version);
return (0);
}
END_OF_MAIN();

void draw_engine(BITMAP *where, int x, int y)
{
int i, ty, tx, fix = 0, fixy = 0, therewas[3], get_wall_result, get_tile_result;

    prepare_vigilante();

    for (ty = 0; ty < 41; ty++) {
        fix = 295 + (ty<<4) + player.sx;
        fixy = (ty<<3) + 32 + player.sy - 80;

        for (tx = 40; tx > 0; tx--) {

            get_wall_result = get_wall(x + tx, y + ty);
            get_tile_result = get_tile(x + tx, y + ty);

            //Tree checkup
            if((get_roof(x+tx,y+ty) == TREE_ROOF)&&(get_roof(x + 20, y + 20) == 0)) masked_blit(trees,where,get_second(x+tx,y+ty)*150,0,fix-59,fixy-115,150,100);

            //Light reserving
            if((get_wall_result==STREET_LIGHT)&&((get_roof(x+tx,y+ty)==0)||(get_roof(x + 20, y + 20) != 0)))reserve_ligt(2,fix + 16,fixy - 35);

//            if((wall[get_wall_result].type==TORCH_LIGHT)&&((get_roof(x+tx,y+ty)==0)||(get_roof(x + 20, y + 20) != 0)))reserve_ligt(0,fix + 16,fixy - 15);
	
            if(get_sprite(x+tx,y+ty)==FX_TILE) {
		if(((get_roof(x+tx,y+ty)==0)||(get_roof(x + 20, y + 20) != 0))&&(get_second(x+tx,y+ty)==0))reserve_ligt(2,fix + 16 +rand () %4,fixy +rand () %4 -wall[get_wall_result].nosto);
		if((get_second(x+tx,y+ty)==2)&&((get_roof(x+tx,y+ty)==0)||(get_roof(x + 20, y + 20) != 0)))reserve_ligt(0,fix + 16,fixy - 15);
		}


            if ((fix >= 520) || (fix < 64) || (fixy < 60)
               || (x + tx <= 0) || (y + ty <= 0))
                goto ohieng;
   
            if ((fixy < 460) && (wall[get_wall_result].hides == 0)) {

                if((get_roof(x + tx-4, y + ty+4)==TREE_ROOF)||(get_roof(x + tx-4, y + ty+4)==0)||(get_roof(x + 20, y + 20) != 0)) {    //NOTICE !!! 'Z-buffer'
                if(tile[get_tile_result].animo == 0)
                    draw_compiled_sprite(where, tiilit[get_tile_result], fix, fixy);
   
                if (tile[get_tile_result].animo == 1)
                    draw_compiled_sprite(where, tiilit[get_tile_result + fr], fix, fixy);
	    }
                                              }
            if ((get_wall_result != 0) && (fixy - 40 < 480)) {

                if ((wall[get_wall_result].type==END_FRAME_3) &&
                    (animo == 25)) {
                    put_wall(x + tx, y + ty, get_wall_result - 2);
   
                    goto pass_animo;
                }
   
                if ((wall[get_wall_result].type == END_FRAME_6) &&
                   (animo == 25)) {
                    put_wall(x + tx, y + ty, get_wall_result - 5);
   
                    goto pass_animo;
                }
   
                if ((wall[get_wall_result].type == UP_FRAME) &&
                   (animo == 25))
                    put_wall(x + tx, y + ty, get_wall_result + 1);
pass_animo:
                draw_compiled_sprite(where, seinat[get_wall_result],
                fix, fixy - 40);
                if (wall[get_wall_result].type==TORCH_LIGHT)draw_fire_noupdate(torch, where, fix + 16, fixy -wall[get_wall_result].nosto);

                if (get_wall_result == FOUNTAIN) {
                    _motoko_alt_group_draw(where, 0, fix + 16, fixy + 20);
                }
            }

            if ((fixy < 460) && (wall[get_wall_result].hides == 0)
               && (get_sprite(x + tx, y + ty)!=0)) {
               if((sprite[get_sprite(x+tx,y+ty)].type!=SFX_TILE)&&
			(sprite[get_sprite(x+tx,y+ty)].type!=BLOCK_TILE)&&
				(sprite[get_sprite(x+tx,y+ty)].type!=TRAP)&&
					(sprite[get_sprite(x+tx,y+ty)].type!=FX_TILE)) draw_compiled_sprite(where, spraitit[get_sprite(x + tx, y +ty)], fix, fixy - wall[get_wall_result].nosto);
               if(sprite[get_sprite(x+tx,y+ty)].type==SFX_TILE) handle_vigilante(get_second(x+tx,y+ty),fix);

		if(get_sprite(x+tx,y+ty)==FX_TILE) {
			if(get_second(x+tx,y+ty)==0) draw_fire(torch,where,fix+16,30+fixy-wall[get_wall(x+tx,y+ty)].nosto);
			if(get_second(x+tx,y+ty)==1) _motoko_alt_group_draw(where,1,fix+16,(fixy+30)-wall[get_wall(x+tx,y+ty)].nosto);
			if(get_second(x+tx,y+ty)==2) draw_fire(fireplace,where,fix+16,(fixy+30)-wall[get_wall(x+tx,y+ty)].nosto);
			if(get_second(x+tx,y+ty)==3) _motoko_alt_group_draw(where,0,fix+16,fixy+20);
			}
            }

            if ((get_roof(x + tx, y + ty) != 0) && (get_roof(x + 20, y + 20) == 0)
               && (fixy - 40 > 60) && (get_roof(x+tx,y+ty)!=TREE_ROOF))
                draw_compiled_sprite(where, katot[get_roof(x + tx, y + ty)],
                fix, fixy - 40);
				
             
ohieng:

            //Character drawing
            if ((tx == 20) && (ty == 20)
                &&(player.walk_mode!=1)
                    &&(player.walk_mode!=5)
                        &&(player.walk_mode!=8))
                {
                draw_sprite(where, phil, fix - player.sx, (fixy - 30) -
                player.sy);
                  solid_mode();
                            }

            if ((tx == 19) && (ty == 21)
                &&((player.walk_mode==5)
                    ||(player.walk_mode==8)))
                {
                draw_sprite(where, phil, fix - player.sx, (fixy - 30) -
                player.sy-16);
            }

            if ((tx == 20) && (ty == 21)
                &&(player.walk_mode==1)
                    &&(player.hanimo!=16)) {
                draw_sprite(where, phil, fix - player.sx-16, (fixy - 38) -
                player.sy);
            }
	    
            if((tx==20) && (ty==20) && (get_sprite(x+20,y+20)==TRAP) && (get_second(x+20,y+20)==ELECTRON)) {
		clear(poks);
		blit(electro,poks,rand ()%3*32,0,16,0,32,80);
                fblend_add(poks,where,fix-16,fixy-60,200);
		}

            if ((fix <= 520) && (fix >= 64)&&(fixy>=60)) {
                for(i=0;i<99;i++) {

                   if(NPC[i].drawed==0) {
			
                        if((NPC[i].mx>player.mx)
                            &&(NPC[i].my>player.my)
                                &&(NPC[i].mx<player.mx+40)
                                    &&(NPC[i].my<player.my+40)) {
                            if((NPC[i].mx==player.mx+tx)
                                &&(NPC[i].my==player.my+ty)
                                    &&(NPC[i].walkmode!=1)
                                        &&(NPC[i].walkmode!=5)
                                            &&(NPC[i].walkmode!=8)){
				if(i!=talkwith)read_line(NPC[i].script_file,NPC[i].sayings[0]+NPC[i].sayings[1],puffi);

                                if(NPC[i].walkmode!=0)blit(character[NPC[i].animset],muu,(NPC[i].animi/2)*32,(NPC[i].walkmode-1)*60,0,0,32,60);
                                else blit(character[NPC[i].animset],muu,288,(NPC[i].history-1)*60,0,0,32,60);
                                draw_sprite(where, muu, fix-NPC[i].sx , (fixy-30)-NPC[i].sy);
				if(i!=talkwith)textprintf_centre(where,filu[console_font].dat,fix-NPC[i].sx+16 , (fixy-30)-NPC[i].sy,-1,"%s",puffi);                 
                                if((sounds==0)&&(NPC[i].animi==1))e_sample(tile[get_tile(NPC[i].mx,NPC[i].my)].step_sound,200,128 + ((-320 + fix) / 2));
                                NPC[i].drawed=1;
                            }

                            if((NPC[i].mx==(player.mx+tx))
                                &&(NPC[i].my==(player.my+ty))
                                    &&((NPC[i].walkmode==5)
                                        ||(NPC[i].walkmode==8))) {
                                blit(character[NPC[i].animset],muu,(NPC[i].animi/2)*32,(NPC[i].walkmode-1)*60,0,0,32,60);
				draw_sprite(where, muu, fix-NPC[i].sx , (fixy-30)-NPC[i].sy);
				if(i!=talkwith)textprintf_centre(where,filu[console_font].dat,fix-NPC[i].sx+16 , (fixy-30)-NPC[i].sy,-1,"%s",puffi);                 
                                if((sounds==0)&&(NPC[i].animi==1))e_sample(tile[get_tile(NPC[i].mx,NPC[i].my)].step_sound,200,128 + ((-320 + fix) / 2));
                                NPC[i].drawed=1;
                            }

                            if((NPC[i].mx==player.mx+tx)
                                &&(NPC[i].my==player.my+ty)
                                    &&(NPC[i].walkmode==1)
                                        &&(NPC[i].animi!=16)) {
                                blit(character[NPC[i].animset],muu,(NPC[i].animi/2)*32,(NPC[i].walkmode-1)*60,0,0,32,60);
                                draw_sprite(where, muu, fix-NPC[i].sx , (fixy-30)-NPC[i].sy);
				if(i!=talkwith)textprintf_centre(where,filu[console_font].dat,fix-NPC[i].sx+16 , (fixy-30)-NPC[i].sy,-1,"%s",puffi);                 
		                if((sounds==0)&&(NPC[i].animi==1))e_sample(tile[get_tile(NPC[i].mx,NPC[i].my)].step_sound,200,128 + ((-320 + fix) / 2));
                                NPC[i].drawed=1;
		

		           }
		        }
		
                    } //Drawed end
                } //For end
            }

            //Explosions
            for(i=0;i<10;i++) {
                if((x+tx==kabuum[i].x)&&(y+ty==kabuum[i].y)&&(kabuum[i].mode==1)) {
                blit(kablam,poks,(kabuum[i].frame/5)<<6,kabuum[i].type*80,0,0,64,80);
                fblend_add(poks,where,fix-16,fixy-80,200);
                if((kabuum[i].frame==10)&&(sounds==0))play_sample(kablami, 255, 128 + ((-320 + fix) / 2),600+rand ()%400, 0);
                }
            }
            fixy = fixy + 8;
            fix = fix - 16;
        }
    }

    update_fire(fireplace);
    update_fire(torch);

    check_if_playing(0);
    check_if_playing(1);
    check_if_playing(2);

    if (therewas[1] == 0) {
        stop_sample(fountain);
        loopperi[1] = 0;
    }

    if (therewas[2]==0) {
        stop_sample(lava);
        loopperi[2] = 0;
    }
}

/* NOTES:
    20.4.2002
        Mutator
            Labels, attributes etc and names in general
            should be converted to english. (Mutator)

            The use of GOTO's might be converted to
            use FUNCTIONs / SWITCHes.

            Advice split up MAIN CORE in sub FUNCTIONs
            if possible, it'll help out alot if there
            is need for debug and or optimization at
            a later point in project progress :)

*/


void super_console (const FONT *blah, char *format, ...)
{
char *token_string, *string, cmdch,intbuf[32]; 
int token_int,i = 0, cmd = 0,size = strlen (format),tokens = 0;

va_list ap;
string = (char *) malloc (200);
//string = (char *) malloc (size);
memset (string, 0, size);
if (!string)
    return;
  va_start (ap, format);
  for (i = 0; i < strlen (format); i++)
    {
      cmd = 0;
      if (format[i] == '%')
	{
	  i++;
	  cmd = 1;
	  cmdch = format[i];
	}
      if (cmd)
	{
	  switch (cmdch)
	    {
	    case 's':		// string
	      token_string = va_arg (ap, char *);
	      if (token_string == 0)
		token_string = "(null)";
	      insert_to_string (&string, token_string, &size);
	      tokens++;
	      break;
	    case 'd':		// integer
	      token_int = va_arg (ap, int);
              sprintf (intbuf, "%d", token_int);
	      insert_to_string (&string, intbuf, &size);
	      tokens++;
	      break;
	    case '%':		// percentmark
	      strcpy (intbuf, "%");
	      insert_to_string (&string, intbuf, &size);
	      break;
	    }
	}
      else
	{
	  intbuf[0] = format[i];
	  intbuf[1] = 0;
	  insert_to_string (&string, intbuf, &size);
	}
    }
  va_end (ap);
  blit(conbuf,conbuf,0,10,0,0,600,290);
  solid_mode();
  rectfill(conbuf,0,290,600,300,makecol16(0,0,0));
  textprintf(conbuf,blah,2,292,-1,"%s",string);
  free (string);
}

void empty_line_to_console() {
blit(conbuf,conbuf,0,10,0,0,600,290);
solid_mode();
rectfill(conbuf,0,290,600,300,makecol16(0,0,0));
}

void setup()
{
char buf[1024];
BITMAP *backround;
SAMPLE *examp;
int wait=0,end=0;
if(sounds==0)
        {
        examp=load_sample("sfx/wood.wav");
        play_sample(examp,sound_vol,128,1000,1);
        }
backround=load_pcx("gfx/setup.pcx",NULL);
while(end!=1)
        {
        blit(backround,buffer,0,0,0,0,640,480);
        textprintf(buffer,filu[REDFONT].dat,120,10,-1,"Ver:%s",version);

        //Fire setup
        read_line(CFG_FILE,12,buf);
        textprintf(buffer,filu[REDFONT].dat,35,65,-1,"Fire quality:%s",buf);
        if((onkopaalla(20,60,30,70)==1)&&(wait==0)&&(mouse_b & 1))
                {
                replace_line(CFG_FILE,12,"HIGH");
                wait=100,fq=1;
                }
        if((onkopaalla(10,65,20,75)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100,fq=2;
                replace_line(CFG_FILE,12,"MEDIUM");
                }
        if((onkopaalla(20,70,30,80)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100,fq=3;
                replace_line(CFG_FILE,12,"LOW");
                }
        //story teller setup
        read_line(CFG_FILE,10,buf);
        textprintf(buffer,filu[REDFONT].dat,35,95,-1,"Story telling:%s",buf);
        if((onkopaalla(20,90,30,100)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100;
                replace_line(CFG_FILE,10,"ON");
                }
        if((onkopaalla(20,100,30,110)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100;
                replace_line(CFG_FILE,10,"OFF");
                }

        //intro sequense setup
        read_line(CFG_FILE,4,buf);
        textprintf(buffer,filu[REDFONT].dat,35,125,-1,"Intro sequense:%s",buf);
        if((onkopaalla(20,120,30,130)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100;
                replace_line(CFG_FILE,4,"ON");
                }
        if((onkopaalla(20,130,30,140)==1)&&(wait==0)&&(mouse_b & 1))
                {
                wait=100;
                replace_line(CFG_FILE,4,"OFF");
                }

        //Music volume setup
        textprintf(buffer,filu[REDFONT].dat,35,155,-1,"Music volume:%d",music_vol);
        if((onkopaalla(20,150,30,160)==1)&&(wait==0)&&(mouse_b & 1))
                {
                music_vol++;
                if(music==0)set_mod_volume(music_vol);
                if(music_vol>255)music_vol=255;
                }
        if((onkopaalla(20,160,30,170)==1)&&(wait==0)&&(mouse_b & 1))
                {
                music_vol--;
                set_mod_volume(music_vol);
                if(music==0)if(music_vol<0)music_vol=0;
                }

        //SFX volume setup
        textprintf(buffer,filu[REDFONT].dat,35,185,-1,"Sound volume:%d",sound_vol);
        if((onkopaalla(20,180,30,190)==1)&&(wait==0)&&(mouse_b & 1)&&(sounds==0))
                {
                sound_vol++;
                if(sound_vol>255)sound_vol=255;
                if(sounds==0)adjust_sample(examp,sound_vol,128,1000,1);
                }
        if((onkopaalla(20,190,30,200)==1)&&(wait==0)&&(mouse_b & 1)&&(sounds==0))
                {
                sound_vol--;
                if(sound_vol<0)sound_vol=0;
                if(sounds==0)adjust_sample(examp,sound_vol,128,1000,1);
                }


        if((mouse_b & 2)||(key[KEY_ESC]))end=1;
        draw_sprite(buffer,mpointer,mouse_x,mouse_y);
        blit(buffer,screen,0,0,0,0,640,480);
        if(wait>0)wait--;
        }
textprintf_centre(screen,filu[REDFONT].dat,320,0,-1,"Sorry about this slow setup quit! :)");
sprintf(puffi,"%d",music_vol);
replace_line(CFG_FILE,14,puffi);
sprintf(puffi,"%d",sound_vol);
replace_line(CFG_FILE,16,puffi);
fireplace=new_fire(20/fq,32,40);
torch=new_fire(10/fq,16,20);
destroy_bitmap(backround);
destroy_sample(examp);
}

void sample(char *fname,int randi)
{
SAMPLE *voice;
voice=load_sample(fname);
if(sounds==0)
        {
        if(voice != 0)
                {                
                if(randi>=10)play_sample(voice,sound_vol,128,1000-rand ()%randi,0);
                else play_sample(voice,sound_vol,128,1000,0);
                }
        else {
            super_console(filu[REDFONT].dat,"Cant play '%s' sample!",fname);
            sounds=1;
            }
        }
}

void e_sample(char *fname,int randi,int pan)
{
SAMPLE *voice;
voice=load_sample(fname);
if(sounds==0)
        {
        if(voice != 0)
                {                
                if(randi>=10)play_sample(voice,sound_vol,pan,1000-rand ()%randi,0);
                else play_sample(voice,sound_vol,pan,1000,0);
                }
        else super_console(filu[REDFONT].dat,"Cant play '%s' sample!",fname);
        }
}


void game_engine()
{

COMPILED_SPRITE *baaris;
BITMAP *pointeris,*waypo;
int end=0,i;
int wupdate_done=0;

//U3S handlers
char fname[20];


if(music==0) {
        ex = load_mod ("music/act1.s3m");
        if(ex != 0) {
            set_mod_volume(music_vol);
            play_mod (ex, TRUE);
            }
        else music=1;
        }

loopperi[0]=0;
loopperi[1]=0;
pointeris = create_bitmap(mpointer->w,mpointer->h);
waypo=load_lbm("gfx/waypoint.lbm",NULL);

if(sounds==0)
        {
        fire=load_sample("sfx/notski.wav");
        fountain=load_sample("sfx/fountain.wav");
        lava=load_sample("sfx/lavapour.wav");
        kablami=load_sample("sfx/explode.wav");
        }
buffer=create_bitmap(640,600);
clear(buffer);
load_map("map/world1.pcx");

update_time();
set_world_color();
//load_fwcbmp("gfx/tilet.pcx",tilet);
load_fwcbmp("gfx/trees.lbm",trees);
load_fwcbmp(PLAYER_ANIM,character[0]);
load_tilet("gfx/tilet.pcx");

read_line(CFG_FILE,10,puffi);
if(strcmp(puffi,"ON")==0)show_story(1);
baaris=get_compiled_sprite(bar,0);
blit(character[0],phil,0,0,0,0,32,60);
clear(muu);
super_console(filu[console_font].dat,"Welcome to Unicorn 3! V. %s",version);
super_console(filu[console_font].dat,"Current Os time is %d:%d",give_time(2),give_time(1));
empty_line_to_console();

for(end=0;end<666;) {

        draw_engine(buffer,player.mx,player.my);
        for(i=0;i<10;i++) {
                if(valox[i].varattu==1)fblend_add(transsu[valox[i].intensity],buffer,valox[i].x-(transsu[valox[i].intensity]->w/2),valox[i].y-(transsu[valox[i].intensity]->h/2),128);
                valox[i].x=0;
                valox[i].y=0;
                valox[i].varattu=0;
                valox[i].intensity=0;

                if(kabuum[i].mode==1) {
                        kabuum[i].frame++;
                        if(kabuum[i].frame==50)kabuum[i].mode=0;
                        }
                }

        draw_compiled_sprite(buffer,baaris,0,0);
        blit(conbuf,buffer,0,console_y,20,10,600,100);

        update_time();

        //statsit
        textprintf(buffer,filu[console_font].dat,466,130,-1,"%d:%d",give_time(2),give_time(1));
        textprintf(buffer,filu[console_font].dat,20,380,-1,"Hp:%d/%d",player.hp,player.hp_max);
        textprintf(buffer,filu[console_font].dat,20,390,-1,"Mn:%d/%d",player.mana,player.mana_max);
        textprintf(buffer,filu[console_font].dat,20,400,-1,"Dex:%d",player.dexterity);
        textprintf(buffer,filu[console_font].dat,20,410,-1,"Ac:%d",player.defense);
        textprintf(buffer,filu[console_font].dat,20,420,-1,"Str:%d",player.damage); //Damage rating
        textprintf(buffer,filu[console_font].dat,20,430,-1,"Lvl:%d",player.level);
        textprintf(buffer,filu[console_font].dat,20,440,-1,"Exp:%d/%d",player.exp,player.exp_needet);
        textprintf(buffer,filu[console_font].dat,20,450,-1,"Gold:%d",player.money);
        textprintf(buffer,filu[console_font].dat,20,460,-1,"FPS:%d/%d",last_fps,hidaste);


        if((give_time(1)==0)&&(give_time(0)==1)&&(wupdate_done==0)) {
                set_world_color();
                load_fwcbmp(PLAYER_ANIM,character[0]);
                load_fwcbmp("gfx/trees.lbm",trees);
                load_tilet("gfx/tilet.pcx");
                wupdate_done=1;
                }
        if(give_time(1)==50)wupdate_done=0;

        //CONSOLE SYSTEMS
        if((onkopaalla(9,101,18,110)==1)&&(mouse_b & 1)&&(console_y<=199))console_y++;
        if((onkopaalla(9,9,18,18)==1)&&(mouse_b & 1)&&(console_y>=1))console_y--;
        if((onkopaalla(621,9,631,23)==1)&&(mouse_b & 1)&&(console_y>=1))console_y=console_y-10;
        if((onkopaalla(621,97,631,111)==1)&&(mouse_b & 1)&&(console_y<=199))console_y=console_y+10;
        if(console_y<0)console_y=0;
        if(console_y>200)console_y=200;

        if(onkopaalla(260,120,280,140)==1) {
                if(mouse_b & 1)setup();
                textprintf_centre(buffer,filu[REDFONT].dat,mouse_x+30,mouse_y+30,-1,"Setup");
                }

        if(onkopaalla(280,120,300,140)==1) {
                if(mouse_b & 1)end=666;
                textprintf_centre(buffer,filu[REDFONT].dat,mouse_x+30,mouse_y+30,-1,"Main menu");
                }

        if(onkopaalla(300,120,320,140)==1)
                {
                textprintf_centre(buffer,filu[REDFONT].dat,mouse_x+30,mouse_y+30,-1,"Save game");
                }

        if(onkopaalla(320,120,340,140)==1) {
                textprintf_centre(buffer,filu[REDFONT].dat,mouse_x+30,mouse_y+30,-1,"Load game");
                }

        if(onkopaalla(340,120,360,140)==1) {
                textprintf_centre(buffer,filu[REDFONT].dat,mouse_x+30,mouse_y+30,-1,"Map");
                }

        if(key[KEY_ESC])end=666;
        if((keypressed()) && (talkwith == -1)) {
                if(key[KEY_T])super_console(filu[console_font].dat,"Time:%d:%d",give_time(2),give_time(1));
                if(key[KEY_SPACE])explode_at(player.mx+10+rand ()%20,player.my+10+rand ()%20);

                if(key[KEY_F10]) {
                        read_line(CFG_FILE,0,puffi);
                        super_console(filu[statu_font].dat,puffi);
                        }

                if(key[KEY_F11]) {
                        read_line("manuska/fortunes.dat",rand ()%20,puffi);
                        super_console(filu[gfont1].dat,puffi);
                        }


                if((key[KEY_DOWN])&&(key[KEY_LEFT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=3;
                if((key[KEY_DOWN])&&(key[KEY_RIGHT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=1;
                if((key[KEY_UP])&&(key[KEY_RIGHT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=4;
                if((key[KEY_UP])&&(key[KEY_LEFT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=2;
                if(player.walk_mode!=0)vimex=player.walk_mode;
                if((key[KEY_RIGHT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=5;
                if((key[KEY_UP])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=6;
                if((key[KEY_LEFT])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=7;
                if((key[KEY_DOWN])&&(player.hanimo==0)&&(player.walk_mode==0))player.walk_mode=8;

                if((key[KEY_HOME])&&(player.my>0))player.my--;
                if((key[KEY_DEL])&&(player.mx>0))player.mx--;
                if((key[KEY_END])&&(player.my<580))player.my++;
                if((key[KEY_PGDN])&&(player.mx<580))player.mx++;

                if((key[KEY_LSHIFT])&&(player.hanimo==0))player.walk_mode=0;

                if((key[KEY_ALT])&&(ready_wait==0)) {
                        switch(vimex) {
                                case 1:
                                operate(player.mx+20,player.my+21,0);
                                break;
                                case 2:
                                operate(player.mx+20,player.my+19,0);
                                break;
                                case 3:
                                operate(player.mx+19,player.my+20,0);
                                break;
                                case 4:
                                operate(player.mx+21,player.my+20,0);
                                break;
                                case 5:
                                operate(player.mx+21,player.my+21,0);
                                break;
                                case 6:
                                operate(player.mx+19,player.my+21,0);
                                break;
                                case 7:
                                operate(player.mx+19,player.my+19,0);
                                break;
                                case 8:
                                operate(player.mx+19,player.my+21,0);
                                break;
                                }
                        ready_wait=40;
                        }
                }
        if(talkwith >= 0) {
            open_chat_terminal(talkwith);
            }

        blit(mpointer,pointeris,0,0,0,0,30,30);

        if(player.hanimo==0) {
                if((player.walk_mode==1)&&(voidaanko_astua(player.mx+20,player.my+20,SOUTH)==1))player.walk_mode=0;
                if((player.walk_mode==2)&&(voidaanko_astua(player.mx+20,player.my+20,NORTH)==1))player.walk_mode=0;
                if((player.walk_mode==3)&&(voidaanko_astua(player.mx+20,player.my+20,WEST)==1))player.walk_mode=0;
                if((player.walk_mode==4)&&(voidaanko_astua(player.mx+20,player.my+20,EAST)==1))player.walk_mode=0;
                if((player.walk_mode==5)&&(voidaanko_astua(player.mx+20,player.my+20,SOUTHEAST)==1))player.walk_mode=0;
                if((player.walk_mode==6)&&(voidaanko_astua(player.mx+20,player.my+20,NORTHEAST)==1))player.walk_mode=0;
                if((player.walk_mode==7)&&(voidaanko_astua(player.mx+20,player.my+20,NORTHWEST)==1))player.walk_mode=0;
                if((player.walk_mode==8)&&(voidaanko_astua(player.mx+20,player.my+20,SOUTHWEST)==1))player.walk_mode=0;
                }

        if(player.walk_mode!=0) {
                player.hanimo++;
                blit(character[0],phil,(player.hanimo/2)<<5,(player.walk_mode-1)*60,0,0,32,60);
                }
        else blit(character[0],phil,288,(vimex-1)*60,0,0,32,60);

        //Movement controll
        if(((player.hanimo==1) ||
            (player.hanimo==3) ||
                (player.hanimo==5) ||
                    (player.hanimo==7) ||
                        (player.hanimo==9) ||
                            (player.hanimo==11) ||
                                (player.hanimo==12) ||
                                    (player.hanimo==14))&&((player.walk_mode>0)&&(player.walk_mode<5))) {
                if(player.walk_mode==1)move_to(SOUTH);
                if(player.walk_mode==2)move_to(NORTH);
                if(player.walk_mode==3)move_to(WEST);
                if(player.walk_mode==4)move_to(EAST);
                }
        if(player.walk_mode==5)move_to(SOUTHEAST);
        if(player.walk_mode==6)move_to(NORTHEAST);
        if(player.walk_mode==7)move_to(NORTHWEST);
        if(player.walk_mode==8)move_to(SOUTHWEST);

        if((player.hanimo==16) && (player.walk_mode<=4)) {
                vimex=player.walk_mode;
                player.walk_mode=0,player.hanimo=0;
                if(sounds==0) sample(tile[get_tile(player.mx+20,player.my+20)].step_sound,200);
		control_slowdown();
                }

        if((player.hanimo==16) && (player.walk_mode>=5)) {
                vimex=player.walk_mode;
                player.walk_mode=0,player.hanimo=0;
                if(sounds==0) sample(tile[get_tile(player.mx+20,player.my+20)].step_sound,200);
		control_slowdown();
                }

	//TRAPS!
	if(get_sprite(player.mx+20,player.my+20)==TRAP) trap_at(player.mx+20,player.my+20,YES);


        draw_sprite(buffer,pointeris,mouse_x,mouse_y);
        if(key[KEY_F12])save_pcx("shot.pcx",buffer,pal);

        _motoko_all_logic();
        evolute_player();

        if(animo==25)animo=0,fr++;
        if(ready_wait>=1)ready_wait--;
        if(fr==3)fr=0;
        animo++;
        do_ai();
        release_draw_for_NPCs();


	        frame_counter++;
        blit(buffer,screen,0,0,0,0,640,480);
	rest(hidaste);
	}

stop_mod();
if(music==0){
    ex = load_mod ("music/main.s3m");
    if(ex != 0) {
        set_mod_volume(music_vol);
        play_mod (ex, TRUE);
        }
    else music=1;
    }

free_playing_vigilante();
destroy_compiled_sprite(baaris);
destroy_bitmap(pointeris);
destroy_bitmap(waypo);
if(loopperi[0]!=0) stop_sample(fire);
if(loopperi[1]!=0) stop_sample(fountain);
if(loopperi[2]!=0) stop_sample(lava);
}

void summon_intro()
{
BITMAP *Image1 = load_pcx("gfx/title1.pcx",NULL);
BITMAP *Image2 = load_pcx("gfx/presents.pcx",NULL);

BITMAP *Lines[640];

BITMAP *Buffer = create_bitmap(640,480);

int i,j,speed = 1;
int pos[640];
int speedy[640];


int counter;
int lowest = 0;

for( i = 0; i < 480; i++ ) {
    Lines[i] = create_bitmap(640,1);
    blit(Image2,Lines[i],0,i,0,0,640,480);
    }

j = 0;

for( counter = 640; counter >= 0; counter-- ) {
    clear(Buffer);
    for( i = 0; i < 480; i++ ) {
        if( j == 0 ) {
            blit(Lines[i],Buffer,0,0,counter,i,640,1);
            j = 1;
            }
        else {
            blit(Lines[i],Buffer,0,0,0-counter,i,640,1);
            j = 0;
            }
        }
    blit(Buffer,screen,0,0,0,0,640,480);
    vsync();
    }

rest(100);

for( i = 0; i < 480; i++ ) {
    destroy_bitmap(Lines[i]);
    }

srand((unsigned)time(NULL));

for( i = 0; i < 640; i++ ) {
    Lines[i] = create_bitmap(1,480);
    blit(Image1,Lines[i],i,0,0,0,640,480);
    pos[i] = rand();
    while( pos[i] > 64 ) {
        pos[i] = rand();
        }

    pos[i] = (-480-64)+pos[i];
    speedy[i] = rand();

    while(( speedy[i] > 20 ) || ( speedy[i] < 10 )) {
        speedy[i] = rand();
        }
    }

for( i = 0; i < 640; i++ ) {
    if( pos[i] < lowest ) lowest = i;
    }

//clear_bitmap(screen);


while( pos[lowest] < 480 ) {
    for( i = 0; i < 640; i++ ) {
        counter = pos[i];
        if( counter > 0 ) counter = 0;
        fblend_trans(Lines[i],Buffer,i,counter,100);
//        fblend_trans(Lines[i],Buffer,i,counter,counter/2);
        pos[i] += speedy[i];
        }
    blit(Buffer,screen,0,0,0,0,640,480);
    vsync();
    }

blit(Image1,screen,0,0,0,0,640,480);
rest(1000);

for( i = 0; i < 640; i++ ) {
    destroy_bitmap(Lines[i]);
    }
}

void clear_ligt()
{
int i;
for(i=0;i<10;i++) {
    valox[i].x=41;
    valox[i].y=-1;
    valox[i].varattu=0;
    valox[i].intensity=0;
    }
}

void reserve_ligt(int intensity,int x,int y)
{
int i;
update_time();
if((give_time(2)>=9)&&(give_time(2)<=18))i=0;
else {
    for(i=0;i<10;i++) {
        if(valox[i].varattu==0) {
            valox[i].varattu=1;
            valox[i].intensity=intensity;
            valox[i].x = x;
            valox[i].y = y;
            break;
            }
        }
    }
}

void open_chat_terminal(int whos)
{
BITMAP *terminaali,*facial;
int done=0,aid=0,waitup=0;
int script_com=0,line=0,questions=0,ask=0;

facial=load_pcx("gfx/faces/noman.pcx",NULL);

while(done!=1) {
    char commando[20];
    read_line(NPC[whos].script_file,line,buffra);
    if(strcmp(commando,"GAMETAG 0")==0) waitup=line;
    sscanf(buffra,"%s",commando);

    if(strcmp(commando,"FACE")==0) {
	read_line(NPC[whos].script_file,line+1,buffra);	
	facial=load_pcx(buffra,NULL);	
	}
    if(strcmp(commando,"GAMETAG")==0) {
        sscanf(buffra,"%s %d",ploti, &aid);
        if(aid == gametag) done=1;
        }
    if(strcmp(commando,"EOF")==0) {
	line=waitup,done=1;
	}
    line++;
    }
done=0,waitup=0;

terminaali = load_lbm("gfx/chatroom.lbm",NULL);

while(done!=1) {

    char commando[20];
    script_com=1;

                   //Command pipelining
    read_line(NPC[whos].script_file,line,buffra);
    sscanf(buffra,"%s",commando);

    if(strcmp(commando,"CONSOLE")==0) { //Complete DONT TOUCH!
        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,"SAY")==0) {
        int lopea=0,aidil=0;
        line++;
        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) {
                textprintf(terminaali,filu[REDFONT].dat,30,15+(aidil*10),-1,"%s",buffra);
                line++,aidil++;
                }
            }
        }

    if(strcmp(commando,"QUESTIONS")==0) {
        int lopea=0;
        line++;
        while(lopea!=-1) {
            read_line(NPC[whos].script_file,line,buffra);
            if(strcmp(buffra,"WAIT_FOR_ANSWERS")==0)lopea=-1;
            if(lopea!=-1) {
                textprintf(terminaali,filu[cfont].dat,30,415+(questions*10),-1,"%s",buffra);
                line++,questions++;
                }
            }//QUEST end
        script_com=0,aid=1;
        }

    if(strcmp(commando,"HALT")==0) script_com=0;
    if((strcmp(commando,"HALT")==0)&&(key[KEY_SPACE])&&(waitup==0))line++,waitup=50;
    if(strcmp(commando,"CLEAR_CHAT")==0)terminaali = load_lbm("gfx/chatroom.lbm",NULL);
    if(strcmp(commando,"LET_ME_GO")==0)talkwith=-1;
    if(strcmp(commando,"END_SCRIPT")==0)done=1;
    if(strcmp(commando,"SOUND")==0) {
        char fname[10];
        read_line(NPC[whos].script_file,line,buffra);
        sscanf(buffra,"%s %s",commando,fname);
        printf("%s",fname);
        if(sounds==0)sample(fname,0);
        }

    if(strcmp(commando,"GOTOLINE")==0) {
        read_line(NPC[whos].script_file,line,buffra);
        sscanf(buffra,"%s %d",commando,&line);
        script_com=0;
        }

    if(aid==1) {
        script_com=0;
        if(waitup==0) {
            if(key[KEY_1])ask=1;
            if((key[KEY_2])&&(questions>=2))ask=2;
            if((key[KEY_3])&&(questions>=3))ask=3;
            if((key[KEY_4])&&(questions>=4))ask=4;
            if((key[KEY_5])&&(questions>=5))ask=5;
            if((key[KEY_6])&&(questions==6))ask=6;
            }
        if((ask!=0)&&(waitup==0)) {
            char commod[128];
            waitup=100,script_com=0;
            //Lets clear the old speech :)
            terminaali = load_lbm("gfx/chatroom.lbm",NULL);      
            line=line+ask;
            read_line(NPC[whos].script_file,line,buffra);
            read_line(NPC[whos].script_file,0,commod);
            while(strcmp(buffra,commod)!=0) {
                line++;
                read_line(NPC[whos].script_file,line,commod);
                }
            ask=0,questions=0,aid=0;
	    }
        }

  
    draw_engine(buffer,player.mx,player.my);
    draw_sprite(buffer,terminaali,0,0);
    draw_sprite(buffer,facial,20,140);

    textprintf(buffer,font,0,0,makecol16(255,0,0),"%d,%d,%d,%s",ask,aid,line,buffra);
    blit(buffer,screen,0,0,0,0,640,480);

    release_draw_for_NPCs();
    do_ai();
    if(waitup>0)waitup--;
    if(script_com==1)line++;
    if(key[KEY_TAB])done=1;
    }
talkwith=-1;
destroy_bitmap(facial);
}

int trap_at(int x,int y,int is_player) {
int zek;
zek=get_second(x,y);
switch(zek) {
	case 1: //Fire
	put_second(x,y,0);
	put_sprite(x,y,1);
	sample("sfx/firetrap.wav",100);
	super_console(filu[REDFONT].dat,"Ground underneath you bursts suddenly into flames!");
	break;
	
	case 2: //Electrocution
	if(ready_wait==0) {
		sample("sfx/electron.wav",100);
		super_console(filu[REDFONT].dat,"You got electrical shock!");
		ready_wait=80;
		}
	break;

	default: //EXPLOSION
	explode_at(x,y);
	put_second(x,y,0);
	put_sprite(x,y,0);
	super_console(filu[REDFONT].dat,"You set of an explosion trap!");
	break;
	}
}

void control_slowdown() {
	if(last_fps>30)hidaste++;
	if(last_fps<30)hidaste--;
	}

void world_tag(int tag,int toa) {
int loop=0,end=0,aid=0;
char puffi[1024];
char commando[32];

while(end!=1)
        {
        read_line("ss/wrldtags.u3s",loop,puffi);
	sscanf(puffi,"%s",commando);
	if(strcmp(puffi,"TAG") {
		sscanf(puffi,"%s %d",commando,&aid);
		if(aid==tag) {
			}
		}
	if(strcmp(puffi,"EOF")end=1;
        loop++;
        }

}