From d16d9980343f2e074ee8530418e340c51b26b963 Mon Sep 17 00:00:00 2001
From: Tim Schubert <tim.schubert@tu-bs.de>
Date: Fri, 29 Jan 2016 18:58:11 +0100
Subject: [PATCH] Changed the method by which to se;ect field to attack.  Fix
 an issue with computing the attacker's cluster from which to draw resources
 from for attacking.

---
 src/Gameplay.cpp | 60 ++++++++++++++++++++++--------------------------
 src/Gameplay.hpp | 16 +++++--------
 src/Gui.cpp      |  1 +
 3 files changed, 35 insertions(+), 42 deletions(-)

diff --git a/src/Gameplay.cpp b/src/Gameplay.cpp
index 902088c..52e4784 100644
--- a/src/Gameplay.cpp
+++ b/src/Gameplay.cpp
@@ -156,9 +156,15 @@ void FieldMeta::regenerate_resources()
     if (this->upgrades[Regeneration_1])
         this->resources *= 2;
     if (this->upgrades[Regeneration_2])
-        this->resources *= 2;
+        this->resources *= 4;
     if (this->upgrades[Regeneration_3])
-        this->resources *= 2;
+        this->resources *= 8;
+    if (this->upgrades[Reproduction_1])
+        this->reproduction *= 1.25;
+    if (this->upgrades[Reproduction_2])
+        this->reproduction *= 1.50;
+    if (this->upgrades[Reproduction_2])
+        this->reproduction *= 2.0;
     trigger_event(BOB_FIELDUPDATEEVENT, 0, (void *) this, nullptr);
     this->changed = true;
 }
@@ -275,21 +281,21 @@ bool Player::fight(FieldMeta *field)
         {
             continue;
         }
-        if (*(neighbor->get_owner()) == *this) // comparison by UUID
-        {
-            power_level -= neighbor->get_offense();
-            is_neighbor = true;
-        }
-        else if (*(neighbor->get_owner()) == *(field->get_owner()))
+        if (*(neighbor->get_owner()) == *this) // comparison by UUID, attacking player
         {
             Cluster temp_attackers_cluster = neighbor->get_grid()->get_cluster(neighbor);
             attackers_cluster.insert(temp_attackers_cluster.begin(), temp_attackers_cluster.end());
+            power_level -= neighbor->get_offense();
+            is_neighbor = true;
+        }
+        else if (*(neighbor->get_owner()) == *(field->get_owner())) // attacked player
+        {
             power_level += neighbor->get_defense();
         }
         // else: ignore, field / player not part of the fight (e.g. default player)
     }
     Resource costs = {(Uint32) std::abs(power_level), (Uint32) std::abs(power_level), (Uint32) std::abs(power_level)};
-    if (power_level < 0 && is_neighbor) // attacking player has won
+    if (power_level < 1 && is_neighbor) // attacking player has won
     {
         field->get_grid()->consume_resources_of_cluster(&attackers_cluster, costs);
         field->get_grid()->consume_resources_of_cluster(&defenders_cluster, costs);
@@ -329,7 +335,7 @@ void FieldMeta::load(SDL_Renderer *renderer, Layout *layout)
     }
     if (this->owner->get_id().is_nil())
         color = {0x77, 0x77, 0x77, 0xff};
-    if (this->fighting)
+    if (this->get_grid()->get_attack_marker() == this)
         color = {0x0, 0x77, 0x77, 0xff};
     filledPolygonRGBA(renderer, vx, vy, 6, color.r, color.g, color.b, 0x77);
     SDL_Color inverse;
