You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are 4 steps which should be generated, but plugging in the initial state and goal state from that example page doesn't work in this GOAP project.
I found an alternative;- simply run a check on all the possible combinations/permutations of actions.
using System;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using GOAP;
using GOAP.Planning;
namespace game1
{
public partial class Game1
{
GOAP_1 test = new GOAP_1();
public class GOAP_1
{
public GOAP_1()
{
var planningActions = new List<PlanningAction<State>>
{
new PlanningAction<State>(
name:"load",
validator: x => x["armedWithGun"] == 1,
executor: x =>
{
x["weaponLoaded"] = 1;
}),
new PlanningAction<State>(
name:"detonateBomb",
validator: x => x["armedWithBomb"] == 1 && x["nearEnemy"] == 1,
executor: x =>
{
x["alive"] = 0;
x["enemyAlive"] = 0;
}),
new PlanningAction<State>(
name:"shoot",
validator: x => x["enemyLinedUp"] == 1,
executor: x =>
{
x["enemyAlive"] = 0;
}),
new PlanningAction<State>(
name:"aim",
validator: x => x["enemyVisible"] == 1 && x["weaponLoaded"] == 1,
executor: x =>
{
x["enemyLinedUp"] = 1;
}),
new PlanningAction<State>(
name:"approach",
validator: x => x["enemyVisible"] == 1,
executor: x =>
{
x["nearEnemy"] = 1;
}),
new PlanningAction<State>(
name: "scout",
validator: x => x["armedWithGun"] == 1,
executor: x =>
{
x["enemyVisible"] = 1;
}),
new PlanningAction<State>(
name:"flee",
validator: x => x["enemyVisible"] == 1,
executor: x =>
{
x["nearEnemy"] = 0;
}),
};
initialState = new State
{
{"armedWithGun" , 1},
{"enemyVisible" , 0},
{"nearEnemy" , 0},
{"weaponLoaded" , 0},
{"enemyLinedUp" , 0},
{"enemyAlive" , 1},
{"armedWithBomb" , 1},
{"alive" , 1},
};
goalState = new State
{
{"enemyAlive" , 0},
{"alive" , 1},
};
var steps = makePlan(initialState, goalState, planningActions);
foreach(var step in steps) //plan
{
//..
}
}
static List<PlanningAction<State>> makePlan(State initialState, State goalState, List<PlanningAction<State>> actions)
{
foreach(var action in actions)
{
var qualified = action.validator((State)initialState.Clone());
if(!qualified) continue;
var state1 = action.Execute((State)initialState.Clone());
foreach(var action2 in actions)
{
if(action2 != action)
{
var qualified2 = action2.validator(state1);
if(!qualified2) continue;
var state2 = action2.Execute(state1);
foreach(var action3 in actions)
{
if(action3 != action && action3 != action2)
{
var qualified3 = action3.validator(state2);
if(!qualified3) continue;
var state3 = action3.Execute(state2);
foreach(var action4 in actions)
{
if(action4 != action && action4 != action2 && action4 != action3)
{
var qualified4 = action4.validator(state3);
if(!qualified4) continue;
var state4 = action4.Execute(state3);
foreach(var action5 in actions)
{
if(action5 != action && action5 != action2 && action5 != action3 && action5 != action4)
{
var qualified5 = action5.validator(state4);
if(!qualified5) continue;
var state5 = action5.Execute(state4);
}
}
if(goalStateSatisfied(state4, goalState)) return makeSteps(action, action2, action3, action4);
}
}
if(goalStateSatisfied(state3, goalState)) return makeSteps(action, action2, action3);
}
}
if(goalStateSatisfied(state2, goalState)) return makeSteps(action, action2);
}
}
if(goalStateSatisfied(state1, goalState)) return makeSteps(action);
}
return new List<PlanningAction<State>>();
}
static bool goalStateSatisfied(State currState, State goalState)
{
foreach(var kvp in goalState)
{
if(kvp.Value == currState[kvp.Key])
continue;
return false;
}
return true;
}
static List<PlanningAction<State>> makeSteps(params PlanningAction<State>[] actions)
{
List<PlanningAction<State>> steps = new List<PlanningAction<State>>();
foreach(var action in actions)
steps.Add(action);
return steps;
}
static State initialState;
static State goalState;
}
}
}
The text was updated successfully, but these errors were encountered:
If we follow the example from here;- https://github.com/stolk/GPGOAP
There are 4 steps which should be generated, but plugging in the initial state and goal state from that example page doesn't work in this GOAP project.
I found an alternative;- simply run a check on all the possible combinations/permutations of actions.
}
The text was updated successfully, but these errors were encountered: