Pocket Plane Group

Friends and Neighbors => Weimer Republic (WeiDU.org) => WeiDU => Topic started by: DavidW on August 11, 2021, 06:42:26 AM

Title: Bug in SET_2DA_ENTRY
Post by: DavidW on August 11, 2021, 06:42:26 AM
Slightly to my surprise, I think I've found a bug in SET_2DA_ENTRY.

Reproduction: take the BG2EE 'wish.2da' file. It's a standard 2da:
Code: [Select]
2DA V1.0
*
               1      2    3    4
1              23     23   23   22
2              24     24   24   24
3              25     10   25   30
4              26     26   33   26
...

Say we want to change the last column header from '4' to '-1'. (There's no earthly reason to do this, it's just to demonstrate the bug). You'd expect this to work:
Code: [Select]
SET_2DA_ENTRY 0 3 4 "-1"
But if you do that, it actually changes the penultimate column header:
Code: [Select]
2DA V1.0
*
               1      2    -1    4
1              23     23   23   22
2              24     24   24   24
3              25     10   25   30
4              26     26   33   26
...
(If you try SET_2DA_ENTRY 0 4 4 "-1", you get an install-time error.)

If you delete the whitespace before the column entry, like this, SET_2DA_ENTRY works correctly:
Code: [Select]
2DA V1.0
*
1      2    3    4
1              23     23   23   22
2              24     24   24   24
3              25     10   25   30
4              26     26   33   26
...
So I assume some bit of the SET_2DA_ENTRY internal is treating the whitespace as an extra column.
(I don't think there's anything special about wish.2da here - I actually discovered it on thiefscl.2da.)

The workaround is to use SET_2DA_ENTRY_LATER:
Code: [Select]
SET_2DA_ENTRY_LATER wish_out 0 3 "-1"
SET_2DA_ENTRIES_NOW wish_out 4
Title: Re: Bug in SET_2DA_ENTRY
Post by: _Luke_ on August 13, 2021, 03:47:57 AM
Slightly to my surprise, I think I've found a bug in SET_2DA_ENTRY.

Reproduction: take the BG2EE 'wish.2da' file. It's a standard 2da:
Code: [Select]
2DA V1.0
*
               1      2    3    4
1              23     23   23   22
2              24     24   24   24
3              25     10   25   30
4              26     26   33   26
...

Say we want to change the last column header from '4' to '-1'. (There's no earthly reason to do this, it's just to demonstrate the bug). You'd expect this to work:
Code: [Select]
SET_2DA_ENTRY 0 3 4 "-1"
But if you do that, it actually changes the penultimate column header:
Code: [Select]
2DA V1.0
*
               1      2    -1    4
1              23     23   23   22
2              24     24   24   24
3              25     10   25   30
4              26     26   33   26
...

FWIW, the same bug occurs if you run
Code: [Select]
SET_2DA_ENTRY 2 3 0 "-1"
Title: Re: Bug in SET_2DA_ENTRY
Post by: Wisp on September 01, 2021, 06:24:27 PM
This is not a bug I can fix without risking random mods catching fire and falling out of the sky.

SET_2DA_ENTRY has 2 code paths. The so-called "fast SET_2DA_ENTRY" is used by default and does not distinguish properly between leading and separating whitespace. The "old method" is used if "fast SET_2DA_ENTRY" fails and does not have this problem. The "fast" and "old" methods consequently count count columns differently, the "fast" method arguably does so incorrectly, but I can't fix it without changing de facto expected behaviour.

SET_2DA_ENTRY 0 4 4 "-1" fails because it's out-of-bounds to the code that counts columns and rows, as the 2DA count starts from 0.
SET_2DA_ENTRY_LATER/NOW does not have this problem because it uses its own code path (analogous to the "old method" of SET_2DA_ENTRY).
Title: Re: Bug in SET_2DA_ENTRY
Post by: DavidW on September 03, 2021, 10:37:51 AM
NO_SET_2DA_ENTRY_BUG ?

(ducks)
Title: Re: Bug in SET_2DA_ENTRY
Post by: Wisp on September 07, 2021, 12:02:28 PM
Yeah, I could make it opt-in.
Title: Re: Bug in SET_2DA_ENTRY
Post by: subtledoctor on January 24, 2022, 12:58:20 PM
Huh. Does this affect READ_2DA_ENTRY as well? Could explain some difficulties I've been having.

EDIT - nope, after more testing, READ seems to work fine.