Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] (PR#2415) autoattack patch
Home

[Freeciv-Dev] (PR#2415) autoattack patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2415) autoattack patch
From: "Per I. Mathisen via RT" <rt@xxxxxxxxxxxxxx>
Date: Mon, 25 Nov 2002 17:14:36 -0800
Reply-to: rt@xxxxxxxxxxxxxx

Changes:
  - autoattack now controlled by old autoattack menu & key, and is a
separate activity like sentry and fortify
  - will now kill diplomats
  - removed stupid bug (didn't idle before moving)
  - saves autoattack setting
  - sends autoattack setting to client

TODO:
  - add autoattack to unit popup-dialog in city dialog
  - allow AI to use autoattack even if game.autoattack == FALSE
  - testing!!

Please have a go at this patch and report what you find.

  - Per

Index: client/control.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/control.c,v
retrieving revision 1.87
diff -u -r1.87 control.c
--- client/control.c    22 Nov 2002 18:52:12 -0000      1.87
+++ client/control.c    26 Nov 2002 01:07:07 -0000
@@ -31,6 +31,7 @@
 #include "mapctrl_g.h"
 #include "mapview_g.h"
 #include "menu_g.h"
+#include "unittype.h"
 
 #include "civclient.h"
 #include "clinet.h"
@@ -790,14 +791,21 @@
 **************************************************************************/
 void request_unit_auto(struct unit *punit)
 {
-  if (can_unit_do_auto(punit)) {
+  if (!can_unit_do_auto(punit)) {
+    append_output_window(_("Game: Only for settler units and military units"
+                          " when server has autoattack turned on."));
+    return;
+  }
+  if (punit->activity != ACTIVITY_AUTO_ATTACK
+      && can_unit_do_activity(punit, ACTIVITY_AUTO_ATTACK)) {
+    request_new_unit_activity(punit, ACTIVITY_AUTO_ATTACK);
+  } else if (unit_flag(punit, F_SETTLERS)) {
     struct packet_unit_request req;
     req.unit_id=punit->id;
     req.name[0]='\0';
     send_packet_unit_request(&aconnection, &req, PACKET_UNIT_AUTO);
   } else {
-    append_output_window(_("Game: Only settler units and military units"
-                          " in cities can be put in auto-mode."));
+    assert(FALSE);
   }
 }
 
@@ -1588,17 +1596,6 @@
   if(get_unit_in_focus())
     if(can_unit_do_activity(punit_focus, ACTIVITY_AIRBASE))
       request_new_unit_activity(punit_focus, ACTIVITY_AIRBASE);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-void key_unit_auto_attack(void)
-{
-  if(get_unit_in_focus())
-    if(!unit_flag(punit_focus, F_SETTLERS) &&
-       can_unit_do_auto(punit_focus))
-      request_unit_auto(punit_focus);
 }
 
 /**************************************************************************
Index: client/control.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/control.h,v
retrieving revision 1.30
diff -u -r1.30 control.h
--- client/control.h    22 Nov 2002 18:52:12 -0000      1.30
+++ client/control.h    26 Nov 2002 01:07:07 -0000
@@ -50,6 +50,7 @@
 void request_unit_change_homecity(struct unit *punit);
 void request_unit_connect(void);
 void request_unit_disband(struct unit *punit);
+void request_unit_autoattack(struct unit *punit);
 void request_unit_fortify(struct unit *punit);
 void request_unit_goto(void);
 void request_unit_move_done(void);
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.263
diff -u -r1.263 packhand.c
--- client/packhand.c   23 Nov 2002 02:55:42 -0000      1.263
+++ client/packhand.c   26 Nov 2002 01:07:08 -0000
@@ -1125,6 +1125,7 @@
   boot_help = (get_client_state() == CLIENT_GAME_RUNNING_STATE
               && game.spacerace != pinfo->spacerace);
   game.spacerace=pinfo->spacerace;
+  game.autoattack = pinfo->autoattack;
   if (game.timeout != 0) {
     if (pinfo->seconds_to_turndone != 0)
       seconds_to_turndone = pinfo->seconds_to_turndone;
Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.91
diff -u -r1.91 tilespec.c
--- client/tilespec.c   21 Nov 2002 02:26:48 -0000      1.91
+++ client/tilespec.c   26 Nov 2002 01:07:08 -0000
@@ -1158,6 +1158,9 @@
     case ACTIVITY_FORTIFIED:
       s = sprites.unit.fortified;
       break;
+    case ACTIVITY_AUTO_ATTACK:
+      s = sprites.unit.auto_attack;
+      break;
     case ACTIVITY_FORTIFYING:
       s = sprites.unit.fortifying;
       break;
@@ -1184,11 +1187,7 @@
   }
 
   if (punit->ai.control && punit->activity != ACTIVITY_EXPLORE) {
-    if (is_military_unit(punit)) {
-      *sprs++ = sprites.unit.auto_attack;
-    } else {
-      *sprs++ = sprites.unit.auto_settler;
-    }
+    *sprs++ = sprites.unit.auto_settler;
   }
 
   if (punit->connecting) {
Index: common/capstr.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/capstr.c,v
retrieving revision 1.115
diff -u -r1.115 capstr.c
--- common/capstr.c     14 Nov 2002 09:15:01 -0000      1.115
+++ common/capstr.c     26 Nov 2002 01:07:08 -0000
@@ -74,7 +74,7 @@
  * are not directly related to the capability strings discussed here.)
  */
 
-#define CAPABILITY "+1.14.0 conn_info +occupied team"
+#define CAPABILITY "+1.14.0 conn_info +occupied team autoattack"
   
 /* "+1.14.0" is protocol for 1.14.0 release.
   
@@ -85,6 +85,8 @@
    cities but instead use the occupied flag of short_city_info.
 
    "team" is support for player teams
+
+   "autoattack" is support for new server-side autoattack
 */
 
 void init_our_capability(void)
Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.152
diff -u -r1.152 game.c
--- common/game.c       14 Nov 2002 09:15:01 -0000      1.152
+++ common/game.c       26 Nov 2002 01:07:08 -0000
@@ -697,6 +697,7 @@
   game.onsetbarbarian = GAME_DEFAULT_ONSETBARBARIAN;
   game.nbarbarians = 0;
   game.occupychance= GAME_DEFAULT_OCCUPYCHANCE;
+  game.autoattack = GAME_DEFAULT_AUTOATTACK;
 
   geff_vector_init(&game.effects);
   ceff_vector_init(&game.destroyed_effects);
Index: common/game.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.h,v
retrieving revision 1.116
diff -u -r1.116 game.h
--- common/game.h       11 Nov 2002 10:00:47 -0000      1.116
+++ common/game.h       26 Nov 2002 01:07:08 -0000
@@ -91,6 +91,7 @@
   int onsetbarbarian;
   int nbarbarians;
   int occupychance;
+  bool autoattack;
   int unhappysize;
   bool angrycitizen;
   char *startmessage;
@@ -428,6 +429,8 @@
 #define GAME_DEFAULT_OCCUPYCHANCE    0
 #define GAME_MIN_OCCUPYCHANCE        0
 #define GAME_MAX_OCCUPYCHANCE        100
+
+#define GAME_DEFAULT_AUTOATTACK      FALSE
 
 #define GAME_DEFAULT_RULESETDIR      "default"
 
Index: common/packets.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.c,v
retrieving revision 1.226
diff -u -r1.226 packets.c
--- common/packets.c    14 Nov 2002 09:15:02 -0000      1.226
+++ common/packets.c    26 Nov 2002 01:07:08 -0000
@@ -1057,6 +1057,9 @@
   dio_put_uint8(&dout, pinfo->foodbox);
   dio_put_uint8(&dout, pinfo->civstyle);
   dio_put_bool8(&dout, pinfo->spacerace);
+  if (has_capability("autoattack", pc->capability)) {
+    dio_put_bool8(&dout, pinfo->autoattack);
+  }
 
   /* computed values */
   dio_put_uint32(&dout, pinfo->seconds_to_turndone);
@@ -1105,6 +1108,9 @@
   dio_get_uint8(&din, &pinfo->foodbox);
   dio_get_uint8(&din, &pinfo->civstyle);
   dio_get_bool8(&din, &pinfo->spacerace);
+  if (has_capability("autoattack", pc->capability)) {
+    dio_get_bool8(&din, &pinfo->autoattack);
+  }
 
   /* computed values */
   dio_get_uint32(&din, &pinfo->seconds_to_turndone);
Index: common/packets.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.h,v
retrieving revision 1.129
diff -u -r1.129 packets.h
--- common/packets.h    11 Nov 2002 10:00:47 -0000      1.129
+++ common/packets.h    26 Nov 2002 01:07:08 -0000
@@ -843,6 +843,7 @@
   int foodbox;
   int techpenalty;
   bool spacerace;
+  bool autoattack;
   /* the following values are computed each time packet_game_info is sent */
   int seconds_to_turndone;
 };
Index: common/unit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.c,v
retrieving revision 1.165
diff -u -r1.165 unit.c
--- common/unit.c       15 Nov 2002 21:24:30 -0000      1.165
+++ common/unit.c       26 Nov 2002 01:07:08 -0000
@@ -498,10 +498,12 @@
 **************************************************************************/
 bool can_unit_do_auto(struct unit *punit) 
 {
-  if (unit_flag(punit, F_SETTLERS))
+  if (unit_flag(punit, F_SETTLERS)) {
     return TRUE;
-  if (is_military_unit(punit) && map_get_city(punit->x, punit->y))
+  }
+  if (game.autoattack && is_military_unit(punit)) {
     return TRUE;
+  }
   return FALSE;
 }
 
@@ -543,6 +545,7 @@
   case ACTIVITY_ROAD:          text = _("Road"); break;
   case ACTIVITY_MINE:          text = _("Mine"); break;
   case ACTIVITY_IRRIGATE:      text = _("Irrigation"); break;
+  case ACTIVITY_AUTO_ATTACK:    text = _("Autoattack"); break;
   case ACTIVITY_FORTIFYING:    text = _("Fortifying"); break;
   case ACTIVITY_FORTIFIED:     text = _("Fortified"); break;
   case ACTIVITY_FORTRESS:      text = _("Fortress"); break;
@@ -650,6 +653,9 @@
   case ACTIVITY_PATROL:
     return TRUE;
 
+  case ACTIVITY_AUTO_ATTACK:
+    return is_military_unit(punit);
+
   case ACTIVITY_POLLUTION:
     return unit_flag(punit, F_SETTLERS) && tile_has_special(ptile, 
S_POLLUTION);
 
@@ -917,6 +923,7 @@
    case ACTIVITY_MINE: 
    case ACTIVITY_IRRIGATE:
    case ACTIVITY_TRANSFORM:
+   case ACTIVITY_AUTO_ATTACK:
    case ACTIVITY_FORTIFYING:
    case ACTIVITY_FORTIFIED:
    case ACTIVITY_AIRBASE:
Index: common/unit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.h,v
retrieving revision 1.90
diff -u -r1.90 unit.h
--- common/unit.h       15 Nov 2002 21:24:30 -0000      1.90
+++ common/unit.h       26 Nov 2002 01:07:08 -0000
@@ -26,7 +26,7 @@
   ACTIVITY_IRRIGATE, ACTIVITY_FORTIFIED, ACTIVITY_FORTRESS, ACTIVITY_SENTRY,
   ACTIVITY_RAILROAD, ACTIVITY_PILLAGE, ACTIVITY_GOTO, ACTIVITY_EXPLORE,
   ACTIVITY_TRANSFORM, ACTIVITY_UNKNOWN, ACTIVITY_AIRBASE, ACTIVITY_FORTIFYING,
-  ACTIVITY_FALLOUT, ACTIVITY_PATROL,
+  ACTIVITY_FALLOUT, ACTIVITY_PATROL, ACTIVITY_AUTO_ATTACK,
   ACTIVITY_LAST   /* leave this one last */
 };
 
Index: server/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/Makefile.am,v
retrieving revision 1.24
diff -u -r1.24 Makefile.am
--- server/Makefile.am  1 Nov 2002 17:40:46 -0000       1.24
+++ server/Makefile.am  26 Nov 2002 01:07:08 -0000
@@ -13,8 +13,6 @@
 libcivserver_a_SOURCES = \
                airgoto.c       \
                airgoto.h       \
-               autoattack.c    \
-               autoattack.h    \
                barbarian.c     \
                barbarian.h     \
                cityhand.c      \
Index: server/gamehand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamehand.c,v
retrieving revision 1.118
diff -u -r1.118 gamehand.c
--- server/gamehand.c   14 Nov 2002 09:15:04 -0000      1.118
+++ server/gamehand.c   26 Nov 2002 01:07:08 -0000
@@ -236,6 +236,7 @@
   ginfo.foodbox = game.foodbox;
   ginfo.civstyle = game.civstyle;
   ginfo.spacerace = game.spacerace;
+  ginfo.autoattack = game.autoattack;
   ginfo.unhappysize = game.unhappysize;
   ginfo.angrycitizen = game.angrycitizen;
   ginfo.diplcost = game.diplcost;
Index: server/savegame.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/savegame.c,v
retrieving revision 1.99
diff -u -r1.99 savegame.c
--- server/savegame.c   19 Nov 2002 13:35:31 -0000      1.99
+++ server/savegame.c   26 Nov 2002 01:07:09 -0000
@@ -1870,6 +1870,8 @@
                             1.10.0 */
     game.occupychance = secfile_lookup_int_default(file, game.occupychance,
                                                   "game.occupychance");
+    game.autoattack = secfile_lookup_bool_default(file, game.autoattack,
+                                                 "game.autoattack");
     game.randseed = secfile_lookup_int_default(file, game.randseed,
                                               "game.randseed");
     game.allowed_city_names =
@@ -2195,6 +2197,7 @@
   secfile_insert_int(file, game.barbarianrate, "game.barbarians");
   secfile_insert_int(file, game.onsetbarbarian, "game.onsetbarbs");
   secfile_insert_int(file, game.occupychance, "game.occupychance");
+  secfile_insert_bool(file, game.autoattack, "game.autoattack");
   secfile_insert_str(file, game.demography, "game.demography");
   secfile_insert_int(file, game.watchtower_vision, "game.watchtower_vision");
   secfile_insert_int(file, game.watchtower_extra_vision, 
"game.watchtower_extra_vision");
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.106
diff -u -r1.106 srv_main.c
--- server/srv_main.c   14 Nov 2002 09:15:05 -0000      1.106
+++ server/srv_main.c   26 Nov 2002 01:07:09 -0000
@@ -65,7 +65,6 @@
 #include "timing.h"
 #include "version.h"
 
-#include "autoattack.h"
 #include "barbarian.h"
 #include "cityhand.h"
 #include "citytools.h"
@@ -1822,8 +1821,6 @@
     /* and now, we must manage our remaining units BEFORE the cities that are
        empty get to refresh and defend themselves.  How totally stupid. */
     ai_start_turn(); /* Misleading name for manage_units -- Syela */
-    freelog(LOG_DEBUG, "Auto-Attack phase");
-    auto_attack();
     freelog(LOG_DEBUG, "Endturn");
     end_turn();
     freelog(LOG_DEBUG, "Gamenextyear");
Index: server/stdinhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/stdinhand.c,v
retrieving revision 1.266
diff -u -r1.266 stdinhand.c
--- server/stdinhand.c  25 Nov 2002 19:18:09 -0000      1.266
+++ server/stdinhand.c  26 Nov 2002 01:07:09 -0000
@@ -513,6 +513,12 @@
          GAME_MIN_OCCUPYCHANCE, GAME_MAX_OCCUPYCHANCE, 
          GAME_DEFAULT_OCCUPYCHANCE)
 
+  GEN_BOOL("autoattack", game.autoattack, SSET_RULES_FLEXIBLE, SSET_TO_CLIENT,
+         N_("Turn on/off server-side autoattack"),
+         N_("If set to on, units with move left will automatically "
+            "attack enemy units that move adjacent to them."), NULL,
+         GAME_DEFAULT_AUTOATTACK)
+
   GEN_INT("killcitizen", game.killcitizen, SSET_RULES, SSET_TO_CLIENT,
          N_("Reduce city population after attack"),
          N_("This flag indicates if city population is reduced "
@@ -2807,6 +2813,7 @@
        my_snprintf(buffer, sizeof(buffer),
                    _("Option: %s has been set to %d."), op->name,
                    *(op->bool_value) ? 1 : 0);
+       do_update = TRUE;
       }
     }
     break;
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.197
diff -u -r1.197 unittools.c
--- server/unittools.c  22 Nov 2002 17:44:45 -0000      1.197
+++ server/unittools.c  26 Nov 2002 01:07:10 -0000
@@ -2806,7 +2806,8 @@
 }
 
 /*****************************************************************
-Will wake up any neighboring enemy sentry units or patrolling units
+  Will wake up any neighboring enemy sentry units or patrolling 
+  units or autoattacking units.
 *****************************************************************/
 static void wakeup_neighbor_sentries(struct unit *punit)
 {
@@ -2846,6 +2847,51 @@
        (void) maybe_cancel_patrol_due_to_enemy(ppatrol);
       }
     } unit_list_iterate_end;
