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

// all global variable and functions are prefixed logpos_

new fileObject("logpos_file");
$logpos_current = 0;

schedule(1000, 0, logpos_wait_for_start);

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

function logpos_wait_for_start()
{
    if ($LoadingMission || ClientGroup.getCount() == 0)
    {
        schedule(1000, 0, logpos_wait_for_start);
        return;
    }

    $logpos_current = $missionSequence;
    $logpos_flag_count = 0;

    %file = "positiondata/" @ $logpos_current @ "_" @ $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("");

    schedule(1000, 0, logpos_write_position_data);
}

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

function logpos_write_position_data()
{
    if ($missionSequence != $logpos_current)
    {
        // the mission has ended
        logpos_file.close();

        schedule(1000, 0, logpos_wait_for_start);
        return;
    }

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

    // players
    for (%ci = 0; %ci < ClientGroup.getCount(); %ci++)
    {
        %client = ClientGroup.getObject(%ci);
        %data = %client.team @ " " @ %client.player.position @ " " @ %client.nameBase;
        logpos_file.writeLine(%client.player @ " " @ %data);
    }

    // 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 @ " " @ %obj.dataBlock @ " " @ %obj.getName();
            %data = %data @ " " @ %obj.position @ " " @ %obj.name;
            logpos_file.writeLine(%obj.team @ " " @ %data);
        }
    }
}

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

