Reputation: 80
Okay, so well I'm having a problem with my batch system, I'm trying to create a batch game via town simulator (kind-of) And one thing I'm trying to do (the problem) is "advertise" your city which just mainly gets random "people" to live in your houses, My problem here is when you advertise It just gets super weird... Heres the code:
:3
if %houses% LSS 2 goto YCGT
cls
set /a send=%hutpop%+%small_housepop%+%housepop%+%slight_housepop%+%medium_housepop%+%big_housepop%+%mansionpop%
set /a hutpop=%hutpop%+%random% %% %maxhutpop%
set /a small_housepop=%small_housepop%+%random% %% %maxsmall_housepop%
set /a housepop=%housepop%+%random% %% %maxhousepop%
set /a slight_housepop=%slight_housepop%+%random% %% %maxslight_housepop%
set /a medium_housepop=%medium_housepop%+%random% %% %maxmedium_housepop%
set /a big_housepop=%big_housepop%+%random% %% %maxbig_housepop%
set /a mansionpop=%mansionpop%+%random% %% %maxmansionpop%
set/a pop=!hutpop!+!small_housepop!+!housepop!+!slight_housepop!+!medium_housepop!+!big_housepop!+!mansionpop!
if %pop% GTR %maxpop% (
set /a pop=%pop%-%maxpop%
)
timeout /t 5 /nobreak >nul
set /a recieve=!pop!-!send!
echo You have gained %recieve%!
echo You now have %pop%
pause>nul
goto rstart
And what happens is It either goes over... Gives me "can't divide by zero" errors, Or always gives me the MAX amount of population you get... (This is DEFININENTLY NOT ALL THE CODE!!! This is a pretty far in game right now... I wouldn't post all 1,000 lines of code... just ask if you need something)
Upvotes: 3
Views: 78
Reputation: 80033
Your divide by zero error is caused by one of %max...pop%
having a value of 0. If %max...pop%
was empty, you'd get a missing operand
error.
You obviously have delayedexpansion
invoked, otherwise !var!
would show errors in a set /a
. It will work outside of a block statement (ie a multi-line parenthesised statement sequence sometimes used in a FOR
loop or IF...ELSE...
but is not necessary. Your set /a pop=...
would operate just as well with %var%
since the value of %var%
is not being changed within the block.
I'd suggest you start using internal subroutines to make your task easier. For instance, I modified some of your code to assign values with which to work:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:3
SET houses=10
set hutpop=12
SET small_housepop=12
SET housepop=12
SET slight_housepop=12
SET medium_housepop=12
SET big_housepop=12
SET mansionpop=12
set maxhutpop=100
SET maxsmall_housepop=0
if %houses% LSS 2 goto YCGT
REM cls
set /a send=%hutpop%+%small_housepop%+%housepop%+%slight_housepop%+%medium_housepop%+%big_housepop%+%mansionpop%
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
set /a hutpop=%hutpop%+%random% %% %maxhutpop%
set /a small_housepop=%small_housepop%+%random% %% %maxsmall_housepop%
set /a housepop=%housepop%+%random% %% %maxhousepop%
set /a slight_housepop=%slight_housepop%+%random% %% %maxslight_housepop%
set /a medium_housepop=%medium_housepop%+%random% %% %maxmedium_housepop%
set /a big_housepop=%big_housepop%+%random% %% %maxbig_housepop%
set /a mansionpop=%mansionpop%+%random% %% %maxmansionpop%
set/a pop=!hutpop!+!small_housepop!+!housepop!+!slight_housepop!+!medium_housepop!+!big_housepop!+!mansionpop!
if %pop% GTR %maxpop% (
set /a pop=%pop%-%maxpop%
)
REM timeout /t 5 /nobreak >nul
set /a recieve=!pop!-!send!
echo You have gained %recieve%!
echo You now have %pop%
pause>nul
goto rstart
:: increment population with limit
:adjust
CALL SET /a %1+=%RANDOM% %%%% %%max%1%%
CALL SET /a hpop=%1 - %%max%1%%
IF %hpop% gtr 0 CALL SET %1=%%max%1%%
CALL ECHO %1=%%%1%%
GOTO :EOF
Note the CALL :adjust hutpop
which invokes the subroutine adjust
to modify the value of hutpop
with a limit of maxhutpop
. I used it five times deliberately to show how hutpop
would be adjusted but top-out. In your case, all you'd need would be
call :adjust hutpop
call :adust small_housepop
...
Note also a potential problem. Because you have delayedexpansion
operating, it's possible that if your message You have gained %recieve%!
needed to be changed to You have gained %recieve%! You need another %need%!
then batch may attempt to find a "variable" You need another %need%
, very likely fail to find it, and hence substitute an empty string - which might be puzzling.
Further 20131201T0511Z
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:3
set hutpop=12
SET small_housepop=17
set maxhutpop=100
SET maxsmall_housepop=70
set /a send=%hutpop%+%small_housepop%
ECHO you had %send%=%hutpop%+%small_housepop%
CALL :adjust hutpop
CALL :adjust small_housepop
set /a pop=%hutpop%+%small_housepop%
set /a recieve=%pop%-%send%
echo You have gained %recieve%!
echo You now have %pop%=%hutpop%+%small_housepop%
ECHO ====== with pop-increase MORE limited
set hutpop=12
SET small_housepop=17
set maxhutpop=100
SET maxsmall_housepop=70
set /a send=%hutpop%+%small_housepop%
ECHO you had %send%=%hutpop%+%small_housepop%
CALL :adjust2 hutpop
CALL :adjust2 small_housepop
set /a pop=%hutpop%+%small_housepop%
set /a recieve=%pop%-%send%
echo You have gained %recieve%!
echo You now have %pop%=%hutpop%+%small_housepop%
goto :eof
:adjust
CALL SET /a %1+=%RANDOM% %%%% %%max%1%%
CALL SET /a hpop=%1 - %%max%1%%
IF %hpop% gtr 0 CALL SET %1=%%max%1%%
CALL ECHO %1=%%%1%%
GOTO :EOF
:adjust2
CALL SET /a %1+=(%RANDOM% %%%% %%max%1%% / 8)
CALL SET /a hpop=%1 - %%max%1%%
IF %hpop% gtr 0 CALL SET %1=%%max%1%%
CALL ECHO %1=%%%1%%
GOTO :EOF
This is a small stand-alone version, using two slightly-different :adjust
procedures. Here's some typical output (naturally, yours will be different, since %RANDOM%
is used)
you had 29=12+17
hutpop=100
small_housepop=62
You have gained 133
You now have 162=100+62
====== with pop-increase MORE limited
you had 29=12+17
hutpop=15
small_housepop=24
You have gained 10
You now have 39=15+24
All you need to do now is follow the bouncing ball and add in your other dwelling-types.
Upvotes: 1