1 | /* |
2 | Copyright 2003 Russell Steffen <rsteffen@bayarea.net> |
3 | Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org> |
4 | Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com> |
5 | Copyright 2006 Inge Wallin <inge@lysator.liu.se> |
6 | Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com> |
7 | |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "fleet.h" |
24 | #include "planet.h" |
25 | |
26 | //--------------------------------------------------------------------------- |
27 | // class Fleet |
28 | // \---class AttackFleet |
29 | // \---class DefenseFleet |
30 | //--------------------------------------------------------------------------- |
31 | |
32 | |
33 | Fleet::Fleet( int initialShipCount ) |
34 | : m_shipCount( initialShipCount ) |
35 | { |
36 | } |
37 | |
38 | void |
39 | Fleet::removeShips( int lostShips ) |
40 | { |
41 | m_shipCount -= lostShips; |
42 | } |
43 | |
44 | AttackFleet::AttackFleet( Planet *src, Planet *dest, int initialCount, int arrival ) |
45 | : Fleet( initialCount ), owner( src->player() ), source( src ), destination( dest ), arrivalTurn( arrival ) |
46 | { |
47 | } |
48 | |
49 | DefenseFleet::DefenseFleet( Planet *newHome, int initialCount ) : Fleet( initialCount ), home( newHome ) |
50 | { |
51 | } |
52 | |
53 | void |
54 | DefenseFleet::absorb( AttackFleet *fleet ) |
55 | { |
56 | m_shipCount += fleet->shipCount(); |
57 | } |
58 | |
59 | void |
60 | DefenseFleet::become( AttackFleet *fleet ) |
61 | { |
62 | m_shipCount = fleet->shipCount(); |
63 | } |
64 | |
65 | |
66 | AttackFleet * |
67 | DefenseFleet::spawnAttackFleet( Planet *dest, int count, int arrivalTurn ) |
68 | { |
69 | if( m_shipCount < count ) { |
70 | return NULL; |
71 | } |
72 | |
73 | AttackFleet *newFleet = new AttackFleet( home, dest, count, arrivalTurn ); |
74 | |
75 | removeShips( count ); |
76 | |
77 | emit update(); |
78 | |
79 | return newFleet; |
80 | } |
81 | |
82 | void |
83 | DefenseFleet::addShips( int newShips ) |
84 | { |
85 | m_shipCount += newShips; |
86 | |
87 | if(m_shipCount < 0) /* to allow for negative production planets */ |
88 | m_shipCount = 0; |
89 | } |
90 | |
91 | |