From 9bb5a76219433190ca7cf38ebe40834157a7ff8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Sat, 26 Nov 2022 16:30:02 +0100
Subject: [PATCH] =?UTF-8?q?!Sourceforge:=20#45892=20S=C5=82awomir=20Lach?=
 =?UTF-8?q?=20<slawek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

From now counters tab in ruledit was accessible. Adding a new counter
is not available, sorry.

diff --git a/common/counters.c b/common/counters.c
index 29dcdfe73f..8b30d88314 100644
--- a/common/counters.c
+++ b/common/counters.c
@@ -87,6 +87,23 @@ void attach_city_counter(struct counter *counter)
   number_city_counters++;
 }
 
+/************************************************************************//**
+    Removes given counter. Can not be called
+    when game is processing.
+****************************************************************************/
+void counter_remove(struct counter *pcount)
+{
+  switch (pcount->target) {
+
+    case CTGT_CITY:
+      counters_city[pcount->index] = counters_city[number_city_counters - 1];
+      counters_city[pcount->index]->index =  pcount->index;
+      --number_city_counters;
+  }
+  // TODO: Must provide global counters counter
+  *pcount = counters[number_city_counters];
+}
+
 /************************************************************************//**
     Return id of a given counter
 ****************************************************************************/
diff --git a/common/counters.h b/common/counters.h
index 3ae191ec8e..29f8e0f40e 100644
--- a/common/counters.h
+++ b/common/counters.h
@@ -50,6 +50,7 @@ int counter_index(struct counter *pcount);
 struct counter *counter_by_index(int index, enum counter_target target);
 int counters_get_city_counters_count(void);
 void attach_city_counter(struct counter *counter);
+void counter_remove(struct counter *pcount);
 
 #define city_counters_iterate(pcount) { \
    int _i_##pcount; \
diff --git a/tools/ruledit/Makefile.am b/tools/ruledit/Makefile.am
index 2e5537e066..f615271e33 100644
--- a/tools/ruledit/Makefile.am
+++ b/tools/ruledit/Makefile.am
@@ -38,6 +38,7 @@ MOC_FILES = \
             meta_requirers_dlg.cpp \
             meta_ruledit_qt.cpp \
             meta_tab_building.cpp \
+            meta_tab_counters.cpp \
             meta_tab_enablers.cpp \
 	    meta_tab_extras.cpp	\
 	    meta_tab_good.cpp	\
@@ -60,6 +61,8 @@ freeciv_ruledit_SOURCES =	\
 		effect_edit.h	\
 		tab_building.cpp \
 		tab_building.h	\
+		tab_counters.cpp	\
+		tab_counters.h	\
 		tab_enablers.cpp \
 		tab_enablers.h	\
 		tab_extras.cpp	\
diff --git a/tools/ruledit/ruledit_qt.cpp b/tools/ruledit/ruledit_qt.cpp
index 967966dfb7..c3d84dbb79 100644
--- a/tools/ruledit/ruledit_qt.cpp
+++ b/tools/ruledit/ruledit_qt.cpp
@@ -46,6 +46,7 @@
 #include "req_vec_fix.h"
 #include "ruledit.h"
 #include "tab_building.h"
+#include "tab_counters.h"
 #include "tab_enablers.h"
 #include "tab_extras.h"
 #include "tab_good.h"
@@ -187,6 +188,8 @@ void ruledit_gui::setup(QWidget *central_in)
   stack->addTab(multipliers, QString::fromUtf8(R__("Multipliers")));
   nation = new tab_nation(this);
   stack->addTab(nation, QString::fromUtf8(R__("Nations")));
+  counter = new tab_counter(this);
+  stack->addTab(counter, QString::fromUtf8(R__("Counters")));
 
   edit_layout->addWidget(stack);
 
@@ -240,6 +243,7 @@ void ruledit_gui::launch_now()
     }
 
     bldg->refresh();
+    counter->refresh();
     misc->ruleset_loaded();
     nation->refresh();
     tech->refresh();
diff --git a/tools/ruledit/ruledit_qt.h b/tools/ruledit/ruledit_qt.h
index 464d443083..23d947ec93 100644
--- a/tools/ruledit/ruledit_qt.h
+++ b/tools/ruledit/ruledit_qt.h
@@ -29,6 +29,7 @@ class QLineEdit;
 class QStackedLayout;
 
 class requirers_dlg;
+class tab_counter;
 class tab_building;
 class tab_good;
 class tab_gov;
@@ -130,6 +131,7 @@ signals:
     QStackedLayout *main_layout;
 
     tab_building *bldg;
+    tab_counter *counter;
     tab_misc *misc;
     tab_tech *tech;
     tab_unit *unit;
