Author: cazfi
Date: Mon May  9 15:18:12 2016
New Revision: 32633

URL: http://svn.gna.org/viewcvs/freeciv?rev=32633&view=rev
Log:
Fixed adding new items in ruledit.

See bug #24271

Modified:
    branches/S2_6/tools/ruledit/tab_building.cpp
    branches/S2_6/tools/ruledit/tab_building.h
    branches/S2_6/tools/ruledit/tab_tech.cpp
    branches/S2_6/tools/ruledit/tab_tech.h
    branches/S2_6/tools/ruledit/tab_unit.cpp
    branches/S2_6/tools/ruledit/tab_unit.h

Modified: branches/S2_6/tools/ruledit/tab_building.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_building.cpp?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_building.cpp        (original)
+++ branches/S2_6/tools/ruledit/tab_building.cpp        Mon May  9 15:18:12 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -199,9 +199,14 @@
 /**************************************************************************
   Initialize new tech for use.
 **************************************************************************/
-void tab_building::initialize_new_bldg(struct impr_type *pimpr)
-{
-  name_set(&(pimpr->name), "None", "None");
+bool tab_building::initialize_new_bldg(struct impr_type *pimpr)
+{
+  if (improvement_by_rule_name("New Building") != nullptr) {
+    return false;
+  }
+
+  name_set(&(pimpr->name), 0, "New Building");
+  return true;
 }
 
 /**************************************************************************
@@ -211,27 +216,34 @@
 {
   struct impr_type *new_bldg;
 
-  /* Try to reuse freed building slot */
+  // Try to reuse freed building slot
   improvement_iterate(pimpr) {
     if (pimpr->disabled) {
-      pimpr->disabled = false;
-      initialize_new_bldg(pimpr);
-      update_bldg_info(pimpr);
-      refresh();
+      if (initialize_new_bldg(pimpr)) {
+        pimpr->disabled = false;
+        update_bldg_info(pimpr);
+        refresh();
+      }
       return;
     }
   } improvement_iterate_end;
 
-  /* Try to add completely new building */
+  // Try to add completely new building
   if (game.control.num_impr_types >= B_LAST) {
     return;
   }
 
-  new_bldg = improvement_by_number(game.control.num_impr_types++);
-  initialize_new_bldg(new_bldg);
-  update_bldg_info(new_bldg);
-
-  refresh();
+  // num_impr_types must be big enough to hold new building or
+  // improvement_by_number() fails.
+  game.control.num_impr_types++;
+  new_bldg = improvement_by_number(game.control.num_impr_types - 1);
+  if (initialize_new_bldg(new_bldg)) {
+    update_bldg_info(new_bldg);
+
+    refresh();
+  } else {
+    game.control.num_impr_types--; // Restore
+  }
 }
 
 /**************************************************************************

Modified: branches/S2_6/tools/ruledit/tab_building.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_building.h?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_building.h  (original)
+++ branches/S2_6/tools/ruledit/tab_building.h  Mon May  9 15:18:12 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -38,7 +38,7 @@
   private:
     ruledit_gui *ui;
     void update_bldg_info(struct impr_type *pimpr);
-    void initialize_new_bldg(struct impr_type *pimpr);
+    bool initialize_new_bldg(struct impr_type *pimpr);
 
     QLineEdit *name;
     QLineEdit *rname;

Modified: branches/S2_6/tools/ruledit/tab_tech.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_tech.cpp?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_tech.cpp    (original)
+++ branches/S2_6/tools/ruledit/tab_tech.cpp    Mon May  9 15:18:12 2016
@@ -204,7 +204,7 @@
 {
   selected = adv;
 
-  if (selected != 0) {
+  if (selected != nullptr) {
     QString dispn = QString::fromUtf8(untranslated_name(&(adv->name)));
     QString rulen = QString::fromUtf8(rule_name_get(&(adv->name)));
 
@@ -218,7 +218,7 @@
       same_name->setChecked(false);
       name->setEnabled(true);
     }
-    
+
     req1_button->setText(tech_name(adv->require[AR_ONE]));
     req2_button->setText(tech_name(adv->require[AR_TWO]));
     root_req_button->setText(tech_name(adv->require[AR_ROOT]));
@@ -365,14 +365,20 @@
 /**************************************************************************
   Initialize new tech for use.
 **************************************************************************/
