Skip to content

Commit

Permalink
otw - Toying around with Behaviour trees again, trying to optimize fo…
Browse files Browse the repository at this point in the history
…r goal discovery / tracking.
  • Loading branch information
Nemquae committed Oct 21, 2013
1 parent 83e0123 commit 39aaa88
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 18 deletions.
Binary file modified Assets/OpenCog Assets/Scenes/Game/Game.unity
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ public IEnumerator Start ()

foreach( OCAction action in actions)
{
if(action.FullName.Contains(treeName) || treeName.Contains("Behaviour"))
if( (action.FullName.Contains(treeName) || treeName.Contains("Behaviour"))
&& !(treeName == "GhostBehaviour" && action.name.Contains("Create"))
&& !(treeName == "GhostBehaviour" && action.name.Contains("Destroy"))
)
{
int actionTypeID = (int)Enum.Parse(typeof(BLOCBehaviours.ActionType), action.FullName);

Expand Down
59 changes: 45 additions & 14 deletions Assets/OpenCog Assets/Scripts/OpenCog/Actions/OCGoalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,29 @@ public Vector3i GoalBlockPos
get { return _goalBlockPos;}
set { _goalBlockPos = value;}
}


public string BlockType
{
get {
return this._blockType;
}
set {
_blockType = value;
if(_map != null)
_goalBlockType = _map.GetBlockSet().GetBlock(_blockType);
}
}

public OCBlock GoalBlockType
{
get {
return this._goalBlockType;
}
set {
_goalBlockType = value;
}
}

//---------------------------------------------------------------------------

#endregion
Expand Down Expand Up @@ -122,8 +144,9 @@ public void UpdateGoal()
{

List3D<OCChunk> chunks = _map.GetChunks ();

FindGoalBlockPositionInChunks(chunks);

// Since this is probably bogging down the gameplay, switch it to block creation only.
//FindGoalBlockPositionInChunks(chunks);

Vector3 sourcePos = gameObject.transform.position;
Vector3 distanceVec = ((Vector3)GoalBlockPos) - sourcePos;
Expand Down Expand Up @@ -163,7 +186,7 @@ public void UpdateGoal()

//---------------------------------------------------------------------------

private void FindGoalBlockPositionInChunks(List3D<OCChunk> chunks)
public void FindGoalBlockPositionInChunks(List3D<OCChunk> chunks)
{
Vector3 sourcePos = gameObject.transform.position;
Vector3 distanceVec = ((Vector3)GoalBlockPos) - sourcePos;
Expand Down Expand Up @@ -215,16 +238,7 @@ private void FindGoalBlockPositionInChunks(List3D<OCChunk> chunks)
GoalBlockPos = candidatePos;
distanceVec = candidateVec;

if(_ShouldMoveTargets)
{
OCAction[] actions = gameObject.GetComponentsInChildren<OCAction>();

foreach(OCAction action in actions)
{
action.EndTarget.transform.position = new Vector3(GoalBlockPos.x, GoalBlockPos.y, GoalBlockPos.z);
action.StartTarget.transform.position = gameObject.transform.position;
}
}
MoveTargetsIfNecessary ();

Debug.Log("We found some " + _goalBlockType.GetName() + " nearby: " + GoalBlockPos + "!");
}
Expand Down Expand Up @@ -253,6 +267,23 @@ private void FindGoalBlockPositionInChunks(List3D<OCChunk> chunks)
}

}

/// <summary>
/// Moves the targets if necessary.
/// </summary>
public void MoveTargetsIfNecessary ()
{
if(_ShouldMoveTargets)
{
OCAction[] actions = gameObject.GetComponentsInChildren<OCAction>();

foreach(OCAction action in actions)
{
action.EndTarget.transform.position = new Vector3(GoalBlockPos.x, GoalBlockPos.y, GoalBlockPos.z);
action.StartTarget.transform.position = gameObject.transform.position;
}
}
}

//---------------------------------------------------------------------------

