Author: pepeto
Date: Sun Jul  6 10:59:49 2014
New Revision: 25397

URL: http://svn.gna.org/viewcvs/freeciv?rev=25397&view=rev
Log:
precalc_tech_data() renamed tech_precalc_data(). This function
is now called only in server side. The client just admits the
values the server send to it. Base tech costs are now cached
into the advance structure, instead of having a separate static
array.

See gna patch #4891

Modified:
    trunk/client/client_main.c
    trunk/client/packhand.c
    trunk/common/packets.def
    trunk/common/tech.c
    trunk/common/tech.h
    trunk/fc_version
    trunk/server/ruleset.c

Modified: trunk/client/client_main.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/client_main.c?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/client/client_main.c  (original)
+++ trunk/client/client_main.c  Sun Jul  6 10:59:49 2014
@@ -830,7 +830,6 @@
     init_city_report_game_data();
     options_dialogs_set();
     create_event(NULL, E_GAME_START, ftc_client, _("Game started."));
-    precalc_tech_data();
     if (pplayer) {
       player_research_update(pplayer);
     }
@@ -878,7 +877,6 @@
       /* From C_S_PREPARING. */
       init_city_report_game_data();
       options_dialogs_set();
-      precalc_tech_data();
       if (pplayer) {
         player_research_update(pplayer);
       }

Modified: trunk/client/packhand.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/packhand.c?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/client/packhand.c     (original)
+++ trunk/client/packhand.c     Sun Jul  6 10:59:49 2014
@@ -3049,7 +3049,7 @@
   a->require[AR_TWO] = advance_by_number(p->req[AR_TWO]);
   a->require[AR_ROOT] = advance_by_number(p->root_req);
   a->flags = p->flags;
-  a->preset_cost = p->preset_cost;
+  a->cost = p->cost;
   a->num_reqs = p->num_reqs;
   PACKET_STRVEC_EXTRACT(a->helptext, p->helptext);
 

Modified: trunk/common/packets.def
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/packets.def?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/common/packets.def    (original)
+++ trunk/common/packets.def    Sun Jul  6 10:59:49 2014
@@ -1333,7 +1333,8 @@
   TECH req[2];
   TECH root_req;
   BV_TECH_FLAGS flags;
-  UINT32 preset_cost, num_reqs;
+  FLOAT10x7 cost;
+  UINT32 num_reqs;
   STRING name[MAX_LEN_NAME];
   STRING rule_name[MAX_LEN_NAME];
   STRING helptext[MAX_LEN_PACKET];

Modified: trunk/common/tech.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/tech.c?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/common/tech.c (original)
+++ trunk/common/tech.c Sun Jul  6 10:59:49 2014
@@ -52,10 +52,6 @@
  * client/packhand.c (for the client)
  */
 struct advance advances[A_LAST];
-
-/* Precalculated costs according to techcost style 1.  These do not include
- * the sciencebox multiplier. */
-static double techcoststyle1[A_LAST];
 
 static int tech_upkeep_calc(const struct player *pplayer);
 