@@ -435,8 +441,6 @@ void HexagonGrid::render(Renderer *renderer)
 
 void HexagonGrid::handle_event(SDL_Event *event)
 {
-    Player *attacking;
-    Player *owner;
     SDL_Point mouse = {0, 0};
     SDL_GetMouseState(&mouse.x, &mouse.y);
     int scroll = this->layout->size / 10 * event->wheel.y;
@@ -490,30 +494,19 @@ void HexagonGrid::handle_event(SDL_Event *event)
                     this->changed = true;
                     break;
                 case SDL_BUTTON_LEFT:
-                    if (this->selecting)
+                    if (this->placing)
                     {
                         trigger_event(BOB_FIELDSELECTEDEVENT, 0, (void *) this->marker, nullptr);
-                        this->selecting = false;
+                        this->placing = false;
                     }
-                    else
+                    else if (this->attack_marker != nullptr && this->attack_marker == this->marker)
                     {
-                        owner = this->marker->get_owner();
-                        if (*owner == *(Player::current_player))
-                        {
-                            if (this->first_attack != nullptr)
-                            {
-                                this->first_attack->set_fighting(false);
-                            }
-                            this->first_attack = this->marker;
-                            this->first_attack->set_fighting(true);
-                        }
-                        else if (this->first_attack != nullptr)
-                        {
-                            attacking = this->first_attack->get_owner();
-                            attacking->fight(this->marker);
-                            this->first_attack->set_fighting(false);
-                            this->first_attack = nullptr;
-                        }
+                        Player::current_player->fight(this->attack_marker);
+                        this->attack_marker = nullptr;
+                    }
+                    else if (this->attack_marker == nullptr)
+                    {
+                        this->attack_marker = this->marker;
                     }
                     changed = true;
                     break;
@@ -554,6 +547,8 @@ void HexagonGrid::handle_event(SDL_Event *event)
                 for (auto foo : aquired)
                 {
                     foo->set_owner(Player::current_player);
+                    foo->set_defense(1);
+                    foo->set_offense(1);
                 }
                 this->changed = true;
             }
@@ -679,6 +674,7 @@ bool HexagonGrid::place(Player *player, FieldMeta *center)
         {
             i->set_owner(player);
             i->set_offense(1);
+            i->set_defense(1);
         }
         return true;
     }
diff --git a/src/Gameplay.hpp b/src/Gameplay.hpp
index 8e84300..8ea2520 100644
--- a/src/Gameplay.hpp
+++ b/src/Gameplay.hpp
@@ -566,7 +566,6 @@ public:
         std::default_random_engine generator;
         std::normal_distribution<double> distribution(0.0, 1.0);
         this->reproduction = distribution(generator);
-        this->fighting = false;
         this->upgrades = 0;
         static std::random_device rd;
         std::mt19937 rng(rd());
@@ -611,15 +610,10 @@ public:
 
     FieldMeta *get_neighbor(Uint8 direction);
 
-    bool get_fighting() { return this->fighting; }
-
-    void set_fighting(bool state) { this->fighting = state; }
-
     double get_reproduction() { return this->reproduction; }
 
 private:
     double reproduction;
-    bool fighting;
     bool changed;
     const Field field;
     HexagonGrid *grid;
@@ -639,7 +633,7 @@ public:
     HexagonGrid(Sint16 grid_radius, Layout *layout_, Renderer *renderer_)
             : layout(layout_), radius(grid_radius), renderer(renderer_)
     {
-        this->first_attack = nullptr;
+        this->attack_marker = nullptr;
         this->texture = nullptr;
         this->panning = false;
         std::unordered_map<Field, FieldMeta *> fields = std::unordered_map<Field, FieldMeta *>();
@@ -702,12 +696,14 @@ public:
 
     bool place(Player *player, FieldMeta *center);
 
-    void set_selecting(bool state) { this->selecting = state; }
+    void set_selecting(bool state) { this->placing = state; }
+
+    FieldMeta *get_attack_marker() { return this->attack_marker; }
 
 private:
     bool changed;
-    bool selecting;
-    FieldMeta *first_attack;
+    bool placing;
+    FieldMeta *attack_marker;
     Renderer *renderer;
     SDL_Texture *texture;
     std::unordered_map<Field, FieldMeta *> fields;
diff --git a/src/Gui.cpp b/src/Gui.cpp
index dd8efe1..a9e082b 100644
--- a/src/Gui.cpp
+++ b/src/Gui.cpp
@@ -100,6 +100,7 @@ void FieldBox::update()
     Resource field_resources = this->field->get_resources();
     std::ostringstream output;
     output << this->field->get_owner()->get_name() << "\n"
+    << "off " << this->field->get_offense() << "\ndef " << this->field->get_defense() << "\n"
     << "● " << (int) cluster_resources.circle << " (" << (int) field_resources.circle << ")" << "\n"
     << "▲ " << (int) cluster_resources.triangle << " (" << (int) field_resources.triangle << ")" << "\n"
     << "■ " << (int) cluster_resources.square << " (" << (int) field_resources.square << ")" << "\n";