Author Topic: Bug in SET_2DA_ENTRY  (Read 1212 times)

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Bug in SET_2DA_ENTRY
« 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

Offline _Luke_

  • Planewalker
  • *****
  • Posts: 15
Re: Bug in SET_2DA_ENTRY
« Reply #1 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"
« Last Edit: August 13, 2021, 03:48:55 AM by _Luke_ »

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Bug in SET_2DA_ENTRY
« Reply #2 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).

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Bug in SET_2DA_ENTRY
« Reply #3 on: September 03, 2021, 10:37:51 AM »
NO_SET_2DA_ENTRY_BUG ?

(ducks)

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Bug in SET_2DA_ENTRY
« Reply #4 on: September 07, 2021, 12:02:28 PM »
Yeah, I could make it opt-in.

Offline subtledoctor

  • Planewalker
  • *****
  • Posts: 131
Re: Bug in SET_2DA_ENTRY
« Reply #5 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.
« Last Edit: January 26, 2022, 12:22:07 PM by subtledoctor »

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: