Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2005:
[Freeciv-Dev] (PR#13354) Free techs cleanup
Home

[Freeciv-Dev] (PR#13354) Free techs cleanup

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13354) Free techs cleanup
From: "Mateusz Stefek" <mstefek@xxxxxxxxx>
Date: Mon, 27 Jun 2005 14:48:05 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch hides free techs implemtation in techtools.c
choose_random_tech() slightly changed it's definition and new functions
pick_random_tech() and give_random_free_techs() are introduced.

Previous implementation was buggy and implemented in two places.

--
mateusz
Index: server/cityturn.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v
retrieving revision 1.321
diff -u -r1.321 cityturn.c
--- server/cityturn.c   27 Jun 2005 14:30:19 -0000      1.321
+++ server/cityturn.c   27 Jun 2005 21:43:14 -0000
@@ -1125,16 +1125,7 @@
                    get_improvement_name(id), mod);
 
       for (i = 0; i < mod; i++) {
-       Tech_type_id tech = get_player_research(pplayer)->researching;
-
-       if (tech == A_UNSET) {
-         choose_random_tech(pplayer);
-         tech = get_player_research(pplayer)->researching;
-       }
-       do_free_cost(pplayer);
-       found_new_tech(pplayer,
-                      get_player_research(pplayer)->researching, TRUE, TRUE, 
-                      A_NONE);
+       Tech_type_id tech = give_random_free_tech(pplayer);
 
        notify_embassies(pplayer, NULL,
            _("The %s have acquired %s from %s."),
Index: server/techtools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/techtools.c,v
retrieving revision 1.6
diff -u -r1.6 techtools.c
--- server/techtools.c  27 Jun 2005 08:30:34 -0000      1.6
+++ server/techtools.c  27 Jun 2005 21:43:20 -0000
@@ -483,10 +483,10 @@
 }
 
 /****************************************************************************
-  Finds and chooses (sets) a random research target from among all those
-  available.
+  Returns random researchable tech or A_FUTURE.
+  No side effects
 ****************************************************************************/
-void choose_random_tech(struct player *plr)
+Tech_type_id pick_random_tech(struct player* plr) 
 {
   int chosen, researchable = 0;
 
@@ -496,8 +496,7 @@
     }
   } tech_type_iterate_end;
   if (researchable == 0) {
-    choose_tech(plr, A_FUTURE);
-    return;
+    return A_FUTURE;
   }
   chosen = myrand(researchable) + 1;
   
@@ -505,11 +504,29 @@
     if (get_invention(plr, i) == TECH_REACHABLE) {
       chosen--;
       if (chosen == 0) {
-       choose_tech(plr, i);
-       break;
+        return i;
       }
     }
   } tech_type_iterate_end;
+  assert(0);
+  return A_FUTURE;
+}
+
+/****************************************************************************
+  Finds and chooses (sets) a random research target from among all those
+  available until plr->research->researching != A_UNSET.
+  Player may research more than one tech in this function.
+  Possible reasons:
+  - techpenalty < 100
+  - research.got_tech = TRUE and enough bulbs was saved
+  - research.researching = A_UNSET and enough bulbs was saved
+****************************************************************************/
+void choose_random_tech(struct player *plr)
+{
+  struct player_research* research = get_player_research(plr);
+  do {
+    choose_tech(plr, pick_random_tech(plr));
+  } while (research->researching == A_UNSET);
 }
 
 /****************************************************************************
@@ -745,3 +762,16 @@
     }
   } players_iterate_end;
 }
+
+/****************************************************************************
+  Gives a player random tech, which he hasn't researched yet. Applies freecost
+  Returns the tech.
+****************************************************************************/
+Tech_type_id give_random_free_tech(struct player* pplayer) {
+  Tech_type_id tech;
+  
+  tech = pick_random_tech(pplayer);
+  do_free_cost(pplayer);
+  found_new_tech(pplayer, tech, FALSE, TRUE, A_NONE);
+  return tech;
+}
Index: server/techtools.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/techtools.h,v
retrieving revision 1.2
diff -u -r1.2 techtools.h
--- server/techtools.h  24 Jun 2005 10:12:47 -0000      1.2
+++ server/techtools.h  27 Jun 2005 21:43:20 -0000
@@ -27,10 +27,12 @@
 void found_new_future_tech(struct player *pplayer);
 void update_tech(struct player *plr, int bulbs);
 void init_tech(struct player *plr, int tech_count);
+Tech_type_id pick_random_tech(struct player* plr);
 void choose_random_tech(struct player *plr);
 void choose_tech(struct player *plr, Tech_type_id tech);
 void choose_tech_goal(struct player *plr, Tech_type_id tech);
 void get_a_tech(struct player *pplayer, struct player *target);
 
+Tech_type_id give_random_free_tech(struct player *pplayer);
 #endif
 
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.363
diff -u -r1.363 unittools.c
--- server/unittools.c  27 Jun 2005 14:30:19 -0000      1.363
+++ server/unittools.c  27 Jun 2005 21:43:26 -0000
@@ -2283,21 +2283,11 @@
 static void hut_get_tech(struct unit *punit)
 {
   struct player *pplayer = unit_owner(punit);
-  int res_ed, res_ing;
   Tech_type_id new_tech;
-  const char *tech_name;
-  struct player_research* research = get_player_research(pplayer);
+  const char* tech_name;
   
-  /* Save old values, choose tech, then restore old values: */
-  res_ed = research->bulbs_researched;
-  res_ing = research->researching;
+  new_tech = give_random_free_tech(pplayer);
   
-  choose_random_tech(pplayer);
-  new_tech = research->researching;
-  
-  research->bulbs_researched = res_ed;
-  research->researching = res_ing;
-
   tech_name = get_tech_name(pplayer, new_tech);
   notify_player_ex(pplayer, punit->tile, E_HUT_TECH,
                   _("You found %s in ancient scrolls of wisdom."),
@@ -2310,13 +2300,6 @@
   notify_embassies(pplayer, NULL, _("The %s have acquired %s"
                                    " from ancient scrolls of wisdom."),
                   get_nation_name_plural(pplayer->nation), tech_name);
-
-  do_free_cost(pplayer);
-  if (!is_future_tech(new_tech)) {
-    found_new_tech(pplayer, new_tech, FALSE, TRUE, A_NONE);
-  } else {
-    found_new_future_tech(pplayer);
-  }
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13354) Free techs cleanup, Mateusz Stefek <=