Index: client/agents/cma_core.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/agents/cma_core.c,v retrieving revision 1.29 diff -u -r1.29 cma_core.c --- client/agents/cma_core.c 2002/09/06 19:07:34 1.29 +++ client/agents/cma_core.c 2002/09/15 09:05:53 @@ -614,6 +614,43 @@ } /**************************************************************************** + Returns TRUE if the city is valid for CMA. Fills parameter if TRUE + is returned. +*****************************************************************************/ +static bool check_city(int city_id, struct cma_parameter *parameter) +{ + struct city *pcity = find_city_by_id(city_id); + size_t len; + struct cma_parameter dummy; + + if (!parameter) { + parameter = &dummy; + } + + if (!pcity) { + return FALSE; + } + + len = attr_city_get(ATTR_CITY_CMA_PARAMETER, pcity->id, + sizeof(*parameter), parameter); + + if (len == 0) { + return FALSE; + } + + if (city_owner(pcity) != game.player_ptr) { + cma_release_city(pcity); + create_event(pcity->x, pcity->y, E_CITY_CMA_RELEASE, + _("CMA: You lost control of %s. Detaching from city."), + pcity->name); + return FALSE; + } + + assert(len == sizeof(*parameter)); + return TRUE; +} + +/**************************************************************************** Change the actual city setting to the given result. Returns TRUE iff the actual data matches the calculated one. *****************************************************************************/ @@ -742,7 +779,12 @@ connection_do_unbuffer(&aconnection); if (last_request_id != 0) { + int city_id = pcity->id; + wait_for_requests("CMA", first_request_id, last_request_id); + if (!check_city(city_id, NULL)) { + return FALSE; + } } /* Return. */ @@ -1709,42 +1751,33 @@ follows the set CMA goal or that the CMA detaches itself from the city. *****************************************************************************/ -static void handle_city(struct city *pcity) +static void handle_city(int city_id) { - struct cma_parameter parameter; struct cma_result result; - size_t len; bool handled; int i; - - len = attr_city_get(ATTR_CITY_CMA_PARAMETER, pcity->id, - sizeof(parameter), ¶meter); - - if (len == 0) { - return; - } + struct city *pcity = find_city_by_id(city_id); freelog(HANDLE_CITY_LOG_LEVEL, "handle_city(city='%s'(%d) pos=(%d,%d) owner=%s)", pcity->name, pcity->id, pcity->x, pcity->y, city_owner(pcity)->name); - if (city_owner(pcity) != game.player_ptr) { - cma_release_city(pcity); - create_event(pcity->x, pcity->y, E_CITY_CMA_RELEASE, - _("CMA: You lost control of %s. Detaching from city."), - pcity->name); - return; - } - - assert(len == sizeof(parameter)); - freelog(HANDLE_CITY_LOG_LEVEL2, "START handle city='%s'(%d)", pcity->name, pcity->id); handled = FALSE; for (i = 0; i < 5; i++) { + struct cma_parameter parameter; + freelog(HANDLE_CITY_LOG_LEVEL2, " try %d", i); + if (!check_city(city_id, ¶meter)) { + handled = TRUE; + break; + } + + pcity = find_city_by_id(city_id); + cma_query_result(pcity, ¶meter, &result); if (!result.found_a_valid) { freelog(HANDLE_CITY_LOG_LEVEL2, " no valid found result"); @@ -1759,7 +1792,7 @@ } else { if (!apply_result_on_server(pcity, &result)) { freelog(HANDLE_CITY_LOG_LEVEL2, " doesn't cleanly apply"); - if (i == 0) { + if (check_city(city_id, NULL) && i == 0) { create_event(pcity->x, pcity->y, E_NOEVENT, _("CMA: %s has changed and the calculated " "result can't be applied. Will retry."), @@ -1774,7 +1807,10 @@ } } + pcity = find_city_by_id(city_id); + if (!handled) { + assert(pcity); freelog(HANDLE_CITY_LOG_LEVEL2, " not handled"); create_event(pcity->x, pcity->y, E_CITY_CMA_RELEASE, @@ -1795,8 +1831,7 @@ #endif } - freelog(HANDLE_CITY_LOG_LEVEL2, "END handle city='%s'(%d)", pcity->name, - pcity->id); + freelog(HANDLE_CITY_LOG_LEVEL2, "END handle city=(%d)", city_id); } /**************************************************************************** @@ -1805,7 +1840,7 @@ static void city_changed(struct city *pcity) { clear_caches(pcity); - handle_city(pcity); + handle_city(pcity->id); } /****************************************************************************