Skip to content

Commit

Permalink
Move error codes for Goap into constants
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinealb committed Aug 21, 2023
1 parent f86a78f commit 1597ec1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/goap/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ int main()

auto len = planner.plan(state, goal, actions.data(), actions.size(), path, max_path_len);

if (len <= 0) {
if (len == goap::kErrorNoPathFound) {
std::cout << "Could not find a plan!" << std::endl;
return 1;
}

if (len == goap::kErrorNotEnoughMemory) {
std::cout << "Not enough memory reserved to explore all options!" << std::endl;
return 1;
}

// Execute all the steps in the plan
for (int i = 0; i < len; i++) {
path[i]->execute(state);
Expand Down
9 changes: 6 additions & 3 deletions lib/goap/include/goap/goap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace goap {

const int kErrorNoPathFound = -1;
const int kErrorNotEnoughMemory = -2;

template <typename State>
class Action {
public:
Expand Down Expand Up @@ -105,7 +108,7 @@ class Planner {
}

if (!gc) {
return -2;
return kErrorNotEnoughMemory;
}

if (gc_prev) {
Expand Down Expand Up @@ -152,8 +155,8 @@ class Planner {
}
}

// No path was found
return -1;
// Reaching here means we did not find a path
return kErrorNoPathFound;
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/goap/tests/goap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ TEST(SimpleScenario, WhatHappensIfThereIsNoPath)
goap::Action<TestState>* path[max_path_len] = {nullptr};

auto cost = planner.plan(state, goal, actions, action_count, path, max_path_len);
CHECK_EQUAL(-1, cost);
CHECK_EQUAL(goap::kErrorNoPathFound, cost);
}

struct FarAwayState {
Expand Down Expand Up @@ -236,7 +236,7 @@ TEST(TooLongPathTestGroup, DoNotFindFarAwayPlan)

// Of course it will fail
auto cost = planner.plan(state, goal, actions, 1);
CHECK_EQUAL(-2, cost);
CHECK_EQUAL(goap::kErrorNotEnoughMemory, cost);
}

TEST(TooLongPathTestGroup, RespectsMaxOutputLength)
Expand Down

0 comments on commit 1597ec1

Please sign in to comment.