Skip to content

Commit

Permalink
Merge branch 'programming'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpages2020 committed May 26, 2020
2 parents ccecd37 + 0b1da3f commit f8be9b2
Show file tree
Hide file tree
Showing 318 changed files with 58,036 additions and 22,981 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "brofiler/Brofiler/Brofiler.h"

AI_Manager::AI_Manager() : j1Module(), beaten_ai_players(0) {
name = ("AI");

name.assign("AI");

players_created = false;
ai_player[0] = ai_player[1] = ai_player[2] = ai_player[3] = nullptr;
}
Expand Down Expand Up @@ -37,6 +39,7 @@ bool AI_Manager::Awake(pugi::xml_node& config) {
ai_info[faction].initial_food = faction_node.attribute("food").as_int();
ai_info[faction].minimum_melees = faction_node.attribute("minimum_melees").as_int();
ai_info[faction].minimum_rangeds = faction_node.attribute("minimum_rangeds").as_int();
ai_info[faction].wave_time = faction_node.attribute("wave_time").as_int();

faction_node = faction_node.next_sibling();
}
Expand Down Expand Up @@ -82,17 +85,7 @@ bool AI_Manager::PostUpdate() {
bool ret = true;
iPoint node_world_position;
SDL_Rect node;
/*
if ((App->render->debug) && (show_nodes))
{
for (int i = 0; i < node_map.size(); i++)
{
node_world_position = App->map->MapToWorld(node_map[i].x, node_map[i].y);
node = { node_world_position.x, node_world_position.y, 6, 6 };
App->render->DrawQuad(node, 0, 0, 255, 255);
}
}
*/

return ret;
}

Expand All @@ -111,4 +104,25 @@ bool AI_Manager::CleanUp() {
return ret;
}

AI_Info AI_Manager::GetAI_PlayerInfo(Faction faction) { return ai_info[faction]; }
AI_Info AI_Manager::GetAI_PlayerInfo(Faction faction) { return ai_info[faction]; }

// Load Game State
bool AI_Manager::Load(pugi::xml_node& data)
{
//camera.x = data.child("camera").attribute("x").as_int();
//camera.y = data.child("camera").attribute("y").as_int();

return true;
}