@@ -336,44 +332,29 @@
 static int tech_upkeep_calc(const struct player *pplayer)
 {
   struct research *research = research_get(pplayer);
-  int tech_cost_style = game.info.tech_cost_style;
   int f = research->future_tech, t = research->techs_researched;
-  double tech_bulb_sum = 0.0;
-
-  if (!pplayer) {
-    return 0;
-  }
+  double tech_bulb_sum;
 
   /* upkeep cost for 'normal' techs (t) */
-  switch (tech_cost_style) {
+  switch (game.info.tech_cost_style) {
   case 0:
     /* sum_1^t x = t * (t + 1) / 2 */
-    tech_bulb_sum += (double)t * (t + 1) / 2 * game.info.base_tech_cost;
+    tech_bulb_sum += game.info.base_tech_cost * t * (t + 1) / 2;
     break;
   case 1:
+  case 2:
   case 3:
-    advance_index_iterate(A_NONE, i) {
-      if (research_invention_state(research, i) == TECH_KNOWN) {
-        tech_bulb_sum += techcoststyle1[i];
-      }
-    } advance_index_iterate_end;
-    break;
-  case 2:
   case 4:
     advance_index_iterate(A_NONE, i) {
       if (research_invention_state(research, i) == TECH_KNOWN) {
-        if (advances[i].preset_cost != -1) {
-          tech_bulb_sum += advances[i].preset_cost;
-        } else {
-          tech_bulb_sum += techcoststyle1[i];
-        }
+        tech_bulb_sum += advances[i].cost;
       }
     } advance_index_iterate_end;
     break;
   default:
-    fc_assert_ret_val_msg(FALSE, 0, "Invalid tech_cost_style %d %d",
-                          game.info.tech_cost_style,
-                          tech_cost_style);
+    fc_assert_msg(FALSE, "Invalid tech_cost_style %d",
+                  game.info.tech_cost_style);
+    tech_bulb_sum = 0.0;
   }
 
   /* upkeep cost for future techs (f) are calculated using style 0:
@@ -580,32 +561,18 @@
     tech_cost_style = 0;
   }
 
-  if (tech_cost_style == 2 && advances[tech].preset_cost == -1) {
-    /* No preset, using style 1 */
-    tech_cost_style = 1;
-  }
-
-  if (tech_cost_style == 4 && advances[tech].preset_cost == -1) {
-    /* No preset, using style 3 */
-    tech_cost_style = 3;
-  }
-
   switch (tech_cost_style) {
   case 0:
-    if (pplayer) {
-      base_cost = research_get(pplayer)->techs_researched
-       * game.info.base_tech_cost;
-    } else {
-      base_cost = 0;
-    }
-    break;
+    if (NULL != pplayer) {
+      base_cost = game.info.base_tech_cost
+                  * research_get(pplayer)->techs_researched;
+      break;
+    }
   case 1:
+  case 2:
   case 3:
-    base_cost = techcoststyle1[tech];
-    break;
-  case 2:
   case 4:
-    base_cost = advances[tech].preset_cost;
+    base_cost = advances[tech].cost;
     break;
   default:
     log_error("Invalid tech_cost_style %d %d", game.info.tech_cost_style,
@@ -730,61 +697,49 @@
   return research_get(pplayer)->inventions[goal].bulbs_required;
 }
 
-/**************************************************************************
- Returns number of requirements for the given tech. To not count techs
- double a memory (the counted array) is needed.
-**************************************************************************/
-static int precalc_tech_data_helper(Tech_type_id tech, bool *counted)
-{
-  if (tech == A_NONE || !valid_advance_by_number(tech) || counted[tech]) {
-    return 0;
-  }
-
-  counted[tech] = TRUE;
-
-  return 1 + 
-      precalc_tech_data_helper(advance_required(tech, AR_ONE), counted)+ 
-      precalc_tech_data_helper(advance_required(tech, AR_TWO), counted);
-}
-
-/**************************************************************************
- Function to precalculate needed data for technologies.
- Styles 3 and 4 use the same table as styles 1 and 2 so we do not have to
- modify any function that reads it.
-**************************************************************************/
-void precalc_tech_data()
-{
-  bool counted[A_LAST];
-
-  advance_index_iterate(A_NONE, tech) {
-    memset(counted, 0, sizeof(counted));
-    advances[tech].num_reqs = precalc_tech_data_helper(tech, counted);
-  } advance_index_iterate_end;
-
-  advance_index_iterate(A_NONE, tech) {
-    /* FIXME: Why are we counting the current tech twice? */
-    double reqs = advances[tech].num_reqs + 1;
-    double cost = 0;
-    const double base = game.info.base_tech_cost;
+/****************************************************************************
+  Function to precalculate needed data for technologies.
+****************************************************************************/
+void techs_precalc_data(void)
+{
+  advance_iterate(A_FIRST, padvance) {
+    int num_reqs = 0;
+
+    advance_req_iterate(padvance, preq) {
+      (void) preq; /* Compiler wants us to do something with 'preq'. */
+      num_reqs++;
+    } advance_req_iterate_end;
+    padvance->num_reqs = num_reqs;
 
     switch (game.info.tech_cost_style) {
     case 0:
+      padvance->cost = game.info.base_tech_cost * num_reqs;
       break;
+    case 2:
+      if (-1 != padvance->cost) {
+        continue;
+      }
     case 1:
-    case 2:
-      cost = base * reqs * sqrt(reqs) / 2;
+      padvance->cost = game.info.base_tech_cost * (1.0 + num_reqs)
+                       * sqrt(1.0 + num_reqs) / 2;
       break;
+    case 4:
+      if (-1 != padvance->cost) {
+        continue;
+      }
     case 3:
-    case 4:
-      cost = base * (reqs - 1) * (reqs - 1) / (1 + sqrt(sqrt(reqs))) - base/2;
+      padvance->cost = game.info.base_tech_cost * ((num_reqs) * (num_reqs)
+                           / (1 + sqrt(sqrt(num_reqs + 1))) - 0.5);
       break;
     default:
       log_error("Invalid tech_cost_style %d", game.info.tech_cost_style);
       break;
     }
 
-    techcoststyle1[tech] = MAX(cost, game.info.base_tech_cost);
-  } advance_index_iterate_end;
+    if (padvance->cost < game.info.base_tech_cost) {
+      padvance->cost = game.info.base_tech_cost;
+    }
+  } advance_iterate_end;
 }
 
 /**************************************************************************
@@ -906,15 +861,21 @@
 ****************************************************************************/
 void techs_init(void)
 {
+  struct advance *a_none = &advances[A_NONE];
   int i;
 
+  memset(advances, 0, sizeof(advances));
   for (i = 0; i < ARRAY_SIZE(advances); i++) {
     advances[i].item_number = i;
+    advances[i].cost = -1;
   }
 
   /* Initialize dummy tech A_NONE */
   /* TRANS: "None" tech */
-  name_set(&advances[A_NONE].name, NULL, N_("None"));
+  name_set(&a_none->name, NULL, N_("None"));
+  a_none->require[AR_ONE] = a_none;
+  a_none->require[AR_TWO] = a_none;
+  a_none->require[AR_ROOT] = A_NEVER;
 }
 
 /***************************************************************

Modified: trunk/common/tech.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/tech.h?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/common/tech.h (original)
+++ trunk/common/tech.h Sun Jul  6 10:59:49 2014
@@ -124,11 +124,10 @@
    */
   char *bonus_message;
 
-  /* 
-   * Cost of advance in bulbs as specified in ruleset. -1 means that
-   * no value was set in ruleset. Server send this to client.
-   */
-  int preset_cost;
+  /* Cost of advance in bulbs. It may be specified in ruleset, or
+   * calculated in tech_precalc_data(). However, this value wouldn't
+   * be right if game.info.tech_cost_style is 0. */
+  double cost;
 
   /* 
    * Number of requirements this technology has _including_
@@ -190,11 +189,13 @@
                            Tech_type_id goal);
 bool is_future_tech(Tech_type_id tech);
 
-void precalc_tech_data(void);
-
-/* Initialization and iteration */
+/* Initialization */
 void techs_init(void);
 void techs_free(void);
+
+void techs_precalc_data(void);
+
+/* Iteration */
 
 /* This iterates over almost all technologies.  It includes non-existent
  * technologies, but not A_FUTURE. */

Modified: trunk/fc_version
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/fc_version?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/fc_version    (original)
+++ trunk/fc_version    Sun Jul  6 10:59:49 2014
@@ -52,7 +52,7 @@
 #   - Avoid adding a new mandatory capability to the development branch for
 #     as long as possible.  We want to maintain network compatibility with
 #     the stable branch for as long as possible.
-NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel-2.6-2014.Jul.03"
+NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel-2.6-2014.Jul.06"
 NETWORK_CAPSTRING_OPTIONAL=""
 
 FREECIV_DISTRIBUTOR=""

Modified: trunk/server/ruleset.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/ruleset.c?rev=25397&r1=25396&r2=25397&view=diff
==============================================================================
--- trunk/server/ruleset.c      (original)
+++ trunk/server/ruleset.c      Sun Jul  6 10:59:49 2014
@@ -1105,12 +1105,6 @@
   }
   sec = secfile_sections_by_name_prefix(file, ADVANCE_SECTION_PREFIX);
 
-  /* Initialize dummy tech A_NONE */
-  a_none->require[AR_ONE] = a_none;
-  a_none->require[AR_TWO] = a_none;
-  a_none->require[AR_ROOT] = A_NEVER;
-  BV_CLR_ALL(a_none->flags);
-
   i = 0;
   advance_iterate(A_FIRST, a) {
     const char *sec_name = section_name(section_list_get(sec, i));
@@ -1174,8 +1168,8 @@
 
     a->helptext = lookup_strvec(file, sec_name, "helptext");
     a->bonus_message = lookup_string(file, sec_name, "bonus_message");
-    a->preset_cost =
-        secfile_lookup_int_default(file, -1, "%s.%s", sec_name, "cost");
+    a->cost = secfile_lookup_int_default(file, -1, "%s.%s",
+                                         sec_name, "cost");
     a->num_reqs = 0;
     
     i++;
@@ -5565,7 +5559,7 @@
                       : advance_count();
 
     packet.flags = a->flags;
-    packet.preset_cost = a->preset_cost;
+    packet.cost = a->cost;
     packet.num_reqs = a->num_reqs;
     PACKET_STRVEC_COMPUTE(packet.helptext, a->helptext);
 
@@ -6430,7 +6424,7 @@
   }
 
   if (ok) {
-    precalc_tech_data();
+    techs_precalc_data();
 
     /* Build advisors unit class cache corresponding to loaded rulesets */
     adv_units_ruleset_init();


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to