Expand Down
18 changes: 18 additions & 0 deletions Assets/OpenCog Assets/Scripts/OpenCog/Builder/OCBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using UnityEngine;
using System.Linq;
using OpenCog.Map;
using OpenCog.Actions;

//The private field is assigned but its value is never used
#pragma warning disable 0414
Expand Down Expand Up @@ -170,6 +171,23 @@ public void Update()
OpenCog.Map.OCBlockData block = OCBlockData.CreateInstance<OCBlockData>().Init(_selectedBlock, OpenCog.Utility.VectorUtil.Vector3ToVector3i(point.Value));
block.SetDirection(GetDirection(-transform.forward));
_map.SetBlockAndRecompute(block, point.Value);

OCGoalController[] goalControllers = (OCGoalController[])GameObject.FindObjectsOfType(typeof(OCGoalController));

foreach(OCGoalController goalController in goalControllers)
{
if(goalController.GoalBlockType == _selectedBlock)
{
Vector3 sourcePos = goalController.gameObject.transform.position;
Vector3 oldDistanceVec = ((Vector3)goalController.GoalBlockPos) - sourcePos;
Vector3 newDistanceVec = point.Value - sourcePos;
if(newDistanceVec.sqrMagnitude < oldDistanceVec.sqrMagnitude)
{
goalController.GoalBlockPos = point.Value;
goalController.MoveTargetsIfNecessary();
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Linq;
using OpenCog.Utility;
using UnityEngine;
using OpenCog.Actions;

//The private field is assigned but its value is never used
#pragma warning disable 0414
Expand Down Expand Up @@ -91,7 +92,7 @@ public void CreateBlock(Vector3i? point)
OCBlock block = map.GetBlockSet().GetBlock(_BlockType);

//block.SetDirection(GetDirection(-gameObject.transform.forward));

OCBlockData blockData = OCBlockData.CreateInstance<OCBlockData>().Init(block, VectorUtil.Vector3ToVector3i(point.Value));
map.SetBlockAndRecompute(blockData, point.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
using GameObject = UnityEngine.GameObject;
using System.Linq;
using System.Collections.Generic;
using OpenCog.Actions;
using OpenCog.BlockSet.BaseBlockSet;

//The private field is assigned but its value is never used
#pragma warning disable 0414
Expand Down Expand Up @@ -84,6 +86,19 @@ public void DestroyBlock(Vector3i? point)
if(point.HasValue)
{
OCMap map = (OCMap)GameObject.FindSceneObjectsOfType(typeof(OCMap)).FirstOrDefault();

OCGoalController[] goalControllers = (OCGoalController[])GameObject.FindObjectsOfType(typeof(OCGoalController));

OCBlock blockType = map.GetBlock(point.Value).block;

foreach(OCGoalController goalController in goalControllers)
{
if(goalController.GoalBlockType == blockType)
{
goalController.FindGoalBlockPositionInChunks(map.GetChunks());
}
}

map.SetBlockAndRecompute(OCBlockData.CreateInstance<OCBlockData>().Init(null, point.Value), point.Value);
}
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/OpenCog Assets/Scripts/OpenCog/Utility/OCConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ public void LoadFromCommandLine()

foreach(string arg in args)
{
if(arg == args[0])
continue;

string[] keyValuePair = arg.Split(':');
if(keyValuePair != null && _settings.ContainsKey(keyValuePair[0]))
{
Expand Down
Binary file modified Assets/Plugins/OCBehaviours.asset
Binary file not shown.
Binary file modified Assets/Plugins/OCBehavioursBuild.dll
Binary file not shown.
3 changes: 1 addition & 2 deletions Assets/Plugins/OCBehavioursInfo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Trees:
Character_ForwardMove
Character_BackwardMove
Character_DownMove
Character_EmoteShow
Character_FaceShow
Character_KickActivate
Character_LeftMove
Expand All @@ -32,7 +31,7 @@ Trees:
Character_TurnAndDestroy
Character_GirlBehaviour
Character_GhostBehaviour
Character_NewTree1
Character_EmoteShow
Unknown

Contexts:
Expand Down

0 comments on commit 39aaa88

Please sign in to comment.