// Dialogue Handler.
// Worthless Bums 2012.


function setPortholeSpaceDialogue (%thisObject)
{//echo("setPortholeSpaceDialogue()");// NOTE: Why is %thisObject not the correct marine object?!?
	if (getRandom(0, 100) <= $PortholeSpaceDialogueChance)
	{
		%dialogueString = $PortholeDialogueList[getRandom(0, $PortholeDialogueListLength - 1)];
		setIndividualDialoguePopup($playerSquad[$activeCharacter], %dialogueString, "dialogue");
	}
}
function setNoAmmoDialogue (%thisObject)
{//echo("setNoAmmoDialogue()");
	%dialogueString = $NoAmmoDialogueList[getRandom(0, $NoAmmoDialogueListLength - 1)];
	setIndividualDialoguePopup(%thisObject, %dialogueString, "dialogue");
}
function setWeaponReloadDialogue (%thisObject)
{//echo("setWeaponReloadDialogue()");
	%dialogueString = $ReloadDialogueList[getRandom(0, $ReloadDialogueListLength - 1)];
	setIndividualDialoguePopup(%thisObject, %dialogueString, "dialogue");
}
function setKnockedIntoSpaceDialogue (%thisObject)//setKnockedIntoSpaceDialogue($playerSquad[$activeCharacter]);
{//echo("setKnockedIntoSpaceDialogue()");
	%dialogueString = $KnockedIntoSpaceDialogueList[getRandom(0, $KnockedIntoSpaceDialogueListLength - 1)];
	setIndividualDialoguePopup(%thisObject, %dialogueString, "dialogue");
}
function setCryoTubeDialogue (%thisObject)
{//echo("setCryoTubeDialogue()");
	%dialogueString = $AlienMerchantDialogueList[getRandom(0, $AlienMerchantDialogueListLength - 1)];
	setIndividualDialoguePopup(%thisObject, %dialogueString, "dialogue");
}
function setIndividualActionPopup (%target, %text, %textType)
{//echo("setIndividualActionPopup()");
//return;		// Uncomment for trailer footage!
	%xPosition = getWord(%target.getPosition(), 0);
	%yPosition = getWord(%target.getPosition(), 1);
	%text = %text;
	if (isObject(%target.dialoguePopup))
	{
		(%target.dialoguePopup).safedelete();
	}
	%moveSpeed = 12.0;
	%lifetime = 4.0;
	%foregroundSceneLayer = 0;
	%foregroundTextColor = "255 255 255";
	%backgroundSceneLayer = 0;
	%backgroundTextColor = "0 0 100";
	// Delete background text.
	if (isObject(%target.dialogueBackgroundPopup))
	{
		(%target.dialogueBackgroundPopup).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup2))
	{
		(%target.dialogueBackgroundPopup2).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup3))
	{
		(%target.dialogueBackgroundPopup3).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup4))
	{
		(%target.dialogueBackgroundPopup4).safeDelete();
	}
	// Up.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText = new ImageFont();
			%backgroundFloatingText.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText.Image = $RenderedFontImage;
			%backgroundFloatingText.FontSize = $RenderedFontSize;
			%backgroundFloatingText.BlendColor = %backgroundTextColor;
			%backgroundFloatingText.Text = %text;
			%backgroundFloatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 - $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText);
			
			%target.dialogueBackgroundPopup = %backgroundFloatingText;
	}
	//%backgroundFloatingText.moveTo(%xPosition, %yPosition - 3.1, %moveSpeed, true, false);
	%backgroundFloatingText.setLifetime(%lifetime);
	// Down.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText2 = new ImageFont();
			%backgroundFloatingText2.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText2.Image = $RenderedFontImage;
			%backgroundFloatingText2.FontSize = $RenderedFontSize;
			%backgroundFloatingText2.BlendColor = %backgroundTextColor;
			%backgroundFloatingText2.Text = %text;
			%backgroundFloatingText2.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 + $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText2);
			
			%target.dialogueBackgroundPopup2 = %backgroundFloatingText2;
	}
	//%backgroundFloatingText2.moveTo(%xPosition, %yPosition - 2.9, %moveSpeed, true, false);
	%backgroundFloatingText2.setLifetime(%lifetime);
	// Left.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText3 = new ImageFont();
			%backgroundFloatingText3.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText3.Image = $RenderedFontImage;
			%backgroundFloatingText3.FontSize = $RenderedFontSize;
			%backgroundFloatingText3.BlendColor = %backgroundTextColor;
			%backgroundFloatingText3.Text = %text;
			%backgroundFloatingText3.Position = getWord(%target.position, 0) - $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText3);
			
			%target.dialogueBackgroundPopup3 = %backgroundFloatingText3;
	}
	//%backgroundFloatingText3.moveTo(%xPosition - 0.1, %yPosition - 3.0, %moveSpeed, true, false);
	%backgroundFloatingText3.setLifetime(%lifetime);
	// Right.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText4 = new ImageFont();
			%backgroundFloatingText4.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText4.Image = $RenderedFontImage;
			%backgroundFloatingText4.FontSize = $RenderedFontSize;
			%backgroundFloatingText4.BlendColor = %backgroundTextColor;
			%backgroundFloatingText4.Text = %text;
			%backgroundFloatingText4.Position = getWord(%target.position, 0) + $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText4);
			
			%target.dialogueBackgroundPopup4 = %backgroundFloatingText4;
	}
	//%backgroundFloatingText4.moveTo(%xPosition + 0.1, %yPosition - 3.0, %moveSpeed, true, false);
	%backgroundFloatingText4.setLifetime(%lifetime);
	switch$ (%textType)
	{
		case "dialogue":
			%floatingText = new ImageFont();
			%floatingText.SceneLayer = $RenderedFontSceneLayer;
			%floatingText.Image = $RenderedFontImage;
			%floatingText.FontSize = $RenderedFontSize;
			%floatingText.BlendColor = %foregroundTextColor;
			%floatingText.Text = %text;
			%floatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%floatingText);
			
			%target.dialoguePopup = %floatingText;
	}
	//%floatingText.moveTo(%xPosition, %yPosition - 3, %moveSpeed, true, false);
	%floatingText.setLifetime(%lifetime);
}
function setIndividualDialoguePopup (%target, %text, %textType)
{//echo("setIndividualDialoguePopup()");
//return;		// Uncomment for trailer footage!
	updateEventText(%target.name @ ": " @ %text);
	%xPosition = getWord(%target.getPosition(), 0);
	%yPosition = getWord(%target.getPosition(), 1);
	%text = %target.name @ ": " @ %text;
	if (isObject(%target.dialoguePopup))
	{
		(%target.dialoguePopup).safedelete();
	}
	%moveSpeed = 12.0;
	%lifetime = 4.0;
	%foregroundSceneLayer = 0;
	%foregroundTextColor = "255 255 255";
	%backgroundSceneLayer = 0;
	%backgroundTextColor = "0 0 100";
	// Delete background text.
	if (isObject(%target.dialogueBackgroundPopup))
	{
		(%target.dialogueBackgroundPopup).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup2))
	{
		(%target.dialogueBackgroundPopup2).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup3))
	{
		(%target.dialogueBackgroundPopup3).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup4))
	{
		(%target.dialogueBackgroundPopup4).safeDelete();
	}
	// Up.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText = new ImageFont();
			%backgroundFloatingText.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText.Image = $RenderedFontImage;
			%backgroundFloatingText.FontSize = $RenderedFontSize;
			%backgroundFloatingText.BlendColor = %backgroundTextColor;
			%backgroundFloatingText.Text = %text;
			%backgroundFloatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 - $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText);
		
			%target.dialogueBackgroundPopup = %backgroundFloatingText;
	}
	//%backgroundFloatingText.moveTo(%xPosition, %yPosition - 3.1, %moveSpeed, true, false);
	%backgroundFloatingText.setLifetime(%lifetime);
	// Down.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText2 = new ImageFont();
			%backgroundFloatingText2.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText2.Image = $RenderedFontImage;
			%backgroundFloatingText2.FontSize = $RenderedFontSize;
			%backgroundFloatingText2.BlendColor = %backgroundTextColor;
			%backgroundFloatingText2.Text = %text;
			%backgroundFloatingText2.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 + $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText2);
			
			%target.dialogueBackgroundPopup2 = %backgroundFloatingText2;
	}
	//%backgroundFloatingText2.moveTo(%xPosition, %yPosition - 2.9, %moveSpeed, true, false);
	%backgroundFloatingText2.setLifetime(%lifetime);
	// Left.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText3 = new ImageFont();
			%backgroundFloatingText3.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText3.Image = $RenderedFontImage;
			%backgroundFloatingText3.FontSize = $RenderedFontSize;
			%backgroundFloatingText3.BlendColor = %backgroundTextColor;
			%backgroundFloatingText3.Text = %text;
			%backgroundFloatingText3.Position = getWord(%target.position, 0) - $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText3);
			
			%target.dialogueBackgroundPopup3 = %backgroundFloatingText3;
	}
	//%backgroundFloatingText3.moveTo(%xPosition - 0.1, %yPosition - 3.0, %moveSpeed, true, false);
	%backgroundFloatingText3.setLifetime(%lifetime);
	// Right.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText4 = new ImageFont();
			%backgroundFloatingText4.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText4.Image = $RenderedFontImage;
			%backgroundFloatingText4.FontSize = $RenderedFontSize;
			%backgroundFloatingText4.BlendColor = %backgroundTextColor;
			%backgroundFloatingText4.Text = %text;
			%backgroundFloatingText4.Position = getWord(%target.position, 0) + $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText4);
			
			%target.dialogueBackgroundPopup4 = %backgroundFloatingText4;
	}
	//%backgroundFloatingText4.moveTo(%xPosition + 0.1, %yPosition - 3.0, %moveSpeed, true, false);
	%backgroundFloatingText4.setLifetime(%lifetime);
	switch$ (%textType)
	{
		case "dialogue":
			%floatingText = new ImageFont();
			%floatingText.SceneLayer = $RenderedFontSceneLayer;
			%floatingText.Image = $RenderedFontImage;
			%floatingText.FontSize = $RenderedFontSize;
			%floatingText.BlendColor = %foregroundTextColor;
			%floatingText.Text = %text;
			%floatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%floatingText);
			
			%target.dialoguePopup = %floatingText;
	}
	//%floatingText.moveTo(%xPosition, %yPosition - 3, %moveSpeed, true, false);
	%floatingText.setLifetime(%lifetime);
}
function executeNewGameDialogue ()
{//echo("executeNewGameDialogue()");
	if ($generateTutorialLevel)
	{
		return;
	}
	if ($NewGamePlus <= 0)
	{
		%dialogueRoll = getRandom(0, $NewGameChatterDialogueListLength / 4 - 1);
		%dialogueRoll *= 4;
		schedule( 2000, 0, "setIndividualDialoguePopup", $playerSquad[0], $NewGameChatterDialogueList[%dialogueRoll], 	  "dialogue");
		schedule( 6000, 0, "setIndividualDialoguePopup", $playerSquad[1], $NewGameChatterDialogueList[%dialogueRoll + 1], "dialogue");
		schedule(10000, 0, "setIndividualDialoguePopup", $playerSquad[2], $NewGameChatterDialogueList[%dialogueRoll + 2], "dialogue");
		schedule(14000, 0, "setIndividualDialoguePopup", $playerSquad[3], $NewGameChatterDialogueList[%dialogueRoll + 3], "dialogue");
	}
	else
	{
		// NOTE: Starting New Game Plus means not necessarily all marines are alive! Need to take that into account!
		%squadSize = 0;
		%marineA = -1;
		%marineB = -1;
		%marineC = -1;
		%marineD = -1;
		if ($leaderPersist)
		{
			%squadSize++;
			%marineA = 0;
		}
		if ($scoutPersist)
		{
			%squadSize++;
			if (%marineA == -1)
			{
				%marineA = 1;
			}
			else
			{
				%marineB = 1;
			}
		}
		if ($grenadierPersist)
		{
			%squadSize++;
			if (%marineA == -1)
			{
				%marineA = 2;
			}
			else if (%marineB == -1)
			{
				%marineB = 2;
			}
			else
			{
				%marineC = 2;
			}
		}
		if ($supportPersist)
		{
			%squadSize++;
			if (%marineA == -1)
			{
				%marineA = 3;
			}
			else if (%marineB == -1)
			{
				%marineB = 3;
			}
			else if (%marineC == -1)
			{
				%marineC = 3;
			}
			else
			{
				%marineD = 3;
			}
		}
		%dialogueRoll = getRandom(0, $NewGamePlusChatterDialogueListLength / 4 - 1);
		if (%marineA != -1)
		{
			schedule( 2000, 0, "setIndividualDialoguePopup", $playerSquad[%marineA], $NewGamePlusChatterDialogueList[%dialogueRoll], "dialogue");
		}
		if (%marineB != -1)
		{
			schedule( 6000, 0, "setIndividualDialoguePopup", $playerSquad[%marineB], $NewGamePlusChatterDialogueList[%dialogueRoll + 1], "dialogue");
		}
		if (%marineC != -1)
		{
			schedule(10000, 0, "setIndividualDialoguePopup", $playerSquad[%marineC], $NewGamePlusChatterDialogueList[%dialogueRoll + 2], "dialogue");
		}
		if (%marineD != -1)
		{
			schedule(14000, 0, "setIndividualDialoguePopup", $playerSquad[%marineD], $NewGamePlusChatterDialogueList[%dialogueRoll + 3], "dialogue");
		}
	}
}
function enemySpottedChatter (%characterIndex, %isHuman, %isRobot, %isAlien)
{//echo("enemySpottedChatter()");
	%dialogueString = $EnemySpottedDialogueList[getRandom(0, $EnemySpottedDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function enemyDownChatter (%characterIndex, %isHuman, %isRobot, %isAlien)
{//echo("enemyDownChatter()");
	%dialogueString = $EnemyDownDialogueList[getRandom(0, $EnemyDownDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function randomLockersChatter (%characterIndex)
{//echo("randomLockersChatter()");
	%dialogueString = $LockerChatterDialogueList[getRandom(0, $LockerChatterDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function randomCantUseModUpgradeChatter (%characterIndex)
{//echo("randomCantUseModUpgradeChatter()");
	%dialogueString = $CantUseUpgradeDialogueList[getRandom(0, $CantUseUpgradeDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function randomLaserModChatter (%characterIndex)
{//echo("randomLaserModChatter()");
	%dialogueString = $LaserModDialogueList[getRandom(0, $LaserModDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function randomPlasmaModChatter (%characterIndex)
{//echo("randomPlasmaModChatter()");
	%dialogueString = $PlasmaModDialogueList[getRandom(0, $PlasmaModDialogueListLength - 1)];
	setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogueString, "dialogue");
}
function randomDeathText (%characterIndex)
{//echo("randomDeathText()");
	%dialogueString = $MarineDeathDialogueList[getRandom(0, $MarineDeathDialogueListLength - 1)];
	setIndividualActionPopup($playerSquad[%characterIndex], "\"" @ $playerSquad[%characterIndex].name SPC %dialogueString @ "\"", "dialogue");
}