Skip to content

Commit

Permalink
Added in the action to deposit resources at the home base
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisonv789 committed Apr 13, 2022
1 parent 9b39d28 commit 16a2211
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 7 deletions.
Binary file modified A2_Goals/Content/Blueprints/BP_Fruit.uasset
Binary file not shown.
Binary file added A2_Goals/Content/Blueprints/BP_Home.uasset
Binary file not shown.
Binary file modified A2_Goals/Content/Levels/MainLevel.umap
Binary file not shown.
Binary file removed A2_Goals/Content/Materials/Mat_Food.uasset
Binary file not shown.
Binary file modified A2_Goals/Content/Materials/Mat_Fruit.uasset
Binary file not shown.
Binary file added A2_Goals/Content/Materials/Mat_Home.uasset
Binary file not shown.
Binary file modified A2_Goals/Content/Materials/Mat_Merchant.uasset
Binary file not shown.
Binary file modified A2_Goals/Content/Materials/Mat_Stone.uasset
Binary file not shown.
Binary file modified A2_Goals/Content/Materials/Mat_Wood.uasset
Binary file not shown.
14 changes: 12 additions & 2 deletions A2_Goals/Source/A2_Goals/Private/Actions/CollectResourceAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ bool CollectResourceAction::PerformAction(AShip* ship, float deltaTime)
// Increase the current resource gathered
ResourceGathered++;

// Remove the target
Target = nullptr;
// Reset the time
ActionTime = 0.0;

// If action is done, reset target and morale
if (IsActionDone())
{
// Remove the target
Target = nullptr;

// Decrease the morale
ship->Morale--;
}
}

// Return a success
Expand Down
141 changes: 141 additions & 0 deletions A2_Goals/Source/A2_Goals/Private/Actions/DepositResourceAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* FIT3094 ASSIGNMENT 2 - GOAL PLANNING
* Author: Harrison Verrios
*/

#include "Actions/DepositResourceAction.h"

#include "Agents/Ship.h"
#include "Items/ResourceActor.h"
#include "World/GridNode.h"

DepositResourceAction::DepositResourceAction(EGridType resourceType, int resourcesToDeposit)
{
// Reset the action
DepositResourceAction::Reset();

// Update the number of the resources to deposit
ResourcesToDeposit = resourcesToDeposit;

// Updates the resource type
SetResourceType(resourceType);
}


DepositResourceAction::~DepositResourceAction()
{
// Nothing currently
}


void DepositResourceAction::Reset()
{
// Ensure the in range is false
SetInRange(false);
Target = nullptr;
ResourcesDeposited = 0;
ActionTime = 0.0;
}


bool DepositResourceAction::IsActionDone()
{
// Return whether or not the treasure is complete
return ResourcesDeposited >= ResourcesToDeposit;
}


bool DepositResourceAction::CheckProceduralPreconditions(AShip* ship)
{
// Ensure the ship exists
if (!ship)
return false;

// Calculate the nearest resource
GridNode* goalNode = ship->Level->CalculateNearestGoal(ship->XPos, ship->YPos, HOME);

// Check if the resource exists
if (!goalNode || !goalNode->ResourceAtLocation)
return false;

// Get the target
Target = goalNode->ResourceAtLocation;

// Get the distance to the target
const FVector distance = ship->GetActorLocation() - Target->GetActorLocation();

// Check if the distance is less than some amount
// TODO update with some actual collision system
SetInRange(distance.Size() <= 5);

// Return a success
return true;
}


bool DepositResourceAction::PerformAction(AShip* ship, float deltaTime)
{
// Add the current time to the count
ActionTime += deltaTime;

// Get the resource actor
AResourceActor* resourceActor = Cast<AResourceActor>(Target);

// Check if the resource is invalid
if (!resourceActor)
return false;

// Check the action time
if (ActionTime >= TimeToDeposit)
{
// Increase the resource gathered based on the type
switch (ResourceType)
{
case WOOD_RESOURCE:
ship->NumWood--;
break;
case STONE_RESOURCE:
ship->NumStone--;
break;
case FRUIT_RESOURCE:
ship->NumFruit--;
break;

// No default case
default:
break;
}

// Increase the current resource gathered
ResourcesDeposited++;

// Reset the time
ActionTime = 0.0;

// If action is done, reset target and morale
if (IsActionDone())
{
// Remove the target
Target = nullptr;

// Decrease the morale
ship->Morale--;
}
}

// Return a success
return true;
}


bool DepositResourceAction::RequiresInRange()
{
// This must be next to or on top of the agent
return true;
}


void DepositResourceAction::SetResourceType(EGridType resourceType)
{
ResourceType = resourceType;
}
17 changes: 15 additions & 2 deletions A2_Goals/Source/A2_Goals/Private/Agents/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Actions/CollectResourceAction.h"
#include "Actions/CollectTreasureAction.h"
#include "Actions/DepositResourceAction.h"
#include "Planning/GOAPPlanner.h"
#include "Actions/GOAPAction.h"

Expand Down Expand Up @@ -49,6 +50,15 @@ void AShip::SetResourceTarget(EGridType resourceTarget)
resourceAction->AddPrecondition("CollectedResource", false);
resourceAction->AddEffect("CollectedResource", true);
AvailableActions.Add(resourceAction);

