//--------------------------- Render ------------------------------------- // //------------------------------------------------------------------------ void Raven_Bot::Render() { //when a bot is hit by a projectile this value is set to a constant user //defined value which dictates how long the bot should have a thick red //circle drawn around it (to indicate it's been hit) The circle is drawn //as long as this value is positive. (see Render) m_iNumUpdatesHitPersistant--; if (isDead() || isSpawning()) return; gdi->BluePen(); m_vecBotVBTrans = WorldTransform(m_vecBotVB, Pos(), Facing(), Facing().Perp(), Scale()); gdi->ClosedShape(m_vecBotVBTrans); //draw the head (gdi->*(m_pTeam->GetPen()))(); gdi->Circle(Pos(), 6.0 * Scale().x); //render the bot's weapon m_pWeaponSys->RenderCurrentWeapon(); //render a thick red circle if the bot gets hit by a weapon if (m_bHit) { gdi->ThickRedPen(); gdi->HollowBrush(); gdi->Circle(m_vPosition, BRadius()+1); if (m_iNumUpdatesHitPersistant <= 0) { m_bHit = false; } } gdi->TransparentText(); gdi->TextColor(0,255,0); if (UserOptions->m_bShowBotIDs) { gdi->TextAtPos(Pos().x -10, Pos().y-20, ttos(ID())); } if (UserOptions->m_bShowBotHealth) { gdi->TextAtPos(Pos().x-40, Pos().y-5, "H:"+ ttos(Health())); } if (UserOptions->m_bShowScore) { gdi->TextAtPos(Pos().x-40, Pos().y+10, "Scr:"+ ttos(Score())); } }
//------------------------------ Render ---------------------------------- //------------------------------------------------------------------------ bool SoccerPitch::Render() { //draw the grass gdi->DarkGreenPen(); gdi->DarkGreenBrush(); gdi->Rect(0,0,m_cxClient, m_cyClient); //render regions if (Prm.bRegions) { for (unsigned int r=0; r<m_Regions.size(); ++r) { m_Regions[r]->Render(true); } } //render the goals gdi->HollowBrush(); gdi->RedPen(); gdi->Rect(m_pPlayingArea->Left(), (m_cyClient-Prm.GoalWidth)/2, m_pPlayingArea->Left()+40, m_cyClient - (m_cyClient-Prm.GoalWidth)/2); gdi->BluePen(); gdi->Rect(m_pPlayingArea->Right(), (m_cyClient-Prm.GoalWidth)/2, m_pPlayingArea->Right()-40, m_cyClient - (m_cyClient-Prm.GoalWidth)/2); //render the pitch markings gdi->WhitePen(); gdi->Circle(m_pPlayingArea->Center(), m_pPlayingArea->Width() * 0.125); gdi->Line(m_pPlayingArea->Center().x, m_pPlayingArea->Top(), m_pPlayingArea->Center().x, m_pPlayingArea->Bottom()); gdi->WhiteBrush(); gdi->Circle(m_pPlayingArea->Center(), 2.0); //the ball gdi->WhitePen(); gdi->WhiteBrush(); m_pBall->Render(); //Render the teams m_pRedTeam->Render(); m_pBlueTeam->Render(); //render the walls gdi->WhitePen(); for (unsigned int w=0; w<m_vecWalls.size(); ++w) { m_vecWalls[w].Render(); } //show the score gdi->TextColor(Cgdi::red); gdi->TextAtPos((m_cxClient/2)-300, m_cyClient-18, m_pRedTeam->Name() + "(Red): " + ttos(m_pBlueGoal->NumGoalsScored())); gdi->TextColor(Cgdi::blue); gdi->TextAtPos((m_cxClient/2)+10, m_cyClient-18, m_pBlueTeam->Name() + "(Blue): " + ttos(m_pRedGoal->NumGoalsScored())); return true; }
//-------------------------- RenderInfo --------------------------------------- //----------------------------------------------------------------------------- void AttackTargetGoal_Evaluator::RenderInfo(Vector2D Position, Raven_Bot* pBot) { gdi->TextAtPos(Position, "AT: " + ttos(CalculateDesirability(pBot), 2)); return; std::string s = ttos(Raven_Feature::Health(pBot)) + ", " + ttos(Raven_Feature::TotalWeaponStrength(pBot)); gdi->TextAtPos(Position+Vector2D(0,12), s); }
//-------------------------- RenderInfo --------------------------------------- //----------------------------------------------------------------------------- void GetHealthGoal_Evaluator::RenderInfo(Vector2D Position, Raven_Bot* pBot) { gdi->TextAtPos(Position, "H: " + ttos(CalculateDesirability(pBot), 2)); return; std::string s = ttos(1-Raven_Feature::Health(pBot)) + ", " + ttos(Raven_Feature::DistanceToItem(pBot, type_health)); gdi->TextAtPos(Position+Vector2D(0,15), s); }
void Raven_WeaponSystem::RenderDesirabilities()const { Vector2D p = m_pOwner->Pos(); int num = 0; WeaponMap::const_iterator curWeap; for (curWeap = m_WeaponMap.begin(); curWeap != m_WeaponMap.end(); ++curWeap) { if (curWeap->second) num++; } int offset = 15 * num; for (curWeap = m_WeaponMap.begin(); curWeap != m_WeaponMap.end(); ++curWeap) { if (curWeap->second) { double score = curWeap->second->GetLastDesirabilityScore(); std::string type = GetNameOfType(curWeap->second->GetType()); gdi->TextAtPos(p.x + 10.0, p.y - offset, ttos(score) + " " + type); offset += 15; } } }
//--------------------------- Render ------------------------------------- // //------------------------------------------------------------------------ void GoalKeeper::Render() { if (Team()->Color() == SoccerTeam::blue) gdi->BluePen(); else gdi->RedPen(); m_vecPlayerVBTrans = WorldTransform(m_vecPlayerVB, Pos(), m_vLookAt, m_vLookAt.Perp(), Scale()); gdi->ClosedShape(m_vecPlayerVBTrans); //draw the head gdi->BrownBrush(); gdi->Circle(Pos(), 6); //draw the ID if (Prm.bIDs) { gdi->TextColor(0, 170, 0);; gdi->TextAtPos(Pos().x-20, Pos().y-20, ttos(ID())); } //draw the state if (Prm.bStates) { gdi->TextColor(0, 170, 0); gdi->TransparentText(); gdi->TextAtPos(m_vPosition.x, m_vPosition.y -20, std::string(m_pStateMachine->GetNameOfCurrentState())); } }
//-------------------------- AddBots -------------------------------------- // // Adds a bot and switches on the default steering behavior //----------------------------------------------------------------------------- void Raven_Game::AddBots(unsigned int NumBotsToAdd) { while (NumBotsToAdd--) { //create a bot. (its position is irrelevant at this point because it will //not be rendered until it is spawned) Raven_Bot* rb = new Raven_Bot(this, Vector2D()); m_lastTeamSpawned = (m_lastTeamSpawned == 1) ? 2 : 1; rb->SetTeamId(m_lastTeamSpawned); //switch the default steering behaviors on rb->GetSteering()->WallAvoidanceOn(); rb->GetSteering()->SeparationOn(); m_Bots.push_back(rb); //register the bot with the entity manager EntityMgr->RegisterEntity(rb); #ifdef LOG_CREATIONAL_STUFF debug_con << "Adding bot with ID " << ttos(rb->ID()) << ""; #endif } }
void SoccerPitch::Render() { ngdi->TransparentText(); ngdi->HollowBrush(); //畫草地 ngdi->DarkGreenPen(); ngdi->DarkGreenBrush(); ngdi->Rect(V2D(0,0),V2D(m_cxClient, m_cyClient)); //畫球門 ngdi->HollowBrush(); ngdi->RedPen(); ngdi->Rect(V2D(m_pPlayingArea->Left(), (m_cyClient-m_pBlueGoal->GoalWidth())/2), V2D(m_pPlayingArea->Left()+40, m_cyClient - (m_cyClient-m_pBlueGoal->GoalWidth())/2)); ngdi->BluePen(); ngdi->Rect(V2D(m_pPlayingArea->Right(), (m_cyClient-m_pRedGoal->GoalWidth())/2), V2D(m_pPlayingArea->Right()-40, m_cyClient - (m_cyClient-m_pRedGoal->GoalWidth())/2)); //render the pitch markings //畫球場中間圓線 ngdi->WhitePen(); ngdi->Circle(m_pPlayingArea->Center(), m_pPlayingArea->Width() * 0.125); //畫球場中線 ngdi->Line(V2D(m_pPlayingArea->Center().x, m_pPlayingArea->Top()), V2D(m_pPlayingArea->Center().x, m_pPlayingArea->Bottom())); ngdi->WhiteBrush(); //畫球場中間小圓 ngdi->Circle(m_pPlayingArea->Center(), 2.0); //the ball //gdi->WhitePen(); //gdi->WhiteBrush(); //m_pBall->Render(); //Render the teams //m_pRedTeam->Render(); //m_pBlueTeam->Render(); //render the walls ngdi->WhitePen(); for (unsigned int w=0; w<m_vecWalls.size(); ++w) { m_vecWalls[w].Render(); } //show the score ngdi->TextColor(NEOgdi::red); //gdi->TextAtPos((m_cxClient/2)-50, m_cyClient-18, "Red: " + ttos(m_pBlueGoal->NumGoalsScored())); ngdi->TextAtPos((m_cxClient/2)-50, m_cyClient-18, L"Red: " + ttos(99)); ngdi->TextColor(NEOgdi::blue); //ngdi->TextAtPos((m_cxClient/2)+10, m_cyClient-18, "Blue: " + ttos(m_pRedGoal->NumGoalsScored())); ngdi->TextAtPos((m_cxClient/2)+10, m_cyClient-18, L"Blue: " + ttos(99)); }
//--------------------- CheckBufferLength -------------------------------- // // this is a replacement for the StringCchLength function found in the // platform SDK. See MSDN for details. Only ever used for checking toolbar // strings //------------------------------------------------------------------------ bool CheckBufferLength(char* buff, int MaxLength, int& BufferLength) { std::string s = ttos(buff); BufferLength = s.length(); if (BufferLength > MaxLength) { BufferLength = 0; return false; } return true; }
//-------------------------- RenderInfo --------------------------------------- //----------------------------------------------------------------------------- void GetWeaponGoal_Evaluator::RenderInfo(Vector2D Position, Raven_Bot* pBot) { std::string s; switch(m_iWeaponType) { case type_rail_gun: s="RG: ";break; case type_rocket_launcher: s="RL: "; break; case type_shotgun: s="SG: "; break; } gdi->TextAtPos(Position, s + ttos(CalculateDesirability(pBot), 2)); }
//-------------------------- AddBots -------------------------------------- // // Adds a bot and switches on the default steering behavior //----------------------------------------------------------------------------- void Raven_Game::AddBot(std::string botClassName) { //create a bot. (its position is irrelevant at this point because it will //not be rendered until it is spawned) //Raven_Bot* rb = new Raven_Bot(this, Vector2D()); AbstRaven_Bot* rb; rb = (AbstRaven_Bot*) BotMaker::newBot(botClassName, this, Vector2D()); //switch the default steering behaviors on rb->GetSteering()->WallAvoidanceOn(); rb->GetSteering()->SeparationOn(); m_Bots.push_back(rb); //register the bot with the entity manager EntityMgr->RegisterEntity(rb); #ifdef LOG_CREATIONAL_STUFF debug_con << "Adding bot of class " << botClassName << " with ID " << ttos(rb->ID()) << ""; #endif }
//-------------------------- RenderInfo --------------------------------------- //----------------------------------------------------------------------------- void ExploreGoal_Evaluator::RenderInfo(Vector2D Position, AbstRaven_Bot* pBot) { gdi->TextAtPos(Position, "EX: " + ttos(CalculateDesirability(pBot), 2)); }
void GameLearn::learn() { learnInvisible(); /* */ enemies.push_back(model); ShipEnemy* body = &enemies.back(); std::vector<ShipEvolutive> evo; for(int i=0;i<NB_INDIVIDUALS;i++){ std::string str = "P"+ttos(i); evo.push_back(ShipEvolutive(body,str)); } for(unsigned int i=0;i<evo.size();i++){ evo[i].setBulletPlayer(&bulletPlayer); } int current = 0; float bestTime = 0.f; /* */ bool end=false; window.setFramerateLimit(FR_LIMIT); initPlayer(); sf::Clock telapsed; txt_time.setPosition(window.getSize().x-200,10); dt = 1.f/FR_LIMIT; static const float offsetsecurity = body->getVelocity()*dt*1.2f+10.f; while (window.isOpen() && !end) { while (window.pollEvent(event)){ if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::KeyPressed){ if (event.key.code == sf::Keyboard::Escape) end = true; if (event.key.code == sf::Keyboard::Right) player.setVx(player.getVelocity()); if (event.key.code == sf::Keyboard::Left) player.setVx(-player.getVelocity()); if (event.key.code == sf::Keyboard::Down) player.setVy(player.getVelocity()); if (event.key.code == sf::Keyboard::Up) player.setVy(-player.getVelocity()); } if (event.type == sf::Event::KeyReleased){ if (event.key.code == sf::Keyboard::Right && player.getV().x>0.f) player.setVx(0.f); if (event.key.code == sf::Keyboard::Left && player.getV().x<0.f) player.setVx(0.f); if (event.key.code == sf::Keyboard::Down && player.getV().y>0.f) player.setVy(0.f); if (event.key.code == sf::Keyboard::Up && player.getV().y<0.f) player.setVy(0.f); } } // update data layer_game.clear(); if(body->getLives()<=LIFE_SECURITY-LIFE_INDIVIDUALS){ current++; if(current >= (int)evo.size()){ current = 0; bestTime = ShipEvolutive::reproduction(evo); } body->addLife(LIFE_SECURITY-body->getLives()); body->setPosition(layer_game.getSize().x/2.f,100.f); body->setV(0.f,0.f); //ennemies.push_back(model3); //body = &ennemies.back(); evo[current].reset(); } if(body->getSprite().getPosition().x < offsetsecurity) body->setPosition(offsetsecurity,body->getSprite().getPosition().y); if(body->getSprite().getPosition().y < offsetsecurity) body->setPosition(body->getSprite().getPosition().x,offsetsecurity); if(body->getSprite().getPosition().x > layer_game.getSize().x-offsetsecurity) body->setPosition(layer_game.getSize().x-offsetsecurity,body->getSprite().getPosition().y); if(body->getSprite().getPosition().y > layer_game.getSize().y-offsetsecurity) body->setPosition(body->getSprite().getPosition().x,layer_game.getSize().y-offsetsecurity); evo[current].update(dt); // move the player if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) player.update(2.5f*dt); else player.update(dt); layer_game.draw(player); for (std::list<ShipEnemy>::iterator it=enemies.begin(); it != enemies.end(); ++it){ it->update(dt); layer_game.draw(*it); // out of screen if(it->getSprite().getGlobalBounds().top > layer_game.getSize().y){ //it = ennemies.erase(it); } } // detect bullet collisions manageBulletCollision(); std::string info(""); for(unsigned int i=0;i<evo.size();i++) info += evo[i].getName()+">"+ttos(evo[i].getTime())+"\n"; txt_time.setString("Temps écoulé: "+ttos(telapsed.getElapsedTime().asSeconds()) +"\nVies: "+ttos(player.getLives()) +"\nfps: "+ttos(getFrameRate()) +"\n ENT: "+evo[current].getName() +"\n GEN: "+ttos(evo[current].getGeneration()) +"\n BestTile: "+ttos(bestTime) +"\n"+info ); for(int i=(int)bulletPlayer.size()-1;i>=0;i--){ bulletPlayer[i].update(dt); layer_game.draw(bulletPlayer[i]); } if(bullet.size() > 0) for(int i=(int)(bullet.size())-1;i>=0;i--){ bullet[i].update(dt); layer_game.draw(bullet[i]); } layer_game.display(); window.clear(); window.draw(sf::Sprite(layer_game.getTexture())); window.draw(txt_time); window.display(); } for(unsigned int i=0;i<evo.size();++i){ evo[i].writeGenome(); } }
void GameLearn::learnInvisible() { const unsigned int TIME_WAITING = 60.f; // in sec window.setFramerateLimit(0); enemies.push_back(model); Ship* body = &enemies.back(); std::vector<ShipEvolutive> evo; for(int i=0;i<NB_INDIVIDUALS;i++){ std::string str = "P"+ttos(i); evo.push_back(ShipEvolutive(body,str)); } for(unsigned int i=0;i<evo.size();i++){ evo[i].setBulletPlayer(&bulletPlayer); } int current = 0; float bestTime = 0.f; float bestTimeCumul = 0.f; dt = 1.f/FR_LIMIT; sf::Text info; info.setFont(*FontManager::getFont("ressources/Symtext.ttf")); info.setCharacterSize(12); info.setPosition(window.getSize().x/2,10); static const float offsetsecurity = body->getVelocity()*dt*1.2f+10.f; float tcounter = -2.f; int gencounter = 1; // generation counter int ncounter = 0; bool end = false; initPlayer(); sf::Clock telapsed; while (window.isOpen() && !end && telapsed.getElapsedTime().asSeconds() < TIME_WAITING){ while (window.pollEvent(event)){ if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::KeyPressed){ if (event.key.code == sf::Keyboard::Escape) end = true; } } // update data if(body->getLives() <= LIFE_SECURITY-LIFE_INDIVIDUALS){ // next individual current++; if(current >= (int)evo.size()){ current = 0; bestTime = ShipEvolutive::reproduction(evo); bestTimeCumul += bestTime; gencounter++; } body->addLife(LIFE_SECURITY-body->getLives()); body->setPosition(layer_game.getSize().x/2.f,100.f); body->setV(0.f,0.f); //ennemies.push_back(model3); //body = &ennemies.back(); evo[current].reset(); } if(body->getSprite().getPosition().x < offsetsecurity) body->setPosition(offsetsecurity,body->getSprite().getPosition().y); if(body->getSprite().getPosition().y < offsetsecurity) body->setPosition(body->getSprite().getPosition().x,offsetsecurity); if(body->getSprite().getPosition().x > layer_game.getSize().x-offsetsecurity) body->setPosition(layer_game.getSize().x-offsetsecurity,body->getSprite().getPosition().y); if(body->getSprite().getPosition().y > layer_game.getSize().y-offsetsecurity) body->setPosition(body->getSprite().getPosition().x,layer_game.getSize().y-offsetsecurity); evo[current].update(dt); // move the player player.update(dt); for (std::list<ShipEnemy>::iterator it=enemies.begin(); it != enemies.end(); ++it){ it->update(dt); // out of screen if(it->getSprite().getGlobalBounds().top > layer_game.getSize().y){ //it = ennemies.erase(it); } } // detect bullet collisions manageBulletCollision(); for(int i=(int)bulletPlayer.size()-1;i>=0;i--){ bulletPlayer[i].update(dt); } if(bullet.size() > 0) for(int i=(int)(bullet.size())-1;i>=0;i--){ bullet[i].update(dt); } if(elapsed.getElapsedTime().asSeconds()-tcounter > 1.f){ window.clear(); // snapshot layer_game.clear(); layer_game.draw(player); for (std::list<ShipEnemy>::iterator it=enemies.begin(); it != enemies.end(); ++it){ layer_game.draw(*it); } for(int i=(int)bulletPlayer.size()-1;i>=0;i--){ layer_game.draw(bulletPlayer[i]); } if(bullet.size() > 0) for(int i=(int)(bullet.size())-1;i>=0;i--){ layer_game.draw(bullet[i]); } layer_game.display(); window.draw(sf::Sprite(layer_game.getTexture())); // --- std::string info2(""); for(unsigned int i=0;i<evo.size();i++) info2 += evo[i].getName()+">"+ttos(evo[i].getTime())+"\n"; info.setString("Temps : "+ttos(telapsed.getElapsedTime().asSeconds())+"\n"+ "Step : "+ttos(ncounter)+"\n"+ "Generation Max : "+ttos(gencounter)+ "\nBestTime : "+ttos(bestTime)+ "\nAvg : "+ttos(bestTimeCumul/(float)gencounter) //+"\nfps : "+ttos(getFrameRate())+"\n---\n"+info2 ); window.draw(info); window.display(); tcounter = elapsed.getElapsedTime().asSeconds(); }else getFrameRate(); ncounter++; } for(unsigned int i=0;i<evo.size();++i){ evo[i].writeGenome(); } clear(); enemies.clear(); }
PsimagLite::String filenameForThread(SizeType thread) const { return params_.logroot + ttos(thread) + ".txt"; }
//--------------------------- Render ------------------------------------------ //----------------------------------------------------------------------------- void Raven_Game::Render() { m_pGraveMarkers->Render(); //render the map m_pMap->Render(); time_t min = gameTimeMin(); time_t sec = gameTimeSec(); std::string timeString = ttos(min) + ":"; if (sec < 10) timeString += "0"; timeString += ttos(sec); gdi->TextColor(0,0,0); gdi->TextAtPos(360, 12, timeString); //render all the bots unless the user has selected the option to only //render those bots that are in the fov of the selected bot if (m_pSelectedBot && UserOptions->m_bOnlyShowBotsInTargetsFOV) { std::vector<AbstRaven_Bot*> VisibleBots = GetAllBotsInFOV(m_pSelectedBot); std::vector<AbstRaven_Bot*>::const_iterator it = VisibleBots.begin(); for (it; it != VisibleBots.end(); ++it) (*it)->Render(); if (m_pSelectedBot) m_pSelectedBot->Render(); } else { //render all the entities std::list<AbstRaven_Bot*>::const_iterator curBot = m_Bots.begin(); for (curBot; curBot != m_Bots.end(); ++curBot) { if ((*curBot)->isAlive()) { (*curBot)->Render(); } } } //render any projectiles std::list<Raven_Projectile*>::const_iterator curW = m_Projectiles.begin(); for (curW; curW != m_Projectiles.end(); ++curW) { (*curW)->Render(); } // gdi->TextAtPos(300, WindowHeight - 70, "Num Current Searches: " + ttos(m_pPathManager->GetNumActiveSearches())); //render a red circle around the selected bot (blue if possessed) if (m_pSelectedBot) { if (m_pSelectedBot->isPossessed()) { gdi->BluePen(); gdi->HollowBrush(); gdi->Circle(m_pSelectedBot->Pos(), m_pSelectedBot->BRadius()+1); } else { gdi->RedPen(); gdi->HollowBrush(); gdi->Circle(m_pSelectedBot->Pos(), m_pSelectedBot->BRadius()+1); } if (UserOptions->m_bShowOpponentsSensedBySelectedBot) { m_pSelectedBot->GetSensoryMem()->RenderBoxesAroundRecentlySensed(); } //render a square around the bot's target if (UserOptions->m_bShowTargetOfSelectedBot && m_pSelectedBot->GetTargetBot()) { gdi->ThickRedPen(); Vector2D p = m_pSelectedBot->GetTargetBot()->Pos(); double b = m_pSelectedBot->GetTargetBot()->BRadius(); gdi->Line(p.x-b, p.y-b, p.x+b, p.y-b); gdi->Line(p.x+b, p.y-b, p.x+b, p.y+b); gdi->Line(p.x+b, p.y+b, p.x-b, p.y+b); gdi->Line(p.x-b, p.y+b, p.x-b, p.y-b); } //render the path of the bot if (UserOptions->m_bShowPathOfSelectedBot) { m_pSelectedBot->GetBrain()->Render(); } //display the bot's goal stack if (UserOptions->m_bShowGoalsOfSelectedBot) { Vector2D p(m_pSelectedBot->Pos().x -50, m_pSelectedBot->Pos().y); m_pSelectedBot->GetBrain()->RenderAtPos(p, GoalTypeToString::Instance()); } if (UserOptions->m_bShowGoalAppraisals) { m_pSelectedBot->GetBrain()->RenderEvaluations(5, 415); } if (UserOptions->m_bShowWeaponAppraisals) { m_pSelectedBot->GetWeaponSys()->RenderDesirabilities(); } if (IS_KEY_PRESSED('Q') && m_pSelectedBot->isPossessed()) { gdi->TextColor(255,0,0); gdi->TextAtPos(GetClientCursorPosition(), "Queuing"); } } }