0ad/source/simulation/TurnManager.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

151 lines
3.1 KiB
C++

#include "precompiled.h"
#include "TurnManager.h"
#include "network/NetMessage.h"
#include "network/Network.h"
#include "ps/GameRecord.h"
#include "ps/CLogger.h"
#include <vector>
CSinglePlayerTurnManager *g_SinglePlayerTurnManager=NULL;
CTurnManager::CTurnManager()
{
for (int i=0; i<3; i++)
m_Batches[i].m_TurnLength = DEFAULT_TURN_LENGTH;
}
void CTurnManager::ClearBatch(uintptr_t batch)
{
typedef std::vector<SMessageSyncEntry> MsgVector;
MsgVector &messages=m_Batches[batch].m_Messages;
MsgVector::iterator it=messages.begin();
while (it != messages.end())
{
delete it->m_pMessage;
++it;
}
messages.clear();
}
void CTurnManager::SBatch::Swap(SBatch &other)
{
std::swap(m_Messages, other.m_Messages);
std::swap(m_TurnLength, other.m_TurnLength);
}
void CTurnManager::RotateBatches()
{
// {a, b, c} => {b, c, a}:
// {a, b, c}
// -- swap (0, 1)
// {b, a, c}
// -- swap (1, 2)
// {b, c, a}
m_Batches[0].Swap(m_Batches[1]);
m_Batches[1].Swap(m_Batches[2]);
}
void CTurnManager::IterateBatch(uintptr_t batch, BatchIteratorFunc *fp, void *userdata)
{
typedef std::vector<SMessageSyncEntry> MsgVector;
MsgVector &messages=m_Batches[batch].m_Messages;
MsgVector::iterator it=messages.begin();
while (it != messages.end())
{
it->m_ClientMask=(*fp)(it->m_pMessage, it->m_ClientMask, userdata);
++it;
}
}
void CTurnManager::SendBatch(uintptr_t batch)
{
typedef std::vector<SMessageSyncEntry> MsgVector;
MsgVector &messages=m_Batches[batch].m_Messages;
MsgVector::iterator it=messages.begin();
while (it != messages.end())
{
SendMessage(it->m_pMessage, it->m_ClientMask);
++it;
}
CEndCommandBatch *pMsg=new CEndCommandBatch();
pMsg->m_TurnLength=m_Batches[batch].m_TurnLength;
SendMessage(pMsg, (size_t)-1);
}
void CTurnManager::SendMessage(CNetMessage *pMsg, size_t clientMask)
{
for (size_t i=0;i<m_Clients.size();i++)
{
if (clientMask & (1<<i))
{
if (m_Clients[i].m_Pipe)
m_Clients[i].m_Pipe->Push(pMsg->Copy());
}
}
}
void CTurnManager::QueueMessage(uintptr_t batch, CNetMessage *pMsg)
{
m_Batches[batch].m_Messages.push_back(SMessageSyncEntry(pMsg));
}
void CTurnManager::SetClientPipe(size_t client, IMessagePipeEnd *pipe)
{
m_Clients[client].m_Pipe=pipe;
}
void CTurnManager::SetTurnLength(uintptr_t batch, int turnLength)
{
m_Batches[batch].m_TurnLength=turnLength;
}
int CTurnManager::GetTurnLength()
{
return m_Batches[0].m_TurnLength;
}
void CTurnManager::Initialize(size_t numClients)
{
m_Clients.resize(numClients);
}
void CTurnManager::RecordBatch(uintptr_t batch)
{
IterateBatch(batch, RecordIterator, m_pRecord);
CEndCommandBatch msg;
m_pRecord->WriteMessage(&msg);
}
size_t CTurnManager::RecordIterator(CNetMessage *pMsg, uintptr_t clientMask, void *userdata)
{
CGameRecord *pRecord=(CGameRecord *)userdata;
pRecord->WriteMessage(pMsg);
return clientMask;
}
CSinglePlayerTurnManager::CSinglePlayerTurnManager()
{}
void CSinglePlayerTurnManager::NewTurn()
{
RecordBatch(2);
RotateBatches();
ClearBatch(2);
}
void CSinglePlayerTurnManager::QueueLocalCommand(CNetMessage *pMsg)
{
QueueMessage(2, pMsg);
}
bool CSinglePlayerTurnManager::NewTurnReady()
{
return true;
}