void Prey::Behavior_Hide() { CONSTRUCT_BEHAVIOR_BEGIN m_hidingtime = 0; Debugger::GetInstance().Output("Prey - Hide\n"); SetInfoText("Hiding"); CONSTRUCT_BEHAVIOR_END TICK_BEHAVIOR_BEGIN { list<Square> l_detectedprdt; m_visibility.GetSeenObjects(e_predator,l_detectedprdt); GetHeardPrdt(l_detectedprdt);//if always here predators, consider stopping the latter //from continuing it's way when it's memory is lost. if(l_detectedprdt.size()) { CHANGE_BEHAVIOR(Prey,Flee); } else { m_hidingtime++; if(m_hidingtime > Prey::HidingTime()) { CHANGE_BEHAVIOR(Prey,Idle); } } } TICK_BEHAVIOR_END DESTRUCT_BEHAVIOR_BEGIN DESTRUCT_BEHAVIOR_END }
void Prey::Behavior_Idle() { CONSTRUCT_BEHAVIOR_BEGIN GetAnyPath(); m_path.StartWalk(); Debugger::GetInstance().Output("Prey - Idle\n"); SetInfoText(""); CONSTRUCT_BEHAVIOR_END TICK_BEHAVIOR_BEGIN list<Square> l_detectedprdt; m_visibility.GetSeenObjects(e_predator,l_detectedprdt); GetHeardPrdt(l_detectedprdt); if(l_detectedprdt.size()) { CHANGE_BEHAVIOR(Prey,Flee); } else if(m_path.Completed()) { GetAnyPath(); m_path.StartWalk(); } else { m_path.Walk(this); } TICK_BEHAVIOR_END DESTRUCT_BEHAVIOR_BEGIN DESTRUCT_BEHAVIOR_END }
bool CStudio::EventProcess(const Event &event) { CWindow* pw; CEdit* edit; CSlider* slider; if ( m_dialog != SD_NULL ) // dialogue exists? { return EventDialog(event); } if ( event.type == EVENT_FRAME ) { EventFrame(event); } pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); if ( pw == nullptr ) return false; edit = static_cast<CEdit*>(pw->SearchControl(EVENT_STUDIO_EDIT)); if ( edit == nullptr ) return false; if ( event.type == pw->GetEventTypeClose() ) { Event newEvent = event; newEvent.type = EVENT_STUDIO_OK; m_event->AddEvent(newEvent); } if ( event.type == EVENT_STUDIO_EDIT ) // text modifief? { ColorizeScript(edit); } if ( event.type == EVENT_STUDIO_LIST ) // list clicked? { m_main->StartDisplayInfo(const_cast<char *>(m_helpFilename.c_str()), -1); // TODO change to std::string when RobotMain changes } if ( event.type == EVENT_STUDIO_NEW ) // new? { m_script->New(edit, ""); } if ( event.type == EVENT_STUDIO_OPEN ) // open? { StartDialog(SD_OPEN); } if ( event.type == EVENT_STUDIO_SAVE ) // save? { StartDialog(SD_SAVE); } if ( event.type == EVENT_STUDIO_UNDO ) // undo? { edit->Undo(); } if ( event.type == EVENT_STUDIO_CUT ) // cut? { edit->Cut(); } if ( event.type == EVENT_STUDIO_COPY ) // copy? { edit->Copy(); } if ( event.type == EVENT_STUDIO_PASTE ) // paste? { edit->Paste(); } if ( event.type == EVENT_STUDIO_SIZE ) // size? { slider = static_cast< CSlider* >(pw->SearchControl(EVENT_STUDIO_SIZE)); if ( slider == nullptr ) return false; m_main->SetFontSize(9.0f+slider->GetVisibleValue()*12.0f); ViewEditScript(); } if ( event.type == EVENT_STUDIO_TOOL && // instructions? m_dialog == SD_NULL ) { m_main->StartDisplayInfo(SATCOM_HUSTON, false); } if ( event.type == EVENT_STUDIO_HELP && // help? m_dialog == SD_NULL ) { m_main->StartDisplayInfo(SATCOM_PROG, false); } if ( event.type == EVENT_STUDIO_COMPILE ) // compile? { if ( m_script->GetScript(edit) ) // compile { std::string res; GetResource(RES_TEXT, RT_STUDIO_COMPOK, res); SetInfoText(res, false); } else { std::string error; m_script->GetError(error); SetInfoText(error, false); } } if ( event.type == EVENT_STUDIO_RUN ) // run/stop? { if ( m_script->IsRunning() ) { Event newEvent = event; newEvent.type = EVENT_OBJECT_PROGSTOP; m_event->AddEvent(newEvent); // stop } else { if ( m_script->GetScript(edit) ) // compile { SetInfoText("", false); Event newEvent = event; newEvent.type = EVENT_OBJECT_PROGSTART; m_event->AddEvent(newEvent); // start } else { std::string error; m_script->GetError(error); SetInfoText(error, false); } } } if ( event.type == EVENT_STUDIO_REALTIME ) // real time? { m_bRealTime = !m_bRealTime; m_script->SetStepMode(!m_bRealTime); UpdateFlux(); UpdateButtons(); } if ( event.type == EVENT_STUDIO_STEP ) // step? { m_script->Step(event); } if ( event.type == EVENT_KEY_DOWN ) { if ( event.key.key == m_main->GetInputBinding(INPUT_SLOT_CBOT).primary || event.key.key == m_main->GetInputBinding(INPUT_SLOT_CBOT).secondary ) { if ( m_helpFilename.length() > 0 ) { m_main->StartDisplayInfo(const_cast<char *>(m_helpFilename.c_str()), -1); // TODO change to std::string when RobotMain changes } } } if ( event.type == EVENT_WINDOW3 ) // window is moved? { m_editActualPos = m_editFinalPos = pw->GetPos(); m_editActualDim = m_editFinalDim = pw->GetDim(); m_main->SetWindowPos(m_editActualPos); m_main->SetWindowDim(m_editActualDim); AdjustEditScript(); } if ( event.type == pw->GetEventTypeReduce() ) { if ( m_bEditMinimized ) { m_editFinalPos = m_main->GetWindowPos(); m_editFinalDim = m_main->GetWindowDim(); m_bEditMinimized = false; m_bEditMaximized = false; } else { m_editFinalPos.x = 0.00f; m_editFinalPos.y = -0.44f; m_editFinalDim.x = 1.00f; m_editFinalDim.y = 0.50f; m_bEditMinimized = true; m_bEditMaximized = false; } m_main->SetEditFull(m_bEditMaximized); pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); if ( pw != nullptr ) { pw->SetMaximized(m_bEditMaximized); pw->SetMinimized(m_bEditMinimized); } } if ( event.type == pw->GetEventTypeFull() ) { if ( m_bEditMaximized ) { m_editFinalPos = m_main->GetWindowPos(); m_editFinalDim = m_main->GetWindowDim(); m_bEditMinimized = false; m_bEditMaximized = false; } else { m_editFinalPos.x = 0.00f; m_editFinalPos.y = 0.00f; m_editFinalDim.x = 1.00f; m_editFinalDim.y = 1.00f; m_bEditMinimized = false; m_bEditMaximized = true; } m_main->SetEditFull(m_bEditMaximized); pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); if ( pw != nullptr ) { pw->SetMaximized(m_bEditMaximized); pw->SetMinimized(m_bEditMinimized); } } return true; }
void Prey::Behavior_Flee() { CONSTRUCT_BEHAVIOR_BEGIN m_fleeingvector=vector2d::Invalid; m_refuge = Square::Invalid; m_fleeingwithoutdangertime=0; Debugger::GetInstance().Output("Prey - Flee\n"); SetInfoText("Fleeing"); CONSTRUCT_BEHAVIOR_END TICK_BEHAVIOR_BEGIN { list<Square> l_detectedprdt; m_visibility.GetSeenObjects(e_predator,l_detectedprdt); GetHeardPrdt(l_detectedprdt); if(l_detectedprdt.size() || m_fleeingvector!=vector2d::Invalid) { bool findnewpath=false; if(l_detectedprdt.size()) { vector2d directionToFleeTo=GetVectorAwayFromAll(l_detectedprdt);//todo:GetVectorAwayFromAll if(m_fleeingvector ==vector2d::Invalid) { m_fleeingvector=directionToFleeTo; m_fleeingvectorangle = getangle2(m_fleeingvector); findnewpath = true; } else { double a_new = getangle2(directionToFleeTo); if(fabs(m_fleeingvectorangle-a_new) > PI_Over_8) { findnewpath = true; m_fleeingvector=directionToFleeTo; m_fleeingvectorangle = a_new; } /* m_fleeingvector.normalize(); directionToFleeTo.normalize(); if(m_fleeingvector!=directionToFleeTo) { findnewpath = true; m_fleeingvector=directionToFleeTo; } */ } //if we are near border and the m_fleeingvector is directing us out of the board Square pos=GetPosition(); if(pos.x==15 && m_fleeingvector.x>0) m_fleeingvector.x=0; if(pos.x==0 && m_fleeingvector.x<0) m_fleeingvector.x=0; if(pos.y==15 && m_fleeingvector.y>0) m_fleeingvector.y=0; if(pos.y==0 && m_fleeingvector.y<0) m_fleeingvector.y=0; if(m_fleeingvector.x==0 && m_fleeingvector.y==0) { m_fleeingvector.x=1,m_fleeingvector.y=1; //just any vector, for this version m_fleeingvectorangle=getangle2(m_fleeingvector); } m_fleeingvector.normalize(); assert(m_fleeingvector!=vector2d(0,0)); m_fleeingwithoutdangertime=0; } else { //we neither see nor hear any predator. may be we can stop running. m_fleeingwithoutdangertime++; if(m_fleeingwithoutdangertime>Prey::InFleeWithoutDangerTime()) { CHANGE_BEHAVIOR(Prey,Idle); } } if(IsAValidRefuge(m_refuge)) { SetInfoText("Refuge"); //keep going to the refuge; Walk(true); } else { list<Square> l_seenobstacles; Path path(m_board); m_visibility.GetSeenObjects(e_obstacle,l_seenobstacles); if(l_seenobstacles.size()) { Square nearestobstacle = super::GetNearestObject(l_seenobstacles); FindARefuge(nearestobstacle, &path); } if(path.IsEmpty()) { m_refuge = Square::Invalid; if(findnewpath) { m_path.GetAStraightPathInDirection(GetSituation(),m_fleeingvector); if(m_path.IsEmpty()) { //if we can't find a path, get any path, for this version. GetAnyPath(); m_fleeingvector=vector2d::Invalid; } assert(!m_path.IsEmpty()); //we need a path. if we can't get any here, see why this happens. m_path.StartWalk(); Walk(false); } else { Walk(false); } } else { m_path=path; m_refuge = m_path.GetGoal(); m_path.StartWalk(); Walk(true); SetInfoText("Refuge"); } } } else { CHANGE_BEHAVIOR(Prey,Idle); } } TICK_BEHAVIOR_END DESTRUCT_BEHAVIOR_BEGIN DESTRUCT_BEHAVIOR_END }
int COGLWin::SetScript(const char *pcScript, bool bResetEnv, bool bReparseLatex) { CCodeVar Var; //printf("Enter SetScript.\n"); // Only execute checks on change of global variables // in idle loop after the next display operation. m_bWaitWithIdleForNextDisplay = true; SetTitle(); if (bReparseLatex) { m_Parse.OverrideSetRenderLatex(false); m_Parse.SetRenderLatex(true); m_Parse.OverrideSetRenderLatex(true); } if (bResetEnv) { // Switch off animation SetAnimCode(false); COGLWinFLTK::StopAnimation(); SetExecModeVar(EXEC_MODE_NEW); AddExecModeVar(EXEC_MODE_CHANGE); ResetTransformVars(); ResetLights(); m_ToolBox.ResetToolInUseFlags(); m_ToolBox.PruneToolMap(); if (!m_bKeepBGColor) { CCodeVar& rVarCol = m_Parse.GetVar("_BGColor"); if (rVarCol.Type() != PDT_NOTYPE) { rVarCol = Black; SetBGColor(Black); } } else { m_bKeepBGColor = false; } CCodeVar& rVarFBS = m_Parse.GetVar("_FrameBoxSize"); if (rVarFBS.Type() != PDT_NOTYPE) { rVarFBS = TCVScalar(3); } CCodeVar& rVar2dV = m_Parse.GetVar("_2dView"); if (rVar2dV.Type() != PDT_NOTYPE) { rVar2dV = 0; } Enable2dView(false); /// Reset visualization mode to OPNS for all spaces. m_Filter.SetMode(MODE_OPNS); // Reset Code Base m_Parse.ResetCodeBase(); } else { SetExecModeVar(EXEC_MODE_CHANGE); } m_Parse.CollectGarbage(); m_Parse.Reset(); m_Parse.GetCodeBase().ResetInfoVars(); SetInfoText("Use <code>SetInfoText([string])</code> to put some text here. " "The text may include some 'simple' HTML tags."); SetInfoWidth(0); bool bShowOutput = true; if (m_bInFullScreenMode || m_bVizOnlyMode) { bShowOutput = false; } if (m_pOutput && bShowOutput) { m_pOutput->SetText("<body size=\"16\"><b>Parsing code...</b></body>"); m_pOutput->show(); } int iLines = m_Parse.InsertText(pcScript); if (m_pOutput && bShowOutput) { m_pOutput->SetText("<body size=\"14\">Parsing code...<b>finished</b>.<br>Running code after change. <b>Please wait...</b></body>"); m_pOutput->show(); } if (!m_bInFullScreenMode) { if (m_bPresentationAction || m_bParseFromOGLWin || m_bVizOnlyMode) { ShowWindow(); } else m_pEditor->show(); } m_bPresentationAction = false; m_bParseFromOGLWin = false; if (iLines >= 0) { //printf("Parsed %d lines.\n", iLines); m_bCheckForAnimate = true; m_bCodeChanged = true; m_bCodeChangedForAnim = true; //glutSetWindow(m_iHWin); //m_pWindow->show(); m_bCheckToolBoxSize = true; // When visualization window is iconized, the script is not executed // because of a glutPostRedisplay(). So we have to call Display explicitly. if (!m_pWindow->visible()) { Display(); } // if (!m_bIsAnimated) // glutPostRedisplay(); } else { ClearError(); m_sError = "<b>Parse Errors:</b><p>"; m_sError += m_Parse.PrintParseErrors().Str(); m_sError += "</p>"; m_bHasError = true; if (m_pOutput && !m_bVizOnlyMode) { m_pOutput->SetText(m_sError.c_str()); m_pOutput->show(); } if (m_pEditor && !m_bVizOnlyMode) { cStr csFilename, csPath; int iLine, iPos, iInputPos; if (iLines == -1) m_Parse.GetParseErrorPos(iLine, iPos, iInputPos, csFilename); else { csFilename = m_Parse.GetErrorFilename().c_str(); csPath = m_Parse.GetErrorPath().c_str(); if (csFilename.Len() == 0) { csFilename = m_sScriptPath.c_str(); csFilename << m_sScriptName.c_str() << ".clu"; } char *pcName = csFilename.Str(); if (!strchr(pcName, ':') && !strchr(pcName, '/') && !strchr(pcName, '\\')) { csFilename = csPath + csFilename; csFilename |= ('.' < csFilename) - csFilename.Len() - 1; } iInputPos = -iLines - 2; } m_pEditor->HighlightLinePos(csFilename.Str(), iInputPos); m_pEditor->show(); } //ClearError(); m_Parse.ClearError(); SetAnimCode(false); /* m_bCodeChanged = true; glutSetWindow(m_iHWin); if (!m_bIsAnimated) glutPostRedisplay(); */ } if (!m_bIsAnimated) glutPostRedisplay(); // CheckToolBoxSize(); // if (iLines >= 0 && !m_bIsAnimated) // glutPostRedisplay(); return iLines; }