-void tab_tech::initialize_new_tech(struct advance *padv)
+bool tab_tech::initialize_new_tech(struct advance *padv)
 {
   struct advance *none = advance_by_number(A_NONE);
+
+  if (advance_by_rule_name("New Tech") != nullptr) {
+    return false;
+  }
 
   padv->require[AR_ONE] = none;
   padv->require[AR_TWO] = none;
   padv->require[AR_ROOT] = none;
-  name_set(&(padv->name), "None", "None");
+  name_set(&(padv->name), 0, "New Tech");
+
+  return true;
 }
 
 /**************************************************************************
@@ -382,25 +388,32 @@
 {
   struct advance *new_adv;
 
-  /* Try to reuse freed tech slot */
+  // Try to reuse freed tech slot
   advance_iterate(A_FIRST, padv) {
     if (padv->require[AR_ONE] == A_NEVER) {
-      initialize_new_tech(padv);
-      update_tech_info(padv);
-      refresh();
+      if (initialize_new_tech(padv)) {
+        update_tech_info(padv);
+        refresh();
+      }
       return;
     }
   } advance_iterate_end;
 
-  /* Try to add completely new tech */
+  // Try to add completely new tech
   if (game.control.num_tech_types >= A_LAST) {
     return;
   }
 
-  new_adv = advance_by_number(game.control.num_tech_types++);
-  initialize_new_tech(new_adv);
-  update_tech_info(new_adv);
-  refresh();
+  // num_tech_types must be big enough to hold new tech or
+  // advance_by_number() fails.
+  game.control.num_tech_types++;
+  new_adv = advance_by_number(game.control.num_tech_types - 1);
+  if (initialize_new_tech(new_adv)) {
+    update_tech_info(new_adv);
+    refresh();
+  } else{
+    game.control.num_tech_types--; // Restore
+  }
 }
 
 /**************************************************************************

Modified: branches/S2_6/tools/ruledit/tab_tech.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_tech.h?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_tech.h      (original)
+++ branches/S2_6/tools/ruledit/tab_tech.h      Mon May  9 15:18:12 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -48,7 +48,7 @@
     QString tech_name(struct advance *padv);
     void techs_to_menu(QMenu *fill_menu);
     QMenu *prepare_req_button(QToolButton *button, enum tech_req rn);
-    void initialize_new_tech(struct advance *padv);
+    bool initialize_new_tech(struct advance *padv);
 
     QLineEdit *name;
     QLineEdit *rname;

Modified: branches/S2_6/tools/ruledit/tab_unit.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_unit.cpp?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_unit.cpp    (original)
+++ branches/S2_6/tools/ruledit/tab_unit.cpp    Mon May  9 15:18:12 2016
@@ -1,4 +1,4 @@
-/**********************************************************************
+/***********************************************************************
  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
@@ -199,9 +199,14 @@
 /**************************************************************************
   Initialize new tech for use.
 **************************************************************************/
-void tab_unit::initialize_new_utype(struct unit_type *ptype)
-{
-  name_set(&(ptype->name), "None", "None");
+bool tab_unit::initialize_new_utype(struct unit_type *ptype)
+{
+  if (unit_type_by_rule_name("New Unit") != nullptr) {
+    return false;
+  }
+
+  name_set(&(ptype->name), 0, "New Unit");
+  return true;
 }
 
 /**************************************************************************
@@ -211,27 +216,34 @@
 {
   struct unit_type *new_utype;
 
-  /* Try to reuse freed utype slot */
+  // Try to reuse freed utype slot
   unit_type_iterate(ptype) {
     if (ptype->disabled) {
-      ptype->disabled = false;
-      initialize_new_utype(ptype);
-      update_utype_info(ptype);
-      refresh();
+      if (initialize_new_utype(ptype)) {
+        ptype->disabled = false;
+        update_utype_info(ptype);
+        refresh();
+      }
       return;
     }
   } unit_type_iterate_end;
 
-  /* Try to add completely new unit type */
+  // Try to add completely new unit type
   if (game.control.num_unit_types >= U_LAST) {
     return;
   }
 
-  new_utype = utype_by_number(game.control.num_unit_types++);
-  initialize_new_utype(new_utype);
-  update_utype_info(new_utype);
-
-  refresh();
+  // num_unit_types must be big enough to hold new unit or
+  // utype_by_number() fails.
+  game.control.num_unit_types++;
+  new_utype = utype_by_number(game.control.num_unit_types - 1);
+  if (initialize_new_utype(new_utype)) {
+    update_utype_info(new_utype);
+
+    refresh();
+  } else {
+    game.control.num_unit_types--; // Restore
+  }
 }
 
 /**************************************************************************

Modified: branches/S2_6/tools/ruledit/tab_unit.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/tools/ruledit/tab_unit.h?rev=32633&r1=32632&r2=32633&view=diff
==============================================================================
--- branches/S2_6/tools/ruledit/tab_unit.h      (original)
+++ branches/S2_6/tools/ruledit/tab_unit.h      Mon May  9 15:18:12 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -38,7 +38,7 @@
   private:
     ruledit_gui *ui;
     void update_utype_info(struct unit_type *ptype);
-    void initialize_new_utype(struct unit_type *ptype);
+    bool initialize_new_utype(struct unit_type *ptype);
 
     QLineEdit *name;
     QLineEdit *rname;


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to