Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2005:
[Freeciv-Dev] (PR#13041) [PATCH] Event unification
Home

[Freeciv-Dev] (PR#13041) [PATCH] Event unification

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13041) [PATCH] Event unification
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Wed, 11 May 2005 12:30:39 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch:

Moves the events struct declaration to common/event.c and does several
cleanups.

Index: client/civclient.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.c,v
retrieving revision 1.224
diff -u -u -r1.224 civclient.c
--- client/civclient.c  11 May 2005 08:03:27 -0000      1.224
+++ client/civclient.c  11 May 2005 19:28:22 -0000
@@ -313,7 +313,7 @@
   charsets_init();
   my_init_network();
   chatline_common_init();
-  init_messages_where();
+  message_options_init();
   init_player_dlg_common();
   settable_options_init();
 
@@ -371,6 +371,7 @@
   tileset_free(tileset);
   control_done();
   chatline_common_done();
+  message_options_free();
   client_game_free();
 
   helpdata_done(); /* ui_exit() unlinks help text list */
Index: client/options.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.c,v
retrieving revision 1.130
diff -u -u -r1.130 options.c
--- client/options.c    10 May 2005 02:25:27 -0000      1.130
+++ client/options.c    11 May 2005 19:28:22 -0000
@@ -16,7 +16,6 @@
 #endif
 
 #include <assert.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "events.h"
@@ -273,167 +272,12 @@
 /** Message Options: **/
 
 unsigned int messages_where[E_LAST];
-int sorted_events[E_LAST];
-
-#define GEN_EV(descr, event) { #event, NULL, descr, NULL, event }
-#define GEN_EV_TERMINATOR { NULL, NULL, NULL, NULL, E_NOEVENT }
-
-/*
- * Holds information about all event types. The entries don't have
- * to be sorted.
- */
-static struct {
-  const char *enum_name;
-  char *tag_name;
-  const char *descr_orig;
-  const char *descr;
-  enum event_type event;
-} events[] = {
-  GEN_EV(N_("City: Building Unavailable Item"),       E_CITY_CANTBUILD),
-  GEN_EV(N_("City: Captured/Destroyed"),              E_CITY_LOST),
-  GEN_EV(N_("City: Celebrating"),                     E_CITY_LOVE),
-  GEN_EV(N_("City: Civil Disorder"),                  E_CITY_DISORDER),
-  GEN_EV(N_("City: Famine"),                          E_CITY_FAMINE),
-  GEN_EV(N_("City: Famine Feared"),                  E_CITY_FAMINE_FEARED),
-  GEN_EV(N_("City: Growth"),                          E_CITY_GROWTH),
-  GEN_EV(N_("City: May Soon Grow"),                   E_CITY_MAY_SOON_GROW),
-  GEN_EV(N_("City: Needs Aqueduct"),                  E_CITY_AQUEDUCT),
-  GEN_EV(N_("City: Needs Aqueduct Being Built"),      E_CITY_AQ_BUILDING),
-  GEN_EV(N_("City: Normal"),                          E_CITY_NORMAL),
-  GEN_EV(N_("City: Nuked"),                           E_CITY_NUKED),
-  GEN_EV(N_("City: Released from citizen governor"),  E_CITY_CMA_RELEASE),
-  GEN_EV(N_("City: Suggest Growth Throttling"),       E_CITY_GRAN_THROTTLE),
-  GEN_EV(N_("City: Transfer"),                        E_CITY_TRANSFER),
-  GEN_EV(N_("City: Was Built"),                       E_CITY_BUILD),
-  GEN_EV(N_("City: Worklist Events"),                 E_WORKLIST),
-  GEN_EV(N_("Civ: Barbarian Uprising"),               E_UPRISING ),
-  GEN_EV(N_("Civ: Civil War"),                        E_CIVIL_WAR),
-  GEN_EV(N_("Civ: Collapse to Anarchy"),              E_ANARCHY),
-  GEN_EV(N_("Civ: First Contact"),                    E_FIRST_CONTACT),
-  GEN_EV(N_("Civ: Learned New Government"),          E_NEW_GOVERNMENT),
-  GEN_EV(N_("Civ: Low Funds"),                        E_LOW_ON_FUNDS),
-  GEN_EV(N_("Civ: Pollution"),                        E_POLLUTION),
-  GEN_EV(N_("Civ: Revolt Ended"),                     E_REVOLT_DONE),
-  GEN_EV(N_("Civ: Revolt Started"),                   E_REVOLT_START),
-  GEN_EV(N_("Civ: Spaceship Events"),                 E_SPACESHIP),
-  GEN_EV(N_("Diplomat Action: Bribe"),              E_MY_DIPLOMAT_BRIBE),
-  GEN_EV(N_("Diplomat Action: Caused Incident"),    E_DIPLOMATIC_INCIDENT),
-  GEN_EV(N_("Diplomat Action: Escape"),             E_MY_DIPLOMAT_ESCAPE),
-  GEN_EV(N_("Diplomat Action: Embassy"),            E_MY_DIPLOMAT_EMBASSY),
-  GEN_EV(N_("Diplomat Action: Failed"),             E_MY_DIPLOMAT_FAILED),
-  GEN_EV(N_("Diplomat Action: Incite"),             E_MY_DIPLOMAT_INCITE),
-  GEN_EV(N_("Diplomat Action: Poison"),             E_MY_DIPLOMAT_POISON),
-  GEN_EV(N_("Diplomat Action: Sabotage"),           E_MY_DIPLOMAT_SABOTAGE),
-  GEN_EV(N_("Diplomat Action: Theft"),              E_MY_DIPLOMAT_THEFT),
-  GEN_EV(N_("Enemy Diplomat: Bribe"),               E_ENEMY_DIPLOMAT_BRIBE),
-  GEN_EV(N_("Enemy Diplomat: Embassy"),             E_ENEMY_DIPLOMAT_EMBASSY),
-  GEN_EV(N_("Enemy Diplomat: Failed"),              E_ENEMY_DIPLOMAT_FAILED),
-  GEN_EV(N_("Enemy Diplomat: Incite"),              E_ENEMY_DIPLOMAT_INCITE),
-  GEN_EV(N_("Enemy Diplomat: Poison"),              E_ENEMY_DIPLOMAT_POISON),
-  GEN_EV(N_("Enemy Diplomat: Sabotage"),            E_ENEMY_DIPLOMAT_SABOTAGE),
-  GEN_EV(N_("Enemy Diplomat: Theft"),               E_ENEMY_DIPLOMAT_THEFT),
-  GEN_EV(N_("Tutorial message"), E_TUTORIAL),
-  GEN_EV(N_("Broadcast Report"),                E_BROADCAST_REPORT),
-  GEN_EV(N_("Game Ended"),                      E_GAME_END),
-  GEN_EV(N_("Game Started"),                    E_GAME_START),
-  GEN_EV(N_("Message from Server Operator"),    E_MESSAGE_WALL),
-  GEN_EV(N_("Nation Selected"),                 E_NATION_SELECTED),
-  GEN_EV(N_("Player Destroyed"),                E_DESTROYED),
-  GEN_EV(N_("Report"),                          E_REPORT),
-  GEN_EV(N_("Turn Bell"),                       E_TURN_BELL),
-  GEN_EV(N_("Year Advance"),                    E_NEXT_YEAR),
-  GEN_EV(N_("Global: Eco-Disaster"),                  E_GLOBAL_ECO),
-  GEN_EV(N_("Global: Nuke Detonated"),                E_NUKE),
-  GEN_EV(N_("Hut: Barbarians in a Hut Roused"),       E_HUT_BARB),
-  GEN_EV(N_("Hut: City Founded from Hut"),            E_HUT_CITY),
-  GEN_EV(N_("Hut: Gold Found in Hut"),                E_HUT_GOLD),
-  GEN_EV(N_("Hut: Killed by Barbarians in a Hut"),    E_HUT_BARB_KILLED),
-  GEN_EV(N_("Hut: Mercenaries Found in Hut"),         E_HUT_MERC),
-  GEN_EV(N_("Hut: Settler Found in Hut"),             E_HUT_SETTLER),
-  GEN_EV(N_("Hut: Tech Found in Hut"),                E_HUT_TECH),
-  GEN_EV(N_("Hut: Unit Spared by Barbarians"),        E_HUT_BARB_CITY_NEAR),
-  GEN_EV(N_("Improvement: Bought"),                   E_IMP_BUY),
-  GEN_EV(N_("Improvement: Built"),                    E_IMP_BUILD),
-  GEN_EV(N_("Improvement: Forced to Sell"),           E_IMP_AUCTIONED),
-  GEN_EV(N_("Improvement: New Improvement Selected"), E_IMP_AUTO),
-  GEN_EV(N_("Improvement: Sold"),                     E_IMP_SOLD),
-  GEN_EV(N_("Tech: Learned From Great Library"),      E_TECH_GAIN),
-  GEN_EV(N_("Tech: Learned New Tech"),                E_TECH_LEARNED),
-  GEN_EV(N_("Treaty: Alliance"),                      E_TREATY_ALLIANCE),
-  GEN_EV(N_("Treaty: Broken"),                        E_TREATY_BROKEN),
-  GEN_EV(N_("Treaty: Ceasefire"),                     E_TREATY_CEASEFIRE),
-  GEN_EV(N_("Treaty: Peace"),                         E_TREATY_PEACE),
-  GEN_EV(N_("Treaty: Shared Vision"),                 E_TREATY_SHARED_VISION),
-  GEN_EV(N_("Unit: Attack Failed"),                   E_UNIT_LOST_ATT),
-  GEN_EV(N_("Unit: Attack Succeeded"),                E_UNIT_WIN_ATT),
-  GEN_EV(N_("Unit: Bought"),                          E_UNIT_BUY),
-  GEN_EV(N_("Unit: Built"),                           E_UNIT_BUILT),
-  GEN_EV(N_("Unit: Defender Destroyed"),              E_UNIT_LOST),
-  GEN_EV(N_("Unit: Defender Survived"),               E_UNIT_WIN),
-  GEN_EV(N_("Unit: Became More Veteran"),             E_UNIT_BECAME_VET),
-  GEN_EV(N_("Unit: Production Upgraded"),             E_UNIT_UPGRADED),
-  GEN_EV(N_("Unit: Relocated"),                       E_UNIT_RELOCATED),
-  GEN_EV(N_("Unit: Orders / goto events"),            E_UNIT_ORDERS),
-  GEN_EV(N_("Wonder: Finished"),                      E_WONDER_BUILD),
-  GEN_EV(N_("Wonder: Made Obsolete"),                 E_WONDER_OBSOLETE),
-  GEN_EV(N_("Wonder: Started"),                       E_WONDER_STARTED),
-  GEN_EV(N_("Wonder: Stopped"),                       E_WONDER_STOPPED),
-  GEN_EV(N_("Wonder: Will Finish Next Turn"),         E_WONDER_WILL_BE_BUILT),
-  GEN_EV(N_("Diplomatic Message"),                    E_DIPLOMACY),
-  GEN_EV(N_("City: Production changed"),              
E_CITY_PRODUCTION_CHANGED),
-  GEN_EV(N_("Treaty: Embassy"),                       E_TREATY_EMBASSY),
-  GEN_EV_TERMINATOR
-};
-
-/* 
- * Maps from enum event_type to indexes of events[]. Set by
- * init_messages_where. 
- */
-static int event_to_index[E_LAST];
-
-static void save_cma_preset(struct section_file *file, char *name,
-                           const struct cm_parameter *const pparam,
-                           int inx);
-static void load_cma_preset(struct section_file *file, int inx);
-
-static void save_global_worklist(struct section_file *file, const char *path, 
-                                 int wlinx, struct worklist *pwl);
-
-static void load_global_worklist(struct section_file *file, const char *path,
-                                int wlinx, struct worklist *pwl);
-
-/**************************************************************************
-  Returns the translated description of the given event.
-**************************************************************************/
-const char *get_message_text(enum event_type event)
-{
-  assert(event >= 0 && event < E_LAST);
-
-  if (events[event_to_index[event]].event == event) {
-    return events[event_to_index[event]].descr;
-  }
-  freelog(LOG_ERROR, "unknown event %d", event);
-  return "UNKNOWN EVENT";
-}
-
-/**************************************************************************
-  Comparison function for qsort; i1 and i2 are pointers to an event
-  (enum event_type).
-**************************************************************************/
-static int compar_message_texts(const void *i1, const void *i2)
-{
-  int j1 = *(const int*)i1;
-  int j2 = *(const int*)i2;
-  
-  return mystrcasecmp(get_message_text(j1), get_message_text(j2));
-}
 
 /****************************************************************
   These could be a static table initialisation, except
   its easier to do it this way.
-  Now also initialise sorted_events[].
 *****************************************************************/
-void init_messages_where(void)
+void message_options_init(void)
 {
   int out_only[] = { E_IMP_BUY, E_IMP_SOLD, E_UNIT_BUY,
                     E_UNIT_LOST_ATT, E_UNIT_WIN_ATT, E_GAME_START,
@@ -452,36 +296,59 @@
   for (i = 0; i < ARRAY_SIZE(all); i++) {
     messages_where[all[i]] = MW_MESSAGES | MW_POPUP;
   }
-  
-  for (i = 0; i < ARRAY_SIZE(event_to_index); i++) {
-    event_to_index[i] = 0;
-  }
 
-  for (i = 0;; i++) {
-    int j;
+  events_init();
+}
 
-    if (events[i].event == E_NOEVENT) {
-      break;
-    }
-    events[i].descr = _(events[i].descr_orig);
-    event_to_index[events[i].event] = i;
-    events[i].tag_name = mystrdup(events[i].enum_name);
-    for (j = 0; j < strlen(events[i].tag_name); j++) {
-      events[i].tag_name[j] = my_tolower(events[i].tag_name[j]);
-    }
-    freelog(LOG_DEBUG,
-           "event[%d]=%d: name='%s' / '%s'\n\tdescr_orig='%s'\n\tdescr='%s'",
-           i, events[i].event, events[i].enum_name, events[i].tag_name,
-           events[i].descr_orig, events[i].descr);
+/****************************************************************
+... 
+*****************************************************************/
+void message_options_free(void)
+{
+  events_free();
+}
+
+/****************************************************************
+... 
+*****************************************************************/
+static void message_options_load(struct section_file *file, const char *prefix)
+{
+  int i;
+
+  for (i = 0; i < E_LAST; i++) {
+    messages_where[i] =
+      secfile_lookup_int_default(file, messages_where[i],
+                                "%s.message_where_%02d", prefix, i);
   }
+}
 
-  for(i=0;i<E_LAST;i++)  {
-    sorted_events[i] = i;
+/****************************************************************
+... 
+*****************************************************************/
+static void message_options_save(struct section_file *file, const char *prefix)
+{
+  int i;
+
+  for (i = 0; i < E_LAST; i++) {
+    secfile_insert_int_comment(file, messages_where[i],
+                              get_event_message_text(i),
+                              "%s.message_where_%02d", prefix, i);
   }
-  qsort(sorted_events, E_LAST, sizeof(int), compar_message_texts);
 }
 
 
+static void save_cma_preset(struct section_file *file, char *name,
+                           const struct cm_parameter *const pparam,
+                           int inx);
+static void load_cma_preset(struct section_file *file, int inx);
+
+static void save_global_worklist(struct section_file *file, const char *path, 
+                                 int wlinx, struct worklist *pwl);
+
+static void load_global_worklist(struct section_file *file, const char *path,
+                                int wlinx, struct worklist *pwl);
+
+
 /****************************************************************
  The "options" file handles actual "options", and also view options,
  message options, city report settings, cma settings, and 
@@ -586,11 +453,8 @@
        secfile_lookup_bool_default(&sf, *(v->p_value), "%s.%s", prefix,
                                    v->name);
   }
-  for (i = 0; i < E_LAST; i++) {
-    messages_where[i] =
-      secfile_lookup_int_default(&sf, messages_where[i],
-                                "%s.message_where_%02d", prefix, i);
-  }
+
+  message_options_load(&sf, prefix);
   
   for(i = 1; i < num_player_dlg_columns; i++) {
     bool *show = &(player_dlg_columns[i].show);
@@ -696,11 +560,7 @@
     secfile_insert_bool(&sf, *(v->p_value), "client.%s", v->name);
   }
 
-  for (i = 0; i < E_LAST; i++) {
-    secfile_insert_int_comment(&sf, messages_where[i],
-                              get_message_text(i),
-                              "client.message_where_%02d", i);
-  }
+  message_options_save(&sf, "client");
 
   for (i = 1; i < num_city_report_spec(); i++) {
     secfile_insert_bool(&sf, *(city_report_spec_show_ptr(i)),
@@ -797,24 +657,6 @@
 }
 
 /****************************************************************
-... 
-*****************************************************************/
-const char *get_sound_tag_for_event(enum event_type event)
-{
-  if (event == E_NOEVENT) {
-    return NULL;
-  }
-
-  assert(event >= 0 && event < E_LAST);
-
-  if (events[event_to_index[event]].event == event) {
-    return events[event_to_index[event]].tag_name;
-  }
-  freelog(LOG_ERROR, "unknown event %d", event);
-  return NULL;
-}
-
-/****************************************************************
  loads global worklist from rc file
 *****************************************************************/
 static void load_global_worklist(struct section_file *file, const char *path,
@@ -876,43 +718,6 @@
   }
 }
 
-/****************************************************************
- If is_city_event is FALSE this event doesn't effect a city even if
- there is a city at the event location.
-*****************************************************************/
-bool is_city_event(enum event_type event)
-{
-  switch (event) {
-  case E_GLOBAL_ECO:
-  case E_CITY_LOST:
-  case E_UNIT_LOST:
-  case E_UNIT_WIN:
-  case E_ENEMY_DIPLOMAT_FAILED:
-  case E_ENEMY_DIPLOMAT_EMBASSY:
-  case E_ENEMY_DIPLOMAT_POISON:
-  case E_ENEMY_DIPLOMAT_BRIBE:
-  case E_ENEMY_DIPLOMAT_INCITE:
-  case E_ENEMY_DIPLOMAT_SABOTAGE:
-  case E_ENEMY_DIPLOMAT_THEFT:
-  case E_MY_DIPLOMAT_FAILED:
-  case E_MY_DIPLOMAT_EMBASSY:
-  case E_MY_DIPLOMAT_POISON:
-  case E_MY_DIPLOMAT_BRIBE:
-  case E_MY_DIPLOMAT_INCITE:
-  case E_MY_DIPLOMAT_SABOTAGE:
-  case E_MY_DIPLOMAT_THEFT:
-  case E_MY_DIPLOMAT_ESCAPE:
-  case E_UNIT_LOST_ATT:
-  case E_UNIT_WIN_ATT:
-  case E_UPRISING:
-  case E_UNIT_RELOCATED:
-    return FALSE;
-
-  default:
-    return TRUE;
-  }
-}
-
 /****************************************************************************
   Callback when a mapview graphics option is changed (redraws the canvas).
 ****************************************************************************/
Index: client/options.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.h,v
retrieving revision 1.51
diff -u -u -r1.51 options.h
--- client/options.h    29 Apr 2005 17:58:09 -0000      1.51
+++ client/options.h    11 May 2005 19:28:22 -0000
@@ -157,19 +157,16 @@
 #define MW_POPUP     4         /* popup an individual window */
 
 extern unsigned int messages_where[];  /* OR-ed MW_ values [E_LAST] */
-extern int sorted_events[];            /* [E_LAST], sorted by the
-                                          translated message text */
-const char *get_message_text(enum event_type event);
 
-void init_messages_where(void);
+void message_options_init(void);
+void message_options_free(void);
 
 void load_general_options(void);
 void load_ruleset_specific_options(void);
 void save_options(void);
-const char *get_sound_tag_for_event(enum event_type event);
-bool is_city_event(enum event_type event);
 
 /* Callback functions for changing options. */
 void mapview_redraw_callback(struct client_option *option);
 
 #endif  /* FC__OPTIONS_H */
+
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.514
diff -u -u -r1.514 packhand.c
--- client/packhand.c   11 May 2005 16:40:02 -0000      1.514
+++ client/packhand.c   11 May 2005 19:28:23 -0000
@@ -843,7 +843,7 @@
 **************************************************************************/
 void play_sound_for_event(enum event_type type)
 {
-  const char *sound_tag = get_sound_tag_for_event(type);
+  const char *sound_tag = get_event_sound_tag(type);
 
   if (sound_tag) {
     audio_play_sound(sound_tag, NULL);
Index: client/gui-gtk-2.0/messagedlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/messagedlg.c,v
retrieving revision 1.20
diff -u -u -r1.20 messagedlg.c
--- client/gui-gtk-2.0/messagedlg.c     10 Apr 2005 03:31:23 -0000      1.20
+++ client/gui-gtk-2.0/messagedlg.c     11 May 2005 19:28:23 -0000
@@ -83,7 +83,7 @@
      G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING, 
G_TYPE_INT);
   }
 
-  for (i=0; i<E_LAST; i++)  {
+  sorted_event_iterate(ev) {
     GtkTreeIter it;
     GValue value = { 0, };
 
@@ -92,17 +92,16 @@
     gtk_list_store_append(model[n], &it);
 
     g_value_init(&value, G_TYPE_STRING);
-    g_value_set_static_string(&value, get_message_text(sorted_events[i]));
+    g_value_set_static_string(&value, get_event_message_text(ev));
     gtk_list_store_set_value(model[n], &it, 3, &value);
     g_value_unset(&value);
 
-    gtk_list_store_set(model[n], &it, 4, sorted_events[i], -1);
+    gtk_list_store_set(model[n], &it, 4, ev, -1);
 
     for (j=0; j<NUM_MW; j++) {
-      gtk_list_store_set(model[n], &it,
-        j, messages_where[sorted_events[i]] & (1<<j), -1);
+      gtk_list_store_set(model[n], &it, j, messages_where[ev] & (1<<j), -1);
     }
-  }
+  } sorted_event_iterate_end;
 
   for (n=0; n<NUM_LISTS; n++) {
     GtkWidget *view, *sw;
Index: client/gui-win32/messagedlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/messagedlg.c,v
retrieving revision 1.8
diff -u -u -r1.8 messagedlg.c
--- client/gui-win32/messagedlg.c       29 Mar 2003 09:57:37 -0000      1.8
+++ client/gui-win32/messagedlg.c       11 May 2005 19:28:23 -0000
@@ -145,7 +145,7 @@
   for(i=start;i<stop;i++) {
     int is_col1 = (i-start)<((stop-start)/2);
     fcwin_box_add_static(table_vboxes[is_col1?0:4],
-                        get_message_text(sorted_events[i]),
+                        get_event_message_text(sorted_events[i]),
                         0,SS_LEFT,TRUE,TRUE,0);
     for(j=0; j<NUM_MW; j++) {
       messageopt_toggles[sorted_events[i]][j]=
@@ -191,7 +191,7 @@
   for (i = 0; i<num_pages; i++) {
     titles[i] = titles_buf + i * 5;
     strncpy(titles[i], 
-           get_message_text(sorted_events[i * OPTIONS_PER_PAGE]),
+           get_event_message_text(sorted_events[i * OPTIONS_PER_PAGE]),
            4);
     titles_buf[i * 5 + 4] = 0;
     wndprocs[i] = messageopt_page_proc;
Index: common/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/Makefile.am,v
retrieving revision 1.57
diff -u -u -r1.57 Makefile.am
--- common/Makefile.am  10 May 2005 17:01:23 -0000      1.57
+++ common/Makefile.am  11 May 2005 19:28:24 -0000
@@ -23,6 +23,7 @@
                diptreaty.h     \
                effects.c       \
                effects.h       \
+               events.c        \
                events.h        \
                fc_types.h      \
                game.c          \
Index: common/events.c
===================================================================
RCS file: common/events.c
diff -N common/events.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ common/events.c     11 May 2005 19:28:24 -0000
@@ -0,0 +1,270 @@
+/********************************************************************** 
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "fcintl.h"
+#include "log.h"
+#include "shared.h"
+#include "support.h"
+
+#include "events.h"
+
+#define GEN_EV(descr, event) { #event, NULL, descr, NULL, event }
+#define GEN_EV_TERMINATOR { NULL, NULL, NULL, NULL, E_NOEVENT }
+
+/*
+ * Holds information about all event types. The entries don't have
+ * to be sorted.
+ */
+static struct {
+  const char *enum_name;
+  char *tag_name;
+  const char *descr_orig;
+  const char *descr;
+  enum event_type event;
+} events[] = {
+  GEN_EV(N_("City: Building Unavailable Item"),       E_CITY_CANTBUILD),
+  GEN_EV(N_("City: Captured/Destroyed"),              E_CITY_LOST),
+  GEN_EV(N_("City: Celebrating"),                     E_CITY_LOVE),
+  GEN_EV(N_("City: Civil Disorder"),                  E_CITY_DISORDER),
+  GEN_EV(N_("City: Famine"),                          E_CITY_FAMINE),
+  GEN_EV(N_("City: Famine Feared"),                  E_CITY_FAMINE_FEARED),
+  GEN_EV(N_("City: Growth"),                          E_CITY_GROWTH),
+  GEN_EV(N_("City: May Soon Grow"),                   E_CITY_MAY_SOON_GROW),
+  GEN_EV(N_("City: Needs Aqueduct"),                  E_CITY_AQUEDUCT),
+  GEN_EV(N_("City: Needs Aqueduct Being Built"),      E_CITY_AQ_BUILDING),
+  GEN_EV(N_("City: Normal"),                          E_CITY_NORMAL),
+  GEN_EV(N_("City: Nuked"),                           E_CITY_NUKED),
+  GEN_EV(N_("City: Released from citizen governor"),  E_CITY_CMA_RELEASE),
+  GEN_EV(N_("City: Suggest Growth Throttling"),       E_CITY_GRAN_THROTTLE),
+  GEN_EV(N_("City: Transfer"),                        E_CITY_TRANSFER),
+  GEN_EV(N_("City: Was Built"),                       E_CITY_BUILD),
+  GEN_EV(N_("City: Worklist Events"),                 E_WORKLIST),
+  GEN_EV(N_("Civ: Barbarian Uprising"),               E_UPRISING ),
+  GEN_EV(N_("Civ: Civil War"),                        E_CIVIL_WAR),
+  GEN_EV(N_("Civ: Collapse to Anarchy"),              E_ANARCHY),
+  GEN_EV(N_("Civ: First Contact"),                    E_FIRST_CONTACT),
+  GEN_EV(N_("Civ: Learned New Government"),          E_NEW_GOVERNMENT),
+  GEN_EV(N_("Civ: Low Funds"),                        E_LOW_ON_FUNDS),
+  GEN_EV(N_("Civ: Pollution"),                        E_POLLUTION),
+  GEN_EV(N_("Civ: Revolt Ended"),                     E_REVOLT_DONE),
+  GEN_EV(N_("Civ: Revolt Started"),                   E_REVOLT_START),
+  GEN_EV(N_("Civ: Spaceship Events"),                 E_SPACESHIP),
+  GEN_EV(N_("Diplomat Action: Bribe"),              E_MY_DIPLOMAT_BRIBE),
+  GEN_EV(N_("Diplomat Action: Caused Incident"),    E_DIPLOMATIC_INCIDENT),
+  GEN_EV(N_("Diplomat Action: Escape"),             E_MY_DIPLOMAT_ESCAPE),
+  GEN_EV(N_("Diplomat Action: Embassy"),            E_MY_DIPLOMAT_EMBASSY),
+  GEN_EV(N_("Diplomat Action: Failed"),             E_MY_DIPLOMAT_FAILED),
+  GEN_EV(N_("Diplomat Action: Incite"),             E_MY_DIPLOMAT_INCITE),
+  GEN_EV(N_("Diplomat Action: Poison"),             E_MY_DIPLOMAT_POISON),
+  GEN_EV(N_("Diplomat Action: Sabotage"),           E_MY_DIPLOMAT_SABOTAGE),
+  GEN_EV(N_("Diplomat Action: Theft"),              E_MY_DIPLOMAT_THEFT),
+  GEN_EV(N_("Enemy Diplomat: Bribe"),               E_ENEMY_DIPLOMAT_BRIBE),
+  GEN_EV(N_("Enemy Diplomat: Embassy"),             E_ENEMY_DIPLOMAT_EMBASSY),
+  GEN_EV(N_("Enemy Diplomat: Failed"),              E_ENEMY_DIPLOMAT_FAILED),
+  GEN_EV(N_("Enemy Diplomat: Incite"),              E_ENEMY_DIPLOMAT_INCITE),
+  GEN_EV(N_("Enemy Diplomat: Poison"),              E_ENEMY_DIPLOMAT_POISON),
+  GEN_EV(N_("Enemy Diplomat: Sabotage"),            E_ENEMY_DIPLOMAT_SABOTAGE),
+  GEN_EV(N_("Enemy Diplomat: Theft"),               E_ENEMY_DIPLOMAT_THEFT),
+  GEN_EV(N_("Tutorial message"),                E_TUTORIAL),
+  GEN_EV(N_("Broadcast Report"),                E_BROADCAST_REPORT),
+  GEN_EV(N_("Game Ended"),                      E_GAME_END),
+  GEN_EV(N_("Game Started"),                    E_GAME_START),
+  GEN_EV(N_("Message from Server Operator"),    E_MESSAGE_WALL),
+  GEN_EV(N_("Nation Selected"),                 E_NATION_SELECTED),
+  GEN_EV(N_("Player Destroyed"),                E_DESTROYED),
+  GEN_EV(N_("Report"),                          E_REPORT),
+  GEN_EV(N_("Turn Bell"),                       E_TURN_BELL),
+  GEN_EV(N_("Year Advance"),                    E_NEXT_YEAR),
+  GEN_EV(N_("Global: Eco-Disaster"),                  E_GLOBAL_ECO),
+  GEN_EV(N_("Global: Nuke Detonated"),                E_NUKE),
+  GEN_EV(N_("Hut: Barbarians in a Hut Roused"),       E_HUT_BARB),
+  GEN_EV(N_("Hut: City Founded from Hut"),            E_HUT_CITY),
+  GEN_EV(N_("Hut: Gold Found in Hut"),                E_HUT_GOLD),
+  GEN_EV(N_("Hut: Killed by Barbarians in a Hut"),    E_HUT_BARB_KILLED),
+  GEN_EV(N_("Hut: Mercenaries Found in Hut"),         E_HUT_MERC),
+  GEN_EV(N_("Hut: Settler Found in Hut"),             E_HUT_SETTLER),
+  GEN_EV(N_("Hut: Tech Found in Hut"),                E_HUT_TECH),
+  GEN_EV(N_("Hut: Unit Spared by Barbarians"),        E_HUT_BARB_CITY_NEAR),
+  GEN_EV(N_("Improvement: Bought"),                   E_IMP_BUY),
+  GEN_EV(N_("Improvement: Built"),                    E_IMP_BUILD),
+  GEN_EV(N_("Improvement: Forced to Sell"),           E_IMP_AUCTIONED),
+  GEN_EV(N_("Improvement: New Improvement Selected"), E_IMP_AUTO),
+  GEN_EV(N_("Improvement: Sold"),                     E_IMP_SOLD),
+  GEN_EV(N_("Tech: Learned From Great Library"),      E_TECH_GAIN),
+  GEN_EV(N_("Tech: Learned New Tech"),                E_TECH_LEARNED),
+  GEN_EV(N_("Treaty: Alliance"),                      E_TREATY_ALLIANCE),
+  GEN_EV(N_("Treaty: Broken"),                        E_TREATY_BROKEN),
+  GEN_EV(N_("Treaty: Ceasefire"),                     E_TREATY_CEASEFIRE),
+  GEN_EV(N_("Treaty: Peace"),                         E_TREATY_PEACE),
+  GEN_EV(N_("Treaty: Shared Vision"),                 E_TREATY_SHARED_VISION),
+  GEN_EV(N_("Unit: Attack Failed"),                   E_UNIT_LOST_ATT),
+  GEN_EV(N_("Unit: Attack Succeeded"),                E_UNIT_WIN_ATT),
+  GEN_EV(N_("Unit: Bought"),                          E_UNIT_BUY),
+  GEN_EV(N_("Unit: Built"),                           E_UNIT_BUILT),
+  GEN_EV(N_("Unit: Defender Destroyed"),              E_UNIT_LOST),
+  GEN_EV(N_("Unit: Defender Survived"),               E_UNIT_WIN),
+  GEN_EV(N_("Unit: Became More Veteran"),             E_UNIT_BECAME_VET),
+  GEN_EV(N_("Unit: Production Upgraded"),             E_UNIT_UPGRADED),
+  GEN_EV(N_("Unit: Relocated"),                       E_UNIT_RELOCATED),
+  GEN_EV(N_("Unit: Orders / goto events"),            E_UNIT_ORDERS),
+  GEN_EV(N_("Wonder: Finished"),                      E_WONDER_BUILD),
+  GEN_EV(N_("Wonder: Made Obsolete"),                 E_WONDER_OBSOLETE),
+  GEN_EV(N_("Wonder: Started"),                       E_WONDER_STARTED),
+  GEN_EV(N_("Wonder: Stopped"),                       E_WONDER_STOPPED),
+  GEN_EV(N_("Wonder: Will Finish Next Turn"),         E_WONDER_WILL_BE_BUILT),
+  GEN_EV(N_("Diplomatic Message"),                    E_DIPLOMACY),
+  GEN_EV(N_("City: Production changed"),              
E_CITY_PRODUCTION_CHANGED),
+  GEN_EV(N_("Treaty: Embassy"),                       E_TREATY_EMBASSY),
+  GEN_EV_TERMINATOR
+};
+
+
+/* 
+ * Maps from enum event_type to indexes of events[]. Set by
+ * events_init. 
+ */
+static int event_to_index[E_LAST];
+
+int sorted_events[E_LAST];
+
+
+/**************************************************************************
+  Returns the translated description of the given event.
+**************************************************************************/
+const char *get_event_message_text(enum event_type event)
+{
+  assert(event >= 0 && event < E_LAST);
+
+  if (events[event_to_index[event]].event == event) {
+    return events[event_to_index[event]].descr;
+  }
+  freelog(LOG_ERROR, "unknown event %d", event);
+  return "UNKNOWN EVENT";
+}
+
+/**************************************************************************
+  Comparison function for qsort; i1 and i2 are pointers to an event
+  (enum event_type).
+**************************************************************************/
+static int compar_event_message_texts(const void *i1, const void *i2)
+{
+  int j1 = *(const int*)i1;
+  int j2 = *(const int*)i2;
+  
+  return mystrcasecmp(get_event_message_text(j1), get_event_message_text(j2));
+}
+
+/****************************************************************
+... 
+*****************************************************************/
+const char *get_event_sound_tag(enum event_type event)
+{
+  if (event == E_NOEVENT) {
+    return NULL;
+  }
+
+  assert(event >= 0 && event < E_LAST);
+
+  if (events[event_to_index[event]].event == event) {
+    return events[event_to_index[event]].tag_name;
+  }
+  freelog(LOG_ERROR, "unknown event %d", event);
+  return NULL;
+}
+
+/****************************************************************
+ If is_city_event is FALSE this event doesn't effect a city even if
+ there is a city at the event location.
+*****************************************************************/
+bool is_city_event(enum event_type event)
+{
+  switch (event) {
+  case E_GLOBAL_ECO:
+  case E_CITY_LOST:
+  case E_UNIT_LOST:
+  case E_UNIT_WIN:
+  case E_ENEMY_DIPLOMAT_FAILED:
+  case E_ENEMY_DIPLOMAT_EMBASSY:
+  case E_ENEMY_DIPLOMAT_POISON:
+  case E_ENEMY_DIPLOMAT_BRIBE:
+  case E_ENEMY_DIPLOMAT_INCITE:
+  case E_ENEMY_DIPLOMAT_SABOTAGE:
+  case E_ENEMY_DIPLOMAT_THEFT:
+  case E_MY_DIPLOMAT_FAILED:
+  case E_MY_DIPLOMAT_EMBASSY:
+  case E_MY_DIPLOMAT_POISON:
+  case E_MY_DIPLOMAT_BRIBE:
+  case E_MY_DIPLOMAT_INCITE:
+  case E_MY_DIPLOMAT_SABOTAGE:
+  case E_MY_DIPLOMAT_THEFT:
+  case E_MY_DIPLOMAT_ESCAPE:
+  case E_UNIT_LOST_ATT:
+  case E_UNIT_WIN_ATT:
+  case E_UPRISING:
+  case E_UNIT_RELOCATED:
+    return FALSE;
+
+  default:
+    return TRUE;
+  }
+}
+
+/****************************************************************
+  Initialize events. 
+  Now also initialise sorted_events[].
+*****************************************************************/
+void events_init(void)
+{
+  int i;
+  
+  for (i = 0; i < ARRAY_SIZE(event_to_index); i++) {
+    event_to_index[i] = 0;
+  }
+
+  for (i = 0;; i++) {
+    int j;
+
+    if (events[i].event == E_NOEVENT) {
+      break;
+    }
+    events[i].descr = _(events[i].descr_orig);
+    event_to_index[events[i].event] = i;
+    events[i].tag_name = mystrdup(events[i].enum_name);
+    for (j = 0; j < strlen(events[i].tag_name); j++) {
+      events[i].tag_name[j] = my_tolower(events[i].tag_name[j]);
+    }
+    freelog(LOG_DEBUG,
+           "event[%d]=%d: name='%s' / '%s'\n\tdescr_orig='%s'\n\tdescr='%s'",
+           i, events[i].event, events[i].enum_name, events[i].tag_name,
+           events[i].descr_orig, events[i].descr);
+  }
+
+  for(i=0;i<E_LAST;i++)  {
+    sorted_events[i] = i;
+  }
+  qsort(sorted_events, E_LAST, sizeof(int), compar_event_message_texts);
+}
+
+/****************************************************************
+  Free events. 
+*****************************************************************/
+void events_free(void)
+{
+}
Index: common/events.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/events.h,v
retrieving revision 1.28
diff -u -u -r1.28 events.h
--- common/events.h     10 May 2005 02:25:27 -0000      1.28
+++ common/events.h     11 May 2005 19:28:24 -0000
@@ -13,6 +13,8 @@
 #ifndef FC__EVENTS_H
 #define FC__EVENTS_H
 
+#include "shared.h"          /* bool type */
+
 enum event_type {
   E_NOEVENT = -1,
   E_CITY_CANTBUILD,
@@ -116,4 +118,30 @@
   E_LAST
 };
 
+extern int sorted_events[];            /* [E_LAST], sorted by the
+                                          translated message text */
+
+const char *get_event_message_text(enum event_type event);
+const char *get_event_sound_tag(enum event_type event);
+
+bool is_city_event(enum event_type event);
+
+void events_init(void);
+void events_free(void);
+
+
+/* Iterates over all events, sorted by the message text string. */
+#define sorted_event_iterate(event)                                           \
+{                                                                             \
+  enum event_type _event, event;                                              \
+  for (_event = 0; _event < E_LAST; _event++) {                               \
+    event = sorted_events[_event];                                            \
+    {
+
+#define sorted_event_iterate_end                                              \
+    }                                                                         \
+  }                                                                           \
+}
+
 #endif /* FC__EVENTS_H */
+

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13041) [PATCH] Event unification, Vasco Alexandre da Silva Costa <=