Skip to content

Commit ca3502a

Browse files
committed
Add Agent Movement Example
1 parent 5e55f2d commit ca3502a

File tree

17 files changed

+322
-77
lines changed

17 files changed

+322
-77
lines changed

Assets/blue_rose.png

-6.19 MB
Binary file not shown.

Assets/purple_rose.png

-18.5 MB
Binary file not shown.

extension/src/Agents/AgentContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ class AgentContext : public BaseContext<WsAgent, uint8_t, AgentWorldState>
1313

1414
bool &Done() { return _done; }
1515

16-
bool HasStateOneParam(WsAgent state)
17-
{
18-
uint8_t one = 1;
19-
return BaseContext::HasState(state, one);
20-
}
21-
2216
void SetStateAgent(WsAgent state, int value)
2317
{
2418
_WorldState->SetState(static_cast<WsAgent>(state), static_cast<uint8_t>(value));

extension/src/Agents/AgentDomainBuilder.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "FluidHTN/BaseDomainBuilder.h"
2+
#include "AgentContext.h"
23

34
#include <godot_cpp/core/class_db.hpp>
45
#include <godot_cpp/classes/script.hpp>
@@ -52,17 +53,11 @@ class AgentDomainBuilder final : public BaseDomainBuilder
5253
if (agentNode->has_method("moveTo"))
5354
{
5455
// Call the
55-
Variant result = agentNode->call("moveTo");
56+
int result = agentNode->call("moveTo");
5657

5758
//? Can print output using: UtilityFunctions::print(result);
58-
if (result)
59-
{
60-
return TaskStatus::Success;
61-
}
62-
else
63-
{
64-
return TaskStatus::Failure;
65-
}
59+
// UtilityFunctions::print(result);
60+
return static_cast<TaskStatus>(result);
6661
}
6762
else
6863
{
@@ -86,4 +81,26 @@ class AgentDomainBuilder final : public BaseDomainBuilder
8681

8782
End();
8883
}
84+
85+
/**
86+
*
87+
* Task : Idle
88+
*
89+
* Description : Always succeeds the default state
90+
*
91+
*/
92+
93+
TaskStatus IdleOperator(IContext &ctx)
94+
{
95+
return TaskStatus::Success;
96+
}
97+
98+
void Idle()
99+
{
100+
AddAction("Idle");
101+
102+
AddOperator(std::bind(&AgentDomainBuilder::IdleOperator, this, std::placeholders::_1));
103+
104+
End();
105+
}
89106
};

extension/src/Agents/Domains/AgentXDomainDefinition.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class AgentXDomainDefinition
4343
builder.MoveTo();
4444
builder.End(); // End the Selector
4545

46+
builder.AddSelector("Idle");
47+
builder.Idle();
48+
builder.End(); // End the Selector
49+
4650
return *builder.Build();
4751
}
4852
};

extension/src/Agents/Knowledge/AgentWorldState.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ typedef uint8_t MovementTy;
2727

2828
class AgentWorldState : public IWorldState<WsAgent, uint8_t, AgentWorldState>
2929
{
30-
uint8_t CurrentWorldState[3];
30+
uint8_t CurrentWorldState[2];
3131
EnemyRange currentRange;
3232

3333
public:
3434
AgentWorldState()
3535
{
3636
// TODO : Initial States for the Agent are setup here
37-
currentRange = static_cast<EnemyRange>(CurrentWorldState[static_cast<int>(WsAgent::wsEnemyRange)]);
3837
CurrentWorldState[(int)(WsAgent::wsEnemyRange)] = (int)(EnemyRange::InViewRange);
3938
CurrentWorldState[(int)(WsAgent::wsEnemyRange)] = (int)(AgentMovement::Arrived);
4039
}
@@ -49,5 +48,5 @@ class AgentWorldState : public IWorldState<WsAgent, uint8_t, AgentWorldState>
4948
void SetState(WsAgent state, uint8_t value) { CurrentWorldState[(int)state] = value; }
5049

5150
//! Size must match the CurrentWorldState length
52-
int GetMaxPropertyCount() { return 3; }
51+
int GetMaxPropertyCount() { return 2; }
5352
};

extension/src/ai_blue_agent.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ void AIBlueAgent::vision_sensor(int state, int value)
7272
_context.SetStateAgent(wsAgentState, MovementTy(movementValue), true, FluidEffectType::PlanAndExecute);
7373
}
7474

