gamerulespanel.cpp
1 //------------------------------------------------------------------------------
2 // gamerulespanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamerulespanel.h"
24 #include "ui_gamerulespanel.h"
25 
26 #include "ini/ini.h"
27 #include "plugins/engineplugin.h"
28 #include "serverapi/gamecreateparams.h"
30 
31 DClass<GameRulesPanel> : public Ui::GameRulesPanel
32 {
33 public:
34  class GameLimitWidget
35  {
36  public:
37  QWidget* label;
38  QSpinBox* spinBox;
39  GameCVar limit;
40  };
41 
42  QList<GameCVar> gameModifiers;
43  QList<GameLimitWidget*> limitWidgets;
44 };
45 
46 DPointered(GameRulesPanel)
47 
48 GameRulesPanel::GameRulesPanel(QWidget *parent)
49 : QWidget(parent)
50 {
51  d->setupUi(this);
52 }
53 
54 GameRulesPanel::~GameRulesPanel()
55 {
56  qDeleteAll(d->limitWidgets);
57 }
58 
59 void GameRulesPanel::fillInParams(GameCreateParams &params)
60 {
61  params.setSkill(d->cboDifficulty->currentIndex());
62  params.setMaxClients(d->spinMaxClients->value());
63  params.setMaxPlayers(d->spinMaxPlayers->value());
64 
65  fillInLimits(params);
66  fillInModifiers(params);
67 
68  d->mapListPanel->fillInParams(params);
69 }
70 
71 void GameRulesPanel::fillInLimits(GameCreateParams &params)
72 {
73  foreach(PrivData<GameRulesPanel>::GameLimitWidget* p, d->limitWidgets)
74  {
75  p->limit.setValue(p->spinBox->value());
76  params.cvars() << p->limit;
77  }
78 }
79 
80 void GameRulesPanel::fillInModifiers(GameCreateParams &params)
81 {
82  int modIndex = d->cboModifier->currentIndex();
83  if (modIndex > 0) // Index zero is always "< NONE >"
84  {
85  --modIndex;
86  d->gameModifiers[modIndex].setValue(1);
87  params.cvars() << d->gameModifiers[modIndex];
88  }
89 }
90 
91 void GameRulesPanel::loadConfig(Ini &config)
92 {
93  IniSection section = config.section("Rules");
94 
95  d->cboDifficulty->setCurrentIndex(section["difficulty"]);
96  d->cboModifier->setCurrentIndex(section["modifier"]);
97  d->spinMaxClients->setValue(section["maxClients"]);
98  d->spinMaxPlayers->setValue(section["maxPlayers"]);
99  foreach (PrivData<GameRulesPanel>::GameLimitWidget* widget, d->limitWidgets)
100  {
101  widget->spinBox->setValue(section[widget->limit.command()]);
102  }
103 
104  d->mapListPanel->loadConfig(config);
105 }
106 
107 void GameRulesPanel::saveConfig(Ini &config)
108 {
109  IniSection section = config.section("Rules");
110 
111  section["difficulty"] = d->cboDifficulty->currentIndex();
112  section["modifier"] = d->cboModifier->currentIndex();
113  section["maxClients"] = d->spinMaxClients->value();
114  section["maxPlayers"] = d->spinMaxPlayers->value();
115  foreach (PrivData<GameRulesPanel>::GameLimitWidget *widget, d->limitWidgets)
116  {
117  section[widget->limit.command()] = widget->spinBox->value();
118  }
119 
120  d->mapListPanel->saveConfig(config);
121 }
122 
123 void GameRulesPanel::setupForEngine(const EnginePlugin *engine, const GameMode &gameMode)
124 {
125  setupDifficulty();
126  setupModifiers(engine);
127  d->mapListPanel->setupForEngine(engine);
128 
129  setupLimitWidgets(engine, gameMode);
130 }
131 
132 void GameRulesPanel::setupForRemoteGame()
133 {
134  QWidget *disableControls[] =
135  {
136  d->spinMaxClients, d->spinMaxPlayers, NULL
137  };
138  for (int i = 0; disableControls[i]; ++i)
139  disableControls[i]->setDisabled(true);
140 }
141 
142 void GameRulesPanel::setupDifficulty()
143 {
144  d->cboDifficulty->clear();
145 
146  d->cboDifficulty->addItem("1 - I'm too young to die", 0);
147  d->cboDifficulty->addItem("2 - Hey, not too rough", 1);
148  d->cboDifficulty->addItem("3 - Hurt me plenty", 2);
149  d->cboDifficulty->addItem("4 - Ultra-violence", 3);
150  d->cboDifficulty->addItem("5 - NIGHTMARE!", 4);
151 }
152 
153 void GameRulesPanel::setupModifiers(const EnginePlugin *engine)
154 {
155  d->cboModifier->clear();
156  d->gameModifiers.clear();
157 
158  const QList<GameCVar> &modifiers = engine->data()->gameModifiers;
159 
160  if (!modifiers.isEmpty())
161  {
162  d->cboModifier->show();
163  d->labelModifiers->show();
164 
165  d->cboModifier->addItem(tr("< NONE >"));
166 
167  foreach (const GameCVar &cvar, modifiers)
168  {
169  d->cboModifier->addItem(cvar.name());
170  d->gameModifiers << cvar;
171  }
172  }
173  else
174  {
175  d->cboModifier->hide();
176  d->labelModifiers->hide();
177  }
178 }
179 
180 void GameRulesPanel::removeLimitWidgets()
181 {
182  foreach (PrivData<GameRulesPanel>::GameLimitWidget *widget, d->limitWidgets)
183  {
184  delete widget->label;
185  delete widget->spinBox;
186  delete widget;
187  }
188 
189  d->limitWidgets.clear();
190 }
191 
192 void GameRulesPanel::setupLimitWidgets(const EnginePlugin *engine, const GameMode &gameMode)
193 {
194  removeLimitWidgets();
195  QList<GameCVar> limits = engine->limits(gameMode);
196  QList<GameCVar>::iterator it;
197 
198  int number = 0;
199  for (it = limits.begin(); it != limits.end(); ++it, ++number)
200  {
201  QLabel* label = new QLabel(this);
202  label->setText(it->name());
203  QSpinBox* spinBox = new QSpinBox(this);
204  spinBox->setMaximum(999999);
205 
206  d->limitsLayout->addRow(label, spinBox);
207 
209  glw->label = label;
210  glw->spinBox = spinBox;
211  glw->limit = (*it);
212  d->limitWidgets << glw;
213  }
214 }
const QString & name() const
Nice name to display to user in Create Game dialog and in other widgets.
Game parametrization data used when creating new games.
QList< GameCVar > gameModifiers
Returns a list of modifiers.
Definition: engineplugin.h:145
Game mode representation.
Configuration handler.
Definition: ini.h:69
Definition: dptr.h:31
QList< GameCVar > & cvars()
Contents of this list will be passed as "+consoleCommand value" to the command line.
INI section representation.
Definition: inisection.h:40
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91
virtual QList< GameCVar > limits(const GameMode &mode) const
Returns a list of limits (like fraglimit) supported by passed gamemode.
Definition: engineplugin.h:250
A general game setting or variable (like fraglimit).