Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2003:
[Freeciv-Dev] (PR#4645) Silent Hunter and Sonar code.
Home

[Freeciv-Dev] (PR#4645) Silent Hunter and Sonar code.

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#4645) Silent Hunter and Sonar code.
From: "Rafa³ Bursig" <bursig@xxxxxxxxx>
Date: Tue, 22 Jul 2003 03:53:13 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Hi All

This code try add SILENT_HUNTER and SONAR unit functionality.

1) SILENT_HUNTER
basicaly it is submartine functionaliy and work like Partial_Invis ( 
replace it ) with differences that sub is not detected until any unit 
enter on tile with sub. When this happend and unit onwer is in war with 
sub owner then sub automaticaly attack first (if can) if not then unit 
stop.
In both cases sub is dettected and visilbe untill end of turn.
2) SONAR
abbility to detect SILENT_HUNTER units (added to Destroyers and Heli)
all cities have this functionality too.

Current patch is only initial version of this code. I don't check many 
aspect of movment (ZOC, PF, etc) but I simple ask community about 
quality of this idea and project.

Rafal
diff -u -r ../fc/freeciv/client/control.c fc/client/control.c
--- freeciv/client/control.c    Sun Jul 20 12:17:53 2003
+++ fc/client/control.c Mon Jul 21 18:38:30 2003
@@ -1225,8 +1225,8 @@
 **************************************************************************/
 void do_move_unit(struct unit *punit, struct packet_unit_info *pinfo)
 {
-  int x, y;
-  bool was_teleported;
+  int x, y, vision_range;
+  bool was_teleported, sonar;
   
   was_teleported=!is_tiles_adjacent(punit->x, punit->y, pinfo->x, pinfo->y);
   x=punit->x;
@@ -1239,20 +1239,23 @@
 
   unit_list_unlink(&map_get_tile(x, y)->units, punit);
 
-  if(!pinfo->carried)
+  if (!pinfo->carried) {
     refresh_tile_mapcanvas(x, y, FALSE);
+  }
   
-  if(game.player_idx==punit->owner && punit->activity!=ACTIVITY_GOTO && 
+  if (game.player_idx==punit->owner && punit->activity!=ACTIVITY_GOTO && 
      auto_center_on_unit && punit->activity!=ACTIVITY_SENTRY &&
-     !tile_visible_and_not_on_border_mapcanvas(pinfo->x, pinfo->y))
+     !tile_visible_and_not_on_border_mapcanvas(pinfo->x, pinfo->y)) {
     center_tile_mapcanvas(pinfo->x, pinfo->y);
+  }
 
-  if(!pinfo->carried && !was_teleported) {
+  if (!pinfo->carried && !was_teleported) {
     int dx, dy;
 
     map_distance_vector(&dx, &dy, punit->x, punit->y, pinfo->x, pinfo->y);
-    if(smooth_move_units)
+    if (smooth_move_units) {
       move_unit_map_canvas(punit, x, y, dx, dy);
+    }
     refresh_tile_mapcanvas(x, y, FALSE);
   }
     
@@ -1262,10 +1265,16 @@
   punit->hp=pinfo->hp;
   unit_list_insert(&map_get_tile(punit->x, punit->y)->units, punit);
 
-  square_iterate(punit->x, punit->y, 2, x, y) {
+  vision_range = get_unit_type(punit->type)->vision_range;
+  sonar = unit_flag(punit, F_SONAR);
+  square_iterate(punit->x, punit->y, vision_range, x, y) {
     bool refresh = FALSE;
+    if (x == punit->x && y == punit->y) {
+      continue;
+    }
     unit_list_iterate(map_get_tile(x, y)->units, pu) {
-      if (unit_flag(pu, F_PARTIAL_INVIS)) {
+      if (unit_flag(pu, F_PARTIAL_INVIS)
+        || (sonar && unit_flag(pu, F_SILENT_HUNTER))) {
        refresh = TRUE;
        goto out;
       }
@@ -1276,10 +1285,13 @@
     }
   } square_iterate_end;
   
-  if(!pinfo->carried && tile_get_known(punit->x,punit->y) == TILE_KNOWN)
+  if (!pinfo->carried && tile_get_known(punit->x, punit->y) == TILE_KNOWN) {
     refresh_tile_mapcanvas(punit->x, punit->y, FALSE);
+  }
 
-  if(get_unit_in_focus()==punit) update_menus();
+  if (get_unit_in_focus()==punit) {
+    update_menus();
+  }
 }
 
 /**************************************************************************
diff -u -r freeciv/common/player.c fc/common/player.c
--- freeciv/common/player.c     Mon Jul  7 18:58:17 2003
+++ fc/common/player.c  Tue Jul 22 00:19:12 2003
@@ -224,24 +224,34 @@
                                      struct unit *punit, 
                                      int x, int y)
 {
+  bool silent;
   if (pplayers_allied(unit_owner(punit), pplayer)
       || !is_hiding_unit(punit)) {
     return TRUE;
   }
 
   /* Search for units/cities that might be able to see the sub/missile */
-  adjc_iterate(x, y, x1, y1) {
-    struct city *pcity = map_get_city(x1, y1);
-    unit_list_iterate(map_get_tile(x1, y1)->units, punit2) {
-      if (pplayers_allied(unit_owner(punit2), pplayer)) {
+  /* I hardcoded 2 here as max allied unit vision_range but IMHO this
+     should be max sonar range - rafal */
+  silent = unit_flag(punit, F_SILENT_HUNTER);
+  square_iterate(x, y, 2, x1, y1) {
+    struct tile *ptile = map_get_tile(x1, y1);
+      
+    /* Note: This will check tiles not belong to city map too ! */  
+    if (ptile->city && pplayers_allied(city_owner(ptile->city), pplayer)) {
+      return TRUE;
+    }
+    
+    unit_list_iterate(ptile->units, punit2) {
+      if (pplayers_allied(unit_owner(punit2), pplayer)
+        && (!silent || (silent && unit_flag(punit2, F_SONAR)))
+         && (map_distance(x, y, punit2->x, punit2->y) <=
+             get_unit_type(punit2->type)->vision_range)) {
        return TRUE;
       }
     } unit_list_iterate_end;
-
-    if (pcity && pplayers_allied(city_owner(pcity), pplayer)) {
-      return TRUE;
-    }
-  } adjc_iterate_end;
+    
+  } square_iterate_end;
 
   return FALSE;
 }
diff -u -r freeciv/common/unit.c fc/common/unit.c
--- freeciv/common/unit.c       Mon Jul 21 17:05:11 2003
+++ fc/common/unit.c    Tue Jul 22 00:20:04 2003
@@ -400,10 +400,13 @@
 **************************************************************************/
 bool is_hiding_unit(struct unit *punit)
 {
-  struct unit *transporter = find_unit_by_id(punit->transported_by);
+  struct unit *transporter;
 
   return (unit_flag(punit, F_PARTIAL_INVIS)
-         || (transporter && unit_flag(transporter, F_PARTIAL_INVIS)));
+         || unit_flag(punit, F_SILENT_HUNTER)
+         || ((transporter = find_unit_by_id(punit->transported_by))
+            && (unit_flag(transporter, F_PARTIAL_INVIS)
+                 || unit_flag(transporter, F_SILENT_HUNTER))));
 }
 
 /**************************************************************************
diff -u -r freeciv/common/unittype.h fc/common/unittype.h
--- freeciv/common/unittype.h   Thu Jul  3 10:58:11 2003
+++ fc/common/unittype.h        Mon Jul 21 18:33:47 2003
@@ -93,7 +93,7 @@
   F_AEGIS,       
   F_FIGHTER,     
   F_MARINES,     
-  F_PARTIAL_INVIS,    /* Invisibile except when adjacent (Submarine) */   
+  F_PARTIAL_INVIS,    /* Invisibile except when adjacent (Stealth) */   
   F_SETTLERS,         /* Does not include ability to found cities */
   F_DIPLOMAT,    
   F_TRIREME,          /* Trireme sinking effect */
@@ -109,8 +109,13 @@
   F_ADD_TO_CITY,      /* unit can add to city population */
   F_FANATIC,          /* Only Fundamentalist government can build
                         these units */
+  F_SILENT_HUNTER,    /* like F_PARTIAL_INVIS but Invisibile untill enemy
+                        enter on unit (start combat (attack) this enemy)
+                        or find by sonar and is visible for rest of turn 
(Submarine) */
+  F_SONAR,           /* Can detect F_SILENT_HUNTERs (Destroyer/Heli) */
   F_LAST
 };
+
 #define F_MAX 64
 
 /* Unit "roles": these are similar to unit flags but differ in that
diff -u -r freeciv/data/default/units.ruleset fc/data/default/units.ruleset
--- freeciv/data/default/units.ruleset  Tue Jun 17 02:11:32 2003
+++ fc/data/default/units.ruleset       Tue Jul 22 11:04:22 2003
@@ -1026,7 +1026,7 @@
 uk_shield     = 1
 uk_food       = 0
 uk_gold       = 0
-flags         = "FieldUnit", "OneAttack"
+flags         = "FieldUnit", "OneAttack", "Sonar"
 roles         = ""
 helptext      = _("\
 The Helicopter is a very powerful unit, as it can both fly and\
@@ -1264,7 +1264,7 @@
 uk_shield     = 1
 uk_food       = 0
 uk_gold       = 0
-flags         = ""
+flags         = "Sonar"
 roles         = ""
 helptext      = _("\
 TIP:  A very fast unit, which is very useful for hunting down enemy\
@@ -1380,7 +1380,7 @@
 uk_shield     = 1
 uk_food       = 0
 uk_gold       = 0
-flags         = "Partial_Invis", 
+flags         = "Silent_Hunter", 
                "Missile_Carrier", "No_Land_Attack"
 roles         = ""
 helptext      = _("\
diff -u -r freeciv/server/unithand.c fc/server/unithand.c
--- freeciv/server/unithand.c   Sun Jul 20 23:16:01 2003
+++ fc/server/unithand.c        Tue Jul 22 00:14:03 2003
@@ -927,7 +927,7 @@
 {
   struct player *pplayer = unit_owner(punit);
   struct tile *pdesttile = map_get_tile(dest_x, dest_y);
-  struct unit *pdefender = get_defender(punit, dest_x, dest_y);
+  struct unit *pdefender = NULL;
   struct city *pcity = pdesttile->city;
 
   if (!is_normal_map_pos(dest_x, dest_y)) {
@@ -996,7 +996,31 @@
       return FALSE;
     }
   }
-
+  
+  /* are there any units that player have no idea about it */
+  if(unit_list_size(&pdesttile->units)
+    && !unit_list_size(&map_get_player_tile(dest_x, dest_y, pplayer)->units)
+    && (pdefender = get_attacker(punit, dest_x, dest_y))
+    && unit_flag(pdefender, F_SILENT_HUNTER)) {
+      
+    send_unit_info(pplayer, pdefender);
+    if (pplayers_at_war(unit_owner(punit), unit_owner(pdefender))
+       && can_unit_attack_unit_at_tile(pdefender, punit, punit->x, punit->y)) {
+      notify_player_ex(pplayer, pdefender->x, pdefender->y, E_NOEVENT,
+                      _("Game: %s's %s attack our %s"),
+                      unit_owner(pdefender)->name,
+                      pdefender->name, punit->name);
+      handle_unit_attack_request(pdefender, punit);
+    } else {
+      notify_player_ex(pplayer, pdefender->x, pdefender->y, E_NOEVENT,
+                      _("Game: Our %s found %s's %s"),
+                       punit->name, unit_owner(pdefender)->name,
+                      pdefender->name);
+    }
+    return FALSE;
+  }
+  
+  pdefender = get_defender(punit, dest_x, dest_y);
   /*** Try to attack if there is an enemy unit on the target tile ***/
   if (pdefender
       && pplayers_at_war(unit_owner(punit), unit_owner(pdefender))) {
diff -u -r freeciv/server/unittools.c fc/server/unittools.c
--- freeciv/server/unittools.c  Mon Jul 21 17:05:11 2003
+++ fc/server/unittools.c       Mon Jul 21 20:26:42 2003
@@ -1824,12 +1824,10 @@
     freelog(LOG_ERROR, "NULL pointer in can_player_see_unit_at");
     return FALSE;
   } else {
-    bool see_tile = map_get_known_and_seen(x, y, pplayer);
-    bool see_unit = player_can_see_unit_at_location(pplayer, punit, x, y);
-    bool not_in_city = (pplayers_allied(unit_owner(punit), pplayer)
-                       || !map_get_city(x, y));
-
-    return (see_tile && see_unit && not_in_city);
+    return (map_get_known_and_seen(x, y, pplayer)
+           && player_can_see_unit_at_location(pplayer, punit, x, y)
+           && (pplayers_allied(unit_owner(punit), pplayer)
+               || !map_get_city(x, y)));
   }
 }
 

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