This Bug is one created by one of my lua scripts. I have therefore put it as an Annals of Ea bug, although the same would happen in ToME itself. -- TheFalcon

The Bug

This script is taken from aWESoME, and exhibits the same bug that one of my scripts experiences, but is shorter, and therefore easier to go through:

function draw_h_snake(seed)

    -- formula: y = x (x^2 -1) (x^2 -1), -1 < x < 1
    local i, j, A, B, cnt, random
    cnt = 40
    random = imod(seed, 2)*2 -1
    
    for i = 1, 198 do
        A = (cnt*(i -99))/99

        B = (A*A -cnt*cnt) * (A*A -cnt*cnt) * A
        j = 33 - random*(33*B)/(cnt*cnt*cnt*cnt*cnt)

        if (i>0) and (i<199) and (j>0) and (j<67) then
            cave(j, i).feat = 200
            cave(j-1, i).feat = 200
            cave(j, i+1).feat = 200
            cave(j-1, i+1).feat = 200
        end
    end
end

Basically, it draws a road, which curves across the map. j is y, i is x.

As you can see, the "if (i>0)..." line limits the alteration of features to the extent of the wilderness square. However, the "cave(j, i+1).feat = 200" and the next line increase x by 1. This means that the actual greatest x value used will be 199, rather than the 198 specified earlier. This in itself is not a problem. However, rather than x=199 simply being ignored, as it is greater than the wilderness map x extent, it wraps round onto the other side of the map!!

It does not, however, actually modify the terrain to feature 200 (cobblestone road), but rather places a "nothing" on the "next line" of the wilderness map, in the edging. If this were extended (e.g. cave(j, i+10).feat) the effect would extend further into the square, and a longer line of "nothings appear".

This is not just isolated to this script. Any attemps to modify terrain using x values above the wilderness square maximum (198) will exhibit such behaviour. I have not tested this with y values, though I suspect the same would happen.

While it is possible to get around this, by splitting into more than one if clause:

        if (i>0) and (i<199) and (j>0) and (j<67) then
            cave(j, i).feat = 200
            cave(j-1, i).feat = 200
        end
        if (i>0) and (i<198) and (j>0) and (j<67) then
            cave(j, i+1).feat = 200
            cave(j-1, i+1).feat = 200

this is not ideal, since it is more complicated and requires interminable series of if clauses for more significant terrain changes.

It might be possible also to get around this with tables of x and y values, but it should rather be the case that anything with x values over 198 is simply discarded. The nothing wrapping should not occur.

[Later...] Oh yes... heh... and it seems to crash the game if the "nothing"s get too far into the other side.

Comments/Suggestions/etc.

Actius: another workaround would be to use a function cave_safe() which does the ignoring you want.

TheFalcon: Thanks, that should save on typing! I'll try it out when I get the chance.

TheFalcon: Hmmm, it seems I was forgetting about while, which solves the problem nicely as well.

AnnalsBug2 (last edited 2005-09-23 10:01:44 by TheFalcon)