#include "diablo.h"

char msgtable[80];
char msgdelay; // weak
char msgflag; // weak
char msgcnt; // weak

char *MsgStrings[44] =
{
  "",
  "No automap available in town",
  "No multiplayer functions in demo",
  "Direct Sound Creation Failed",
  "Not available in shareware version",
  "Not enough space to save",
  "No Pause in town",
  "Copying to a hard disk is recommended",
  "Multiplayer sync problem",
  "No pause in multiplayer",
  "Loading...",
  "Saving...",
  "Some are weakened as one grows strong",
  "New strength is forged through destruction",
  "Those who defend seldom attack",
  "The sword of justice is swift and sharp",
  "While the spirit is vigilant the body thrives",
  "The powers of mana refocused renews",
  "Time cannot diminish the power of steel",
  "Magic is not always what it seems to be",
  "What once was opened now is closed",
  "Intensity comes at the cost of wisdom",
  "Arcane power brings destruction",
  "That which cannot be held cannot be harmed",
  "Crimson and Azure become as the sun",
  "Knowledge and wisdom at the cost of self",
  "Drink and be refreshed",
  "Wherever you go, there you are",
  "Energy comes at the cost of wisdom",
  "Riches abound when least expected",
  "Where avarice fails, patience gains reward",
  "Blessed by a benevolent companion!",
  "The hands of men may be guided by fate",
  "Strength is bolstered by heavenly faith",
  "The essence of life flows from within",
  "The way is made clear when viewed from above",
  "Salvation comes at the cost of wisdom",
  "Mysteries are revealed in the light of reason",
  "Those who are last may yet be first",
  "Generosity brings its own rewards",
  "You must be at least level 8 to use this.",
  "You must be at least level 13 to use this.",
  "You must be at least level 17 to use this.",
  "Arcane knowledge gained!"
};

void InitDiabloMsg(char e)
{
	int i;

	for(i = 0; i < msgcnt; i++) {
		if(msgtable[i] == e) {
			return;
		}
	}

	msgtable[msgcnt] = e;

	if(msgcnt < 80) {
		msgcnt++;
	}

	msgflag = msgtable[0];
	msgdelay = 70;
}

void ClrDiabloMsg()
{
	int i;

	for(i = 0; i < 80; i++) {
		msgtable[i] = 0;
	}

	msgflag = 0;
	msgcnt = 0;
}

void DrawDiabloMsg()
{
	int i, len, off, width, sx, sy;
	BYTE c;

	CelDecodeOnly(165, 318, (BYTE *)pSTextSlidCels, 1, 12);
	CelDecodeOnly(591, 318, (BYTE *)pSTextSlidCels, 4, 12);
	CelDecodeOnly(165, 366, (BYTE *)pSTextSlidCels, 2, 12);
	CelDecodeOnly(591, 366, (BYTE *)pSTextSlidCels, 3, 12);

	sx = 173;
	for(i = 0; i < 35; i++) {
		CelDecodeOnly(sx, 318, (BYTE *)pSTextSlidCels, 5, 12);
		CelDecodeOnly(sx, 366, (BYTE *)pSTextSlidCels, 7, 12);
		sx += 12;
	}
	sy = 330;
	for(i = 0; i < 3; i++) {
		CelDecodeOnly(165, sy, (BYTE *)pSTextSlidCels, 6, 12);
		CelDecodeOnly(591, sy, (BYTE *)pSTextSlidCels, 8, 12);
		sy += 12;
	}

	/// ASSERT: assert(gpBuffer);

#ifdef USE_ASM
	__asm {
		mov		edi, gpBuffer
		add		edi, SCREENXY(104, 203)
		xor		eax, eax
		mov		edx, 54 / 2
	label1:
		mov		ecx, 432 / 2
	label2:
		stosb
		inc		edi
		loop	label2
		sub		edi, BUFFER_WIDTH + 432
		mov		ecx, 432 / 2
	label3:
		inc		edi
		stosb
		loop	label3
		sub		edi, BUFFER_WIDTH + 432
		dec		edx
		jnz		label1
	}
#else
	int wdt, hgt;
	BYTE *dst;

	dst = &gpBuffer[SCREENXY(104, 203)];

	for(hgt = 54 / 2; hgt != 0; hgt--) {
		for(wdt = 432 / 2; wdt != 0; wdt--) {
			dst[0] = 0;
			dst += 2;
		}
		dst -= BUFFER_WIDTH + 432;
		for(wdt = 432 / 2; wdt != 0; wdt--) {
			dst[1] = 0;
			dst += 2;
		}
		dst -= BUFFER_WIDTH + 432;
	}
#endif

	strcpy(tempstr, MsgStrings[msgflag]);
	off = 165 + PitchTbl[342];
	len = strlen(tempstr);
	width = 0;

	for(i = 0; i < len; i++) {
		width += fontkern[fontframe[gbFontTransTbl[(BYTE)tempstr[i]]]] + 1;
	}

	if(width < 442) {
		off += (442 - width) >> 1;
	}

	for(i = 0; i < len; i++) {
		c = fontframe[gbFontTransTbl[(BYTE)tempstr[i]]];
		if(c != '\0') {
			PrintChar(off, c, COL_GOLD);
		}
		off += fontkern[c] + 1;
	}

	if(msgdelay > 0) {
		msgdelay--;
	}
	if(msgdelay == 0) {
		msgcnt--;
		msgdelay = 70;
		if(msgcnt == 0) {
			msgflag = 0;
		} else {
			msgflag = msgtable[msgcnt];
		}
	}
}
