the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 174 lines 3.8 kB view raw
1#include "stdafx.h" 2#include "Goal.h" 3#include "GoalSelector.h" 4 5 6GoalSelector::InternalGoal::InternalGoal(int prio, Goal *goal, bool canDeletePointer) 7{ 8 this->prio = prio; 9 this->goal = goal; 10 this->canDeletePointer = canDeletePointer; 11} 12 13GoalSelector::GoalSelector() 14{ 15 tickCount = 0; 16 newGoalRate = 3; 17} 18 19GoalSelector::~GoalSelector() 20{ 21 for(AUTO_VAR(it, goals.begin()); it != goals.end(); ++it) 22 { 23 if((*it)->canDeletePointer) delete (*it)->goal; 24 delete (*it); 25 } 26} 27 28void GoalSelector::addGoal(int prio, Goal *goal, bool canDeletePointer /*= true*/) // 4J Added canDelete param 29{ 30 goals.push_back(new InternalGoal(prio, goal, canDeletePointer)); 31} 32 33void GoalSelector::removeGoal(Goal *toRemove) 34{ 35 for(AUTO_VAR(it, goals.begin()); it != goals.end(); ) 36 { 37 InternalGoal *ig = *it; 38 Goal *goal = ig->goal; 39 40 if (goal == toRemove) 41 { 42 AUTO_VAR(it2, find(usingGoals.begin(), usingGoals.end(), ig) ); 43 if (it2 != usingGoals.end()) 44 { 45 goal->stop(); 46 usingGoals.erase(it2); 47 } 48 49 if(ig->canDeletePointer) delete ig->goal; 50 delete ig; 51 it = goals.erase(it); 52 } 53 else 54 { 55 ++it; 56 } 57 } 58} 59 60void GoalSelector::tick() 61{ 62 vector<InternalGoal *> toStart; 63 64 if(tickCount++ % newGoalRate == 0) 65 { 66 //for (InternalGoal ig : goals) 67 for(AUTO_VAR(it, goals.begin()); it != goals.end(); ++it) 68 { 69 InternalGoal *ig = *it; 70 //bool isUsing = usingGoals.contains(ig); 71 AUTO_VAR(usingIt, find(usingGoals.begin(), usingGoals.end(), ig)); 72 73 //if (isUsing) 74 if(usingIt != usingGoals.end()) 75 { 76 if (!canUseInSystem(ig) || !canContinueToUse(ig)) 77 { 78 ig->goal->stop(); 79 //usingGoals.remove(ig); 80 usingGoals.erase(usingIt); 81 } 82 else continue; 83 } 84 85 if (!canUseInSystem(ig) || !ig->goal->canUse()) continue; 86 87 toStart.push_back(ig); 88 usingGoals.push_back(ig); 89 } 90 } 91 else 92 { 93 for(AUTO_VAR(it, usingGoals.begin() ); it != usingGoals.end(); ) 94 { 95 InternalGoal *ig = *it; 96 if (!ig->goal->canContinueToUse()) 97 { 98 ig->goal->stop(); 99 it = usingGoals.erase(it); 100 } 101 else 102 { 103 ++it; 104 } 105 } 106 } 107 108 109 //bool debug = false; 110 //if (debug && toStart.size() > 0) System.out.println("Starting: "); 111 //for (InternalGoal ig : toStart) 112 for(AUTO_VAR(it, toStart.begin()); it != toStart.end(); ++it) 113 { 114 //if (debug) System.out.println(ig.goal.toString() + ", "); 115 (*it)->goal->start(); 116 } 117 118 //if (debug && usingGoals.size() > 0) System.out.println("Running: "); 119 //for (InternalGoal ig : usingGoals) 120 for(AUTO_VAR(it, usingGoals.begin()); it != usingGoals.end(); ++it) 121 { 122 //if (debug) System.out.println(ig.goal.toString()); 123 (*it)->goal->tick(); 124 } 125} 126 127vector<GoalSelector::InternalGoal *> *GoalSelector::getRunningGoals() 128{ 129 return &usingGoals; 130} 131 132bool GoalSelector::canContinueToUse(InternalGoal *ig) 133{ 134 return ig->goal->canContinueToUse(); 135} 136 137bool GoalSelector::canUseInSystem(GoalSelector::InternalGoal *goal) 138{ 139 //for (InternalGoal ig : goals) 140 for(AUTO_VAR(it, goals.begin()); it != goals.end(); ++it) 141 { 142 InternalGoal *ig = *it; 143 if (ig == goal) continue; 144 145 AUTO_VAR(usingIt, find(usingGoals.begin(), usingGoals.end(), ig)); 146 147 if (goal->prio >= ig->prio) 148 { 149 if (usingIt != usingGoals.end() && !canCoExist(goal, ig)) return false; 150 } 151 else if (usingIt != usingGoals.end() && !ig->goal->canInterrupt()) return false; 152 } 153 154 return true; 155} 156 157bool GoalSelector::canCoExist(GoalSelector::InternalGoal *goalA, GoalSelector::InternalGoal *goalB) 158{ 159 return (goalA->goal->getRequiredControlFlags() & goalB->goal->getRequiredControlFlags()) == 0; 160} 161 162void GoalSelector::setNewGoalRate(int newGoalRate) 163{ 164 this->newGoalRate = newGoalRate; 165} 166 167void GoalSelector::setLevel(Level *level) 168{ 169 for(AUTO_VAR(it, goals.begin()); it != goals.end(); ++it) 170 { 171 InternalGoal *ig = *it; 172 ig->goal->setLevel(level); 173 } 174}