Project

General

Profile

Actions

Bug #2467

open

ai: city danger arithmetic overflows.

Added by Sweet Tea Dorminy 2 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
AI
Target version:
-
Start date:
11/09/2026
Due date:
% Done:

0%

Estimated time:

Description

assess_danger() sums over enemy units v^2 / move_time, where v is attack rating (attack * hp * firepower). A Cruiser, say, rates about 360, so each cruiser three turns out adds about 43,000 to unsigned city_data->danger.

military_advisor_choose_build() then squares that into an int:

  int qdanger = city_data->danger * city_data->danger;

Except it's squaring two unsigned and then casting to an int, so danger above 46,340 wraps -- two Cruisers. If it wraps to a negative number, the city takes the "qdanger <= 0" branch: no defender want, no walls.

It then decides what percent of the city's defense qdanger is, which can also wrap:

  else                     danger = MIN(200, 100 * qdanger / our_def) + urgency;

Two Cannons already wrap.

The defense calculation is already clamping its own rating at 1<<15 in assess_defense_quadratic() - and it's only summing defense values, not squared values, so it's much harder to reach the max.


Files

No data to display

Actions

Also available in: Atom PDF