| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
#include "KeySeeker.h" |
|---|
| 7 |
|
|---|
| 8 |
void Append(vector<PlanNode*>& ret, vector<PlanNode*>& newp) |
|---|
| 9 |
{ |
|---|
| 10 |
for(int n=0; n<newp.size(); ++n) |
|---|
| 11 |
ret.push_back(newp[n]); |
|---|
| 12 |
} |
|---|
| 13 |
|
|---|
| 14 |
bool AllPrevsSat(PlanNode* plan) |
|---|
| 15 |
{ |
|---|
| 16 |
for(int p=0; p<plan->prevs.size(); ++p) |
|---|
| 17 |
if(plan->prevs[p]->num != 1) return false; |
|---|
| 18 |
return true; |
|---|
| 19 |
} |
|---|
| 20 |
vector<PlanNode*> _SquashPlan(PlanNode* plan) |
|---|
| 21 |
{ |
|---|
| 22 |
if(plan == NULL) return vector<PlanNode*>(); |
|---|
| 23 |
if(!AllPrevsSat(plan)) return vector<PlanNode*>(); |
|---|
| 24 |
plan->num = 1; |
|---|
| 25 |
vector<PlanNode*> ret; |
|---|
| 26 |
for(int b=0; b<plan->branches.size(); ++b) |
|---|
| 27 |
{ |
|---|
| 28 |
Append(ret, _SquashPlan(plan->branches[b])); |
|---|
| 29 |
} |
|---|
| 30 |
return ret; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
void ResetFlags(PlanNode* plan) |
|---|
| 34 |
{ |
|---|
| 35 |
if(plan == NULL) return; |
|---|
| 36 |
plan->num = 0; |
|---|
| 37 |
for(int b=0; b<plan->branches.size(); ++b) |
|---|
| 38 |
ResetFlags(plan->branches[b]); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
vector<PlanNode*> SquashPlan(PlanNode* plan) |
|---|
| 42 |
{ |
|---|
| 43 |
ResetFlags(plan); |
|---|
| 44 |
return _SquashPlan(plan); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
int KeySeeker::NewSeek(StoryNode* root) |
|---|
| 53 |
{ |
|---|
| 54 |
ResetAttempteds(root->frame); |
|---|
| 55 |
attempts = 0; |
|---|
| 56 |
return ContinueSeek(root); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
int KeySeeker::Resatisfy(void) |
|---|
| 60 |
{ |
|---|
| 61 |
PlanNode* plan = NULL; |
|---|
| 62 |
int timer = time(0); |
|---|
| 63 |
try { |
|---|
| 64 |
if(!RP.Replan(plan, timer)) return 0; |
|---|
| 65 |
pending_execution.push_back(SquashPlan(plan)); |
|---|
| 66 |
} catch(TimeOutException tout) { throw tout; } |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
return 1; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
int KeySeeker::ContinueSeek(StoryNode* root) |
|---|
| 75 |
{ |
|---|
| 76 |
|
|---|
| 77 |
ResetTimer(); |
|---|
| 78 |
|
|---|
| 79 |
try { |
|---|
| 80 |
while(!root->visited && attempts++ < MAX_ATTEMPTS) |
|---|
| 81 |
root->visited = Achieve(root->frame); |
|---|
| 82 |
}catch(TimeOutException) { --attempts; return -1; } |
|---|
| 83 |
return root->visited; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
int KeySeeker::Achieve(KeyFrameNode* frame) |
|---|
| 88 |
{ |
|---|
| 89 |
if (!frame) return 1; |
|---|
| 90 |
|
|---|
| 91 |
if(frame->conditions.satisfied) return 1; |
|---|
| 92 |
if(frame->attempted == attempts) return 0; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
try { |
|---|
| 96 |
if(frame->preconds.size() == 0) |
|---|
| 97 |
{ |
|---|
| 98 |
if(PlanAndExecute(frame)) |
|---|
| 99 |
{ |
|---|
| 100 |
frame->conditions.satisfied = 1; |
|---|
| 101 |
return 1; |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
else |
|---|
| 105 |
{ |
|---|
| 106 |
for(int p=0; p<frame->preconds.size(); ++p) |
|---|
| 107 |
{ |
|---|
| 108 |
if(Achieve(frame->preconds[p])) |
|---|
| 109 |
{ |
|---|
| 110 |
try{ |
|---|
| 111 |
if(PlanAndExecute(frame)) |
|---|
| 112 |
{ |
|---|
| 113 |
frame->conditions.satisfied = 1; |
|---|
| 114 |
return 1; |
|---|
| 115 |
} |
|---|
| 116 |
}catch(TimeOutException pae_to) { |
|---|
| 117 |
frame->attempted = - attempts; |
|---|
| 118 |
throw pae_to; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
frame->attempted = attempts; |
|---|
| 124 |
return 0; |
|---|
| 125 |
}catch(TimeOutException to) { throw to; } |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
void KeySeeker::ResetAttempteds(KeyFrameNode* frame) |
|---|
| 129 |
{ |
|---|
| 130 |
if (!frame) return; |
|---|
| 131 |
|
|---|
| 132 |
frame->attempted = 0; |
|---|
| 133 |
for(int i=0; i<frame->preconds.size(); ++i) |
|---|
| 134 |
ResetAttempteds(frame->preconds[i]); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
int KeySeeker::PlanAndExecute(KeyFrameNode* frame) |
|---|
| 138 |
{ |
|---|
| 139 |
try { |
|---|
| 140 |
if(frame->attempted < 0) |
|---|
| 141 |
{ |
|---|
| 142 |
frame->attempted = attempts - 1; |
|---|
| 143 |
PlanNode* plan = NULL; |
|---|
| 144 |
if(RP.ResumePlanning(plan, timer)) |
|---|
| 145 |
{ |
|---|
| 146 |
|
|---|
| 147 |
pending_execution.push_back(SquashPlan(plan)); |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
return 1; |
|---|
| 151 |
} |
|---|
| 152 |
else |
|---|
| 153 |
return 0; |
|---|
| 154 |
} |
|---|
| 155 |
else |
|---|
| 156 |
{ |
|---|
| 157 |
PlanNode* plan = NULL; |
|---|
| 158 |
if(RP.MakeNewPlan(frame, plan, timer)) |
|---|
| 159 |
{ |
|---|
| 160 |
pending_execution.push_back(SquashPlan(plan)); |
|---|
| 161 |
return 1; |
|---|
| 162 |
} |
|---|
| 163 |
else |
|---|
| 164 |
return 0; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
}catch(TimeOutException to) { throw to; } |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|