// Add in the depositing resource action
DepositResourceAction* depositAction = new DepositResourceAction(ResourceType, resourceAction->ResourceToGather);
depositAction->AddPrecondition("HasMorale", true);
depositAction->AddPrecondition("CollectedResource", true);
depositAction->AddPrecondition("ResourcesDeposited", false);
depositAction->AddEffect("CollectedResource", false);
depositAction->AddEffect("ResourcesDeposited", true);
AvailableActions.Add(depositAction);
}


Expand Down Expand Up @@ -300,6 +310,9 @@ TMap<FString, bool> AShip::GetWorldState()
// TODO: Update with the actual resource
worldState.Add("CollectedResource", GetResourceCollected() > 0);

// TODO: Update this with the home base information
worldState.Add("ResourcesDeposited", false);

// Returns the state
return worldState;
}
Expand All @@ -311,10 +324,10 @@ TMap<FString, bool> AShip::GetGoalState()
TMap<FString, bool> goalState;

// Make sure the morale is valid
//goalState.Add("HasMorale", true);
goalState.Add("HasMorale", true);

// TODO: Update with the real goal
goalState.Add("CollectedResource", true);
goalState.Add("ResourcesDeposited", true);

// Returns the state
return goalState;
Expand Down
7 changes: 6 additions & 1 deletion A2_Goals/Source/A2_Goals/Private/World/LevelGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ void ALevelGenerator::SpawnWorldActors(char grid[MAX_MAP_SIZE][MAX_MAP_SIZE])
world->SpawnActor(LandBlueprint, &position, &FRotator::ZeroRotator);
break;
case 'H':
world->SpawnActor(ShallowBlueprint, &position, &FRotator::ZeroRotator);
world->SpawnActor(HomeBlueprint, &position, &FRotator::ZeroRotator);
tempResource = Cast<AResourceActor>(world->SpawnActor(ResourceBlueprint, &position, &FRotator::ZeroRotator));
tempResource->ResourceType = HOME;
tempResource->XPos = x;
tempResource->YPos = y;
WorldArray[x][y]->ResourceAtLocation = tempResource;
break;
case 'W':
world->SpawnActor(WoodBlueprint, &position, &FRotator::ZeroRotator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class CollectResourceAction : public GOAPAction
{
/************************************************************/
public:

// The resource looking for
EGridType ResourceType;
Expand All @@ -22,7 +23,7 @@ class CollectResourceAction : public GOAPAction
const int ResourceToGather = 50;

// How long does it take to gather treasure?
const int TimeToCollect = 1;
const float TimeToCollect = 0.2f;

// How much resource has been gathered so far?
int ResourceGathered;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CollectTreasureAction : public GOAPAction
const int TreasureToGather = 1;

// How long does it take to gather treasure?
const int TimeToCollect = 1;
const float TimeToCollect = 1.0;

// How many treasure have been gathered so far?
int TreasureGathered;
Expand Down
89 changes: 89 additions & 0 deletions A2_Goals/Source/A2_Goals/Public/Actions/DepositResourceAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* FIT3094 ASSIGNMENT 2 - GOAL PLANNING
* Author: Harrison Verrios
*/

#pragma once

#include "Actions/GOAPAction.h"
#include "World/GridType.h"

/**
* @brief An action that attempts to deposit some resource at the home base
*/
class DepositResourceAction : public GOAPAction
{
/************************************************************/

// The resource looking for
EGridType ResourceType;

// How much resources will be deposited
int ResourcesToDeposit = 50;

// How long does it take to deposit treasure?
const float TimeToDeposit = 0.1f;

// How much resource has been deposited so far?
int ResourcesDeposited;

// How much time has elapsed inside of this action?
float ActionTime;


/************************************************************/

/**
* @brief Resets the action status
*/
virtual void Reset() override;


/************************************************************/
public:

/**
* @brief Deposits some resource of some type
* @param resourceType The type of resource to look for
*/
DepositResourceAction(EGridType resourceType, int resourcesToDeposit);

/**
* @brief Default destructor
*/
virtual ~DepositResourceAction() override;

/**
* @brief Returns whether the action is completed or not
* @return A success flag
*/
virtual bool IsActionDone() override;

/**
* @brief Checks if the current preconditions are valid
* @param ship A reference to the ship agent
* @return A state flag for the conditions
*/
virtual bool CheckProceduralPreconditions(AShip* ship) override;

/**
* @brief Performs the action on a particular agent
* @param ship A reference to the ship agent
* @param deltaTime The current time difference for the action to run at
* @return A success flag for the action
*/
virtual bool PerformAction(AShip* ship, float deltaTime) override;

/**
* @brief Whether or not the action requires something in range
* @return In Range flag
*/
virtual bool RequiresInRange() override;

/**
* @brief Updates the resource type to look for
* @param resourceType A new resource grid type
*/
void SetResourceType(EGridType resourceType);

};
2 changes: 2 additions & 0 deletions A2_Goals/Source/A2_Goals/Public/World/LevelGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class A2_GOALS_API ALevelGenerator : public AActor
UPROPERTY(EditAnywhere, Category = "Entities")
TSubclassOf<AActor> ShallowBlueprint;
UPROPERTY(EditAnywhere, Category = "Entities")
TSubclassOf<AActor> HomeBlueprint;
UPROPERTY(EditAnywhere, Category = "Entities")
TSubclassOf<AActor> WoodBlueprint;
UPROPERTY(EditAnywhere, Category = "Entities")
TSubclassOf<AActor> FruitBlueprint;
Expand Down

0 comments on commit 16a2211

Please sign in to comment.