///////////////////////////////////////////////////////////////////////////////////////////
//                                     DIALOGUE FUNCTIONS                                //
//																						 //
// Initialize Dialogue(type)						- 1=Destroy, 2=VanishEnemy           //
// AddDialogueText(string, a, r, g, b)				- Normal							 //
// AddDialogueTextB(time, string, a, r, g, b)		- Timed								 //
// DialogueWait(time)								- Wait for time						 //
// DialogueVariableChange(variable, amnt)			- changevariables[x]=n				 //
// SetCharImg(side, img, left, top, right, bottom)	- "LEFT", "RIGHT" for side			 //
///////////////////////////////////////////////////////////////////////////////////////////

let dialogueimg="script\img\Dialogue.png";
let textimg="script\img\ascii.png";
LoadGraphic(dialogueimg);
LoadGraphic(textimg);
let drawnids=[];
let drawnidsb=[];
let drawnidsc=[];
let strings=[];
let stringcolors=[];
let imageleft;
let imageright;
let steps=[];
let dialogueobj;
let charaddvertex=[];
let changestepside=[];
let changestepimg=[];
let textwaittime=[];
let waittime=[];
let changevariables=[];
let changenumber=[];
let changeamnt=[];
let skipcount=0;

function CreateRObjEffect(texture, layer)
{
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, texture);
	ObjEffect_SetLayer(obj, layer);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj, 4);
	return obj;
}

function ObjEffect_SetXYRect(id, left, top, right, bottom)
{
	ObjEffect_SetVertexXY(id, 0, left, top);
	ObjEffect_SetVertexXY(id, 1, right, top);
	ObjEffect_SetVertexXY(id, 2, left, bottom);
	ObjEffect_SetVertexXY(id, 3, right, bottom);
}

function ObjEffect_SetUVRect(id, left, top, right, bottom)
{
	ObjEffect_SetVertexUV(id, 0, left, top);
	ObjEffect_SetVertexUV(id, 1, right, top);
	ObjEffect_SetVertexUV(id, 2, left, bottom);
	ObjEffect_SetVertexUV(id, 3, right, bottom);
}

function ObjEffect_SetColor(id, vertices, a, r, g, b)
{
	ascent(i in 0..vertices){ObjEffect_SetVertexColor(id, i, a, r, g, b);}
}