// Save Game State
bool AI_Manager::Save(pugi::xml_node& data) const
{
//pugi::xml_node cam = data.append_child("camera");

//cam.append_attribute("x") = camera.x;
//cam.append_attribute("y") = camera.y;


return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct AI_Info {
int initial_food;
int minimum_melees;
int minimum_rangeds;
int wave_time;
};

class AI_Manager : public j1Module
Expand All @@ -28,6 +29,10 @@ class AI_Manager : public j1Module
bool PostUpdate();
bool CleanUp();

// Load / Save
bool Load(pugi::xml_node&);
bool Save(pugi::xml_node&) const;

AI_Info GetAI_PlayerInfo(Faction faction);

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@
#include "j1MovementManager.h"
#include "brofiler/Brofiler/Brofiler.h"
#include "j1Pathfinding.h"
#include "Troop.h"
#include "Gatherer.h"
#include <vector>
#include <math.h>

AI_Player::AI_Player(Faction g_faction) : GenericPlayer(), is_attacking(false), last_barrack_to_spawn(1) {
AI_Player::AI_Player(Faction g_faction) : GenericPlayer(), is_attacking(false), last_barrack_to_spawn(1), gatherers_commanded(false) {

name.assign("AI");

faction = g_faction;
caps = App->ai_manager->GetAI_PlayerInfo(faction).initial_caps;
water = App->ai_manager->GetAI_PlayerInfo(faction).initial_water;
food = App->ai_manager->GetAI_PlayerInfo(faction).initial_food;
melee_minimum = App->ai_manager->GetAI_PlayerInfo(faction).minimum_melees;
ranged_minimum = App->ai_manager->GetAI_PlayerInfo(faction).minimum_rangeds;
wave_time = App->ai_manager->GetAI_PlayerInfo(faction).wave_time;
defeated = false;
goal_tile_set = false;
target_player = nullptr;
target_building = nullptr;
target_building_position = { -1, -1 };
base = barrack[0] = barrack[1] = laboratory = nullptr;
wave_timer.Start();
is_ai = true;
}

AI_Player::~AI_Player()
Expand All @@ -45,39 +53,40 @@ bool AI_Player::Update(float dt) {
bool ret = true;

// Gather -----------------------------------------------------

for (int i = 0; i < gatherers_vector.size(); i++)
{
//authomatic gathering
if (gatherers_vector[i]->resource_building == nullptr) {
gatherers_vector[i]->resource_building = App->entities->GetClosestResourceBuilding(gatherers_vector[i]->current_tile);
//if there is at least a resource building left, go there
if (gatherers_vector[i]->resource_building != nullptr) {
gatherers_vector[i]->PathfindToPosition(App->entities->ClosestTile(gatherers_vector[i]->current_tile, gatherers_vector[i]->resource_building->tiles));
gatherers_vector[i]->state = WALK;
}
//if there are no resource buildings left
else
{
gatherers_vector[i]->state = IDLE;

if (!gatherers_commanded) {
for (int i = 0; i < gatherers_vector.size(); i++)
{
//authomatic gathering
if (gatherers_vector[i]->GetResourceBuilding() == nullptr) {
gatherers_vector[i]->AssignResourceBuilding(App->entities->GetClosestResourceBuilding(gatherers_vector[i]->current_tile));
//if there is at least a resource building left, go there
if (gatherers_vector[i]->GetResourceBuilding() != nullptr) {
gatherers_vector[i]->PathfindToPosition(App->entities->ClosestTile(gatherers_vector[i]->current_tile, gatherers_vector[i]->GetResourceBuilding()->tiles));
gatherers_vector[i]->state = WALK;
}
//if there are no resource buildings left
else
{
gatherers_vector[i]->state = IDLE;
}
}
}
gatherers_commanded = true;
}


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

//Spawn Units -------------------------------------------------

//melee-ranged proportion
/*

float mr_proportion = 0;
if (rangeds > 0)
mr_proportion = melees / rangeds;
*/

//spawn melee
if ((barrack[0] != nullptr) &&(water > App->entities->unit_data[faction][MELEE].cost_water)&&(caps > App->entities->unit_data[faction][MELEE].cost_meat) && (last_barrack_to_spawn == 1) ) {
if ((barrack[0] != nullptr) &&(water > App->entities->unit_data[faction][MELEE].cost_water)&&(caps > App->entities->unit_data[faction][MELEE].cost_meat) && (last_barrack_to_spawn == 1) && (mr_proportion < 2)) {
barrack[0]->SpawnUnit(MELEE);
water -= App->entities->unit_data[faction][MELEE].cost_water;
food -= App->entities->unit_data[faction][MELEE].cost_meat;
Expand All @@ -95,26 +104,31 @@ bool AI_Player::Update(float dt) {
//Choose enemy player -----------------------------------------

//if the ai_player is ready choose a player to attack
if ((rangeds >= ranged_minimum) && (melees >= melee_minimum) && (target_player == nullptr)) {
ChooseRandomPlayerEnemy();
is_attacking = true;
if ((wave_timer.ReadSec() > wave_time)&&(!is_attacking)) {
if ((rangeds >= ranged_minimum) && (melees >= melee_minimum) && (target_player == nullptr)) {
ChooseRandomPlayerEnemy();
is_attacking = true;
wave_timer.Start();
LOG("attacking");
}
}

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

// Fight -------------------------------------------------------

//Assign all attacking units an entity to attack
/*

if (is_attacking)
{
//if the target building is destroyed forget about it
if ((target_building!=nullptr) && (target_building->state == DIE))
if ((target_building != nullptr) && (target_building->state == DIE))
target_building = nullptr;

//if there is no target building find one
if (target_building == nullptr) {
target_building = ChooseTargetBuilding();
//if the enemy player has no buildings left choose another player
if (target_building == nullptr) {
ChooseRandomPlayerEnemy();
target_building = ChooseTargetBuilding();
Expand All @@ -126,35 +140,11 @@ bool AI_Player::Update(float dt) {

for (int i = 0; i < troops.size(); i++)
{
if (target_building != nullptr)
{
//if the troop has no target assign one
if ((troops[i]->target_entity == nullptr)||(troops[i]->target_entity != target_building))
{
troops[i]->target_entity = target_building;
}
}
//forget about the target if it is null because it means it has been destroyed
else { troops[i]->target_entity = nullptr; }
//if the unit has no path
if (troops[i]->path_to_target.size() == 0)
{
//and has no node path
if (troops[i]->node_path.size() == 0)
{
//check if it is too far to get to position
//if it is create a node path for it
if (troops[i]->current_tile.DistanceManhattan(target_building_position) > 40)
troops[i]->node_path = App->pathfinding->CreateNodePath(troops[i]->current_tile, target_building_position);
//if it is close enough pathfind to the target
else
troops[i]->PathfindToPosition(target_building_position);
}
}
troops[i]->target_building = target_building;
}

is_attacking = false;
}
*/

return ret;
}
Expand All @@ -171,14 +161,6 @@ void AI_Player::ChooseRandomPlayerEnemy() {

if (target_player == nullptr)
target_player = (GenericPlayer*)App->player;

iPoint origin = { (int)troops[0]->current_tile.x, (int)troops[0]->current_tile.y };
if (target_player->base != nullptr) {
iPoint enemy_base_position = App->entities->ClosestTile(origin, target_player->base->tiles);
}

//CreateNodePath(troops[0]->current_tile, enemy_base_position, path_to_enemy_base);
//App->Mmanager->CreateGroup(troops);
}

DynamicEntity* AI_Player::GetClosestDynamicEntity() {
Expand Down Expand Up @@ -209,4 +191,17 @@ StaticEntity* AI_Player::ChooseTargetBuilding() {
target_building = target_player->base;

return target_building;
}
}

void AI_Player::GatherFood(ResourceBuilding* resource_spot) {

for (int i = 0; i < gatherers; i++)
{
if ((gatherers_vector[i]->GetResourceBuilding() == nullptr)||(gatherers_vector[i]->GetResourceCollected() == 0)) {
gatherers_vector[i]->AssignResourceBuilding(resource_spot);
gatherers_vector[i]->PathfindToPosition(resource_spot->tiles.front());
return;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "GenericPlayer.h"
#include <vector>
#include "p2Point.h"
#include "j1Timer.h"

class j1Entity;
class DynamicEntity;
enum Faction;
class j1Group;
struct ResourceBuilding;

class AI_Player : public GenericPlayer
{
Expand All @@ -16,6 +18,9 @@ class AI_Player : public GenericPlayer
~AI_Player();

bool Update(float dt);
void GatherFood(ResourceBuilding* resource_spot);



private:
void ChooseRandomPlayerEnemy();
Expand All @@ -32,6 +37,9 @@ class AI_Player : public GenericPlayer
j1Group* group;
iPoint target_building_position;
int last_barrack_to_spawn;
int wave_time;
j1Timer wave_timer;
bool gatherers_commanded;
};

#endif // !_AI_PLAYER_H_
Expand Down
Loading

0 comments on commit f8be9b2

Please sign in to comment.