File indexing completed on 2024-03-24 04:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net>
0003     SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org>
0004     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0006     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "fleet.h"
0012 #include "planet.h"
0013 
0014 //---------------------------------------------------------------------------
0015 // class Fleet
0016 //   \---class AttackFleet
0017 //    \---class DefenseFleet
0018 //---------------------------------------------------------------------------
0019 
0020 
0021 Fleet::Fleet( long long initialShipCount )
0022   : m_shipCount( initialShipCount )
0023 {
0024 }
0025 
0026 void
0027 Fleet::removeShips( long long lostShips )
0028 {
0029     m_shipCount -= lostShips;
0030 }
0031 
0032 AttackFleet::AttackFleet( Planet *src, Planet *dest, long long initialCount, int arrival )
0033 : Fleet( initialCount ), owner( src->player() ), source( src ), destination( dest ), arrivalTurn( arrival )
0034 {
0035 }
0036 
0037 DefenseFleet::DefenseFleet( Planet *newHome, long long initialCount ) : Fleet( initialCount ), home( newHome )
0038 {
0039 }
0040 
0041 void
0042 DefenseFleet::absorb( AttackFleet *fleet )
0043 {
0044     m_shipCount += fleet->shipCount();
0045 }
0046 
0047 void
0048 DefenseFleet::become( AttackFleet *fleet )
0049 {
0050     m_shipCount = fleet->shipCount();
0051 }
0052 
0053 
0054 AttackFleet *
0055 DefenseFleet::spawnAttackFleet( Planet *dest, long long count, int arrivalTurn )
0056 {
0057     if( m_shipCount < count ) {
0058         return nullptr;
0059     }
0060 
0061     AttackFleet *newFleet = new AttackFleet( home, dest, count, arrivalTurn  );
0062 
0063     removeShips( count );
0064 
0065     Q_EMIT update();
0066     
0067     return newFleet;
0068 }
0069 
0070 void
0071 DefenseFleet::addShips( long long newShips )
0072 {
0073     m_shipCount += newShips;
0074 
0075     if(m_shipCount < 0) /* to allow for negative production planets */
0076         m_shipCount = 0;
0077 }
0078 
0079 
0080 #include "moc_fleet.cpp"