#include "StdAfx.h" #include "T2Archive.h" #include "T2MoverArray.h" #include "T2MoverArrayList.h" T2MoverArrayList::T2MoverArrayList() : LArray(sizeof(T2MoverArray *)) { T2MoverArray *theArray = new T2MoverArray; Add(theArray); } /*virtual*/ T2MoverArrayList::~T2MoverArrayList() { LArrayIterator iterator(*this); T2MoverArray *theArray; while (iterator.Next(&theArray)) delete theArray; } void T2MoverArrayList::Add(T2MoverArray* inArray) { InsertItemsAt(1, mItemCount + 1, &inArray); } unsigned int T2MoverArrayList::GetItemCount() { return GetCount(); } T2MoverArray* T2MoverArrayList::GetItemAt(int inIndex) { T2MoverArray *theArray; FetchItemAt(inIndex, &theArray); return theArray; } T2Mover* T2MoverArrayList::GetMoverByID(unsigned int inMoverID) { if (inMoverID == 0) return NULL; int groupNum = (inMoverID - 1) / T2MoverArray::kGroupSize; int indexInGroup = (inMoverID - 1) % T2MoverArray::kGroupSize; if (GetCount() > groupNum) return GetItemAt(groupNum + 1)->GetIndexMover(indexInGroup); else return NULL; } T2Mover* T2MoverArrayList::FindUnusedMover() { T2Mover *theMover = NULL; LArrayIterator iter(*this); T2MoverArray *theArray = NULL; while (!theMover && iter.Next(&theArray)) theMover = theArray->FindUnusedMover(); if (!theMover) { unsigned int lastStartID = 1; if (theArray) lastStartID = theArray->GetStartID() + T2MoverArray::kGroupSize; if ((lastStartID + T2MoverArray::kGroupSize) < 1000) { theArray = new T2MoverArray(lastStartID); if (theArray) { Add(theArray); theMover = theArray->FindUnusedMover(); } } } return theMover; } void T2MoverArrayList::DrawMoverAll(T2TowerDoc* inDoc, const RECT& inRect) { LArrayIterator iterator(*this); T2MoverArray *theArray; while (iterator.Next(&theArray)) theArray->DrawMoverAll(inDoc, inRect); } void T2MoverArrayList::DispatchIdle(T2TowerDoc* inDoc) { LArrayIterator iterator(*this); T2MoverArray *theArray; while (iterator.Next(&theArray)) theArray->DispatchIdle(inDoc, 0); } void T2MoverArrayList::Read(T2Archive& inArchive, T2TowerDoc* inDoc) { int groupCount; inArchive >> groupCount; RemoveItemsAt(GetCount(), 1); unsigned int startID = 1; for (int i = 0; i < groupCount; i++) { Add(new T2MoverArray(startID)); startID += T2MoverArray::kGroupSize; } LArrayIterator iter(*this); T2MoverArray *theArray; while (iter.Next(&theArray)) theArray->Read(inArchive, inDoc); } void T2MoverArrayList::Write(T2Archive& inArchive) { int groupCount = GetItemCount(); inArchive << groupCount; LArrayIterator iterator(*this); T2MoverArray *theArray; while (iterator.Next(&theArray)) theArray->Write(inArchive); } int T2MoverArrayList::CalcMentenanceCost(T2TowerDoc* inDoc) const { int cost = 0; LArrayIterator iter(*this); T2MoverArray *theArray; while (iter.Next(&theArray)) cost += theArray->CalcMentenanceCost(inDoc); return cost; } LArray* T2MoverArrayList::MakeMoverList(int inType) { LArray *result = new LArray(sizeof(T2Mover *)); LArrayIterator iter(*this); T2MoverArray *theMoverArray; while (iter.Next(&theMoverArray)) { for (int i = 0; i < T2MoverArray::kGroupSize; i++) { T2Mover *theMover = &theMoverArray->mMover[i]; if (theMover->IsUsed() && theMover->GetEquipType() == inType) result->Add(&theMover); } } if (result->GetCount() > 0) { return result; } else { delete result; return NULL; } }