]> ruin.nu Git - hbs.git/blob - bs/bsdoc.cpp
added code so you lose ships if you steal..
[hbs.git] / bs / bsdoc.cpp
1 /***************************************************************************
2                           bcdoc.cpp  -  description
3                              -------------------
4     begin                : Sun May 27 22:13:58 CEST 2001
5     copyright            : (C) 2001 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "bsdoc.h"
19
20 #include "planet.h"
21
22 using namespace std;
23
24 BSDoc::BSDoc()
25 {
26   modified = false;
27 }
28
29 BSDoc::~BSDoc()
30 {
31 }
32
33 void BSDoc::newDoc()
34 {
35 }
36
37 bool BSDoc::save()
38 {
39   return true;
40 }
41
42 bool BSDoc::saveAs(const QString &filename)
43 {
44         QString test = filename;
45   return true;
46 }
47
48 bool BSDoc::load(const QString &filename)
49 {
50   QString test = filename;
51         emit documentChanged();
52   return true;
53 }
54
55 bool BSDoc::isModified() const
56 {
57   return modified;
58 }
59
60 ///////////////////////////////////////////////////////////////////////////
61 //
62
63 int BSDoc::newBattle(QString name)
64 {
65         m_Battles[name]["Friendly"]["Home Planet"] = new Planet();;
66         m_Battles[name]["Friendly"]["Home Fleet"] = new Fleet();
67         m_Battles[name]["Hostile"]["Evil guy"] = new Fleet();
68
69         m_Battles[name]["Friendly"]["Home Planet"]->setName("Home Planet");
70         m_Battles[name]["Friendly"]["Home Fleet"]->setName("Home Fleet");
71         m_Battles[name]["Friendly"]["Home Fleet"]->setRace("Terran");
72         m_Battles[name]["Friendly"]["Home Fleet"]->setStays(-1);
73         m_Battles[name]["Hostile"]["Evil guy"]->setName("Evil guy");
74         m_Battles[name]["Hostile"]["Evil guy"]->setETA(1);
75
76         modified = true;
77         emit documentChanged();
78         return 0;
79 }
80
81 /////////////////////////////////////////////////////////////////////////
82 //
83
84 const BattleList& BSDoc::battles() const
85 {
86         return m_Battles;
87 }
88
89 //////////////////////////////////////////////////////////////////////////
90 //
91 const Fleet* BSDoc::specificFleet(QString battle, QString group, QString fleet) const
92 {
93         for (BattleList::const_iterator i = m_Battles.begin(); i != m_Battles.end(); ++i)
94         {
95                 if (i->first == battle)
96                 {
97                         for (map<QString, map<QString, Fleet*> >::const_iterator j = i->second.begin(); j != i->second.end(); j++)
98                         {
99                                 if (j->first == group)
100                                 {
101                                         for (map<QString, Fleet*>::const_iterator k = j->second.begin(); k != j->second.end(); ++k)
102                                         {
103                                                 if (k->first == fleet)
104                                                 {
105                                                         return k->second;
106                                                 }
107                                         }
108                                 }
109                         }
110                 }
111         }
112         return '\0';
113 }
114
115 //////////////////////////////////////////////////////////////////////////
116 //
117 void BSDoc::changeFleet(QString battle, QString group, QString fleet, const Fleet* fl)
118 {
119         delete m_Battles[battle][group][fleet];
120
121         const Planet* planet = 0;
122         if((planet = dynamic_cast<const Planet*>(fl)))
123         {
124                         m_Battles[battle][group][fleet] = new Planet(*planet);
125         }
126         else
127         {
128                 m_Battles[battle][group][fleet] = new Fleet(*fl);
129         }
130         emit documentChanged();
131 }
132
133
134 //////////////////////////////////////////////////////////////////////////
135 //
136 void BSDoc::newFleet(QString battle, QString group, QString fleet, Fleet* fl)
137 {
138         m_Battles[battle][group][fleet] = fl;
139         emit documentChanged();
140 }
141
142 //////////////////////////////////////////////////////////////////////////
143 //
144 void BSDoc::removeFleet(QString battle, QString group, QString fleet)
145 {
146         delete m_Battles[battle][group][fleet];
147         m_Battles[battle][group].erase(fleet);
148         emit documentChanged();
149 }
150
151 //////////////////////////////////////////////////////////////////////////
152 //
153 void BSDoc::runBattleSimulation()
154 {
155         for(BattleList::iterator i = m_Battles.begin(); i != m_Battles.end(); ++i)
156         {
157                 map<QString, vector<Fleet*> > battle;
158                 for (map<QString, map<QString, Fleet*> >::iterator j = i->second.begin(); j != i->second.end(); ++j)
159                 {
160                         for (map<QString, Fleet*>::iterator k = j->second.begin(); k != j->second.end(); ++k)
161                                 battle[j->first].push_back(k->second);
162                 }
163
164                 Planet* pl = dynamic_cast<Planet*>(i->second["Friendly"]["Home Planet"]);
165
166                 if (pl)
167                 {
168                         pl->runBattle(battle["Friendly"], battle["Hostile"]);
169                 }
170         }
171
172         emit documentChanged();
173 }