View Full Version : Warning: Negative numbers and getSorenRandNum


jdog5000
Oct 26, 2006, 05:18 PM
Negative numbers and getSorenRandNum do not mix!

In a few places in my revolution mod, I have lines like:

iNumMobile = 0 + CyGame().getSorenRandNum( baseUnits + extraUnits - 1, 'BC give mobile')

This determines the number of horse units a particular flavor of barbarian civ gets when settling down into a normal civ. Anyway, when this occurs very early in the game, baseUnits and extraUnits can be 0 ... and then trouble arises! The line:

CyGame().getSorenRandNum( -1, 'test')

Appears to return a value between 0 and 2^16 or so! I think the issue is the conversion from int to unsigned short in CvGame::getSorenRandNum (CvRandom::get believes you're passing it an unsigned short) ... when the game attempted to spawn 12,000 chariots it locked up my computer in interesting ways ... just a heads up.

Déja
Oct 26, 2006, 07:02 PM
yeah, you'll want to be careful with that. I'd replace it with (in C++, translate to python if necessary):


int factor = baseUnits + extraUnits - 1;
iNumMobile = 0 + CyGame().getSorenRandNum( factor >= 0 ? factor : 0, 'blah' );

Dom Pedro II
Oct 26, 2006, 07:11 PM
So 12,000 chariots is a bit much? :confused:

;)