Freeciv21
Develop your civilization from humble roots to a global empire
pregameoptions.cpp
Go to the documentation of this file.
1 /*
2  ____ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3  / \__ contributors. This file is part of Freeciv21.
4 |\ / @ \ Freeciv21 is free software: you can redistribute it
5 \ \_______| \ .:|> and/or modify it under the terms of the GNU
6  \ ##| | \__/ General Public License as published by the Free
7  | ####\__/ \ Software Foundation, either version 3 of the License,
8  / / ## \| or (at your option) any later version.
9  / /__________\ \ You should have received a copy of the
10  L_JJ \__JJ GNU General Public License along with Freeciv21.
11  If not, see https://www.gnu.org/licenses/.
12  */
13 
14 // Qt
15 #include "fcintl.h"
16 #include <QAction>
17 #include <QComboBox>
18 #include <QGridLayout>
19 #include <QSpinBox>
20 #include <QSplitter>
21 // common
22 #include "chatline_common.h"
23 #include "colors_common.h"
24 #include "connectdlg_common.h"
25 // client
26 #include "client_main.h"
27 #include "climisc.h"
28 #include "dialogs.h"
29 #include "fc_client.h"
30 #include "icons.h"
31 
32 #include "pregameoptions.h"
33 
34 void option_dialog_popup(const char *name, const struct option_set *poptset);
38 pregame_options::pregame_options(QWidget *parent) : QWidget(parent)
39 {
40  int level;
41  ui.setupUi(this);
42 
43  ui.max_players->setRange(1, MAX_NUM_PLAYERS);
44  connect(ui.nation, &QPushButton::clicked, this,
46  ui.qclientoptions->setText(_("Interface Options"));
47  connect(ui.qclientoptions, &QAbstractButton::clicked,
48  [=]() { popup_client_options(); });
49  for (level = 0; level < AI_LEVEL_COUNT; level++) {
50  if (is_settable_ai_level(static_cast<ai_level>(level))) {
51  const char *level_name =
52  ai_level_translated_name(static_cast<ai_level>(level));
53  ui.ailevel->addItem(level_name, level);
54  }
55  }
56  ui.ailevel->setCurrentIndex(-1);
57  connect(ui.max_players, QOverload<int>::of(&QSpinBox::valueChanged), this,
59  connect(ui.ailevel, QOverload<int>::of(&QComboBox::currentIndexChanged),
61  connect(ui.cruleset, QOverload<int>::of(&QComboBox::currentIndexChanged),
63  ui.qserveroptions->setText(_("More Game Options"));
64  ui.qserveroptions->setIcon(
65  fcIcons::instance()->getIcon(QStringLiteral("preferences-other")));
66  connect(ui.qserveroptions, &QPushButton::clicked,
67  [=]() { option_dialog_popup(_("Game Options"), server_optset); });
68  ui.lnations->setText(_("Nation:"));
69  ui.lrules->setText(_("Rules:"));
70  ui.lplayers->setText(_("Players:"));
71  setLayout(ui.gridLayout);
73 }
74 
78 void pregame_options::set_rulesets(int num_rulesets, QStringList rulesets)
79 {
80  int i = 0;
81  int def_idx = -1;
82 
83  ui.cruleset->clear();
84  ui.cruleset->blockSignals(true);
85 
86  for (auto r : rulesets) {
87  ui.cruleset->addItem(r, i);
88  if (QString("default") == r) {
89  def_idx = i;
90  }
91  i++;
92  }
93 
94  // HACK: HAXXOR WAS HERE : server should tell us the current ruleset.
95  ui.cruleset->setCurrentIndex(def_idx);
96  ui.cruleset->blockSignals(false);
97 }
98 
104 {
105  ui.max_players->blockSignals(true);
106  ui.max_players->setValue(aifill);
107  ui.max_players->blockSignals(false);
108 }
109 
114 {
115  const struct player *pplayer = client_player();
116 
117  // Update the "Select Nation" button
118  if (pplayer != nullptr) {
119  if (pplayer->nation != nullptr) {
120  // Defeat keyboard shortcut mnemonics
121  ui.nation->setText(
122  QString(nation_adjective_for_player(pplayer))
123  .replace(QLatin1String("&"), QLatin1String("&&")));
124  auto pixmap = get_nation_shield_sprite(tileset, pplayer->nation);
125  ui.nation->setIconSize(pixmap->size());
126  ui.nation->setIcon(QIcon(*pixmap));
127  } else {
128  ui.nation->setText(_("Random"));
129  ui.nation->setIcon(
130  fcIcons::instance()->getIcon(QStringLiteral("flush-random")));
131  }
132  }
133 }
134 
139 {
140  enum ai_level level = server_ai_level();
141 
142  if (ai_level_is_valid(level)) {
143  int i = ui.ailevel->findData(level);
144 
145  ui.ailevel->setCurrentIndex(i);
146  } else {
147  ui.ailevel->setCurrentIndex(-1);
148  }
149 }
150 
155 {
157 }
158 
163 {
164  Q_UNUSED(i)
165  QVariant v = ui.ailevel->currentData();
166 
167  if (v.isValid()) {
168  enum ai_level k = static_cast<ai_level>(v.toInt());
169 
170  // Suppress changes provoked by server rather than local user
171  if (server_ai_level() != k) {
172  const char *name = ai_level_cmd(k);
173 
174  send_chat_printf("/%s", name);
175  }
176  }
177 }
178 
183 {
184  Q_UNUSED(i)
185  if (!ui.cruleset->currentText().isEmpty()) {
186  QByteArray rn_bytes;
187 
188  rn_bytes = ui.cruleset->currentText().toLocal8Bit();
189  set_ruleset(rn_bytes.data());
190  }
191 }
192 
int send_chat_printf(const char *format,...)
Send the message as a chat to the server.
static fcIcons * instance()
Returns instance of fc_icons.
Definition: icons.cpp:36
void pick_nation()
Slot for picking a nation.
void update_ai_level()
Updates the AI skill level control.
void ailevel_change(int i)
Slot for changing level of AI.
Ui::FormPregameOptions ui
void set_aifill(int aifill)
Sets the value of the "aifill" option.
void max_players_change(int i)
Slot for changing aifill value.
void set_rulesets(int num_rulesets, QStringList rulesets)
Update the ruleset list.
pregame_options(QWidget *parent)
Pregame options contructor.
void ruleset_change(int i)
Slot for changing ruleset.
void update_buttons()
Updates the buttons whenever the game state has changed.
struct player * client_player()
Either controlling or observing.
enum ai_level server_ai_level()
Returns the current AI skill level on the server, if the same level is currently used for all current...
Definition: climisc.cpp:1211
void set_ruleset(const char *ruleset)
Called by the GUI code when the user sets the ruleset.
void popup_races_dialog(struct player *pplayer)
Popup the nation selection dialog.
Definition: dialogs.cpp:997
#define MAX_NUM_PLAYERS
Definition: fc_types.h:28
#define _(String)
Definition: fcintl.h:50
const char * name
Definition: inputfile.cpp:118
const char * nation_adjective_for_player(const struct player *pplayer)
Return the (translated) adjective for the given nation of a player.
Definition: nation.cpp:146
const struct option_set * server_optset
Definition: options.cpp:2430
struct option * optset_option_by_name(const struct option_set *poptset, const char *name)
Returns the option corresponding of the name in this option set.
Definition: options.cpp:110
bool option_int_set(struct option *poption, int val)
Sets the value of this integer option.
Definition: options.cpp:538
bool is_settable_ai_level(enum ai_level level)
Return is AI can be set to given level.
Definition: player.cpp:1825
#define ai_level_cmd(_level_)
Definition: player.h:551
void option_dialog_popup(const char *name, const struct option_set *poptset)
Popup the option dialog for the option set.
Definition: optiondlg.cpp:722
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
const char * aifill(int amount)
Fill or remove players to meet the given aifill.
Definition: srv_main.cpp:2440
Option set structure.
Definition: options.cpp:88
Definition: player.h:231
struct nation_type * nation
Definition: player.h:242
const QPixmap * get_nation_shield_sprite(const struct tileset *t, const struct nation_type *pnation)
Return the shield sprite for the nation.
Definition: tilespec.cpp:3376