News:

Simutrans Sites
Know our official sites. Find tools and resources for Simutrans.

r12418 pak64 - town attractions lose their town when a game is loaded

Started by wasname123, Today at 03:05:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wasname123

Greetings. Attractions that a town builds itself lose their town when the game is loaded.

In stadt_t::check_bau_spezial() the town builds the attraction and calls add_gebaeude_to_stadt(), so in the running game it belongs to the town, and the savegame writes its town index. But when loading, gebaeude_t::rdwr() only restores the town if is_connected_with_town() is true, which is false for attraction_city. So the attraction comes back without a town, and finish_rd() doesn't add it to the town again.

Clients reload on every join, so in network games this happens on every join too. On r12117 the towns' passenger statistics were already different on the first step after loading.

To reproduce:
  • Start a new pak64 game (I used 128x128 with 8 towns) and let it run for a while so the towns build some attractions.
  • Save and load.
  • The town-built attractions now have no town (get_stadt() returns NULL), even though the save has their town index.

In the attached save (seed 7, r12418) there are 15 of them, for example 10_WOHN at (40,1) and Raven_Kirche at (43,3) of Ashbury, and Residenz at (5,7) of Oldstead. All 15 lose their town on loading. Same in two other maps I tried (11 and 13 attractions).

I found topic 576 from 2008, where attractions placed by the player with the curiosity tool were stopped from joining a town. But towns still add the ones they build themselves, so the running game and a loaded game don't agree. I'm not sure which one is intended. If attractions shouldn't belong to towns at all, the fix would be to drop the add_gebaeude_to_stadt() call in check_bau_spezial() instead.

Here is a possible patch against r12418 that keeps the town on loading. With it, every attraction kept its town in the same three maps.

diff --git a/src/simutrans/obj/gebaeude.cc b/src/simutrans/obj/gebaeude.cc
index 382a93091..57f46df1e 100644
--- a/src/simutrans/obj/gebaeude.cc
+++ b/src/simutrans/obj/gebaeude.cc
@@ -1017,7 +1017,8 @@ void gebaeude_t::rdwr(loadsave_t *file)
  city_index = welt->get_cities().index_of( ptr.stadt );
  }
  file->rdwr_long(city_index);
- if(  file->is_loading()  &&  (tile==NULL  ||  tile->get_desc()==NULL  ||  tile->get_desc()->is_connected_with_town())  ) {
+ // city attractions built by a town belong to it (stadt_t::check_bau_spezial), so restore their town too
+ if(  file->is_loading()  &&  (tile==NULL  ||  tile->get_desc()==NULL  ||  tile->get_desc()->is_connected_with_town()  ||  tile->get_desc()->get_type()==building_desc_t::attraction_city)  ) {
  if (city_index != -1) {
  ptr.stadt = welt->get_cities()[city_index];
  }
@@ -1058,6 +1059,16 @@ bool gebaeude_t::finish_rd()
 #endif
  }
  }
+ else if(  tile->get_desc()->get_type()==building_desc_t::attraction_city  &&  ptr.stadt!=NULL  ) {
+ // a city attraction that its town built: rejoin the town, as before saving
+#ifdef MULTI_THREAD
+ pthread_mutex_lock( &add_to_city_mutex );
+#endif
+ ptr.stadt->add_gebaeude_to_stadt(this, true);
+#ifdef MULTI_THREAD
+ pthread_mutex_unlock( &add_to_city_mutex );
+#endif
+ }
  else if(  !is_factory  ) {
  ptr.stadt = NULL;
  }

I tried attaching the savegame and patch but I couldn't