File: AI_Diplomacy.cpp

package info (click to toggle)
lordsawar 0.3.2%2Bfrogknows-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,716 kB
  • sloc: cpp: 103,193; sh: 4,965; xml: 2,220; makefile: 1,371
file content (182 lines) | stat: -rw-r--r-- 5,300 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//  Copyright (C) 2007, 2008, 2009, 2014, 2015 Ben Asselstine
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
//  02110-1301, USA.

#include <iostream>
#include "AI_Diplomacy.h"
#include "player.h"
#include "playerlist.h"
#include "citylist.h"
#include "history.h"
#include "game.h"
#include "GameScenarioOptions.h"
#include "city.h"

//#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::flush<<std::endl;}
#define debug(x)

AI_Diplomacy* AI_Diplomacy::instance = 0;

AI_Diplomacy::AI_Diplomacy(Player *owner)
:d_owner(owner)
{
  instance = this;
}

AI_Diplomacy::~AI_Diplomacy()
{
  instance = 0;
}

void AI_Diplomacy::considerCuspOfWar()
{
  if (GameScenarioOptions::s_cusp_of_war &&
      GameScenarioOptions::s_round == CUSP_OF_WAR_ROUND)
  {
    for (auto other: *Playerlist::getInstance())
    {
      if (other->getType() == Player::HUMAN && !other->isDead() &&
          d_owner->getDiplomaticState(other) != Player::AT_WAR)
      {
        d_owner->proposeDiplomacy (Player::PROPOSE_WAR, other);
        other->proposeDiplomacy (Player::PROPOSE_WAR, d_owner);
        d_owner->declareDiplomacy (Player::AT_WAR, other, false);
      }
    }
  }
}

void AI_Diplomacy::makeFriendsAndEnemies()
{
  // Declare war with enemies, make peace with friends
  // according to their diplomatic scores
  for (auto it: *Playerlist::getInstance())
    {
      if (Playerlist::getInstance()->getNeutral() == it)
	continue;
      if (it->isDead())
	continue;
      if (it == d_owner)
	continue;
      if (d_owner->getDiplomaticState(it) != Player::AT_WAR)
	{
	  if (d_owner->getDiplomaticScore (it) < DIPLOMACY_MIN_SCORE + 2)
	    d_owner->proposeDiplomacy (Player::PROPOSE_WAR , it);
	}
      else if (d_owner->getDiplomaticState(it) != Player::AT_PEACE)
	{
	  if (d_owner->getDiplomaticScore (it) > DIPLOMACY_MAX_SCORE - 2)
	    d_owner->proposeDiplomacy (Player::PROPOSE_PEACE, it);
	}
    }
}

void AI_Diplomacy::makeRequiredEnemies()
{
  for (std::list<Player *>::iterator it = new_enemies.begin();
       it != new_enemies.end(); it++)
    d_owner->proposeDiplomacy (Player::PROPOSE_WAR , *it);
}

void AI_Diplomacy::neutralsDwindlingNeedFirstEnemy()
{
  // find a close player if neutral cities are getting low
  int target_level = (int)((float)Citylist::getInstance()->size() * (float) 0.06);
  target_level++;
  bool at_war = false;
  guint32 neutral_cities = 
    Citylist::getInstance()->countCities(Playerlist::getInstance()->getNeutral());
  if (neutral_cities && (int)neutral_cities > target_level)
    {
      //Pick a new opponent if we don't already have one.
      for (auto it: *Playerlist::getInstance())
	{
	  if (Playerlist::getInstance()->getNeutral() == it)
	    continue;
	  if (it->isDead())
	    continue;
	  if (it == d_owner)
	    continue;
	  if (d_owner->getDiplomaticState(it) != Player::AT_WAR)
	    at_war = true;
	}
      if (at_war == false)
	{
	  // not at war?  great.  let's pick a player to attack.
	  City *first = d_owner->getFirstCity();
	  if (first)
	    {
	      City *c =
                Citylist::getInstance()->getNearestForeignCity(first->getPos());
	      if (c)
		d_owner->proposeDiplomacy(Player::PROPOSE_WAR, c->getOwner());
	    }
	}
    }
}

void AI_Diplomacy::gangUpOnTheBully()
{
  // declare war with the strong player.
  // declare peace with every other.
  int target_level = (int)((float)Citylist::getInstance()->size() * (float)0.35);
  if (Playerlist::getInstance()->countPlayersAlive() <  MAX_PLAYERS / 2)
    return;
  for (auto it: *Playerlist::getInstance())
    {
      if (Playerlist::getInstance()->getNeutral() == it)
	continue;
      if (it->isDead())
	continue;
      if (it == d_owner)
	continue;
      if (Citylist::getInstance()->countCities(it) > target_level && 
          Playerlist::getInstance()->countPlayersAlive() > 4)
	{
	  if (d_owner->getDiplomaticState(it) != Player::AT_WAR)
	    d_owner->proposeDiplomacy(Player::PROPOSE_WAR, it);
          for (auto pit: *Playerlist::getInstance())
	    {
	      if (Playerlist::getInstance()->getNeutral() == pit)
		continue;
	      if (pit->isDead())
		continue;
	      if (pit == d_owner)
		continue;
	      if (pit == it)
		continue;
	      if (pit->getType() == Player::HUMAN)
		continue;
	      if (d_owner->getDiplomaticState(pit) != Player::AT_PEACE)
		d_owner->declareDiplomacy(Player::AT_PEACE, pit, false);
	    }
	}
    }
}

void AI_Diplomacy::makeProposals()
{
  makeFriendsAndEnemies();
  makeRequiredEnemies();
  neutralsDwindlingNeedFirstEnemy();
  gangUpOnTheBully();
}

void AI_Diplomacy::needNewEnemy(Player *player)
{
  new_enemies.push_back(player);
}
// End of file