shithub: cstory

Download patch

ref: 1bcbfa846f0e3597af8087ab4df3e6455a6b20f8
parent: e796a55a75d6b4adf5611165e193b14f8ce3c024
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Feb 11 23:29:26 EST 2019

Made TextScr.cpp ASM-accurate

Or, at least as accurate as I can make it for now.

--- a/src/TextScr.cpp
+++ b/src/TextScr.cpp
@@ -34,6 +34,8 @@
 
 #define TSC_BUFFER_SIZE 0x5000
 
+#define TEXT_LEFT (WINDOW_WIDTH / 2 - 108)
+
 TEXT_SCRIPT gTS;
 
 int gNumberTextScript[4];
@@ -42,22 +44,26 @@
 RECT gRect_line = {0, 0, 216, 16};
 
 //Initialize and end tsc
-bool InitTextScript2()
+BOOL InitTextScript2()
 {
 	//Clear flags
 	gTS.mode = 0;
 	g_GameFlags &= ~0x04;
-	
+
 	//Create line surfaces
 	for (int i = 0; i < 4; i++)
 		MakeSurface_Generic(gRect_line.right, gRect_line.bottom, i + 30);
-	
+
 	//Clear text
 	memset(text, 0, sizeof(text));
-	
+
 	//Allocate script buffer
 	gTS.data = (char*)malloc(TSC_BUFFER_SIZE);
-	return gTS.data != nullptr;
+
+	if (gTS.data == NULL)
+		return FALSE;
+	else
+		return TRUE;
 }
 
 void EndTextScript()
