Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2005:
[Freeciv-Dev] (PR#13005) [PATCH] get_effect_bonus varargs
Home

[Freeciv-Dev] (PR#13005) [PATCH] get_effect_bonus varargs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: bdunstan149@xxxxxxxxx
Subject: [Freeciv-Dev] (PR#13005) [PATCH] get_effect_bonus varargs
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Sun, 8 May 2005 05:08:25 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=13005 >

> [vasc - Sun May 08 11:35:24 2005]:
> 
> int get_effect_bonus(enum effect_type effect_type, const char *fmt, ...)
> 
> get_effect_bonus(EFT_ENABLE_NUKE, "")
>       -> get_world_bonus
> 
> get_effect_bonus(EFT_HAVE_EMBASSIES, "p", pplayer)
>       -> get_player_bonus
> 
> get_effect_bonus(EFT_MAKE_HAPPY, "c", pcity)
>       -> get_city_bonus
> 
> get_effect_bonus(EFT_OUTPUT_ADD_TILE, "cto", pcity, ptile, poutput)
>       -> get_city_tile_output_bonus

Attached is a small patch which implements this function.

Index: common/effects.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/effects.c,v
retrieving revision 1.41
diff -u -u -r1.41 effects.c
--- common/effects.c    8 May 2005 06:19:04 -0000       1.41
+++ common/effects.c    8 May 2005 12:05:16 -0000
@@ -16,6 +16,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <stdarg.h>
 #include <string.h>
 
 #include "fcintl.h"
@@ -714,6 +715,76 @@
 }
 
 /**************************************************************************
+  Returns the effect bonus of a given type for any target.
+
+  effect_type gives the effect type to be considered
+  format gives the type of the targets
+**************************************************************************/
+int get_effect_bonus(enum effect_type effect_type, const char *format, ...)
+{
+  va_list args;
+
+  struct player *target_player = NULL;
+  struct city *target_city = NULL;
+  struct impr_type *target_building = NULL;
+  struct tile *target_tile = NULL;
+  struct unit *target_unit = NULL;
+  struct output_type *target_output = NULL;
+
+  /* Snarf arguments */
+  va_start(args, format);
+  while (*format) {
+    switch (*format) {
+    case 'p':
+       target_player = va_arg(args, struct player *);
+       break;
+    case 'c':
+       target_city = va_arg(args, struct city *);
+       break;
+    case 'u':
+       target_unit = va_arg(args, struct unit *);
+       break;
+    case 't':
+       target_tile = va_arg(args, struct tile *);
+       break;
+    case 'i':
+       target_building = va_arg(args, struct impr_type *);
+       break;
+    case 'o':
+       target_output = va_arg(args, struct output_type *);
+       break;
+    default:
+       freelog(LOG_ERROR, "Invalid effect argument '%c'", *format);
+       assert(0);
+
+       va_end(args);
+       return 0;
+    }
+    format++;
+  }
+  va_end(args);
+
+  /* Automagically get more arguments */
+  if (!target_player) {
+    if (target_city) {
+      target_player = city_owner(target_city);
+    } else if (target_unit) {
+      target_player = unit_owner(target_unit);
+    }
+  }
+
+  /* Return effect bonus */
+  return get_target_bonus_effects(NULL,
+                                 target_player,
+                                 target_city,
+                                 target_building,
+                                 target_tile,
+                                 target_unit,
+                                 target_output,
+                                 effect_type);
+}
+
+/**************************************************************************
   Returns the effect bonus for the whole world.
 **************************************************************************/
 int get_world_bonus(enum effect_type effect_type)
Index: common/effects.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/effects.h,v
retrieving revision 1.25
diff -u -u -r1.25 effects.h
--- common/effects.h    7 May 2005 18:58:02 -0000       1.25
+++ common/effects.h    8 May 2005 12:05:16 -0000
@@ -165,6 +165,8 @@
 bool is_building_replaced(const struct city *pcity, Impr_type_id building);
 
 /* functions to know the bonuses a certain effect is granting */
+int get_effect_bonus(enum effect_type effect_type, const char *format, ...);
+
 int get_world_bonus(enum effect_type effect_type);
 int get_player_bonus(const struct player *plr, enum effect_type effect_type);
 int get_city_bonus(const struct city *pcity, enum effect_type effect_type);

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