function DrawTextReturn(string, left, top, right, a, r, g, b)
{
	let stringarray=string~" ";
	let drawnarray=[];
	let drawnarrayb=[];
	let drawnarrayc=[];
	let xdist=right-left;
	let curdistx=0;
	let curdisty=0;
	let curdistxb=0;
	let num=0;
	let numb=0;
	let numc=0;
	ascent(i in 0..length(stringarray))
	{
	drawnarray=drawnarray~[CreateRObjEffect(textimg, 8)];
	}
	while(num<length(drawnarray)){
		curdistx=0; curdistxb=0;
		while(curdistx+14<xdist && num<length(drawnarray)){
			SetLetter(drawnarray[num], stringarray[num], 0, 1);
			curdistx+=curdistxb;
			curdistxb=0;
			if(stringarray[num]==' ' || num==length(drawnarray)-1){
				numc=num;
			}
			num++;
		}
		curdistx=0;
		if(curdisty>0){curdistx=4;}
		curdistxb=0;
		if(numb<numc){
			ascent(i in numb..numc){
				SetLetter(drawnarray[i], stringarray[i], 1, 1);
				ObjEffect_SetColor(drawnarray[i], 4, a, r, g, b);
				ObjEffect_SetXYRect(drawnarray[i], left+curdistx, top+curdisty, left+curdistx+14, top+curdisty+14);
				curdistx+=curdistxb;
				curdistxb=0;
				drawnarrayc=drawnarrayc~[drawnarray[i]];
			}
			drawnarrayb=drawnarrayb~[drawnarrayc];
		}
		drawnarrayc=[];
		numb=numc+1;
		num=numb;
		if(numc==length(drawnarray)-1){break;}
		curdisty+=14;
	}
	return drawnarrayb;
	
	function SetLetter(obj, character, UV, mov){
		let characterarray=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e',
			'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', '!', '?', ',', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ',
			'(', ')', ':', '/', '%'];
		ascent(i in 0..length(characterarray)){
			if(character==characterarray[i]){
				if(UV==1){
					if(i<=2){ObjEffect_SetUVRect(obj, (15+i)*14, 14, (16+i)*14, 28);}
					if(i>=3 && i<=20){ObjEffect_SetUVRect(obj, (i-3)*14, 28, (i-2)*14, 42);}
					if(i>=21 && i<=25){ObjEffect_SetUVRect(obj, (i-21)*14, 42, (i-20)*14, 56);}
					if(i>=26 && i<=32){ObjEffect_SetUVRect(obj, (i-15)*14, 42, (i-14)*14, 56);}
					if(i>=33 && i<=50){ObjEffect_SetUVRect(obj, (i-33)*14, 56, (i-32)*14, 70);}
					if(i==51){ObjEffect_SetUVRect(obj, (i-51)*14, 70, (i-50)*14, 84);}
					if(i==52){ObjEffect_SetUVRect(obj, (i-38)*14, 0, (i-37)*14, 14);}
					if(i==53){ObjEffect_SetUVRect(obj, (i-52)*14, 0, (i-51)*14, 14);}
					if(i==54){ObjEffect_SetUVRect(obj, (i-41)*14, 14, (i-40)*14, 28);}
					if(i==55){ObjEffect_SetUVRect(obj, (i-43)*14, 0, (i-42)*14, 14);}
					if(i==56 || i==57){ObjEffect_SetUVRect(obj, (i-40)*14, 0, (i-39)*14, 14);}
					if(i>=58 && i <=65){ObjEffect_SetUVRect(obj, (i-58)*14, 14, (i-57)*14, 28);}
					if(i==67){ObjEffect_SetUVRect(obj, 8*14, 0, 9*14, 14);}
					if(i==68){ObjEffect_SetUVRect(obj, 9*14, 0, 10*14, 14);}
					if(i==69){ObjEffect_SetUVRect(obj, 8*14, 14, 9*14, 28);}
					if(i==70){ObjEffect_SetUVRect(obj, 15*14, 0, 16*14, 14);}
					if(i==71){ObjEffect_SetUVRect(obj, 5*14, 0, 6*14, 14);}
				}
				if(mov==1){
					if(i<=11 || (i>=13 && i<=21) || (i>=23 && i<=25)){curdistx+=10;}
					if(i==8){curdistxb-=2;}
					if(i==12 || i==22){curdistx+=12; curdistxb+=2;}
					if( (i>=26 && i<=33) || i==36 || (i>=38 && i<=44) || (i>=46 && i<=51)){curdistx+=9; curdistxb-=1;}
					if(i==34 || i==35 || i==37 || i==45){curdistx+=9; curdistxb-=2;}
					if(i==38 || i==48){curdistx+=2; curdistxb+=2;}
					if(i==52 || i==55){curdistx+=12; curdistxb-=8;}
					if(i==53){curdistx+=8; curdistxb-=2;}
					if(i==54 || (i>=56 && i<=65) ){curdistx+=10; curdistxb-=1;}
					if(i==66){curdistx+=5;}
					if(i==67){curdistx+=6; curdistxb-=0;}
					if(i==68){curdistx+=12; curdistxb-=4;}
					if(i==69){curdistx+=6; curdistxb-=4;}
					if(i==70){curdistx+=12; curdistxb-=2;}
					if(i==71){curdistx+=12; curdistxb-=2;}
				}
			}
		}
	}
}

