#include "StdAfx.h" #include "T2Archive.h" #include "T2FloorInfo.h" #include "../T2TowerDoc.h" #include "../T2TowerMainView.h" #include "T2TrafficInfo.h" #include "URect.h" T2TrafficInfo::T2TrafficInfo(RECT inArea, unsigned int inA, unsigned int inB, unsigned int inC, unsigned int inUnitSize) { mUnitSize = inUnitSize; if (mUnitSize == 0) mUnitSize = 4; m8 = inA / 4; mC = inB / 4; m10 = inC / 4; mArea = inArea; mHeight = URect::Height(inArea); mWidth = URect::Width(inArea) / mUnitSize; mUnitData = new T2TrafficUnit[mWidth * mHeight]; unsigned int nUnits = mWidth * mHeight; T2TrafficUnit *unt = mUnitData; for (unsigned int i = 0; i < nUnits; i++, unt++) { unt->x0 = 0; unt->x4 = 0; } } /*virtual*/ T2TrafficInfo::~T2TrafficInfo() { if (mUnitData) delete[] mUnitData; } void T2TrafficInfo::Pass(POINT inPt1, POINT inPt2, short inCost) { POINT point1; POINT point2; if (inPt1.x < inPt2.x) { point1 = inPt1; point2 = inPt2; point2.y = inPt1.y; } else { point1 = inPt2; point1.y = inPt1.y; point2 = inPt1; } T2TrafficUnit *un1 = GetUnit(point1); T2TrafficUnit *un2 = GetUnit(point2); if (un1 && un2) { for (T2TrafficUnit *unit = un1; unit <= un2; unit++) unit->x0 += inCost; } } void T2TrafficInfo::HourChanged(T2TowerDoc* inDoc) { unsigned int nUnits = mWidth * mHeight; T2TrafficUnit *unt = mUnitData; for (unsigned int i = 0; i < nUnits; i++, unt++) { unt->x4 *= 0.96875f; if ((unt->x4 + unt->x0) > 0xFFFF) unt->x4 = 0xFFFF; else unt->x4 += unt->x0; unt->x0 = 0; int range = CalcRange(unt->x4); if (range != unt->range) { unt->range = range; if (inDoc->GetViewMode() == kTransView) { RECT area; T2FloorInfo *theFloorInfo = inDoc->GetFloorInfo(); CalcUnitArea(i, area); theFloorInfo->SetTenantDrawModeByRect(area, DrawMode3); inDoc->GetMainView()->InvalUnitRect(area); } } } } unsigned int T2TrafficInfo::CalcRange(unsigned int inValue) const { short range = 3; if (inValue < m8) range = 0; else if (inValue < mC) range = 1; else if (inValue < m10) range = 2; return range; } void T2TrafficInfo::CalcUnitArea(unsigned int inIndex, RECT& outRect) const { unsigned int h = inIndex / mWidth; unsigned int v = (inIndex % mWidth) * mUnitSize; SetRect(&outRect, h, v, h + mUnitSize, v + 1); OffsetRect(&outRect, mArea.left, mArea.top); } short T2TrafficInfo::GetRange(POINT inPt) const { short range = 0; T2TrafficUnit *theUnit = GetUnit(inPt); if (theUnit) range = theUnit->range; return range; } T2TrafficUnit* T2TrafficInfo::GetUnit(POINT inPt) const { T2TrafficUnit *theUnit = NULL; if (mUnitData) { int index = CalcUnitIndex(inPt); if (index > 0) theUnit = mUnitData + index; } return theUnit; } int T2TrafficInfo::CalcUnitIndex(POINT inPt) const { int index = -1; POINT point = inPt; if (PtInRect(&mArea, point)) { int h = (inPt.x - mArea.left) / mUnitSize; int v = inPt.y - mArea.top; index = mWidth * v + h; } return index; } void T2TrafficInfo::Read(T2Archive& inArchive) { unsigned int nUnits = mWidth * mHeight; T2TrafficUnit *unt = mUnitData; for (unsigned int i = 0; i < nUnits; i++, unt++) { unsigned short v; inArchive >> v; unt->x0 = v; inArchive >> v; unt->x4 = v; unt->range = CalcRange(unt->x4); } } void T2TrafficInfo::Write(T2Archive& inArchive) const { unsigned int nUnits = mWidth * mHeight; T2TrafficUnit *unt = mUnitData; for (unsigned int i = 0; i < nUnits; i++, unt++) { inArchive << (unsigned short) unt->x0; inArchive << (unsigned short) unt->x4; } }