View Issue Details

IDProjectCategoryView StatusLast Update
0002978FSSCPscriptingpublic2013-12-18 09:15
ReporterAxem Assigned Tom_m  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Summary0002978: Off by 1 error with getShipClassIndex()
DescriptionIssue where getShipClassIndex() returns a 0 based index, while tb.ShipClasses is a 1 based index.

And sort of relatedly, getWeaponClassIndex() is a 0 based index, but tb.WeaponClasses also starts at 0. So there's no mismatch there, but lua indices typically start at one.
Steps To ReproducePut debug-sct.tbm into a table folder and run a debug build. When you go to the mainhall, the debug log will get reported indexes from ships and what they actually belong to. Sample output excerpt is also attached.
TagsNo tags attached.

Activities

Axem

2013-12-15 18:34

reporter  

outputexcerpt.txt (1,474 bytes)   
*****************
thisShip: GTF Ulysses with index 0
Index actually points at 
*****************
*****************
thisShip: GTF Hercules with index 1
Index actually points at GTF Ulysses
*****************
*****************
thisShip: GTF Hercules Mark II with index 2
Index actually points at GTF Hercules
*****************
*****************
thisShip: GTF Ares with index 3
Index actually points at GTF Hercules Mark II
*****************
*****************
thisShip: GTF Erinyes with index 4
Index actually points at GTF Ares
*****************
*****************
thisShip: GTF Loki with index 5
Index actually points at GTF Erinyes
*****************
*****************
thisShip: GTF Pegasus with index 6
Index actually points at GTF Loki
*****************
*****************
thisShip: GTF Perseus with index 7
Index actually points at GTF Pegasus
*****************

.
.
.

*****************
thisWeapon: Subach HL-D with index 1
Index actually points at Subach HL-D
*****************
*****************
thisWeapon: Mekhu HL-7 with index 2
Index actually points at Mekhu HL-7
*****************
*****************
thisWeapon: Mekhu HL-7D with index 3
Index actually points at Mekhu HL-7D
*****************
*****************
thisWeapon: Akheton SDG with index 4
Index actually points at Akheton SDG
*****************
*****************
thisWeapon: Morning Star with index 5
Index actually points at Morning Star
*****************
outputexcerpt.txt (1,474 bytes)   

Axem

2013-12-15 18:35

reporter  

debug-sct.tbm (1,118 bytes)

Goober5000

2013-12-16 04:17

administrator   ~0015515

Assigning to m!m since he's a scripter that should know where to fix this.

m_m

2013-12-16 06:45

developer   ~0015517

The fix is easy but fixing it could break existing scripts. I'm in favor of just fixing it and see if anyone complains.

niffiwan

2013-12-16 08:11

developer   ~0015518

seems like a good idea to just fix it. It would be messy if we needed to maintain support for the non-standard behaviour. And authors of any scripts relying on this behaviour obviously didn't report the issue, and added some ugly workaround hack :)

m_m

2013-12-16 09:00

developer  

lua.cpp.patch (1,106 bytes)   
Index: code/parse/lua.cpp
===================================================================
--- code/parse/lua.cpp	(revision 9923)
+++ code/parse/lua.cpp	(working copy)
@@ -4420,7 +4420,7 @@
 	if(idx < 0 || idx >= Num_weapon_types)
 		return ade_set_args(L, "i", -1);
 
-	return ade_set_args(L, "i", idx);
+	return ade_set_args(L, "i", idx + 1);
 }
 
 ADE_FUNC(isLaser, l_Weaponclass, NULL, "Return true if the weapon is a primary weapon (this includes Beams). This function is deprecated, use isPrimary instead.", "boolean", "true if the weapon is a primary, false otherwise")
@@ -6280,7 +6280,7 @@
 	if(idx < 0 || idx >= Num_ship_classes)
 		return ade_set_args(L, "i", -1);
 
-	return ade_set_args(L, "i", idx);
+	return ade_set_args(L, "i", idx + 1); // Lua is 1-based
 }
 
 //**********HANDLE: Debris
@@ -14212,7 +14212,13 @@
 	if(idx < 0) {
 		idx = atoi(name);
 
-		if(idx < 1 || idx > Num_weapon_types) {
+		// atoi is good enough here, 0 is invalid anyway
+		if (idx > 0)
+		{
+			idx--; // Lua --> C/C++
+		}
+		else
+		{
 			return ade_set_args(L, "o", l_Weaponclass.Set(-1));
 		}
 	}
lua.cpp.patch (1,106 bytes)   

m_m

2013-12-16 09:01

developer   ~0015519

Patch with fixed functions added.

m_m

2013-12-18 09:15

developer   ~0015520

Fix committed to trunk@10249.

Related Changesets

fs2open: trunk r10249

2013-12-18 04:47

m_m


Ported: N/A

Details Diff
Fix Mantis 2978: Off by 1 error with getShipClassIndex() Affected Issues
0002978
mod - /trunk/fs2_open/code/parse/lua.cpp Diff File

Issue History

Date Modified Username Field Change
2013-12-15 18:34 Axem New Issue
2013-12-15 18:34 Axem File Added: outputexcerpt.txt
2013-12-15 18:35 Axem File Added: debug-sct.tbm
2013-12-16 04:17 Goober5000 Note Added: 0015515
2013-12-16 04:17 Goober5000 Assigned To => m_m
2013-12-16 04:17 Goober5000 Status new => assigned
2013-12-16 06:45 m_m Note Added: 0015517
2013-12-16 08:11 niffiwan Note Added: 0015518
2013-12-16 09:00 m_m File Added: lua.cpp.patch
2013-12-16 09:01 m_m Note Added: 0015519
2013-12-18 09:15 m_m Changeset attached => fs2open trunk r10249
2013-12-18 09:15 m_m Note Added: 0015520
2013-12-18 09:15 m_m Status assigned => resolved
2013-12-18 09:15 m_m Resolution open => fixed