task InitializeDialogue(type)
{
	ForbidShot(true);
	let stepnum=0;
	dialogueobj=CreateRObjEffect(dialogueimg, 7);
	ObjEffect_SetXYRect(dialogueobj, GetClipMinX, GetClipMinY, GetClipMaxX, GetClipMaxY);
	ObjEffect_SetUVRect(dialogueobj, 0, 0, 384, 448);
	ascent(i in 0..32){
		ObjEffect_SetColor(dialogueobj, 4, i*6, 255, 255, 255);
		yield;
	}
	yield;
	imageleft=CreateRObjEffect("", 7);
	imageright=CreateRObjEffect("", 7);
	ObjEffect_SetXYRect(imageleft, GetClipMinX+15, GetClipMinY+8, GetClipMinX+99, GetClipMinY+92);
	ObjEffect_SetXYRect(imageright, GetClipMinX+284, GetClipMinY+8, GetClipMinX+368, GetClipMinY+92);
	while(Obj_BeDeleted(dialogueobj)==false)
	{
	yield;
		if(stepnum<length(steps))
		{
		if(stepnum%1==0)
		{
			if(steps[stepnum]=="STRING")
			{
			drawnidsb=DrawTextReturn(strings[stepnum], GetClipMinX+5, GetClipMaxY-5, GetClipMaxX-5,
			stringcolors[stepnum][0], stringcolors[stepnum][1], stringcolors[stepnum][2], stringcolors[stepnum][3]);
			drawnids=drawnids~drawnidsb;
			loop(length(drawnidsb)){
				drawnidsc=drawnidsc~[[stringcolors[stepnum][1], stringcolors[stepnum][2], stringcolors[stepnum][3]]];
			}
			ascent(l in 0..10*(length(drawnidsb))){
				ascent(i in 0..length(drawnids)){
					ascent(j in 0..length(drawnids[i])){
						Obj_SetY(drawnids[i][j], Obj_GetY(drawnids[i][j])-1.5);
						if(length(drawnids)-i>19 && length(drawnids)-i<23){
							ObjEffect_SetColor(drawnids[i][j], 4, (23-length(drawnids)+i)*85-l*8.5,
							drawnidsc[i][0], drawnidsc[i][1], drawnidsc[i][2]);
						}
						if(length(drawnids)-i>22){
							ObjEffect_SetColor(drawnids[i][j], 4, 0,
							drawnidsc[i][0], drawnidsc[i][1], drawnidsc[i][2]);
						}
					}
				}
				yield;
			}
			stepnum+=0.5;
            skipcount=0;
			}
			else if(steps[stepnum]=="STRINGB")
			{
			drawnidsb=DrawTextReturn(strings[stepnum], GetClipMinX+5, GetClipMaxY-5, GetClipMaxX-5,
			stringcolors[stepnum][0], stringcolors[stepnum][1], stringcolors[stepnum][2], stringcolors[stepnum][3]);
			drawnids=drawnids~drawnidsb;
			loop(length(drawnidsb)){
				drawnidsc=drawnidsc~[[stringcolors[stepnum][1], stringcolors[stepnum][2], stringcolors[stepnum][3]]];
			}
			ascent(l in 0..10*(length(drawnidsb))){
				ascent(i in 0..length(drawnids)){
					ascent(j in 0..length(drawnids[i])){
						Obj_SetY(drawnids[i][j], Obj_GetY(drawnids[i][j])-1.5);
						if(length(drawnids)-i>19 && length(drawnids)-i<23){
							ObjEffect_SetColor(drawnids[i][j], 4, (23-length(drawnids)+i)*85-l*8.5,
							drawnidsc[i][0], drawnidsc[i][1], drawnidsc[i][2]);
						}
					}
				}
				yield;
			}
			stepnum+=0.5;
            skipcount=0;
			}
			else if(steps[stepnum]=="CHANGE")
			{
			if(changestepside[stepnum]=="LEFT"){
				ObjEffect_SetTexture(imageleft, changestepimg[stepnum]);
				ObjEffect_SetUVRect(imageleft, charaddvertex[stepnum][0], charaddvertex[stepnum][1],
												charaddvertex[stepnum][2], charaddvertex[stepnum][3]);
			}
			if(changestepside[stepnum]=="RIGHT"){
				ObjEffect_SetTexture(imageright, changestepimg[stepnum]);
				ObjEffect_SetUVRect(imageright, charaddvertex[stepnum][0], charaddvertex[stepnum][1],
												charaddvertex[stepnum][2], charaddvertex[stepnum][3]);
			}
			stepnum++;
			}
			else if(steps[stepnum]=="WAIT")
			{
                loop(waittime[stepnum]){yield;}
                stepnum++;
			}
			else if(steps[stepnum]=="CHANGEVAR")
			{
                changevariables[changenumber[stepnum]]=changeamnt[stepnum];
                stepnum++;
			}
        }else{
			if(steps[stepnum-0.5]=="STRINGB"){
                loop(textwaittime[stepnum-0.5]){yield;}
                skipcount=4;
                stepnum+=0.5;
			}else if(steps[stepnum-0.5]=="STRING"){
                if(GetKeyState(VK_SHOT)==KEY_PUSH){stepnum+=0.5; skipcount=4;}
                if(GetKeyState(VK_SKIP)==KEY_PUSH || GetKeyState(VK_SKIP)==KEY_HOLD){skipcount++;}
                if(skipcount>=5){stepnum+=0.5; skipcount=0;}
            }
		}
		}
		if(stepnum==length(steps))
		{
		Obj_Delete(dialogueobj);
        if(length(imageleft)>=1){ascent(i in 0..length(imageleft)){Obj_Delete(imageleft[i]);}}
        if(length(imageright)>=1){ascent(i in 0..length(imageright)){Obj_Delete(imageright[i]);}}
		ascent(i in 0..length(drawnids)){
			ascent(j in 0..length(drawnids[i])){
				Obj_Delete(drawnids[i][j]);
			}
		}
        skipcount=4;
		ForbidShot(false);
        yield;
		if(type==1){
			AddLife(-GetLife);
		}else{
			VanishEnemy;
		}
		}
	}
}

