~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/LocationBox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "stacklist.h"
26
26
#include "stack.h"
27
27
#include "FogMap.h"
 
28
#include "GameMap.h"
 
29
#include "stacktile.h"
28
30
 
29
31
#include "xmlhelper.h"
30
32
 
55
57
{
56
58
}
57
59
 
 
60
LocationBox::LocationBox(const LocationBox& loc, Vector<int> pos)
 
61
  : Immovable(pos), d_size(loc.d_size)
 
62
{
 
63
}
 
64
 
58
65
LocationBox::LocationBox(XML_Helper* helper, guint32 size)
59
66
    :Immovable(helper)
60
67
{
67
74
 
68
75
Stack *LocationBox::addArmy(Army *a) const
69
76
{
70
 
    Stack* stack = getFreeStack(a->getOwner());
71
 
 
72
 
    // No stack found in the entire location
73
 
    if (!stack)
74
 
      return NULL;
75
 
 
76
 
    // add army to stack
77
 
    stack->push_back(a);
78
 
    if (stack->countGroupedArmies() == 0)
79
 
      stack->ungroup();
80
 
    if (stack->size() > 1)
81
 
      stack->sortForViewing(true);
82
 
    stack->setDefending(false);
83
 
    stack->setParked(false);
84
 
    return stack;
85
 
}
86
 
 
87
 
Stack* LocationBox::getFreeStack(Player *p) const
88
 
{
89
 
    for (unsigned int i = 0; i < d_size; i++)
90
 
        for (unsigned int j = 0; j < d_size; j++)
91
 
        {
92
 
            Stack* stack = Stacklist::getObjectAt(getPos().x + j, 
93
 
                                                  getPos().y + i);
94
 
 
95
 
            if (stack == NULL)
96
 
            {
97
 
                Vector<int> temp;
98
 
                temp.x = getPos().x + j;
99
 
                temp.y = getPos().y + i;
100
 
                stack = new Stack(p, temp);
101
 
                p->addStack(stack);
102
 
                return stack;
103
 
            }
104
 
            else if (stack->size() < MAX_STACK_SIZE) return stack;
 
77
  Vector<int> pos = Vector<int>(-1,-1);
 
78
  Stack* stack = getFreeStack(a->getOwner(), pos);
 
79
 
 
80
  //no stacks with enough room for one more army, found lets create one.
 
81
  if (!stack)
 
82
    {
 
83
      // No stack found in the entire location
 
84
      if (pos == Vector<int>(-1,-1))
 
85
        return NULL;
 
86
            
 
87
      Player *p = a->getOwner();
 
88
      stack = new Stack(p, pos);
 
89
      stack->add(a);
 
90
      p->addStack(stack);
 
91
    }
 
92
  else
 
93
    stack->add(a);
 
94
 
 
95
  if (stack->size() > 1)
 
96
    stack->sortForViewing(true);
 
97
  stack->setDefending(false);
 
98
  stack->setParked(false);
 
99
  return stack;
 
100
}
 
101
 
 
102
bool LocationBox::isFull(Player *p) const
 
103
{
 
104
  for (unsigned int i = 0; i < d_size; i++)
 
105
    for (unsigned int j = 0; j < d_size; j++)
 
106
      {
 
107
        Vector<int> pos = getPos() + Vector<int>(j,i);
 
108
        StackTile *stile = GameMap::getInstance()->getTile(pos)->getStacks();
 
109
        if (stile->canAdd(1, p) == true)
 
110
          return false;
 
111
      }
 
112
    return true;
 
113
}
 
114
 
 
115
Stack* LocationBox::getFreeStack(Player *p, Vector<int> &tile) const
 
116
{
 
117
  for (unsigned int i = 0; i < d_size; i++)
 
118
    for (unsigned int j = 0; j < d_size; j++)
 
119
      {
 
120
        Vector<int> pos = getPos() + Vector<int>(j,i);
 
121
        if (GameMap::canAddArmy(pos) == false)
 
122
          continue;
 
123
        StackTile *stile = GameMap::getInstance()->getTile(pos)->getStacks();
 
124
        Stack *stack = stile->getFriendlyStack(p);
 
125
        if (stack == NULL)
 
126
          {
 
127
            tile = pos;
 
128
            return NULL;
 
129
          }
 
130
        else 
 
131
          {
 
132
            Stack *enemy = stile->getEnemyStack(p);
 
133
            if (!enemy && stack->isFull() == false)
 
134
              return stack;
 
135
          }
105
136
        }
106
 
    return NULL;
 
137
  tile = Vector<int>(-1,-1);
 
138
  return NULL;
107
139
}
108
140
 
109
141
bool LocationBox::isVisible(Player *player) const
119
151
      }
120
152
  return false;
121
153
}
122
 
void LocationBox::deFog()
 
154
 
 
155
void LocationBox::deFog() const
123
156
{
124
157
  Player *p = Playerlist::getActiveplayer();
125
158
  if (!p)
128
161
  fogmap->alterFogRadius (getPos(), 3, FogMap::OPEN);
129
162
}
130
163
 
131
 
void LocationBox::deFog(Player *p)
 
164
void LocationBox::deFog(Player *p) const
132
165
{
133
166
  if (!p)
134
167
    return;
156
189
      }
157
190
  return true;
158
191
}
 
192
 
 
193
Vector<int> LocationBox::getNearestPos(Movable *m) const
 
194
{
 
195
  return getNearestPos(m->getPos());
 
196
}
 
197
 
 
198
Vector<int> LocationBox::getNearestPos(Vector<int> pos) const
 
199
{
 
200
  int min_dist = -1;
 
201
  Vector<int> closest_tile = Vector<int>(-1,-1);
 
202
  for (unsigned int i = 0; i < d_size; i++)
 
203
    for (unsigned int j = 0; j < d_size; j++)
 
204
      {
 
205
        Vector<int> target = Vector<int>(i,j) + getPos();
 
206
        int d = dist(pos, target);
 
207
        if (d < min_dist || min_dist == -1)
 
208
          {
 
209
            min_dist = d;
 
210
            closest_tile = target;
 
211
          }
 
212
      }
 
213
  return closest_tile;
 
214
}