@@ -64,7 +70,7 @@
 {
 	//Free TSC buffer
 	free(gTS.data);
-	
+
 	//Release buffers
 	ReleaseSurface(SURFACE_ID_TEXT_BOX);
 	for (int i = 0; i < 4; i++)
@@ -75,17 +81,20 @@
 void EncryptionBinaryData2(uint8_t *pData, int size)
 {
 	int val1;
-	
+
 	int half = size / 2;
-	if (pData[half])
-		val1 = -pData[half];
-	else
+	if (pData[half] == 0)
 		val1 = -7;
-	
+	else
+		val1 = (pData[half] % 256) * -1;
+
 	for (int i = 0; i < size; i++)
 	{
-		if ( i != half )
-			pData[i] += val1;
+		int work = pData[i];
+		work += val1;
+
+		if (i != half)
+			pData[i] = work % 256;
 	}
 }
 
@@ -95,21 +104,21 @@
 	//Get path
 	char path[260];
 	sprintf(path, "%s/%s", gDataPath, name);
-	
+
 	//Open file
 	SDL_RWops *fp = SDL_RWFromFile(path, "rb");
 	if (!fp)
 		return false;
-	
+
 	//Read data
 	gTS.size = SDL_RWsize(fp);
 	fp->read(fp, gTS.data, 1, gTS.size);
 	gTS.data[gTS.size] = 0;
 	fp->close(fp);
-	
+
 	//Set path
 	strcpy(gTS.path, name);
-	
+
 	//Decrypt data
 	EncryptionBinaryData2((uint8_t*)gTS.data, gTS.size);
 	return true;
@@ -121,11 +130,11 @@
 	//Open Head.tsc
 	char path[PATH_LENGTH];
 	sprintf(path, "%s/%s", gDataPath, "Head.tsc");
-	
+
 	SDL_RWops *fp = SDL_RWFromFile(path, "rb");
 	if (!fp)
 		return false;
-	
+
 	//Read Head.tsc
 	int head_size = SDL_RWsize(fp);
 	fp->read(fp, gTS.data, 1, head_size);
@@ -132,14 +141,14 @@
 	EncryptionBinaryData2((uint8_t*)gTS.data, head_size);
 	gTS.data[head_size] = 0;
 	SDL_RWclose(fp);
-	
+
 	//Open stage's .tsc
 	sprintf(path, "%s/%s", gDataPath, name);
-	
+
 	fp = SDL_RWFromFile(path, "rb");
 	if (!fp)
 		return false;
-	
+
 	//Read stage's tsc
 	int body_size = SDL_RWsize(fp);
 	fp->read(fp, &gTS.data[head_size], 1, body_size);
@@ -146,7 +155,7 @@
 	EncryptionBinaryData2((uint8_t*)&gTS.data[head_size], body_size);
 	gTS.data[head_size + body_size] = 0;
 	SDL_RWclose(fp);
-	
+
 	//Set parameters
 	gTS.size = head_size + body_size;
 	strcpy(gTS.path, name);
@@ -162,14 +171,16 @@
 //Get 4 digit number from TSC data
 int GetTextScriptNo(int a)
 {
-	return gTS.data[a + 3] - 48
-		+ 10 * gTS.data[a + 2] - 480
-		+ 100 * gTS.data[a + 1] - 4800
-		+ 1000 * gTS.data[a] - 48000;
+	int b = 0;
+	b += (gTS.data[a++] - '0') * 1000;
+	b += (gTS.data[a++] - '0') * 100;
+	b += (gTS.data[a++] - '0') * 10;
+	b += gTS.data[a] - '0';
+	return b;
 }
 
 //Start TSC event
-bool StartTextScript(int no)
+BOOL StartTextScript(int no)
 {
 	//Reset state
 	gTS.mode = 1;
@@ -182,11 +193,15 @@
 	gTS.face = 0;
 	gTS.item = 0;
 	gTS.offsetY = 0;
-	
+
 	gMC.shock = 0;
-	
-	gTS.rcText = {WINDOW_WIDTH / 2 - 108, WINDOW_HEIGHT - 56, WINDOW_WIDTH + 108, WINDOW_HEIGHT - 8};
-	
+
+	gTS.rcText.left = TEXT_LEFT;
+	gTS.rcText.top = WINDOW_HEIGHT - 56;
+	gTS.rcText.right = WINDOW_WIDTH + 108;
+	gTS.rcText.bottom = gTS.rcText.top + 48;
+
+	/* This is present in the Linux port, but not the Windows version (1.0.0.6, at least)
 	//Clear text lines
 	for (int i = 0; i < 4; i++)
 	{
@@ -193,37 +208,40 @@
 		gTS.ypos_line[i] = 16 * i;
 		CortBox2(&gRect_line, 0x000000, i + 30);
 		memset(&text[i * 0x40], 0, 0x40);
-	}
-	
+	}*/
+
 	//Find where event starts
-	for (gTS.p_read = 0; ; gTS.p_read++)
+	gTS.p_read = 0;
+	while (1)
 	{
 		//Check if we are still in the proper range
-		if (!gTS.data[gTS.p_read])
-			return false;
-		
+		if (gTS.data[gTS.p_read] == '\0')
+			return FALSE;
+
 		//Check if we are at an event
-		if (gTS.data[gTS.p_read] != '#')
-			continue;
-		
-		//Check if this is our event
-		int event_no = GetTextScriptNo(++gTS.p_read);
-		
-		if (no == event_no)
-			break;
-		if (no < event_no)
-			return false;
+		if (gTS.data[gTS.p_read] == '#')
+		{
+			//Check if this is our event
+			int event_no = GetTextScriptNo(++gTS.p_read);
+
+			if (no == event_no)
+				break;
+			if (no < event_no)
+				return FALSE;
+		}
+
+		++gTS.p_read;
 	}
-	
+
 	//Advance until new-line
 	while (gTS.data[gTS.p_read] != '\n')
 		++gTS.p_read;
 	++gTS.p_read;
-	
-	return true;
+
+	return TRUE;
 }
 
-bool JumpTextScript(int no)
+BOOL JumpTextScript(int no)
 {
 	//Set state
 	gTS.mode = 1;
@@ -232,7 +250,7 @@
 	gTS.p_write = 0;
 	gTS.wait = 4;
 	gTS.wait_beam = 0;
-	
+
 	//Clear text lines
 	for (int i = 0; i < 4; i++)
 	{
@@ -240,33 +258,36 @@
 		CortBox2(&gRect_line, 0x000000, i + 30);
 		memset(&text[i * 0x40], 0, 0x40);
 	}
-	
+
 	//Find where event starts
-	for (gTS.p_read = 0; ; gTS.p_read++)
+	gTS.p_read = 0;
+	while(1)
 	{
 		//Check if we are still in the proper range
-		if (!gTS.data[gTS.p_read])
-			return false;
-		
+		if (gTS.data[gTS.p_read] == '\0')
+			return FALSE;
+
 		//Check if we are at an event
-		if (gTS.data[gTS.p_read] != '#')
-			continue;
-		
-		//Check if this is our event
-		int event_no = GetTextScriptNo(++gTS.p_read);
-		
-		if (no == event_no)
-			break;
-		if (no < event_no)
-			return false;
+		if (gTS.data[gTS.p_read] == '#')
+		{
+			//Check if this is our event
+			int event_no = GetTextScriptNo(++gTS.p_read);
+
+			if (no == event_no)
+				break;
+			if (no < event_no)
+				return FALSE;
+		}
+
+		++gTS.p_read;
 	}
-	
+
 	//Advance until new-line
 	while (gTS.data[gTS.p_read] != '\n')
 		++gTS.p_read;
 	++gTS.p_read;
-	
-	return true;
+
+	return TRUE;
 }
 
 //End event
@@ -282,7 +303,7 @@
 //Prepare a new line
 void CheckNewLine()
 {
-	if (gTS.ypos_line[gTS.line % 4] == 48)
+	if (gTS.ypos_line[gTS.line % 4] == '0')
 	{
 		gTS.mode = 3;
 		g_GameFlags |= 4;
@@ -299,44 +320,44 @@
 	table[0] = 1000;
 	table[1] = 100;
 	table[2] = 10;
-	
+
 	//Get number to print
 	int a = gNumberTextScript[index];
 	int b;
-	
+
 	char str[5];
-	bool bZero = false;
+	BOOL bZero = false;
 	int offset = 0;
-	
+
+	//Trim leading zeroes
 	for (int i = 0; i < 3; i++)
 	{
-		if (a / table[i] || bZero)
+		if (a / table[i] || bZero != FALSE)
 		{
-			//Do whatever the fuck this is
 			b = a / table[i];
-			str[offset] = b + 0x30;
-			bZero = true;
+			str[offset] = (char)b + '0';
+			bZero = TRUE;
 			a -= b * table[i];
 			++offset;
 		}
 	}
-	
+
 	//Set last digit of string, and add null terminator
-	str[offset] = a + 0x30;
+	str[offset] = a + '0';
 	str[offset + 1] = 0;
-	
+
 	//Append number to line
 	PutText2(6 * gTS.p_write, 0, str, 0xFFFFFE, gTS.line % 4 + 30);
 	strcat(&text[gTS.line % 4 * 0x40], str);
-	
+
 	//Play sound and reset blinking cursor
 	PlaySoundObject(2, 1);
 	gTS.wait_beam = 0;
-	
+
 	//Check if should move to next line (prevent a memory overflow, come on guys, this isn't a leftover of pixel trying to make text wrapping)
 	gTS.p_write += strlen(str);
-	
-	if (gTS.p_write > 34)
+
+	if (gTS.p_write >= 35)
 	{
 		gTS.p_write = 0;
 		gTS.line++;
@@ -350,7 +371,7 @@
 	gTS.line = 0;
 	gTS.p_write = 0;
 	gTS.offsetY = 0;
-	
+
 	for (int i = 0; i < 4; i++)
 	{
 		gTS.ypos_line[i] = 16 * i;
@@ -362,120 +383,123 @@
 //Draw textbox and whatever else
 void PutTextScript()
 {
-	if (gTS.mode && (gTS.flags & 1))
+	if (gTS.mode == 0)
+		return;
+
+	if ((gTS.flags & 1) == 0)
+		return;
+
+	//Set textbox position
+	if (gTS.flags & 0x20)
 	{
-		//Set textbox position
-		if (gTS.flags & 0x20)
+		gTS.rcText.top = 32;
+		gTS.rcText.bottom = gTS.rcText.top + 48;
+	}
+	else
+	{
+		gTS.rcText.top = WINDOW_HEIGHT - 56;
+		gTS.rcText.bottom = gTS.rcText.top + 48;
+	}
+
+	//Draw textbox
+	if (gTS.flags & 2)
+	{
+		RECT rcFrame1 = {0, 0, 244, 8};
+		RECT rcFrame2 = {0, 8, 244, 16};
+		RECT rcFrame3 = {0, 16, 244, 24};
+
+		PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, gTS.rcText.top - 10, &rcFrame1, 26);
+		int i;
+		for (i = 1; i < 7; i++)
+			PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, 8 * i + gTS.rcText.top - 10, &rcFrame2, 26);
+		PutBitmap3(&grcFull, WINDOW_WIDTH / 2 - 122, 8 * i + gTS.rcText.top - 10, &rcFrame3, 26);
+	}
+
+	//Draw face picture
+	RECT rcFace;
+	rcFace.left = 48 * (gTS.face % 6);
+	rcFace.top = 48 * (gTS.face / 6);
+	rcFace.right = rcFace.left + 48;
+	rcFace.bottom = rcFace.top + 48;
+
+	if (gTS.face_x < (TEXT_LEFT * 0x200))
+		gTS.face_x += 0x1000;
+	PutBitmap3(&gTS.rcText, gTS.face_x / 0x200, gTS.rcText.top - 3, &rcFace, 27);
+
+	//Draw text
+	int text_offset;
+	if (gTS.face)
+		text_offset = 56;
+	else
+		text_offset = 0;
+
+	for (int i = 0; i < 4; i++)
+		PutBitmap3(&gTS.rcText, text_offset + TEXT_LEFT, gTS.offsetY + gTS.ypos_line[i] + gTS.rcText.top, &gRect_line, i + 30);
+
+	//Draw NOD cursor
+	if ((gTS.wait_beam++ % 20 > 12) && gTS.mode == 2)
+	{
+		RECT rect;
+		rect.left = TEXT_LEFT + text_offset + 6 * gTS.p_write;
+		rect.top = gTS.ypos_line[gTS.line % 4] + gTS.rcText.top + gTS.offsetY;
+		rect.right = rect.left + 5;
+		rect.bottom = rect.top + 11;
+		CortBox(&rect, 0xFFFFFE);
+	}
+
+	//Draw GIT
+	RECT rcItemBox1 = {0, 0, 72, 16};
+	RECT rcItemBox2 = {0, 8, 72, 24};
+	RECT rcItemBox3 = {240, 0, 244, 8};
+	RECT rcItemBox4 = {240, 8, 244, 16};
+	RECT rcItemBox5 = {240, 16, 244, 24};
+
+	if (gTS.item)
+	{
+		PutBitmap3(&grcFull, (WINDOW_WIDTH - 80) / 2, WINDOW_HEIGHT - 112, &rcItemBox1, 26);
+		PutBitmap3(&grcFull, (WINDOW_WIDTH - 80) / 2, WINDOW_HEIGHT - 96, &rcItemBox2, 26);
+		PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 112, &rcItemBox3, 26);
+		PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 104, &rcItemBox4, 26);
+		PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 96, &rcItemBox4, 26);
+		PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 88, &rcItemBox5, 26);
+
+		if (gTS.item_y < WINDOW_HEIGHT - 104)
+			++gTS.item_y;
+
+		RECT rect;
+		if (gTS.item < 1000)
 		{
-			gTS.rcText.top = 32;
-			gTS.rcText.bottom = 80;
+			rect.left = 16 * (gTS.item % 16);
+			rect.right = rect.left + 16;
+			rect.top = 16 * (gTS.item / 16);
+			rect.bottom = rect.top + 16;
+			PutBitmap3(&grcFull, (WINDOW_WIDTH - 24) / 2, gTS.item_y, &rect, 12);
 		}
 		else
 		{
-			gTS.rcText.top = WINDOW_HEIGHT - 56;
-			gTS.rcText.bottom = WINDOW_HEIGHT - 8;
+			rect.left = 32 * ((gTS.item - 1000) % 8);
+			rect.right = rect.left + 32;
+			rect.top = 16 * ((gTS.item - 1000) / 8);
+			rect.bottom = rect.top + 16;
+			PutBitmap3(&grcFull, (WINDOW_WIDTH - 40) / 2, gTS.item_y, &rect, 8);
 		}
-		
-		//Draw textbox
-		if (gTS.flags & 2)
-		{
-			RECT rcFrame1 = {0, 0, 244, 8};
-			RECT rcFrame2 = {0, 8, 244, 16};
-			RECT rcFrame3 = {0, 16, 244, 24};
-			
-			PutBitmap3(&grcFull, gTS.rcText.left - 14, gTS.rcText.top - 10, &rcFrame1, 26);
-			int i;
-			for (i = 1; i < 7; i++)
-				PutBitmap3(&grcFull, gTS.rcText.left - 14, 8 * i + gTS.rcText.top - 10, &rcFrame2, 26);
-			PutBitmap3(&grcFull, gTS.rcText.left - 14, 8 * i + gTS.rcText.top - 10, &rcFrame3, 26);
-		}
-		
-		//Draw face picture
-		RECT rcFace;
-		rcFace.left = 48 * (gTS.face % 6);
-		rcFace.top = 48 * (gTS.face / 6);
-		rcFace.right = 48 * (gTS.face % 6) + 48;
-		rcFace.bottom = 48 * (gTS.face / 6) + 48;
-		
-		if (gTS.face_x < (gTS.rcText.left << 9))
-			gTS.face_x += 0x1000;
-		PutBitmap3(&gTS.rcText, gTS.face_x / 0x200, gTS.rcText.top - 3, &rcFace, 27);
-		
-		//Draw text
-		int text_offset;
-		if (gTS.face)
-			text_offset = 56;
+	}
+
+	//Draw Yes / No selection
+	RECT rect_yesno = {152, 48, 244, 80};
+	RECT rect_cur = {112, 88, 128, 104};
+
+	if (gTS.mode == 6)
+	{
+		int i;
+		if (gTS.wait < 2)
+			i = (WINDOW_HEIGHT - 96) + (2 - gTS.wait) * 4;
 		else
-			text_offset = 0;
-		
-		for (int i = 0; i < 4; i++)
-			PutBitmap3(&gTS.rcText, text_offset + gTS.rcText.left, gTS.offsetY + gTS.ypos_line[i] + gTS.rcText.top, &gRect_line, i + 30);
-		
-		//Draw NOD cursor
-		if ((gTS.wait_beam++ % 20 > 12) && gTS.mode == 2)
-		{
-			RECT rect;
-			rect.left = gTS.rcText.left + text_offset + 6 * gTS.p_write;
-			rect.top = gTS.rcText.top + gTS.ypos_line[gTS.line % 4] + gTS.offsetY;
-			rect.right = rect.left + 5;
-			rect.bottom = rect.top + 11;
-			CortBox(&rect, 0xFFFFFE);
-		}
-		
-		//Draw GIT
-		RECT rcItemBox1 = {0, 0, 72, 16};
-		RECT rcItemBox2 = {0, 8, 72, 24};
-		RECT rcItemBox3 = {240, 0, 244, 8};
-		RECT rcItemBox4 = {240, 8, 244, 16};
-		RECT rcItemBox5 = {240, 16, 244, 24};
-		
-		if (gTS.item)
-		{
-			PutBitmap3(&grcFull, (WINDOW_WIDTH - 80) / 2, WINDOW_HEIGHT - 112, &rcItemBox1, 26);
-			PutBitmap3(&grcFull, (WINDOW_WIDTH - 80) / 2, WINDOW_HEIGHT - 96, &rcItemBox2, 26);
-			PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 112, &rcItemBox3, 26);
-			PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 104, &rcItemBox4, 26);
-			PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 96, &rcItemBox4, 26);
-			PutBitmap3(&grcFull, (WINDOW_WIDTH + 64) / 2, WINDOW_HEIGHT - 88, &rcItemBox5, 26);
-			
-			if (gTS.item_y < WINDOW_HEIGHT - 104)
-				++gTS.item_y;
-			
-			RECT rect;
-			if (gTS.item >= 1000)
-			{
-				rect.left = 32 * ((gTS.item - 1000) % 8);
-				rect.right = 32 * ((gTS.item - 1000) % 8) + 32;
-				rect.top = 16 * ((gTS.item - 1000) / 8);
-				rect.bottom = 16 * ((gTS.item - 1000) / 8) + 16;
-				PutBitmap3(&grcFull, (WINDOW_WIDTH - 40) / 2, gTS.item_y, &rect, 8);
-			}
-			else
-			{
-				rect.left = 16 * (gTS.item % 16);
-				rect.right = 16 * (gTS.item % 16) + 16;
-				rect.top = 16 * (gTS.item / 16);
-				rect.bottom = 16 * (gTS.item / 16) + 16;
-				PutBitmap3(&grcFull, (WINDOW_WIDTH - 24) / 2, gTS.item_y, &rect, 12);
-			}
-		}
-		
-		//Draw Yes / No selection
-		RECT rect_yesno = {152, 48, 244, 80};
-		RECT rect_cur = {112, 88, 128, 104};
-		
-		if (gTS.mode == 6 )
-		{
-			int i;
-			if (gTS.wait > 1)
-				i = WINDOW_HEIGHT - 96;
-			else
-				i = WINDOW_HEIGHT - 88 - gTS.wait * 4;
-			
-			PutBitmap3(&grcFull, (WINDOW_WIDTH + 112) / 2, i, &rect_yesno, 26);
-			if (gTS.wait == 16)
-				PutBitmap3(&grcFull, 41 * gTS.select + (WINDOW_WIDTH + 102) / 2, WINDOW_HEIGHT - 86, &rect_cur, 26);
-		}
+			i = WINDOW_HEIGHT - 96;
+
+		PutBitmap3(&grcFull, (WINDOW_WIDTH + 112) / 2, i, &rect_yesno, 26);
+		if (gTS.wait == 16)
+			PutBitmap3(&grcFull, 41 * gTS.select + (WINDOW_WIDTH + 102) / 2, WINDOW_HEIGHT - 86, &rect_cur, 26);
 	}
 }
 