75+
void AIBlueAgent::movement_update(int state, int value)
76+
{
77+
// Get Agent Movement State in the Agents Knowledge
78+
WsAgent wsAgentState = static_cast<WsAgent>(state);
79+
// Get the value to assign to that agents Knowledge State
80+
AgentMovement movementValue = static_cast<AgentMovement>(value);
81+
82+
_context.SetStateAgent(wsAgentState, MovementTy(movementValue), true, FluidEffectType::PlanAndExecute);
83+
}
84+
7585
/**
7686
* Bind Methods
7787
*
@@ -84,4 +94,5 @@ void AIBlueAgent::_bind_methods()
8494
ClassDB::bind_method(D_METHOD("agent_setup", "node"), &AIBlueAgent::agent_setup);
8595
ClassDB::bind_method(D_METHOD("planner_tick"), &AIBlueAgent::planner_tick);
8696
ClassDB::bind_method(D_METHOD("vision_sensor", "state", "value"), &AIBlueAgent::vision_sensor);
97+
ClassDB::bind_method(D_METHOD("movement_update", "state", "value"), &AIBlueAgent::vision_sensor);
8798
}

extension/src/ai_blue_agent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class AIBlueAgent : public Node3D
4545
// TODO : Add all your sensors to the top level class here
4646

4747
void vision_sensor(int state, int value);
48+
void movement_update(int state, int value);
4849
};
4950

5051
#endif // AIBlueAgent_CLASS_H

game/assets/textures/Prototype/Dark/texture_02.png.import

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
importer="texture"
44
type="CompressedTexture2D"
55
uid="uid://u0gogcd360ri"
6-
path="res://.godot/imported/texture_02.png-d4187abbfcd14895e27c92dfa618c8ab.ctex"
6+
path.s3tc="res://.godot/imported/texture_02.png-d4187abbfcd14895e27c92dfa618c8ab.s3tc.ctex"
77
metadata={
8-
"vram_texture": false
8+
"imported_formats": ["s3tc_bptc"],
9+
"vram_texture": true
910
}
1011

1112
[deps]
1213

1314
source_file="res://assets/textures/Prototype/Dark/texture_02.png"
14-
dest_files=["res://.godot/imported/texture_02.png-d4187abbfcd14895e27c92dfa618c8ab.ctex"]
15+
dest_files=["res://.godot/imported/texture_02.png-d4187abbfcd14895e27c92dfa618c8ab.s3tc.ctex"]
1516

1617
[params]
1718

18-
compress/mode=0
19+
compress/mode=2
1920
compress/high_quality=false
2021
compress/lossy_quality=0.7
2122
compress/hdr_compression=1
2223
compress/normal_map=0
2324
compress/channel_pack=0
24-
mipmaps/generate=false
25+
mipmaps/generate=true
2526
mipmaps/limit=-1
2627
roughness/mode=0
2728
roughness/src_normal=""
@@ -31,4 +32,4 @@ process/normal_map_invert_y=false
3132
process/hdr_as_srgb=false
3233
process/hdr_clamp_exposure=false
3334
process/size_limit=0
34-
detect_3d/compress_to=1
35+
detect_3d/compress_to=0

game/controller/agent/Age98B7.tmp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[gd_scene load_steps=6 format=3 uid="uid://d1l8jr35roq11"]
2+
3+
[ext_resource type="Script" path="res://controller/agent/AgentController.gd" id="1_3lcxf"]
4+
[ext_resource type="PackedScene" uid="uid://cmax004dml8eg" path="res://assets/models/triangle.tscn" id="1_j6i1g"]
5+
6+
[sub_resource type="GDScript" id="GDScript_iqtkw"]
7+
8+
[sub_resource type="SphereShape3D" id="SphereShape3D_2sch0"]
9+
10+
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_lhsr8"]
11+
points = PackedVector3Array(-0.000414371, -1.08583, -1.59394, -0.413111, -1.08583, -1.56336, -0.000414371, 1, -0.00031209, 0.412696, -1.08583, -1.56336, -0.810062, -1.08583, -1.47286, -1.17594, -1.08583, -1.32558, -1.49665, -1.08583, -1.12712, -1.76018, -1.08583, -0.885591, -1.95575, -1.08583, -0.610053, -2.07591, -1.08583, -0.311111, -2.11652, -1.08583, -0.00031209, -2.07591, -1.08583, 0.310799, -1.95575, -1.08583, 0.609741, -1.76018, -1.08583, 0.885279, -1.49665, -1.08583, 1.1268, -1.17594, -1.08583, 1.32527, -0.810062, -1.08583, 1.47255, -0.413111, -1.08583, 1.56305, -0.000414371, -1.08583, 1.59394, 0.412696, -1.08583, 1.56305, 0.809648, -1.08583, 1.47255, 1.17552, -1.08583, 1.32527, 1.49623, -1.08583, 1.1268, 1.75976, -1.08583, 0.885279, 1.95534, -1.08583, 0.609741, 2.0755, -1.08583, 0.310799, 2.11652, -1.08583, -0.00031209, 2.0755, -1.08583, -0.311111, 1.95534, -1.08583, -0.610053, 1.75976, -1.08583, -0.885591, 1.49623, -1.08583, -1.12712, 1.17552, -1.08583, -1.32558, 0.809648, -1.08583, -1.47286)
12+
13+
[node name="AgentController" type="CharacterBody3D" groups=["Agent"]]
14+
script = ExtResource("1_3lcxf")
15+
16+
[node name="AIBlueAgent" type="AIBlueAgent" parent="."]
17+
18+
[node name="AgentBrain" type="Node3D" parent="."]
19+
script = SubResource("GDScript_iqtkw")
20+
21+
[node name="AgentModel" type="Node3D" parent="."]
22+
23+
[node name="triangle" parent="AgentModel" instance=ExtResource("1_j6i1g")]
24+
25+
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
26+
transform = Transform3D(2.2, 0, 0, 0, 2.2, 0, 0, 0, 2.2, 0, 1.11032, 0)
27+
shape = SubResource("SphereShape3D_2sch0")
28+
29+
[node name="VisionArea" type="Area3D" parent="."]
30+
31+
[node name="CollisionShape3D" type="CollisionShape3D" parent="VisionArea"]
32+
transform = Transform3D(12, 0, 0, 0, -5.24537e-07, -12, 0, 12, -5.24537e-07, 0, 1, -11.057)
33+
shape = SubResource("ConvexPolygonShape3D_lhsr8")
34+
35+
[node name="AgentTickRate" type="Timer" parent="."]
36+
autostart = true
37+
38+
[node name="VisionSensor" type="Timer" parent="."]
39+
wait_time = 0.15
40+
autostart = true
41+
42+
[node name="VisionRayCast" type="RayCast3D" parent="."]
43+
44+
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
45+
avoidance_enabled = true
46+
47+
[connection signal="timeout" from="VisionSensor" to="." method="_on_vision_sensor_timeout"]
48+
49+
[editable path="AgentModel/triangle"]

0 commit comments

Comments
 (0)