////////////////////////////////////////////////////////////////////////////////
// logpositiondata.cs
////////////////////////////////////////////////////////////////////////////////

// all global variable and functions are prefixed logpos_

new fileObject("logpos_file");

package t2matchlog {

////////////////////////////////////////////////////////////////////////////////

function DefaultGame::startMatch(%game)
{
	$logpos_flag_count = 0;

	%file = "positiondata/" @ $missionSequence @ "_" @ $CurrentMissionType @
		"_" @ $currentMission @ "_";
	%i = 0;
	while (isFile(%file @ %i))
		%i++;
	logpos_file.openForWrite(%file @ %i);

	logpos_file.writeLine($CurrentMission);
	logpos_file.writeLine($CurrentMissionType);
	logpos_file.writeLine($MissionDisplayName);
	logpos_file.writeLine($MissionTypeDisplayName);
	logpos_file.writeLine(MissionArea.area);
	logpos_dump_landmarks(MissionGroup);
	logpos_file.writeLine("");

	for (%ci = 0; %ci != ClientGroup.getCount(); %ci++) {
		%client = ClientGroup.getObject(%ci);
		if (%client.player) {
			%name = stripChars(detag(getTaggedString(%client.name)),
					"\cp\co\c6\c7\c8\c9");
			logpos_file.writeLine("S " @ %client SPC
					%client.player.team SPC %name);
		}
	}

	$logpos_vehicle_count = 0;
	$logpos_gameover = false;
	schedule(1000, 0, logpos_write_position_data);

	Parent::startMatch(%game);
}

////////////////////////////////////////////////////////////////////////////////

function DefaultGame::gameOver(%game)
{
	for (%ci = 0; %ci != ClientGroup.getCount(); %ci++) {
		%client = ClientGroup.getObject(%ci);
		logpos_file.writeLine("L " @ %client SPC %client.score);
	}
	for (%team = 1; %team <= %game.numTeams; %team++)
		logpos_file.writeLine("Z " @ %team SPC $TeamScore[%team] SPC
				getTaggedString(%game.getTeamName(%team)));
	logpos_file.close();
	$logpos_gameover = true;

	Parent::gameOver(%game);
}

////////////////////////////////////////////////////////////////////////////////

function logpos_write_position_data()
{
	if ($logpos_gameover)
		return;

    // flags
    for (%i = 0; %i != $logpos_flag_count; %i++)
    {
        %flag = $logpos_flag[%i];
        if (%flag.carrier $= "")
            logpos_file.writeLine("F " @ %flag.team SPC %flag.position);
        else
            logpos_file.writeLine("F " @ %flag.team SPC %flag.carrier.client);
    }

    // players
    for (%ci = 0; %ci != ClientGroup.getCount(); %ci++)
    {
        %client = ClientGroup.getObject(%ci);
	if (!%client.player)
		continue;
	%vehicle = %client.player.getObjectMount();
	%seat = "";
	if (%client.player.isPilot()) { %seat = "p"; }
	if (%client.player.isWeaponOperator()) { %seat = "w"; }
        %data = %client.player.position SPC %vehicle SPC %seat;
        logpos_file.writeLine(%client SPC %data);
    }

	// vehicles
	for (%i = 0; %i != $logpos_vehicle_count; %i++) {
		%vehicle = $logpos_vehicle[%i];
		if (%vehicle)
			logpos_file.writeLine(%vehicle SPC %vehicle.position);
	}

    // terminate each set of position coordinates with a blank line
    logpos_file.writeLine("");

    schedule(1000, 0, logpos_write_position_data);
}

////////////////////////////////////////////////////////////////////////////////

function logpos_dump_landmarks(%group)
{
    for (%i = 0; %i < %group.getCount(); %i++)
    {
        %obj = %group.getObject(%i);
        %type = %obj.getClassName();

        if (%type $= "SimGroup")
        {
            logpos_dump_landmarks(%obj);
        }
        else if (%type $= "Item" && %obj.dataBlock $= "Flag")
        {
            $logpos_flag[$logpos_flag_count] = %obj;
            $logpos_flag_count++;
        }
        else if (%obj.team !$= "")
        {
            %data = %type SPC %obj.dataBlock SPC %obj.getName();
            %data = %data SPC %obj.position SPC %obj.name;
            logpos_file.writeLine(%obj.team SPC %data);
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

function serverCmdSuicide(%client)
{
	logpos_file.writeLine("serverCmdSuicide " @ %client);
	Parent::serverCmdSuicide(%client);
}

function vehicleListAdd(%blockName, %obj)
{
	logpos_file.writeLine("V " @ %obj SPC %obj.team SPC %blockName);
	$logpos_vehicle[$logpos_vehicle_count] = %obj;
	$logpos_vehicle_count++;
	Parent::vehicleListAdd(%blockName, %obj);
}

function vehicleListRemove(%data, %obj)
{
	logpos_file.writeLine("W " @ %obj);
	for (%i = 0; %i != $logpos_vehicle_count; %i++)
		if ($logpos_vehicle[%i] == %obj)
			$logpos_vehicle[%i] = 0;
	Parent::vehicleListRemove(%data, %obj);
}

function DefaultGame::playerSpawned(%game, %player)
{
	%name = stripChars(detag(getTaggedString(%player.client.name)),
			"\cp\co\c6\c7\c8\c9");
	logpos_file.writeLine("S " @ %player.client SPC %player.team SPC %name);
	Parent::playerSpawned(%game, %player);
}

function DefaultGame::onClientKilled(%game, %clVictim, %clKiller, %damageType,
		%implement, %damageLocation)
{
        if (%damageType == $DamageType::Impact ||
                        (isObject(%implement) && (%implement.getClassName() $= "Turret" ||
                                                  %implement.getClassName() $= "VehicleTurret" ||
                                                  %implement.getClassName() $= "FlyingVehicle")))
                if (%controller = %implement.getControllingClient())
                        %clKiller = %controller;
	logpos_file.writeLine("K " @ %clVictim SPC %clKiller SPC %damageType);
	Parent::onClientKilled(%game, %clVictim, %clKiller, %damageType,
			%implement, %damageLocation);
}

function DefaultGame::onClientDamaged(%game, %clVictim, %clAttacker,
		%damageType, %sourceObject)
{
	logpos_file.writeLine("D " @ %clVictim SPC %clAttacker SPC %damageType);
	Parent::onClientDamaged(%game, %clVictim, %clAttacker, %damageType,
			%sourceObject);
}

function DefaultGame::onClientLeaveGame(%game, %client)
{
	logpos_file.writeLine("L " @ %client SPC %client.score);
	Parent::onClientLeaveGame(%game, %client);
}

function Player::setArmor(%this, %size)
{
	logpos_file.writeLine("A " @ %this.client SPC %size);
	Parent::setArmor(%this, %size);
}

function ShapeBase::setInventory(%this, %data, %value, %force)
{
	logpos_file.writeLine("I " @ %this.client SPC %data.getName() SPC %value);
	Parent::setInventory(%this, %data, %value, %force);
}

function AIDeployObject(%client, %object)
{
	logpos_file.writeLine("O " @ %object SPC %client SPC
			%object.getDataBlock().getName() SPC %object.position);
	Parent::AIDeployObject(%client, %object);
}

function ShapeBaseData::onDestroyed(%data, %obj, %prevstate)
{
	logpos_file.writeLine("P " @ %obj SPC %obj.lastDamagedBy);
	Parent::onDestroyed(%data, %obj, %prevstate);
}

};

activatePackage(t2matchlog);

////////////////////////////////////////////////////////////////////////////////

