Author: cazfi
Date: Mon May 15 23:15:19 2017
New Revision: 35605

URL: http://svn.gna.org/viewcvs/freeciv?rev=35605&view=rev
Log:
Tex: Un/register units from/to the world

See hrm Feature #659527

Modified:
    trunk/ai/tex/texai.c
    trunk/ai/tex/texaimsg.h
    trunk/ai/tex/texaiplayer.c
    trunk/ai/tex/texaiworld.c
    trunk/ai/tex/texaiworld.h

Modified: trunk/ai/tex/texai.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/tex/texai.c?rev=35605&r1=35604&r2=35605&view=diff
==============================================================================
--- trunk/ai/tex/texai.c        (original)
+++ trunk/ai/tex/texai.c        Mon May 15 23:15:19 2017
@@ -621,6 +621,9 @@
   ai->funcs.unit_alloc = texwai_unit_alloc;
   ai->funcs.unit_free = texwai_unit_free;
 
+  ai->funcs.unit_created = texai_unit_created;
+  ai->funcs.unit_destroyed = texai_unit_destroyed;
+
   ai->funcs.unit_got = texwai_ferry_init_ferry;
   ai->funcs.unit_lost = texwai_ferry_lost;
   ai->funcs.unit_transformed = texwai_ferry_transformed;

Modified: trunk/ai/tex/texaimsg.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/tex/texaimsg.h?rev=35605&r1=35604&r2=35605&view=diff
==============================================================================
--- trunk/ai/tex/texaimsg.h     (original)
+++ trunk/ai/tex/texaimsg.h     Mon May 15 23:15:19 2017
@@ -30,6 +30,10 @@
 #define SPECENUM_VALUE6NAME "CityCreated"
 #define SPECENUM_VALUE7 TEXAI_MSG_CITY_DESTROYED
 #define SPECENUM_VALUE7NAME "CityDestroyed"
+#define SPECENUM_VALUE8 TEXAI_MSG_UNIT_CREATED
+#define SPECENUM_VALUE8NAME "UnitCreated"
+#define SPECENUM_VALUE9 TEXAI_MSG_UNIT_DESTROYED
+#define SPECENUM_VALUE9NAME "UnitDestroyed"
 #include "specenum_gen.h"
 
 #define SPECENUM_NAME texaireqtype

Modified: trunk/ai/tex/texaiplayer.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/tex/texaiplayer.c?rev=35605&r1=35604&r2=35605&view=diff
==============================================================================
--- trunk/ai/tex/texaiplayer.c  (original)
+++ trunk/ai/tex/texaiplayer.c  Mon May 15 23:15:19 2017
@@ -79,8 +79,9 @@
 
   log_debug("New AI thread launched");
 
+  texai_world_init();
   if (!map_is_empty()) {
-    texai_world_init();
+    texai_map_init();
   }
 
   /* Just wait until we are signaled to shutdown */
@@ -94,6 +95,8 @@
   }
   fc_release_mutex(&exthrai.msgs_to.mutex);
 
+  texai_world_close();
+
   log_debug("AI thread exiting");
 }
 
@@ -110,7 +113,7 @@
 **************************************************************************/
 static void texai_game_start_recv(void)
 {
-  texai_world_init();
+  texai_map_init();
 }
 
 /**************************************************************************
@@ -126,7 +129,7 @@
 **************************************************************************/
 static void texai_game_free_recv(void)
 {
-  texai_world_close();
+  texai_map_close();
 }
 
 /**************************************************************************
@@ -178,6 +181,12 @@
     case TEXAI_MSG_TILE_INFO:
       texai_tile_info_recv(msg->data);
       break;
+    case TEXAI_MSG_UNIT_CREATED:
+      texai_unit_info_recv(msg->data, msg->type);
+      break;
+    case TEXAI_MSG_UNIT_DESTROYED:
+      texai_unit_destruction_recv(msg->data);
+      break;
     case TEXAI_MSG_CITY_CREATED:
       texai_city_info_recv(msg->data, msg->type);
       break;

Modified: trunk/ai/tex/texaiworld.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/tex/texaiworld.c?rev=35605&r1=35604&r2=35605&view=diff
==============================================================================
--- trunk/ai/tex/texaiworld.c   (original)
+++ trunk/ai/tex/texaiworld.c   Mon May 15 23:15:19 2017
@@ -41,28 +41,50 @@
   int tindex;
 };
 
-struct texai_city_id_msg
+struct texai_id_msg
 {
   int id;
 };
 
+struct texai_unit_info_msg
+{
+  int id;
+  int owner;
+  int tindex;
+  int type;
+};
+
 /**************************************************************************
   Initialize world object for texai
 **************************************************************************/
 void texai_world_init(void)
