]> ruin.nu Git - hbs.git/blob - bs/bsdoc.cpp
aa093e896cf625eda216db6308e6602f70362bd5
[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]["Hostile"]["Evil guy"]->setName("Evil guy");
73
74         modified = true;
75         emit documentChanged();
76         return 0;
77 }
78
79 /////////////////////////////////////////////////////////////////////////
80 //
81
82 const BattleList& BSDoc::battles() const
83 {
84         return m_Battles;
85 }
86
87 //////////////////////////////////////////////////////////////////////////
88 //
89 const Fleet* BSDoc::specificFleet(QString battle, QString group, QString fleet) const
90 {
91         for (BattleList::const_iterator i = m_Battles.begin(); i != m_Battles.end(); ++i)
92         {
93                 if (i->first == battle)
94                 {
95                         for (map<QString, map<QString, Fleet*> >::const_iterator j = i->second.begin(); j != i->second.end(); j++)
96                         {
97                                 if (j->first == group)
98                                 {
99                                         for (map<QString, Fleet*>::const_iterator k = j->second.begin(); k != j->second.end(); ++k)
100                                         {
101                                                 if (k->first == fleet)
102                                                 {
103                                                         return k->second;
104                                                 }
105                                         }
106                                 }
107                         }
108                 }
109         }
110         return '\0';
111 }
112
113 //////////////////////////////////////////////////////////////////////////
114 //
115 void BSDoc::changeFleet(QString battle, QString group, QString fleet, const Fleet* fl)
116 {
117         delete m_Battles[battle][group][fleet];
118
119         const Planet* planet = 0;
120         if((planet = dynamic_cast<const Planet*>(fl)))
121         {
122                         m_Battles[battle][group][fleet] = new Planet(*planet);
123         }
124         else
125         {
126                 m_Battles[battle][group][fleet] = new Fleet(*fl);
127         }
128         emit documentChanged();
129 }
130
131
132 //////////////////////////////////////////////////////////////////////////
133 //
134 void BSDoc::newFleet(QString battle, QString group, QString fleet, Fleet* fl)
135 {
136         m_Battles[battle][group][fleet] = fl;
137         emit documentChanged();
138 }
139
140 //////////////////////////////////////////////////////////////////////////
141 //
142 void BSDoc::removeFleet(QString battle, QString group, QString fleet)
143 {
144         delete m_Battles[battle][group][fleet];
145         m_Battles[battle][group].erase(fleet);
146         emit documentChanged();
147 }
148
149 //////////////////////////////////////////////////////////////////////////
150 //
151 void BSDoc::runBattleSimulation()
152 {
153         for(BattleList::iterator i = m_Battles.begin(); i != m_Battles.end(); ++i)
154         {
155                 map<QString, vector<Fleet*> > battle;
156                 for (map<QString, map<QString, Fleet*> >::iterator j = i->second.begin(); j != i->second.end(); ++j)
157                 {
158                         for (map<QString, Fleet*>::iterator k = j->second.begin(); k != j->second.end(); ++k)
159                                 battle[j->first].push_back(k->second);
160                 }
161
162                 Planet* pl = dynamic_cast<Planet*>(i->second["Friendly"]["Home Planet"]);
163
164                 if (pl)
165                 {
166                         pl->runBattle(battle["Friendly"], battle["Hostile"]);
167                 }
168         }
169
170         emit documentChanged();
171 }