Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] [PATCH] chance to win in civclient
Home

[Freeciv-Dev] [PATCH] chance to win in civclient

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] [PATCH] chance to win in civclient
From: Andreas Røsdal <andrearo@xxxxxxxxxxxx>
Date: Sun, 26 Jan 2003 11:39:31 +0100

Hello,

I've made a patch that makes the planning of attacks quicker
and makes defending units from enemies easier. I think that
calculating your chance to win against an enemy unit takes
too much time to do manually. Therefore this patch calculates
the probability to win if your active unit attacks the selected
enemy unit, and the probability to win if your active unit
is defending against the selected enemy unit.

The information is displayed in the rightclick popupwindow on the map
as "Chance to win: A:83% D:30%", where A is when your unit attacking and
D is when your unit is defending. It is only displayed when you have an
active unit and you right click on en enemy.

The patch doesn't take into consideration that it takes some moves
to get to the enemy unit, but still useful for planning attacks etc.

The patch is against CVS 01.22.2003. It's for the GTK-2.0 client.

Andreas Røsdal

diff -r -u freeciv-old/client/gui-gtk-2.0/mapctrl.c 
freeciv/client/gui-gtk-2.0/mapctrl.c
--- freeciv-old/client/gui-gtk-2.0/mapctrl.c    2003-01-26 10:52:04.000000000 
+0100
+++ freeciv/client/gui-gtk-2.0/mapctrl.c        2003-01-26 10:52:36.000000000 
+0100
@@ -18,6 +18,7 @@
 #include <assert.h>
 #include <gtk/gtk.h>
 
+#include "combat.h"
 #include "fcintl.h"
 #include "game.h"
 #include "map.h"
@@ -171,6 +172,39 @@
          cross_head++;
         }
       } else {
+
+      /* Calculate chance to win  */
+      struct unit *apunit;
+      if((apunit=get_unit_in_focus()))   {
+       
+        /*   Chance to win when active unit is attacking the selected unit    
*/
+       int attackpower = get_total_attack_power(apunit, punit);
+       int defensepower = get_total_defense_power(apunit,punit);
+       int attack_fp, defense_fp;
+       get_modified_firepower(apunit, punit, &attack_fp, &defense_fp);
+       int attack_hp = apunit->hp;
+       int defense_hp = punit->hp;     
+       int chance_to_win_attacking = (win_chance(attackpower, attack_hp, 
attack_fp,
+                defensepower,defense_hp, defense_fp)*100);
+
+       /*   Chance to win when selected unit is attacking the active unit    */
+       attackpower = get_total_attack_power(punit, apunit);
+       defensepower = get_total_defense_power(punit, apunit);
+       get_modified_firepower(punit, apunit, &defense_fp, &attack_fp);
+       attack_hp = apunit->hp;
+       defense_hp = punit->hp;         
+       int chance_to_win_defending = 100 - (win_chance(attackpower, attack_hp, 
attack_fp,
+                defensepower,defense_hp, defense_fp)*100);
+
+
+       char ke[64] = "";
+       my_snprintf(ke, sizeof(ke), _("Chance to win: A:%d%% D:%d%%"),
+               chance_to_win_attacking, chance_to_win_defending);
+       gtk_widget_new(GTK_TYPE_LABEL, "GtkWidget::parent", b,
+                                    "GtkLabel::label", ke, NULL);
+        count++;
+      }
+
         my_snprintf(s, sizeof(s), _("A:%d D:%d FP:%d HP:%d0%%"),
                    ptype->attack_strength, 
                    ptype->defense_strength, ptype->firepower, 
diff -r -u freeciv-old/client/gui-gtk-2.0/mapctrl.h 
freeciv/client/gui-gtk-2.0/mapctrl.h
--- freeciv-old/client/gui-gtk-2.0/mapctrl.h    2003-01-26 10:52:04.000000000 
+0100
+++ freeciv/client/gui-gtk-2.0/mapctrl.h        2003-01-26 10:52:40.000000000 
+0100
@@ -34,4 +34,10 @@
 /* Color to use to display the workers */
 extern int city_workers_color;
 
+/* Calculate win-chance */
+void get_modified_firepower(struct unit *attacker, struct unit *defender,
+                           int *att_fp, int *def_fp);
+int get_total_attack_power(struct unit *attacker, struct unit *defender);
+int get_total_defense_power(struct unit *attacker, struct unit *defender);
+
 #endif  /* FC__MAPCTRL_H */


[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] [PATCH] chance to win in civclient, Andreas Røsdal <=