/*public void LoadMainGhost()
{
	//Something went wrong, escape.
	if (!g_iGhost)
		return;

	char path[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, path, sizeof(path), "data/ctimer/replay/%s.bin", g_sMapName);
	if (!FileExists(path))
	{
		Format(g_sGhostNames, MAX_NAME_LENGTH, "No replay available");
		g_iGhostFrame = -1;
		return;
	}
	File file = OpenFile(path, "rb", false, "GAME");
	if (file)
	{
		g_arrayGhost.Clear();
		float pos[3], ang[2], vel[3];
		int currentframe, buttons;
		while (!file.EndOfFile())
		{
			currentframe = g_arrayGhost.Length;
			currentframe++;
			g_arrayGhost.Resize(currentframe);
			file.Read(view_as<int>(pos), 3, 4);
			file.Read(view_as<int>(ang), 2, 4);
			file.Read(view_as<int>(vel), 3, 4);
			file.ReadInt32(buttons);
			SetArrayCell(g_arrayGhost, currentframe, pos[0], 0);
			SetArrayCell(g_arrayGhost, currentframe, pos[1], 1);
			SetArrayCell(g_arrayGhost, currentframe, pos[2], 2);
			SetArrayCell(g_arrayGhost, currentframe, ang[0], 3);
			SetArrayCell(g_arrayGhost, currentframe, ang[1], 4);
			SetArrayCell(g_arrayGhost, currentframe, vel[0], 5);
			SetArrayCell(g_arrayGhost, currentframe, vel[1], 6);
			SetArrayCell(g_arrayGhost, currentframe, vel[2], 7);
			SetArrayCell(g_arrayGhost, currentframe, buttons, 8);
			
		}
		file.Close();
		PrintToServer("Main replay loaded");
		g_iGhostFrame = -2;
		pos[0] = GetArrayCell(g_arrayGhost, 0, 0);
		pos[1] = GetArrayCell(g_arrayGhost, 0, 1);
		pos[2] = GetArrayCell(g_arrayGhost, 0, 2);
		TeleportEntity(g_iGhost, pos, NULL_VECTOR, NULL_VECTOR);
	}
}*/

bool SaveRecord(int client)
{
	if ( !isValidClient(client) )
	{
		LogError("Client %d is not valid", client);
		return false;
	}
	
	char path[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, path, sizeof(path), "%s/%s/%s.steamedhams", REPLAYS_PATH, g_sMapName, g_sMapName);
	
	if (FileExists(path))
	{
		#if defined BACKUP_REPLAYS
		BackupRecord(path);
		#endif
		
		DeleteFile(path); // Kinda unnecessary? Opening the file later will truncate it.
	}
	
	File file = OpenFile(path, "wb");
	if (file)
	{
		int size = g_arrayRun[client].Length;
		if (!size)
		{
			LogError("Couldn't save record. Run array is empty.");
			return false;
		}
		
		int values[RUNDATA_MAX];
		
		for (int i = 0; i < size; ++i)
		{
			g_arrayRun[client].GetArray(i, values, RUNDATA_MAX);
			
			file.Write(values, RUNDATA_MAX-1, 4);
			file.WriteInt8(values[RUNDATA_WEAPONID]);
			
		}
		file.Close();
		return true;
	}
	LogError("Could not open the file \"%s\" for writing.", path);
	return false;
}

void BackupRecord(char[] recordPath)
{
	char sPath[PLATFORM_MAX_PATH];
	FormatEx(sPath, sizeof(sPath), "%s_old", recordPath);
	
	// Delete the last backup
	if (FileExists(sPath))
	{
		DeleteFile(sPath);
	}
	
	RenameFile(sPath, recordPath);
}

public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon, int &subtype, int &cmdnum, int &tickcount, int &seed, int mouse[2])
{
	// Handle humans
	if (isValidClient(client))
	{
		if (IsPlayerAlive(client))
		{
			// Is the player running?
			if (g_iActivity[client] == 0)
			{
				if(GetEntityMoveType(client) != MOVETYPE_NOCLIP)
				{
					// Record run data
					int values[RUNDATA_MAX];
					
					float origin[3];
					GetEntDataVector(client, m_vecOrigin, origin);
					
					values[RUNDATA_POSITION_X] = view_as<int>(origin[0]);
					values[RUNDATA_POSITION_Y] = view_as<int>(origin[1]);
					values[RUNDATA_POSITION_Z] = view_as<int>(origin[2]);
					values[RUNDATA_PITCH] = view_as<int>(angles[0]);
					values[RUNDATA_YAW] = view_as<int>(angles[1]);
					values[RUNDATA_BUTTONS] = buttons;
					values[RUNDATA_IMPULSE] = impulse;
					values[RUNDATA_WEAPONID] = view_as<int>( GetWeaponID(client) );
					
					g_arrayRun[client].PushArray(values, RUNDATA_MAX);
				}
				
				else
				{
					g_iActivity[client] = -1;
					TimerPrintToChat(client, false, "%T", "TimerCheatStopped", LANG_SERVER);
				}
			}
		}
	}
}