This is the mail archive of the
xconq7@sources.redhat.com
mailing list for the Xconq project.
update material display
- From: Peter Garrone <pgarrone at acay dot com dot au>
- To: xconq7 at sources dot redhat dot com
- Date: Mon, 6 Oct 2003 14:45:53 +1000
- Subject: update material display
Hi,
I do have a few points with the recent materials update.
1) The calculation is grossly inaccurate for bellum anyway, I cant
figure out why advances seems correct, possibly because it has
only advanced units for cities. The code from
tkmain.c:update_material_display is as follows:
/* Compute the production. */
for_all_side_units(dside, unit) {
u = unit->type;
/* This is an advanced unit. */
if (u_advanced(unit->type)) {
/* Collect materials from cells controlled by the unit. */
for_all_cells_within_reach(unit, x, y) {
if (!inside_area(x, y)) {
continue;
}
if (user_at(x, y) == unit->id) {
production += production_at(x, y, m);
}
}
} else {
production += num_units_in_play(dside, u) * um_base_production(u,m);
}
}
The main loop iterates over all units in a side, and if not taking the
advanced branch, multiplies the base production inadvertently.
2) The capacity was removed. For Bellum, this is the most important
parameter, so I have put it back. Here is my patch on the current code.
------------------------------------------------------------------------
diff -r -U 5 -p tcltk_cvs/tkconq.tcl tcltk/tkconq.tcl
--- tcltk_cvs/tkconq.tcl Mon Oct 6 09:28:24 2003
+++ tcltk/tkconq.tcl Mon Oct 6 14:18:08 2003
@@ -2393,23 +2393,25 @@ proc fill_in_unit_type_list { map } {
set mtype_entry_height [ expr $mtype_icon_size + 4 ]
set numm [nummtypes ]
if { $numm > 0 } {
set tsy [ expr $sy + $utype_icon_size / 2 + 6 ]
$unitlist create text 12 $tsy -anchor sw -font $bigfont -fill $fgcolor \
- -text "Materials: supply (prod)"
+ -text "Material supply prod capacity"
incr sy $utype_entry_height
for { set i 0 } { $i < $numm } { incr i } {
set tsy [ expr $sy + $mtype_icon_size / 2 ]
imfsample $unitlist.m$i -width 16 -iwidth 16 -height 16 -iheight 16 \
-bg $bgcolor
$unitlist.m$i add imf [ m_image_name $i ]
$unitlist create window 12 [ expr $sy - 6 ] -window $unitlist.m$i -anchor nw
$unitlist create text 40 $tsy -tag material$i \
-anchor sw -font $sdtfont -fill $fgcolor
- $unitlist create text 100 $tsy -tag supply$i \
+ $unitlist create text 130 $tsy -tag supply$i \
-anchor se -font $sdtfont -fill $fgcolor
- $unitlist create text 140 $tsy -tag production$i \
+ $unitlist create text 180 $tsy -tag production$i \
+ -anchor se -font $sdtfont -fill $fgcolor
+ $unitlist create text 230 $tsy -tag capacity$i \
-anchor se -font $sdtfont -fill $fgcolor
incr sy $mtype_entry_height
}
}
}
@@ -3485,10 +3487,19 @@ proc update_unitlist_production { n str
global nummaps map_widget
for { set i 1 } { $i <= $nummaps } { incr i } {
set unitlist $map_widget($i).rightside.listf.unitlist
$unitlist itemconfig production$n -text $str
+ }
+}
+
+proc update_unitlist_capacity { n str } {
+ global nummaps map_widget
+
+ for { set i 1 } { $i <= $nummaps } { incr i } {
+ set unitlist $map_widget($i).rightside.listf.unitlist
+ $unitlist itemconfig capacity$n -text $str
}
}
proc update_unit_info { mapn tag str } {
global curunit
diff -r -U 5 -p tcltk_cvs/tkmain.c tcltk/tkmain.c
--- tcltk_cvs/tkmain.c Mon Oct 6 09:28:24 2003
+++ tcltk/tkmain.c Mon Oct 6 14:23:41 2003
@@ -238,10 +238,32 @@ tcl_cmd(char *name, TclCmdFn tcmd)
{
Tcl_CreateCommand(interp, name, tcmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
}
+/* format a number up to 32 bits with 3 digits of precision. */
+static
+char * format_number(char * spbuf1, int value)
+{
+ const int thousand = 1000;
+ const int million = thousand*thousand;
+ char * spbuf = spbuf1;
+ if(value < 0) {
+ value = -value;
+ *spbuf++ = '-';
+ }
+ *spbuf = 0;
+ if(value < 10*thousand) sprintf(spbuf, "%d", value);
+ else if(value < 100*thousand) sprintf(spbuf,"%4.1fK", ((double)value)/thousand);
+ else if(value < million) sprintf(spbuf,"%dK", value/thousand);
+ else if(value < 10*million) sprintf(spbuf,"%4.2fM", ((double)value)/million);
+ else if(value < 100*million) sprintf(spbuf,"%4.1fM", ((double)value)/million);
+ else if(value < 1000*million) sprintf(spbuf,"%dM", value/million);
+ else sprintf(spbuf,"%4.2G",((double)value)/(1000*million));
+ return spbuf1;
+}
+
void
initial_ui_init(void)
{
char pathbuf[BUFSIZE];
int rslt;
@@ -2472,18 +2494,22 @@ static void
update_material_display(void)
{
int m, u, x, y;
int production;
int supply;
+ int capacity;
+ int capacity_is_infinite;
Unit * unit;
if(nummtypes == 0) {
return;
}
for_all_material_types(m) {
production = 0;
supply = 0;
+ capacity = 0;
+ capacity_is_infinite = 0;
/* Add up unit supplies. */
for_all_side_units(dside, unit) {
supply += unit->supply[m];
}
/* Add in the side treasury. */
@@ -2502,25 +2528,45 @@ update_material_display(void)
}
if (user_at(x, y) == unit->id) {
production += production_at(x, y, m);
}
}
- } else {
- production += num_units_in_play(dside, u) * um_base_production(u,m);
+ }
+ else {
+ if(completed(unit)) {
+ production += um_base_production(u,m);
+ }
}
}
- eval_tcl_cmd("update_unitlist_material %d \"%s\"", m, m_type_name(m));
- spbuf[0] = '\0';
- if (supply > 0) {
- sprintf(spbuf, "%d", supply);
- }
- eval_tcl_cmd("update_unitlist_supply %d \"%s\"", m, spbuf);
- spbuf[0] = '\0';
- if (production > 0) {
- sprintf(spbuf, "(%d)", production);
+ /* Compute capacity */
+ for_all_unit_types(u) {
+ int num = num_units_in_play(dside, u);
+ int num_incomplete = num_units_incomplete(dside,u);
+ int storage = um_storage_x(u,m);
+ /* now to see if storage is infinite
+ * Assume that if divisible by 9, e.g. 99, 999 etc,
+ * and not divisible by 10, it is infinite.
+ */
+ if(storage > 10 && (storage+1)%10 == 0) capacity_is_infinite++;
+ else capacity += (num + num_incomplete)*storage;
+ }
+
+ eval_tcl_cmd("update_unitlist_material %d \"%s\"", m, m_type_name(m));
+
+ eval_tcl_cmd("update_unitlist_supply %d \"%s\"", m,
+ format_number(spbuf,supply));
+
+ eval_tcl_cmd("update_unitlist_production %d \"%s\"", m,
+ format_number(spbuf,production));
+
+ if(capacity_is_infinite) {
+ eval_tcl_cmd("update_unitlist_capacity %d \" \"", m );
+ }
+ else {
+ sprintf(spbuf,"%d%%",(int)((supply*100.0)/capacity));
+ eval_tcl_cmd("update_unitlist_capacity %d \"%s\"", m, spbuf);
}
- eval_tcl_cmd("update_unitlist_production %d \"%s\"", m, spbuf);
}
}
/* Transfer tcl preference settings to C code preferences. */
------------------------------------------------------------------------