diff --git a/tools/ruledit/tab_counters.cpp b/tools/ruledit/tab_counters.cpp
new file mode 100644
index 0000000000..74303b7aee
--- /dev/null
+++ b/tools/ruledit/tab_counters.cpp
@@ -0,0 +1,332 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QCheckBox>
+#include <QGridLayout>
+#include <QLineEdit>
+#include <QListWidget>
+#include <QMenu>
+#include <QPushButton>
+#include <QToolButton>
+
+// utility
+#include "fcintl.h"
+#include "log.h"
+
+// common
+#include "counters.h"
+#include "game.h"
+
+// ruledit
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_counters.h"
+
+/**********************************************************************//**
+  Setup tab_counter object
+**************************************************************************/
+tab_counter::tab_counter(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *counter_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  counter_list = new QListWidget(this);
+
+  default_val = new QSpinBox(this);
+  label = new QLabel(QString::fromUtf8(R__("Default")));
+  label->setParent(this);
+
+  connect(default_val, SIGNAL(valueChanged(int)), this, SLOT(change_def_val(int)));
+
+  counter_layout->addWidget(label, 2, 0);
+  counter_layout->addWidget(default_val, 2, 2);
+
+  checkpoint = new QSpinBox(this);
+  label = new QLabel(QString::fromUtf8(R__("Checkpoint")));
+  label->setParent(this);
+
+  connect(checkpoint, SIGNAL(valueChanged(int)), this, SLOT(change_checkpoint_val(int)));
+
+  counter_layout->addWidget(label, 4, 0);
+  counter_layout->addWidget(checkpoint, 4, 2);
+
+  connect(counter_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_counter()));
+  main_layout->addWidget(counter_list);
+
+  counter_layout->setSizeConstraint(QLayout::SetMaximumSize);
+
+  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
+  label->setParent(this);
+  rname = new QLineEdit(this);
+  rname->setText(R__("None"));
+  connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
+  counter_layout->addWidget(label, 0, 0);
+  counter_layout->addWidget(rname, 0, 2);
+
+  label = new QLabel(QString::fromUtf8(R__("Name")));
+  label->setParent(this);
+
+  name = new QLineEdit(this);
+  name->setText(R__("None"));
+  connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
+  counter_layout->addWidget(label, 1, 0);
+  counter_layout->addWidget(name, 1, 2);
+
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Counter")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  counter_layout->addWidget(add_button, 6, 0);
+  show_experimental(add_button);
+
+  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Counter")), this);
+  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  counter_layout->addWidget(delete_button, 6, 2);
+  show_experimental(delete_button);
+
+  refresh();
+  update_counter_info(nullptr);
+
+  main_layout->addLayout(counter_layout);
+
+  setLayout(main_layout);
+}
+
+/**********************************************************************//**
+  Refresh the information.
+**************************************************************************/
+void tab_counter::refresh()
+{
+  counter_list->clear();
+
+  city_counters_iterate(pcount) {
+      QListWidgetItem *item = new QListWidgetItem(counter_rule_name(pcount));
+
+      counter_list->insertItem(counter_index(pcount), item);
+  } city_counters_iterate_end;
+}
+
+/**********************************************************************//**
+  Display name of the counter
+**************************************************************************/
+QString tab_counter::counter_name(struct counter *pcounter)
+{
+
+  return QString::fromUtf8(counter_rule_name(pcounter));
+}
+
+/**********************************************************************//**
+  Update info of the counter
+**************************************************************************/
+void tab_counter::update_counter_info(struct counter *counter)
+{
+  selected = counter;
+
+  if (selected != nullptr) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(counter->name)));
+    QString rulen = QString::fromUtf8(rule_name_get(&(counter->name)));
+
+    name->setText(dispn);
+    rname->setText(rulen);
+
+    if (dispn == rulen) {
+      name->setEnabled(false);
+    } else {
+      name->setEnabled(true);
+    }
+    default_val->setValue(counter->def);
+    checkpoint->setValue(counter->checkpoint);
+
+  } else {
+    name->setText(R__("None"));
+    rname->setText(R__("None"));
+    name->setEnabled(false);
+  }
+}
+
+static struct counter *obtain_selected_counter_from_list(QListWidget *list)
+{
+  QByteArray tn_bytes;
+  char *cname;
+  QList<QListWidgetItem *> select_list = list->selectedItems();
+
+  if (select_list.isEmpty()) {
+    return nullptr;
+  }
+
+  tn_bytes = select_list.at(0)->text().toUtf8();
+  cname = tn_bytes.data();
+
+  return counter_by_rule_name(cname);
+}
+
+/**********************************************************************//**
+  User selected counter from the list.
+**************************************************************************/
+void tab_counter::select_counter()
+{
+  struct counter *count = obtain_selected_counter_from_list(counter_list);
+
+  if (nullptr == count) {
+    return;
+  }
+  update_counter_info(count);
+}
+
+/**********************************************************************//**
+  Set checkpoint value of selected counter.
+**************************************************************************/
+void tab_counter::change_checkpoint_val(int i)
+{
+  struct counter *count = obtain_selected_counter_from_list(counter_list);
+
+  if (nullptr == count) {
+
+    return;
+  }
+
+  count->checkpoint = i;
+}
+
+/**********************************************************************//**
+  Set default counter's value (this apply to selected counter).
+**************************************************************************/
+void tab_counter::change_def_val(int i)
+{
+  struct counter *count = obtain_selected_counter_from_list(counter_list);
+
+  if (nullptr == count) {
+    return;
+  }
+
+  count->def= i;
+}
+
+
+/**********************************************************************//**
+  User entered name for counter
+**************************************************************************/
+void tab_counter::name_given()
+{
+  if (selected != nullptr) {
+    QByteArray name_bytes;
+    QByteArray rname_bytes;
+
+    city_counters_iterate(pcounter) {
+      if (pcounter != selected) {
+        rname_bytes = rname->text().toUtf8();
+        if (!strcmp(counter_rule_name(pcounter), rname_bytes.data())) {
+          ui->display_msg(R__("A counter with that rule name already exists!"));
+          return;
+        }
+      }
+    } city_counters_iterate_end;
+
+    name_bytes = name->text().toUtf8();
+    rname_bytes = rname->text().toUtf8();
+    names_set(&(selected->name), 0,
+              name_bytes.data(),
+              rname_bytes.data());
+    refresh();
+  }
+}
+
+/**********************************************************************//**
+  User requested counter deletion
+**************************************************************************/
+void tab_counter::delete_now()
+{
+  if (selected != 0) {
+
+    initialize_new_counter(selected);
+    selected->type = COUNTER_BEHAVIOUR_LAST;
+
+    counter_remove(selected);
+
+    refresh();
+    update_counter_info(nullptr);
+  }
+}
+
+/**********************************************************************//**
+  Initialize new counter for use.
+**************************************************************************/
+bool tab_counter::initialize_new_counter(struct counter *counter)
+{
+  counter->checkpoint = 0;
+  counter->def = 0;
+  counter->target = CTGT_CITY;
+  counter->type = CB_CITY_OWNED_TURNS;
+  counter->index = counters_get_city_counters_count();
+
+  name_set(&counter->name, 0, "New counter");
+
+  return true;
+}
+
+/**********************************************************************//**
+  User requested new counter
+**************************************************************************/
+void tab_counter::add_now()
+{
+   struct counter *new_counter;
+
+  // Try to reuse freed counter slot
+  city_counters_iterate(pcount) {
+    if (pcount->type == COUNTER_BEHAVIOUR_LAST) {
+      if (initialize_new_counter(pcount)) {
+        update_counter_info(pcount);
+        refresh();
+      }
+      return;
+    }
+  } city_counters_iterate_end;
+
+  // Try to add completely new counter
+  if (counters_get_city_counters_count() >= MAX_COUNTERS) {
+    return;
+  }
+
+  // num_counter_types must be big enough to hold new counter or
+  // counter_by_number() fails.
+
+  new_counter = counter_by_id(counters_get_city_counters_count());
+  if (initialize_new_counter(new_counter)) {
+    update_counter_info(new_counter);
+    attach_city_counter(new_counter);
+    refresh();
+  }
+}
+
+/**********************************************************************//**
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_counter::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}
diff --git a/tools/ruledit/tab_counters.h b/tools/ruledit/tab_counters.h
new file mode 100644
index 0000000000..eebd8b9624
--- /dev/null
+++ b/tools/ruledit/tab_counters.h
@@ -0,0 +1,75 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__TAB_COUNTER_H
+#define FC__TAB_COUNTER_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QWidget>
+#include <QSpinBox>
+
+// common
+#include "counters.h"
+
+class QCheckBox;
+class QGridLayout;
+class QLabel;
+class QLineEdit;
+class QListWidget;
+class QMenu;
+class QToolButton;
+
+class ruledit_gui;
+
+class tab_counter : public QWidget
+{
+  Q_OBJECT
+
+  public:
+    explicit tab_counter(ruledit_gui *ui_in);
+    void refresh();
+    static void techs_to_menu(QMenu *fill_menu);
+    static QString counter_name(struct counter *padv);
+
+  private:
+    ruledit_gui *ui;
+
+    QLineEdit *name;
+    QLineEdit *rname;
+
+    QSpinBox *default_val;
+    QSpinBox *checkpoint;
+
+    QListWidget *counter_list;
+
+    struct counter *selected;
+
+  private slots:
+    void update_counter_info(struct counter *counter);
+    void name_given();
+    void select_counter();
+    void change_def_val(int);
+    void change_checkpoint_val(int);
+    void add_now();
+    void delete_now();
+    void same_name_toggle(bool checked);
+    bool initialize_new_counter(struct counter *padv);
+    //QMenu *prepare_counter_button(QToolButton *button, enum tech_req rn);
+};
+
+
+#endif // FC__TAB_TECH_H
-- 
2.38.1