function AddDialogueText(string, a, r, g, b)
{
	steps=steps~["STRING"];
	strings=strings~[string];
	stringcolors=stringcolors~[ [a,r,g,b] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepimg=changestepimg~[""];
    textwaittime=textwaittime~[0];
    waittime=waittime~[0];
    changenumber=changenumber~[0];
    changeamnt=changeamnt~[0];
}

function AddDialogueTextB(time, string, a, r, g, b)
{
	steps=steps~["STRINGB"];
	strings=strings~[string];
	stringcolors=stringcolors~[ [a,r,g,b] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepimg=changestepimg~[""];
    textwaittime=textwaittime~[time];
    waittime=waittime~[0];
    changenumber=changenumber~[0];
    changeamnt=changeamnt~[0];
}

function DialogueWait(time)
{
	steps=steps~["WAIT"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [0,0,0] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepimg=changestepimg~[""];
    textwaittime=textwaittime~[0];
    waittime=waittime~[time];
    changenumber=changenumber~[0];
    changeamnt=changeamnt~[0];
}

function DialogueVariableChange(variable, amnt)
{
	steps=steps~["CHANGEVAR"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [0,0,0] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepimg=changestepimg~[""];
    textwaittime=textwaittime~[0];
    waittime=waittime~[0];
    while(variable>=length(changevariables)){changevariables=changevariables~[0];}
    changenumber=changenumber~[variable];
    changeamnt=changeamnt~[amnt];
}

function SetCharImg(side, img, left, top, right, bottom)
{
	steps=steps~["CHANGE"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [NULL,NULL,NULL] ];
	charaddvertex=charaddvertex~[ [left, top, right, bottom] ];
	changestepside=changestepside~[side];
	changestepimg=changestepimg~[img];
    textwaittime=textwaittime~[0];
    waittime=waittime~[0];
    changenumber=changenumber~[0];
    changeamnt=changeamnt~[0];
}