Cleaner digit parsing of ammo schemes
authorWuzzy <Wuzzy2@mail.ru>
Fri, 05 Jun 2020 15:48:41 +0200
changeset 15616 fdca2af677a9
parent 15615 f0ac916a0608
child 15617 b988d3be7100
child 15619 3409433a236a
Cleaner digit parsing of ammo schemes Because invalid ammo scheme files could break the game somewhat
QTfrontend/ui/widget/selectWeapon.cpp
QTfrontend/ui/widget/selectWeapon.h
--- a/QTfrontend/ui/widget/selectWeapon.cpp	Fri Jun 05 01:30:03 2020 +0200
+++ b/QTfrontend/ui/widget/selectWeapon.cpp	Fri Jun 05 15:48:41 2020 +0200
@@ -84,6 +84,16 @@
     item->setEnabled(value);
 }
 
+int SelWeaponWidget::readWeaponValue(const QChar chr, int max)
+{
+    int value = chr.digitValue();
+    if (value == -1)
+        value = 0;
+    else if (value > max)
+        value = max;
+    return value;
+}
+
 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
     QFrame(parent),
     m_numItems(numItems)
@@ -187,19 +197,19 @@
         // TODO: Unhide amCreeper when this weapon is done
         if (i == 6 || i == 57) continue;
         if (k % 4 == 0) ++j;
-        SelWeaponItem * swi = new SelWeaponItem(true, i, currentState[i].digitValue(), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this);
+        SelWeaponItem * swi = new SelWeaponItem(true, i, readWeaponValue(currentState[i], 9), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this);
         weaponItems[i].append(swi);
         p1Layout->addWidget(swi, j, k % 4);
 
-        SelWeaponItem * pwi = new SelWeaponItem(false, i, currentState[numItems + i].digitValue(), QImage(":/res/ammopicbox.png"), QImage(":/res/ammopicboxgrey.png"), this);
+        SelWeaponItem * pwi = new SelWeaponItem(false, i, readWeaponValue(currentState[numItems + i], 8), QImage(":/res/ammopicbox.png"), QImage(":/res/ammopicboxgrey.png"), this);
         weaponItems[i].append(pwi);
         p2Layout->addWidget(pwi, j, k % 4);
 
-        SelWeaponItem * dwi = new SelWeaponItem(false, i, currentState[numItems*2 + i].digitValue(), QImage(":/res/ammopicdelay.png"), QImage(":/res/ammopicdelaygrey.png"), this);
+        SelWeaponItem * dwi = new SelWeaponItem(false, i, readWeaponValue(currentState[numItems*2 + i], 8), QImage(":/res/ammopicdelay.png"), QImage(":/res/ammopicdelaygrey.png"), this);
         weaponItems[i].append(dwi);
         p3Layout->addWidget(dwi, j, k % 4);
 
-        SelWeaponItem * awi = new SelWeaponItem(false, i, currentState[numItems*3 + i].digitValue(), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this);
+        SelWeaponItem * awi = new SelWeaponItem(false, i, readWeaponValue(currentState[numItems*3 + i], 8), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this);
         weaponItems[i].append(awi);
         p4Layout->addWidget(awi, j, k % 4);
 
@@ -229,10 +239,10 @@
     {
         twi::iterator it = weaponItems.find(i);
         if (it == weaponItems.end()) continue;
-        it.value()[0]->setItemsNum(ammo[i].digitValue());
-        it.value()[1]->setItemsNum(ammo[m_numItems + i].digitValue());
-        it.value()[2]->setItemsNum(ammo[m_numItems*2 + i].digitValue());
-        it.value()[3]->setItemsNum(ammo[m_numItems*3 + i].digitValue());
+        it.value()[0]->setItemsNum(readWeaponValue(ammo[i], 9));
+        it.value()[1]->setItemsNum(readWeaponValue(ammo[m_numItems + i], 8));
+        it.value()[2]->setItemsNum(readWeaponValue(ammo[m_numItems*2 + i], 8));
+        it.value()[3]->setItemsNum(readWeaponValue(ammo[m_numItems*3 + i], 8));
         it.value()[0]->setEnabled(enable);
         it.value()[1]->setEnabled(enable);
         it.value()[2]->setEnabled(enable);
--- a/QTfrontend/ui/widget/selectWeapon.h	Fri Jun 05 01:30:03 2020 +0200
+++ b/QTfrontend/ui/widget/selectWeapon.h	Fri Jun 05 15:48:41 2020 +0200
@@ -95,6 +95,7 @@
         QGridLayout* p4Layout;
 
         QString fixWeaponSet(const QString & s);
+        int readWeaponValue(const QChar chr, int max);
 };
 
 #endif // _SELECT_WEAPON_INCLUDED