Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2001:
[Freeciv-Dev] Re: Tech cost patch
Home

[Freeciv-Dev] Re: Tech cost patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Juha Litola <slave@xxxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: Tech cost patch
From: Petr Baudis <pasky@xxxxxxxxxxx>
Date: Wed, 28 Nov 2001 15:05:29 +0100

It doesn't look bad, only the coding style needs to be fined a bit, IMHO.

> diff -u -b -r1.166 packets.c
> --- common/packets.c  2001/10/18 16:45:32     1.166
> +++ common/packets.c  2001/11/28 12:48:29
> @@ -3754,6 +3754,7 @@
>    cptr=put_uint8(cptr, packet->nuke_contamination);
>    cptr=put_uint8(cptr, packet->granary_food_ini);
>    cptr=put_uint8(cptr, packet->granary_food_inc);
> +  cptr=put_uint8(cptr, packet->tech_cost_style);
>    if (has_capability("init_techs", pc->capability)) {
>      cptr = put_tech_list(cptr, packet->global_init_techs);
>    }
> @@ -3784,6 +3785,7 @@
>    iget_uint8(&iter, &packet->nuke_contamination);
>    iget_uint8(&iter, &packet->granary_food_ini);
>    iget_uint8(&iter, &packet->granary_food_inc);
> +  iget_uint8(&iter, &packet->tech_cost_style);
>    if (has_capability("init_techs", pc->capability)) {
>      iget_tech_list(&iter, packet->global_init_techs);
>    }
You should definitively add a capability for that. Otherwise
compatibility breaks, which we certainly don't want.

> diff -u -b -r1.29 tech.c
> --- common/tech.c     2001/09/06 21:23:08     1.29
> +++ common/tech.c     2001/11/28 12:48:30
> @@ -232,3 +232,64 @@
>  
>    return  ((research_time(pplayer) + res - 1) / res);
>  }
> +
> +/**************************************************************************
> + Function to determine cost for technology.
> + Equation is determined from game.rgame.tech_cost_style
> + 0 == Old style. Every tech increases cost by game.researchcost.
> + 1 == Cost is game.researchcost*(1+numparenttechs).
> + 2 == Cost is (numplayers-civswithtech)/numplayers*costinstyle1
> + 3 == Cost is (numplayers-civsincontactwithtech)/numplayers*costinstyle1
> +**************************************************************************/
> +int tech_cost(struct player *pplayer, int tech)
> +{
> +  if (game.rgame.tech_cost_style > 0) {
> +    int advance_count[A_LAST];
> +    int i;
+
> +    for (i = 0; i < A_LAST; ++i)
> +      advance_count[i] = 0;
> +    if (tech > game.num_tech_types)  // future tech
Eek? // ? :-) This is C, not C++ ;p.
> +      return tech * game.researchcost;
Just curious, why it's different than last return?
> +
It would be maybe better to make switch from the bellow..
> +    if (game.rgame.tech_cost_style == 1)
> +      return tech_cost_recursive(pplayer, tech, advance_count);
> +    else if (game.rgame.tech_cost_style == 2) {
> +      int players = get_num_human_and_ai_players();
> +      float mul = (players - game.global_advances[tech]) / (float) players;
+
Also notice that floats aren't very favourite type in FreeCiv, especially not
in server. * 10 and / 10 should do the job well too, and probably faster.
> +      return mul * tech_cost_recursive(pplayer, tech, advance_count);
> +    } else if (game.rgame.tech_cost_style == 3) {
> +      int playerswithtech = 0;
> +      int players = get_num_human_and_ai_players();
> +      float mul;
+
> +      for (i = 0; i < players; ++i) {        // Find out how many players 
> have tech
Eek.. ditto
> +        if (!player_has_embassy(pplayer, &(game.players[i])))
> +          continue;
> +        if (game.players[i].research.inventions[tech] == TECH_KNOWN)
> +          ++playerswithtech;
> +      }
+
> +      mul = (players - playerswithtech) / (float) players;
ditto
> +      return mul * tech_cost_recursive(pplayer, tech, advance_count);
> +    }
> +  } else {
> +    return pplayer->research.researchpoints * game.researchcost;
> +  }
> +}
> +
> +/**************************************************************************
> + Count cost for technology recursively. 
> + Result is researchcost*(1+numparents)
> +**************************************************************************/
can't we cache that somewhat?
> +int tech_cost_recursive(struct player *pplayer, int tech, int *advance_count)
> +{
> +  int price = 0;
+
> +  if (tech == 0)
> +    return 0;
> +  if (advance_count[tech])   // No double counting of parents
eek :)
> +
-
> +    return 0;
> +
> +  advance_count[tech] = 1;
> +  price += tech_cost_recursive(pplayer, advances[tech].req[0], 
> advance_count);
> +  price += tech_cost_recursive(pplayer, advances[tech].req[1], 
> advance_count);
> +  return price += game.researchcost;
     return price + game.researchcost; ?
> +}

Shouldn't you also modify ai/advmilitary.c ? >:)

-- 

                                Petr "Pasky" Baudis

UN*X programmer, UN*X administrator, hobbies = IPv6, IRC
Real Users hate Real Programmers.
Public PGP key, geekcode and stuff: http://pasky.ji.cz/~pasky/


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