Skip to content

Commit

Permalink
city: remove city if shrunk to zero (#302)
Browse files Browse the repository at this point in the history
* city: remove city if shrunk to zero

* add CityEventCityAbandoned
  • Loading branch information
msom authored Feb 9, 2025
1 parent 44a225f commit 115fc57
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
16 changes: 12 additions & 4 deletions game/magic/city/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type CityEventPopulationGrowth struct {
Grow bool
}

type CityEventCityAbandoned struct {
}

type CityEventNewUnit struct {
Unit units.Unit
WeaponBonus data.WeaponBonus
Expand Down Expand Up @@ -1435,10 +1438,6 @@ func (city *City) DoNextTurn(garrison []units.StackUnit, mapObject *maplib.Map)
}
}

if city.Population > city.MaximumCitySize() * 1000 {
city.Population = city.MaximumCitySize() * 1000
}

if math.Abs(float64(city.Population/1000 - oldPopulation/1000)) > 0 {
cityEvents = append(cityEvents, &CityEventPopulationGrowth{Size: (city.Population - oldPopulation)/1000, Grow: city.Population > oldPopulation})
}
Expand All @@ -1465,6 +1464,15 @@ func (city *City) DoNextTurn(garrison []units.StackUnit, mapObject *maplib.Map)
}
}
}

if city.Population > city.MaximumCitySize() * 1000 {
city.Population = city.MaximumCitySize() * 1000
}

if city.Population < 1000 {
cityEvents = append(cityEvents, &CityEventCityAbandoned{})
}

}

if city.HasEnchantment(data.CityEnchantmentGaiasBlessing) {
Expand Down
30 changes: 17 additions & 13 deletions game/magic/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -6065,25 +6065,29 @@ func (game *Game) StartPlayerTurn(player *playerlib.Player) {
for _, event := range cityEvents {
switch event.(type) {
case *citylib.CityEventPopulationGrowth:
// growth := event.(*citylib.CityEventPopulationGrowth)

growthEvent := event.(*citylib.CityEventPopulationGrowth)
if player.IsHuman() {
growthEvent := event.(*citylib.CityEventPopulationGrowth)

verb := "grown"
if !growthEvent.Grow {
verb = "shrunk"
}
verb := "grown"
if !growthEvent.Grow {
verb = "shrunk"
}

// FIXME: can citizens shrink to 0, in which case the city should be removed?
scrollEvent := GameEventScroll{
Title: "CITY GROWTH",
Text: fmt.Sprintf("%v has %v to a population of %v.", city.Name, verb, city.Citizens()),
}

scrollEvent := GameEventScroll{
Title: "CITY GROWTH",
Text: fmt.Sprintf("%v has %v to a population of %v.", city.Name, verb, city.Citizens()),
select {
case game.Events<- &scrollEvent:
default:
}
}

case *citylib.CityEventCityAbandoned:
removeCities = append(removeCities, city)
if player.IsHuman() {
select {
case game.Events<- &scrollEvent:
case game.Events<- &GameEventNotice{Message: fmt.Sprintf("The city of %v has been abandoned.", city.Name)}:
default:
}
}
Expand Down
44 changes: 44 additions & 0 deletions test/overworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func createScenario4(cache *lbx.LbxCache) *gamelib.Game {
return game
}

// city growth message
func createScenario5(cache *lbx.LbxCache) *gamelib.Game {
log.Printf("Running scenario 5")
wizard := setup.WizardCustom{
Expand Down Expand Up @@ -3397,6 +3398,48 @@ func createScenario40(cache *lbx.LbxCache) *gamelib.Game {
return game
}

// shrink city to zero
func createScenario41(cache *lbx.LbxCache) *gamelib.Game {
log.Printf("Running scenario 41")
wizard := setup.WizardCustom{
Name: "bob",
Banner: data.BannerRed,
Race: data.RaceTroll,
Abilities: []setup.WizardAbility{},
Books: []data.WizardBook{},
}

game := gamelib.MakeGame(cache, setup.NewGameSettings{
Magic: data.MagicSettingNormal,
Difficulty: data.DifficultyAverage,
})

game.Plane = data.PlaneArcanus

player := game.AddPlayer(wizard, true)

x, y, _ := game.FindValidCityLocation(game.Plane)

city := citylib.MakeCity("Erfurt", x, y, player.Wizard.Race, player.Wizard.Banner, player.TaxRate, game.BuildingInfo, game.CurrentMap(), game)
city.Population = 1000
city.Plane = data.PlaneArcanus
city.Banner = wizard.Banner
city.ProducingUnit = units.TrollSettlers
city.Production = float32(city.ProducingUnit.ProductionCost)
city.Farmers = 1
city.ResetCitizens(nil)

player.AddCity(city)
player.Gold = 1000
player.Mana = 1000

player.LiftFog(x, y, 4, data.PlaneArcanus)

game.Camera.Center(x, y)

return game
}

func NewEngine(scenario int) (*Engine, error) {
cache := lbx.AutoCache()

Expand Down Expand Up @@ -3443,6 +3486,7 @@ func NewEngine(scenario int) (*Engine, error) {
case 38: game = createScenario38(cache)
case 39: game = createScenario39(cache)
case 40: game = createScenario40(cache)
case 41: game = createScenario41(cache)
default: game = createScenario1(cache)
}

Expand Down

0 comments on commit 115fc57

Please sign in to comment.