2018-09-25 18:50 EDT


View Issue Details Jump to Notes ] Related Changesets ]
IDProjectCategoryView StatusLast Update
0002978FSSCPscriptingpublic2013-12-18 04:15
ReporterAxem 
Assigned Tom_m 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
Product Version 
Target VersionFixed in Version 
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.
Attached Files
  • txt file icon outputexcerpt.txt (1,474 bytes) 2013-12-15 13:34 -
    *****************
    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
    *****************
    txt file icon outputexcerpt.txt (1,474 bytes) 2013-12-15 13:34 +
  • ? file icon debug-sct.tbm (1,118 bytes) 2013-12-15 13:35
  • patch file icon lua.cpp.patch (1,106 bytes) 2013-12-16 04:00 -
    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));
     		}
     	}
    
    patch file icon lua.cpp.patch (1,106 bytes) 2013-12-16 04:00 +

-Relationships
+Relationships

-Notes

~0015515

Goober5000 (administrator)

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

~0015517

m_m (developer)

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

~0015518

niffiwan (developer)

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 :)

~0015519

m_m (developer)

Patch with fixed functions added.

~0015520

m_m (developer)

Fix committed to trunk@10249.
+Notes

+Related Changesets

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