+{
+  idex_init(&texai_world);
+}
+
+/**************************************************************************
+  Free resources allocated for texai world object
+**************************************************************************/
+void texai_world_close(void)
+{
+  idex_free(&texai_world);
+}
+
+/**************************************************************************
+  Initialize world map for texai
+**************************************************************************/
+void texai_map_init(void)
 {
   map_init(&(texai_world.map), TRUE);
   map_allocate(&(texai_world.map));
-  idex_init(&texai_world);
-}
-
-/**************************************************************************
-  Free resources allocated for texai world object
-**************************************************************************/
-void texai_world_close(void)
+}
+
+/**************************************************************************
+  Free resources allocated for texai world map
+**************************************************************************/
+void texai_map_close(void)
 {
   map_free(&(texai_world.map));
-  idex_free(&texai_world);
 }
 
 /**************************************************************************
@@ -151,7 +173,7 @@
 void texai_city_destroyed(struct city *pcity)
 {
   if (texai_thread_running()) {
-    struct texai_city_id_msg *info = fc_malloc(sizeof(struct 
texai_city_id_msg));
+    struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
 
     info->id = pcity->id;
 
@@ -164,9 +186,84 @@
 **************************************************************************/
 void texai_city_destruction_recv(void *data)
 {
-  struct texai_city_id_msg *info = (struct texai_city_id_msg *)data;
+  struct texai_id_msg *info = (struct texai_id_msg *)data;
   struct city *pcity = idex_lookup_city(&texai_world, info->id);
 
   idex_unregister_city(&texai_world, pcity);
   destroy_city_virtual(pcity);
 }
+
+/**************************************************************************
+  Send unit information to the thread.
+**************************************************************************/
+static void texai_unit_update(struct unit *punit, enum texaireqtype msgtype)
+{
+  struct texai_unit_info_msg *info
+    = fc_malloc(sizeof(struct texai_unit_info_msg));
+
+  info->id = punit->id;
+  info->owner = player_number(unit_owner(punit));
+  info->tindex = tile_index(unit_tile(punit));
+  info->type = utype_number(unit_type_get(punit));
+
+  texai_send_msg(msgtype, NULL, info);
+}
+
+/**************************************************************************
+  New unit has been added to the main map.
+**************************************************************************/
+void texai_unit_created(struct unit *punit)
+{
+  if (texai_thread_running()) {
+    texai_unit_update(punit, TEXAI_MSG_UNIT_CREATED);
+  }
+}
+
+/**************************************************************************
+  Receive unit update to the thread.
+**************************************************************************/
+void texai_unit_info_recv(void *data, enum texaimsgtype msgtype)
+{
+  struct texai_unit_info_msg *info = (struct texai_unit_info_msg *)data;
+  struct unit *punit;
+  struct player *pplayer = player_by_number(info->owner);
+  struct unit_type *type = utype_by_number(info->type);
+  struct tile *ptile = index_to_tile(&(texai_world.map), info->tindex);
+
+  if (msgtype == TEXAI_MSG_UNIT_CREATED) {
+    punit = unit_virtual_create(pplayer, NULL, type, 0);
+    punit->id = info->id;
+
+    idex_register_unit(&texai_world, punit);
+  } else {
+    punit = idex_lookup_unit(&texai_world, info->id);
+  }
+
+  punit->tile = ptile;
+}
+
+/**************************************************************************
+  Unit has been removed from the main map.
+**************************************************************************/
+void texai_unit_destroyed(struct unit *punit)
+{
+  if (texai_thread_running()) {
+    struct texai_id_msg *info = fc_malloc(sizeof(struct texai_id_msg));
+
+    info->id = punit->id;
+
+    texai_send_msg(TEXAI_MSG_UNIT_DESTROYED, NULL, info);
+  }
+}
+
+/**************************************************************************
+  Receive unit destruction to the thread.
+**************************************************************************/
+void texai_unit_destruction_recv(void *data)
+{
+  struct texai_id_msg *info = (struct texai_id_msg *)data;
+  struct unit *punit = idex_lookup_unit(&texai_world, info->id);
+
+  idex_unregister_unit(&texai_world, punit);
+  unit_virtual_destroy(punit);
+}

Modified: trunk/ai/tex/texaiworld.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/tex/texaiworld.h?rev=35605&r1=35604&r2=35605&view=diff
==============================================================================
--- trunk/ai/tex/texaiworld.h   (original)
+++ trunk/ai/tex/texaiworld.h   Mon May 15 23:15:19 2017
@@ -18,6 +18,9 @@
 void texai_world_init(void);
 void texai_world_close(void);
 
+void texai_map_init(void);
+void texai_map_close(void);
+
 void texai_tile_info(struct tile *ptile);
 void texai_tile_info_recv(void *data);
 
@@ -26,4 +29,9 @@
 void texai_city_destroyed(struct city *pcity);
 void texai_city_destruction_recv(void *data);
 
+void texai_unit_created(struct unit *punit);
+void texai_unit_info_recv(void *data, enum texaimsgtype msgtype);
+void texai_unit_destroyed(struct unit *punit);
+void texai_unit_destruction_recv(void *data);
+
 #endif /* FC__TEXAIWORLD_H */


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to