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