DavidKinder / Inform6

The latest version of the Inform 6 compiler, used for generating interactive fiction games.
http://inform-fiction.org/
Other
199 stars 32 forks source link

Glulx random() built-in function now follows the DM4 spec #256

Closed erkyrath closed 7 months ago

erkyrath commented 7 months ago

See https://github.com/DavidKinder/Inform6/issues/255 .

The implementation of y = random(x) now looks roughly like:

    temp_global = x;
    if (temp_global > 0) {
        y = (@random temp_global) + 1;
    }
    else {
        @setrandom (-temp_global);
        y = 0;
    }

(This isn't legal I6 assembly but it gets the idea across.)

The minus sign in the @setrandom case means that random(-1) does @setrandom 1. I think this is slightly cleaner for testing.

If x is a compile-time constant, we can skip the branch and simply compile one of (@random CONST) + 1 or @setrandom CONST. As a result, this change does not affect the compiled output of random(CONST) for positive CONST.

The multi-argument random(C1, C2, C3...) case is not affected by this change either.

This change does affect the compiled output of Glulx games that use the standard library (I6 or I7). They will now be slightly larger: about 50 bytes for a minimal I6 game, about 200 for an I7 game. Glulx users are not likely to worry about this.

erkyrath commented 7 months ago

Test case lives at https://github.com/erkyrath/Inform6-Testing/blob/g-random/src/randomfunc.inf .