@@ -483,131 +507,31 @@
 int TextScriptProc()
 {
 	RECT rcSymbol = {64, 48, 72, 56};
-	
-	bool bExit;
-	
+
+	BOOL bExit;
+
 	switch (gTS.mode)
 	{
 		case 1: //PARSE
 			//Type out (faster if ok or cancel are held)
 			++gTS.wait;
-			
+
 			if (!(g_GameFlags & 2) && (gKeyCancel | gKeyOk) & gKey)
 				gTS.wait += 4;
-			
+
 			if (gTS.wait < 4)
 				break;
-			
+
 			gTS.wait = 0;
-			
+
 			//Parsing time
 			int w, x, y, z;
-			bExit = false;
-			
-			while (!bExit)
+			bExit = FALSE;
+
+			while (bExit == FALSE)
 			{
-				if (gTS.data[gTS.p_read] != '<')
+				if (gTS.data[gTS.p_read] == '<')
 				{
-					if (gTS.data[gTS.p_read] == '\r')
-					{
-						//Go to new-line
-						gTS.p_read += 2;
-						gTS.p_write = 0;
-						
-						if (gTS.flags & 1)
-						{
-							++gTS.line;
-							CheckNewLine();
-						}
-					}
-					else if (gTS.flags & 0x10)
-					{
-						//SAT/CAT/TUR printing
-						for (x = gTS.p_read; ; x++)
-						{
-							//Break if reaches command, or new-line
-							if (gTS.data[x] == '<' || gTS.data[x] == '\r')
-								break;
-							//Skip if SHIFT-JIS
-							if (gTS.data[x] & 0x80)
-								++x;
-						}
-						
-						//Get text to copy
-						char str[72];
-						int length = x - gTS.p_read;
-						memcpy(str, &gTS.data[gTS.p_read], length);
-						str[length] = 0;
-						
-						gTS.p_write = x;
-						
-						//Print text
-						PutText2(0, 0, str, 0xFFFFFE, gTS.line % 4 + 30);
-						sprintf(&text[gTS.line % 4 * 0x40], str);
-						
-						//Check if should move to next line (prevent a memory overflow, come on guys, this isn't a leftover of pixel trying to make text wrapping)
-						gTS.p_read += length;
-
-						if (gTS.p_write > 34)
-							CheckNewLine();
-						
-						bExit = true;
-					}
-					else
-					{
-						//Get text to print
-						char c[3];
-						c[0] = gTS.data[gTS.p_read];
-						
-						if (!(c[0] & 0x80))
-						{
-							c[1] = 0;
-						}
-						else
-						{
-							c[1] = gTS.data[gTS.p_read + 1];
-							c[2] = 0;
-						}
-						
-						//Print text
-						if (c[0] == '=')
-						{
-							Surface2Surface(6 * gTS.p_write, 2, &rcSymbol, gTS.line % 4 + 30, 26);
-						}
-						else
-						{
-							PutText2(6 * gTS.p_write, 0, c, 0xFFFFFE, gTS.line % 4 + 30);
-						}
-						
-						strcat(&text[gTS.line % 4 * 0x40], c);
-						PlaySoundObject(2, 1);
-						gTS.wait_beam = 0;
-						
-						//Offset read and write positions
-						if (!(c[0] & 0x80))
-						{
-							gTS.p_read++;
-							gTS.p_write++;
-						}
-						else
-						{
-							gTS.p_read += 2;
-							gTS.p_write += 2;
-						}
-						
-						if (gTS.p_write > 34)
-						{
-							CheckNewLine();
-							gTS.p_write = 0;
-							++gTS.line;
-							CheckNewLine();
-						}
-						
-						bExit = true;
-					}
-				}
-				else
-				{
 					if (IS_COMMAND('E','N','D'))
 					{
 						gTS.mode = 0;
@@ -614,7 +538,7 @@
 						gMC.cond &= ~1;
 						g_GameFlags |= 3;
 						gTS.face = 0;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('L','I','+'))
 					{
@@ -724,7 +648,7 @@
 					{
 						x = GetTextScriptNo(gTS.p_read + 4);
 						y = GetTextScriptNo(gTS.p_read + 9);
-						SetMyCharPosition(x << 13, y << 13);
+						SetMyCharPosition(x * 0x200 * 0x10, y * 0x200 * 0x10);
 						gTS.p_read += 13;
 					}
 					else if (IS_COMMAND('H','M','C'))
@@ -784,7 +708,7 @@
 					{
 						gTS.mode = 2;
 						gTS.p_read += 4;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('C','L','R'))
 					{
@@ -799,28 +723,28 @@
 						if (gTS.flags & 0x40)
 							gTS.flags |= 0x10;
 						gTS.p_read += 4;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('M','S','2'))
 					{
 						ClearTextLine();
-						gTS.flags |= 0x21;
 						gTS.flags &= ~0x12;
+						gTS.flags |= 0x21;
 						if (gTS.flags & 0x40)
 							gTS.flags |= 0x10;
 						gTS.face = 0;
 						gTS.p_read += 4;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('M','S','3'))
 					{
 						ClearTextLine();
-						gTS.flags |= 0x23;
 						gTS.flags &= ~0x10;
+						gTS.flags |= 0x23;
 						if (gTS.flags & 0x40)
 							gTS.flags |= 0x10;
 						gTS.p_read += 4;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('W','A','I'))
 					{
@@ -827,13 +751,13 @@
 						gTS.mode = 4;
 						gTS.wait_next = GetTextScriptNo(gTS.p_read + 4);
 						gTS.p_read += 8;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('W','A','S'))
 					{
 						gTS.mode = 7;
 						gTS.p_read += 4;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('T','U','R'))
 					{
@@ -868,13 +792,13 @@
 						PlaySoundObject(5, 1);
 						gTS.wait = 0;
 						gTS.select = 0;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('F','L','J'))
 					{
 						x = GetTextScriptNo(gTS.p_read + 4);
 						z = GetTextScriptNo(gTS.p_read + 9);
-						
+
 						if (GetNPCFlag(x))
 							JumpTextScript(z);
 						else
@@ -884,7 +808,7 @@
 					{
 						x = GetTextScriptNo(gTS.p_read + 4);
 						z = GetTextScriptNo(gTS.p_read + 9);
-						
+
 						if (GetSkipFlag(x))
 							JumpTextScript(z);
 						else
@@ -959,7 +883,7 @@
 					else if (IS_COMMAND('S','P','S'))
 					{
 						SetNoise(2, x);
-						gTS.p_read += 8;
+						gTS.p_read += 4;
 					}
 					else if (IS_COMMAND('C','P','S'))
 					{
@@ -983,7 +907,7 @@
 						StartFadeIn(z);
 						gTS.mode = 5;
 						gTS.p_read += 8;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('F','A','O'))
 					{
@@ -991,7 +915,7 @@
 						StartFadeOut(z);
 						gTS.mode = 5;
 						gTS.p_read += 8;
-						bExit = true;
+						bExit = TRUE;
 					}
 					else if (IS_COMMAND('M','N','A'))
 					{
@@ -1043,23 +967,27 @@
 					else if (IS_COMMAND('M','L','P'))
 					{
 						gTS.p_read += 4;
-						bExit = true;
-						
-						int tscRet = MiniMapLoop();
-						if (tscRet == 0)
-							return 0;
-						if (tscRet == 2)
-							return 2;
+						bExit = TRUE;
+
+						switch (MiniMapLoop())
+						{
+							case 0:
+								return 0;
+							case 2:
+								return 2;
+						}
 					}
 					else if (IS_COMMAND('S','L','P'))
 					{
-						bExit = true;
+						bExit = TRUE;
 
-						int selRet = StageSelectLoop(&z);
-						if (selRet == 0)
-							return 0;
-						if (selRet == 2)
-							return 2;
+						switch (StageSelectLoop(&z))
+						{
+							case 0:
+								return 0;
+							case 2:
+								return 2;
+						}
 
 						JumpTextScript(z);
 						g_GameFlags &= ~3;
@@ -1112,7 +1040,7 @@
 						x = GetTextScriptNo(gTS.p_read + 9);
 						y = GetTextScriptNo(gTS.p_read + 14);
 						z = GetTextScriptNo(gTS.p_read + 19);
-						SetNpChar(w, x << 13, y << 13, 0, 0, z, 0, 0x100);
+						SetNpChar(w, x * 0x200 * 0x10, y * 0x200 * 0x10, 0, 0, z, 0, 0x100);
 						gTS.p_read += 23;
 					}
 					else if (IS_COMMAND('M','N','P'))
@@ -1121,7 +1049,7 @@
 						x = GetTextScriptNo(gTS.p_read + 9);
 						y = GetTextScriptNo(gTS.p_read + 14);
 						z = GetTextScriptNo(gTS.p_read + 19);
-						MoveNpChar(w, x << 13, y << 13, z);
+						MoveNpChar(w, x * 0x200 * 0x10, y * 0x200 * 0x10, z);
 						gTS.p_read += 23;
 					}
 					else if (IS_COMMAND('S','M','P'))
@@ -1185,10 +1113,10 @@
 					else if (IS_COMMAND('F','A','C'))
 					{
 						z = GetTextScriptNo(gTS.p_read + 4);
-						if (gTS.face != z)
+						if (gTS.face != (signed char)z)
 						{
-							gTS.face = z;
-							gTS.face_x = (gTS.rcText.left - 48) << 9;
+							gTS.face = (signed char)z;
+							gTS.face_x = (WINDOW_WIDTH / 2 - 156) * 0x200;
 						}
 						gTS.p_read += 8;
 					}
@@ -1195,10 +1123,10 @@
 					else if (IS_COMMAND('F','A','C'))
 					{
 						z = GetTextScriptNo(gTS.p_read + 4);
-						if (gTS.face != z)
+						if (gTS.face != (signed char)z)
 						{
-							gTS.face = z;
-							gTS.face_x = (gTS.rcText.left - 48) << 9;
+							gTS.face = (signed char)z;
+							gTS.face_x = (WINDOW_WIDTH / 2 - 156) * 0x200;
 						}
 						gTS.p_read += 8;
 					}
@@ -1234,13 +1162,17 @@
 					}
 					else if (IS_COMMAND('X','X','1'))
 					{
-						bExit = true;
+						bExit = TRUE;
 						z = GetTextScriptNo(gTS.p_read + 4);
-						int islRet = Scene_DownIsland(z);
-						if (islRet == 0)
-							return 0;
-						if (islRet == 2)
-							return 2;
+
+						switch (Scene_DownIsland(z))
+						{
+							case 0:
+								return 0;
+							case 2:
+								return 2;
+						}
+
 						gTS.p_read += 8;
 					}
 					else if (IS_COMMAND('E','S','C'))
@@ -1254,63 +1186,168 @@
 						return 1;
 					}
 				}
+				else
+				{
+					if (gTS.data[gTS.p_read] == '\r')
+					{
+						//Go to new-line
+						gTS.p_read += 2;
+						gTS.p_write = 0;
+
+						if (gTS.flags & 1)
+						{
+							++gTS.line;
+							CheckNewLine();
+						}
+					}
+					else if (gTS.flags & 0x10)
+					{
+						//SAT/CAT/TUR printing
+						x = gTS.p_read;
+						//Break if reaches command, or new-line
+						while (gTS.data[x] != '<' && gTS.data[x] != '\r')
+						{
+							//Skip if SHIFT-JIS
+							if (gTS.data[x] & 0x80)
+								++x;
+
+							++x;
+						}
+
+						//Get text to copy
+						char str[72];
+						int length = x - gTS.p_read;
+						memcpy(str, &gTS.data[gTS.p_read], length);
+						str[length] = 0;
+
+						gTS.p_write = x;
+
+						//Print text
+						PutText2(0, 0, str, 0xFFFFFE, gTS.line % 4 + 30);
+						sprintf(&text[gTS.line % 4 * 0x40], str);
+
+						//Check if should move to next line (prevent a memory overflow, come on guys, this isn't a leftover of pixel trying to make text wrapping)
+						gTS.p_read += length;
+
+						if (gTS.p_write >= 35)
+							CheckNewLine();
+
+						bExit = TRUE;
+					}
+					else
+					{
+						//Get text to print
+						char c[3];
+						c[0] = gTS.data[gTS.p_read];
+
+						if (c[0] & 0x80)
+						{
+							c[1] = gTS.data[gTS.p_read + 1];
+							c[2] = 0;
+						}
+						else
+						{
+							c[1] = 0;
+						}
+
+						//Print text
+						if (c[0] == '=')
+						{
+							Surface2Surface(6 * gTS.p_write, 2, &rcSymbol, gTS.line % 4 + 30, 26);
+						}
+						else
+						{
+							PutText2(6 * gTS.p_write, 0, c, 0xFFFFFE, gTS.line % 4 + 30);
+						}
+
+						strcat(&text[gTS.line % 4 * 0x40], c);
+						PlaySoundObject(2, 1);
+						gTS.wait_beam = 0;
+
+						//Offset read and write positions
+						if (c[0] & 0x80)
+						{
+							gTS.p_read += 2;
+							gTS.p_write += 2;
+						}
+						else
+						{
+							gTS.p_read++;
+							gTS.p_write++;
+						}
+
+						if (gTS.p_write >= 35)
+						{
+							CheckNewLine();
+							gTS.p_write = 0;
+							++gTS.line;
+							CheckNewLine();
+						}
+
+						bExit = TRUE;
+					}
+				}
 			}
 			break;
-			
+
 		case 2: //NOD
 			if ((gKeyCancel | gKeyOk) & gKeyTrg)
 				gTS.mode = 1;
 			break;
-			
+
 		case 3: //NEW LINE
 			for (int i = 0; i < 4; i++)
 			{
 				gTS.ypos_line[i] -= 4;
-				
+
 				if (!gTS.ypos_line[i])
 					gTS.mode = 1;
-				
+
 				if (gTS.ypos_line[i] == -16)
 					gTS.ypos_line[i] = 48;
 			}
 			break;
-			
+
 		case 4: //WAI
-			if (gTS.wait_next != 9999)
-			{
-				if (gTS.wait != 9999)
-					++gTS.wait;
-				
-				if (gTS.wait >= gTS.wait_next)
-				{
-					gTS.mode = 1;
-					gTS.wait_beam = 0;
-				}
-			}
+			if (gTS.wait_next == 9999)
+				break;
+
+			if (gTS.wait != 9999)
+				++gTS.wait;
+
+			if (gTS.wait < gTS.wait_next)
+				break;
+
+			gTS.mode = 1;
+			gTS.wait_beam = 0;
 			break;
-			
+
 		case 5: //FAI/FAO
-			if (!GetFadeActive())
-			{
-				gTS.mode = 1;
-				gTS.wait_beam = 0;
-			}
+			if (GetFadeActive())
+				break;
+
+			gTS.mode = 1;
+			gTS.wait_beam = 0;
 			break;
 		case 7: //WAS
-			if (gMC.flag & 8)
-			{
-				gTS.mode = 1;
-				gTS.wait_beam = 0;
-			}
+			if ((gMC.flag & 8) == 0)
 			break;
+
+			gTS.mode = 1;
+			gTS.wait_beam = 0;
+			break;
 		case 6: //YNJ
-			if (gTS.wait >= 16)
+			if (gTS.wait < 16)
 			{
+				gTS.wait++;
+			}
+			else
+			{
 				//Select option
 				if (gKeyTrg & gKeyOk)
 				{
 					PlaySoundObject(18, 1);
-					
+
 					if (gTS.select == 1)
 					{
 						JumpTextScript(gTS.next_event);
@@ -1334,16 +1371,12 @@
 					PlaySoundObject(1, 1);
 				}
 			}
-			else
-			{
-				gTS.wait++;
-			}
 			break;
 	}
-	
-	if (gTS.mode)
-		g_GameFlags |= 4;
-	else
+
+	if (gTS.mode == 0)
 		g_GameFlags &= ~4;
+	else
+		g_GameFlags |= 4;
 	return 1;
 }
--- a/src/TextScr.h
+++ b/src/TextScr.h
@@ -54,13 +54,13 @@
 	uint8_t wait_beam;
 };
 
-bool InitTextScript2();
+BOOL InitTextScript2();
 void EndTextScript();
 void EncryptionBinaryData2(uint8_t *pData, int size);
 bool LoadTextScript2(const char *name);
 bool LoadTextScript_Stage(char *name);
 void GetTextScriptPath(char *path);
-bool StartTextScript(int no);
+BOOL StartTextScript(int no);
 void StopTextScript();
 void PutTextScript();
 int TextScriptProc();
\ No newline at end of file