+  } square_iterate_end;
+
+  /* Do server-side autoattack */
+  square_iterate(punit->x, punit->y, 1, x, y) {
+    unit_list_iterate_safe(map_get_tile(x, y)->units, penemy) {
+      if (game.autoattack
+          && penemy->moves_left > 0
+          && (penemy->activity == ACTIVITY_AUTO_ATTACK
+              || unit_owner(penemy)->ai.control)
+          && pplayers_at_war(unit_owner(punit), unit_owner(penemy))
+          && map_get_known_and_seen(punit->x, punit->y, unit_owner(penemy))
+          && player_can_see_unit(unit_owner(penemy), punit)
+          && can_unit_attack_unit_at_tile(penemy, punit, punit->x, punit->y)) {
+        int sanity1 = punit->id, sanity2 = penemy->id;
+        int moves = punit->moves_left;
+        double punitwin, penemywin;
+
+        /* kludge to prevent attack power from dropping to zero during calc */
+        punit->moves_left = MAX(punit->moves_left, 1);
+
+        punitwin = unit_win_chance(punit, penemy);
+        penemywin = unit_win_chance(penemy, punit);
+        punit->moves_left = moves;
+
+        freelog(LOG_DEBUG, "autoattack if %f < %f with %s -> %s at (%d, %d)",
+                punitwin, penemywin, unit_type(penemy)->name, 
+                unit_type(punit)->name, punit->x, punit->y);
+
+        if (penemywin > 1.0 - punitwin || unit_flag(punit, F_DIPLOMAT)) {
+          /* Yep, that's all there is to it! */
+          handle_unit_activity_request(penemy, ACTIVITY_IDLE);
+          (void) handle_unit_move_request(penemy, punit->x, 
+                                          punit->y, FALSE, FALSE);
+        }
+
+        if (find_unit_by_id(sanity2)) {
+          send_unit_info(NULL, penemy);
+        }
+        if (find_unit_by_id(sanity1)) {
+          send_unit_info(NULL, punit);
+        } else {
+          return; /* done, gone */
+        }
+      }
+    } unit_list_iterate_safe_end;
   } square_iterate_end;
 }
 

[Prev in Thread] Current Thread [Next in Thread]