📄 ccontroller.cpp
字号:
m_vecBestSweepers[i].Reset();
}
//this will call WM_PAINT which will render our scene
InvalidateRect(m_hwndInfo, NULL, TRUE);
UpdateWindow(m_hwndInfo);
}
return true;
}
//---------------------------------- RenderNetworks ----------------------
//
// Renders the best four phenotypes from the previous generation
//------------------------------------------------------------------------
void CController::RenderNetworks(HDC &surface)
{
if (m_iGenerations < 1)
{
return;
}
//draw the network of the best 4 genomes. First get the dimensions of the
//info window
RECT rect;
GetClientRect(m_hwndInfo, &rect);
int cxInfo = rect.right;
int cyInfo = rect.bottom;
//now draw the 4 best networks
m_vecBestSweepers[0].DrawNet(surface, 0, cxInfo/2, cyInfo/2, 0);
m_vecBestSweepers[1].DrawNet(surface, cxInfo/2, cxInfo, cyInfo/2, 0);
m_vecBestSweepers[2].DrawNet(surface, 0, cxInfo/2, cyInfo, cyInfo/2);
m_vecBestSweepers[3].DrawNet(surface, cxInfo/2, cxInfo, cyInfo, cyInfo/2);
}
//------------------------------------Render()--------------------------------------
//
//----------------------------------------------------------------------------------
void CController::Render(HDC &surface)
{
//do not render if running at accelerated speed
if (!m_bFastRender)
{
string s = "Generation: " + itos(m_iGenerations);
TextOut(surface, 5, 0, s.c_str(), s.size());
//select in the blue pen
m_OldPen = (HPEN)SelectObject(surface, m_BluePen);
if (m_bRenderBest)
{
//render the best sweepers memory cells
m_vecBestSweepers[m_iViewThisSweeper].Render(surface);
//render the best sweepers from the last generation
RenderSweepers(surface, m_vecBestSweepers);
//render the best sweepers sensors
RenderSensors(surface, m_vecBestSweepers);
}
else
{
//render the sweepers
RenderSweepers(surface, m_vecSweepers);
}
SelectObject(surface, m_OldPen);
//render the objects
for (int i=0; i<NumObjectVerts; i+=2)
{
MoveToEx(surface, m_ObjectsVB[i].x, m_ObjectsVB[i].y, NULL);
LineTo(surface, m_ObjectsVB[i+1].x, m_ObjectsVB[i+1].y);
}
}//end if
else
{
PlotStats(surface);
RECT sr;
sr.top = m_cyClient-50;
sr.bottom = m_cyClient;
sr.left = 0;
sr.right = m_cxClient;
//render the species chart
m_pPop->RenderSpeciesInfo(surface, sr);
}
}
//------------------------- RenderSweepers -------------------------------
//
// given a vector of sweepers this function renders them to the screen
//------------------------------------------------------------------------
void CController::RenderSweepers(HDC &surface, vector<CMinesweeper> &sweepers)
{
for (int i=0; i<sweepers.size(); ++i)
{
//if they have crashed into an obstacle draw
if ( sweepers[i].Collided())
{
SelectObject(surface, m_RedPen);
}
else
{
SelectObject(surface, m_BluePen);
}
//grab the sweeper vertices
vector<SPoint> sweeperVB = m_SweeperVB;
//transform the vertex buffer
sweepers[i].WorldTransform(sweeperVB, sweepers[i].Scale());
//draw the sweeper left track
MoveToEx(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y, NULL);
for (int vert=1; vert<4; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y);
//draw the sweeper right track
MoveToEx(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y, NULL);
for (vert=5; vert<8; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y);
MoveToEx(surface, (int)sweeperVB[8].x, (int)sweeperVB[8].y, NULL);
LineTo(surface, (int)sweeperVB[9].x, (int)sweeperVB[9].y);
MoveToEx(surface, (int)sweeperVB[10].x, (int)sweeperVB[10].y, NULL);
for (vert=11; vert<16; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
}//next sweeper
}
//----------------------------- RenderSensors ----------------------------
//
// renders the sensors of a given vector of sweepers
//------------------------------------------------------------------------
void CController::RenderSensors(HDC &surface, vector<CMinesweeper> &sweepers)
{
//render the sensors
for (int i=0; i<sweepers.size(); ++i)
{
//grab each sweepers sensor data
vector<SPoint> tranSensors = sweepers[i].Sensors();
vector<double> SensorReadings = sweepers[i].SensorReadings();
vector<double> MemoryReadings = sweepers[i].MemoryReadings();
for (int sr=0; sr<tranSensors.size(); ++sr)
{
if (SensorReadings[sr] > 0)
{
SelectObject(surface, m_RedPen);
}
else
{
SelectObject(surface, m_GreyPenDotted);
}
//make sure we clip the drawing of the sensors or we will get
//unwanted artifacts appearing
if (!((fabs(sweepers[i].Position().x - tranSensors[sr].x) >
(CParams::dSensorRange+1))||
(fabs(sweepers[i].Position().y - tranSensors[sr].y) >
(CParams::dSensorRange+1))))
{
MoveToEx(surface,
(int)sweepers[i].Position().x,
(int)sweepers[i].Position().y,
NULL);
LineTo(surface, (int)tranSensors[sr].x, (int)tranSensors[sr].y);
//render the cell sensors
RECT rect;
rect.left = (int)tranSensors[sr].x - 2;
rect.right = (int)tranSensors[sr].x + 2;
rect.top = (int)tranSensors[sr].y - 2;
rect.bottom= (int)tranSensors[sr].y + 2;
if (MemoryReadings[sr] < 0)
{
FillRect(surface, &rect, m_BlueBrush);
}
else
{
FillRect(surface, &rect, m_RedBrush);
}
}
}
}
}
//--------------------------PlotStats-------------------------------------
//
// Given a surface to draw on this function displays some simple stats
//------------------------------------------------------------------------
void CController::PlotStats(HDC surface)const
{
string s = "Generation: " + itos(m_iGenerations);
TextOut(surface, 5, 25, s.c_str(), s.size());
s = "Num Species: " + itos(m_pPop->NumSpecies());
TextOut(surface, 5, 45, s.c_str(), s.size());
s = "Best Fitness so far: " + ftos(m_pPop->BestEverFitness());
TextOut(surface, 5, 5, s.c_str(), s.size());
}
//------------------------------- GetFitnessScores -----------------------
//
// returns a std::vector containing the genomes fitness scores
//------------------------------------------------------------------------
vector<double> CController::GetFitnessScores()const
{
vector<double> scores;
for (int i=0; i<m_vecSweepers.size(); ++i)
{
scores.push_back(m_vecSweepers[i].Fitness());
}
return scores;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -