Classic bug: CYBGOLD zero crashes Cybertron creation

Bug ID GE-BUG-058
Affected versions Classic Galactic Empire 3.2e when CYBGOLD is configured as 0
Status Verified in published source; runtime crash has not been reproduced here
Fixed in ge-next 0.2 Cybertron gold rewrite
Primary source CYBGOLD in MBMGEMSG.MSG; iniwara() in GEMAIN.C; cyb_init() in GECYBS.C

Summary

The classic CYBGOLD option permits zero, a reasonable setting for a game whose Cybertrons should carry no gold. Classic accepts that value at startup but uses it as the divisor of a remainder operation when it creates a new Cybertron. Remainder by zero is undefined in C and will ordinarily cause a divide fault when that creation path runs. The danger is delayed until gameplay: the module can start normally, and loading an existing Cybertron ship does not take this particular path.

Configuration and execution path

The stock configuration declares CYBGOLD {Max amount of gold a cyb gets: 1200} N 0 32000. iniwara() loads it with the same lower bound, so neither the MSG validation range nor the game's option-reading call rejects zero:

cyb_gold = numopt(CYBGOLD,0,32000);

The NPC scheduler calls cyb_init() when it fills an available automatic-ship slot. If the corresponding Cybertron ship is not already in the ship database, cyb_init() creates a new ship and assigns its inventory. The gold assignment has no zero guard:

ptr->items[I_GOLD] = gernd()%cyb_gold;

With CYBGOLD=0, this evaluates gernd()%0. The failure can therefore occur when the first new Cybertron is generated or later when an NPC slot is repopulated. Existing saved Cybertron ships can be loaded without executing the new-ship inventory assignment, so a running game may not fail immediately after the setting is changed.

Correction in ge-next

The Cybertron gold allocation was rewritten as part of the ge-next 0.2 NPC changes. It still uses CYBGOLD as the upper bound of a random roll, but performs the roll only when cyb_gold > 0. A newly initialized ship starts with zero gold, so a zero setting leaves it with none and never evaluates a zero divisor. Current ge-next retains that guard; ge-arena also guards the roll and only assigns Cybertron gold for Hoard mode.

For a classic-compatible fix, test cyb_gold before taking a remainder and assign zero gold when the configured value is zero. Raising the configured minimum to one would avoid the crash but would remove the advertised no-gold setting.

See also