1 // Copyright (C) 2004 John Farrell
2 // Copyright (C) 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2005 Andrea Paternesi
4 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Library General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "stackreflist.h"
27 #include "playerlist.h"
28 #include "AI_Analysis.h"
32 Threat::Threat(City *c)
33 :Ownable(*c), d_city(c), d_ruin(0), d_danger(0), d_value(0), d_strength(0)
35 d_stacks = new StackReflist();
39 Threat::Threat(Stack *s)
40 :Ownable(*s), d_city(0), d_ruin(0), d_danger(0), d_value(0), d_strength(0)
42 d_stacks = new StackReflist();
43 d_stacks->addStack(new Stack(*s));
44 d_strength += AI_Analysis::assessStackStrength(s);
47 Threat::Threat(Ruin *r)
48 :Ownable((Player *)0), d_city(0), d_ruin(r), d_danger(0), d_value(0), d_strength(0)
50 d_stacks = new StackReflist();
55 for (StackReflist::iterator i = d_stacks->begin(); i != d_stacks->end(); i++)
61 std::string Threat::toString() const
65 return d_city->getName() + " owned by " + d_owner->getName();
69 return d_ruin->getName();
73 return "stack owned by " + d_owner->getName();
77 bool Threat::Near(Vector<int> pos, Player *p) const
84 return d_city->contains(pos);
88 return d_ruin->contains(pos);
91 for (StackReflist::const_iterator it = d_stacks->begin();
92 it != d_stacks->end(); ++it)
94 Vector<int> spos = (*it)->getPos();
95 if (abs(pos.x - spos.x) <= 1 && abs(pos.y - spos.y <= 1))
102 void Threat::addStack(Stack *stack)
104 d_stacks->addStack(new Stack (*stack));
105 if (d_city && d_city->getOwner() != Playerlist::getInstance()->getNeutral())
106 d_strength += AI_Analysis::assessStackStrength(stack);
109 // this is the strength of the threat to us
110 void Threat::calculateStrength()
112 // neutral cities poses a small threat
114 if (d_city->getOwner() == Playerlist::getInstance()->getNeutral())
120 for (StackReflist::const_iterator i = d_stacks->begin();
121 i != d_stacks->end(); ++i)
122 score += AI_Analysis::assessStackStrength(*i);
127 void Threat::calculateValue()
134 Vector<int> Threat::getClosestPoint(Vector<int> location) const
136 Vector<int> result(-1,-1);
138 result = d_city->getNearestPos(location);
140 result = d_ruin->getPos();
144 for (StackReflist::const_iterator it = d_stacks->begin();
145 it != d_stacks->end(); ++it)
147 Vector<int> spos = (*it)->getPos();
149 int distance = dist(spos, location);
150 if (distance < min_dist || min_dist == -1)
161 void Threat::deleteStack(guint32 id)
163 d_stacks->removeStack(id);
164 if (d_city && d_city->getOwner() != Playerlist::getInstance()->getNeutral())
167 void Threat::deleteStack(Stack* s)
169 d_stacks->removeStack(s->getId());
170 if (d_city && d_city->getOwner() != Playerlist::getInstance()->getNeutral())
174 void Threat::addDanger(float danger)