Question

So there is some problem in this code, its probably just some punctuation error that I haven't noticed yet but for some reason I cant find it.

I read that this is how to do a loop with Do then the number of times, in this case '%dmg%', tell me if this wasn't true.

I do have parts that define the variables; set dmgt=0 set /a wl=1000*%upts%/%epts% and I do have all of the others set.

Using the pause command I found this is the problem;

if %upts%*%utroop% LEQ %epts%*%etroopt% ( goto fail
) else ( set /a dmg=%wl%*%etroop%/1000
    set /a dmgt=%dmg%+%dmgt%
do %dmg%
SET /A type=%RANDOM% * 3 / 32768 + 1
if %type% == 1 ( if %snip% GEQ 1 ( set /a dmg=%dmg%-1
        set /a snip=%snip%-1
    )
)
if %type% == 2 ( if %mgs% GEQ 1 ( set /a dmg=%dmg%-2
        set /a mgs=%mgs%-1
    )
)
if %type% == 3 ( if %rif% GEQ 1 ( set /a dmg=%dmg%-1
        set /a rif=%rif%-1
    )
)
enddo
)
Was it helpful?

Solution

if %upts%*%utroop% LEQ %epts%*%etroopt% does not calculate the value of each side, it strings together the contants of upts*the contents of utroop and compares that string against the RHS. You need to explicitly calculate the two values into variables using a SET.

Your ELSE clause invokes a "block statement" that is, the ENTIRE contents of the parenthesised series of statements up to the closing ) following enddo. Batch parses the statement from the IF through to the closing parenthesis of the ELSE block, substitutes for any %var% with the PARSE-TIME value of var and THEN executes the result. Consequently, TYPE will appear unchanged to the logic within the block.

You'd need to invoke delayedexpansion mode and use !var! to access the RUN-TIME value of var. This is done with a SETLOCAL ENABLEDELAYEDEXPANSION statement, usually inserted after the opening @echo off at the start of the batch (hence turning delayedexpansion and the !var! facility ON for the entire duration of the batch)

Actually, there's no need for an ELSE clause as you have it because you GOTO a label in the true part. This may simplify your code.

Finally, there's no DO/ENDDO facility in batch - or at least, not in that form. I'll presume that you intend to do the intervening code %dmg% times. You can achieve this by using

for /L %%z in (1,1,%dmg%) do (
 intervening code
)

where the metavariable %%z will be incremented from the first element in the parentheses to the last in steps of the second. %%z can be any letter, but is one of the very few cases in batch which IS case-sensitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top