]> git.pond.sub.org Git - empserver/commitdiff
Replace the revolting build pointer data structure by a proper data
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 13 Jan 2007 09:07:59 +0000 (09:07 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 13 Jan 2007 09:07:59 +0000 (09:07 +0000)
type.  Make it abstract because that's possible.  Change data layout
so that the slots belonging to a sector are together in memory, it's
nicer to the cache.
(bp): The new type.  Users changed.
(get_wp): Update accordingly.
(alloc_bp): New.
(update_main, calc_all): Use it.  Before, calc_all() allocated 1/7
more than necessary.

13 files changed:
include/budg.h
include/prototypes.h
include/types.h
src/lib/commands/budg.c
src/lib/update/bp.c
src/lib/update/human.c
src/lib/update/land.c
src/lib/update/main.c
src/lib/update/material.c
src/lib/update/plane.c
src/lib/update/prepare.c
src/lib/update/sect.c
src/lib/update/ship.c

index 6bb5d11980646a16155220b52e0c986870252959..f33c3167cf8ca042b3c89f9fed591ca4bc96ca77 100644 (file)
 
 #define SCT_EFFIC (SCT_TYPE_MAX + 1)
 
-void fill_update_array(int *bp, struct sctstr *sp);
-int gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm);
-void pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount);
-int get_materials(struct sctstr *, int *, int *, int);
+struct bp *alloc_bp(void);
+void fill_update_array(struct bp *, struct sctstr *);
+int gt_bg_nmbr(struct bp *, struct sctstr *, i_type);
+void pt_bg_nmbr(struct bp *, struct sctstr *, i_type, int);
+int get_materials(struct sctstr *, struct bp *, int *, int);
 
 extern long money[MAXNOC];
 extern long pops[MAXNOC];
index c39dbb92dfb5c5e139eed03f84115a2a78d6c629..bd22a830740807470df73a42dc0181e70ba5ba6a 100644 (file)
@@ -735,12 +735,12 @@ extern void finish_sects(int);
 /* human.c */
 extern int new_work(struct sctstr *, int);
 extern int do_feed(struct sctstr *, struct natstr *,
-                  short *, int *, int *, int);
+                  short *, int *, struct bp *, int);
 extern int feed_people(short *, int);
 extern double food_needed(short *, int);
 extern int famine_victims(short *, int);
 /* land.c */
-extern int prod_land(int, int, int *, int);
+extern int prod_land(int, int, struct bp *, int);
 /* main.c */
 /* in server.h */
 /* material.c */
@@ -774,7 +774,7 @@ extern void *nxtitemp(struct nstr_item *);
 extern void do_plague(struct sctstr *, struct natstr *, int);
 extern int plague_people(struct natstr *, short *, int *, int *, int);
 /* plane.c */
-extern int prod_plane(int, int, int *, int);
+extern int prod_plane(int, int, struct bp *, int);
 /* populace.c */
 extern void populace(struct natstr *, struct sctstr *, int);
 extern int total_work(int, int, int, int, int, int);
@@ -782,7 +782,7 @@ extern int total_work(int, int, int, int, int, int);
 extern void tax(struct sctstr *, struct natstr *, int, long *, int *,
                int *, int *);
 extern int upd_slmilcosts(natid, int);
-extern void prepare_sects(int, int *);
+extern void prepare_sects(int, struct bp *);
 extern int bank_income(struct sctstr *, int);
 /* produce.c */
 extern int produce(struct natstr *, struct sctstr *, short *, int, int,
@@ -799,9 +799,9 @@ extern void sail_ship(natid);
 extern void do_fallout(struct sctstr *, int);
 extern void spread_fallout(struct sctstr *, int);
 extern void decay_fallout(struct sctstr *, int);
-extern void produce_sect(int, int, int *, long [][2]);
+extern void produce_sect(int, int, struct bp *, long [][2]);
 /* ship.c */
-extern int prod_ship(int, int, int *, int);
+extern int prod_ship(int, int, struct bp *, int);
 
 /*
  * src/server
index c5cf337a0f96ebb8229d8b1332aa5cabbe0926ce..d685255bdc8cc781e967ab0e1001452de58f9ebf 100644 (file)
@@ -37,6 +37,7 @@
 typedef unsigned char natid;   /* NSC_NATID must match this */
 typedef short coord;
 
+struct bp;
 struct emp_qelem;
 struct empobj;
 struct lndstr;
index e1e939f12b05d22746d59156cca18c7f5c947d0f..6befd20529cee9611027afac35ccd642562d6917 100644 (file)
@@ -184,7 +184,7 @@ calc_all(long p_sect[][2],
         int *planes, int *pbuild, int *npbuild, int *pmaint)
 {
     struct natstr *np;
-    int *bp;
+    struct bp *bp;
     long pop = 0;
     int n, civ_tax, uw_tax, mil_pay;
     struct sctstr *sp;
@@ -197,7 +197,7 @@ calc_all(long p_sect[][2],
     *planes = *pbuild = *npbuild = *pmaint = 0;
     
     np = getnatp(player->cnum);
-    bp = calloc(WORLD_X * WORLD_Y * 8, sizeof(int));
+    bp = alloc_bp();
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
        fill_update_array(bp, sp);
        if (sp->sct_own == player->cnum) {
index 6ad99936ca3c9d74719a1c3d5daddd5c8b6cbe99..b68953974db701dd41d187643e6dfc6a3fb90a9d 100644 (file)
@@ -29,6 +29,7 @@
  * 
  *  Known contributors to this file:
  *     Ville Virrankoski, 1996
+ *     Markus Armbruster, 2007
  */
 
 #include <config.h>
 #include "budg.h"
 #include "update.h"
 
+struct bp {
+    int val[7];
+};
+
 static int bud_key[I_MAX + 2] =
     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 5, 6, 0, 0, 7 };
 
 static int *
-get_wp(int *bp, struct sctstr *sp, int cm)
+get_wp(struct bp *bp, struct sctstr *sp, int cm)
 {
-    return (bp + (sp->sct_x + (sp->sct_y * WORLD_X)) +
-           WORLD_X * WORLD_Y * (cm - 1));
+    return &bp[sp->sct_x + sp->sct_y * WORLD_X].val[cm - 1];
 }
 
 int
-gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm)
+gt_bg_nmbr(struct bp *bp, struct sctstr *sp, i_type comm)
 {
     int *wp;
     int cm;
@@ -61,7 +65,7 @@ gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm)
 }
 
 void
-pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount)
+pt_bg_nmbr(struct bp *bp, struct sctstr *sp, i_type comm, int amount)
 {
     int *wp;
     int cm;
@@ -73,7 +77,7 @@ pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount)
 }
 
 void
-fill_update_array(int *bp, struct sctstr *sp)
+fill_update_array(struct bp *bp, struct sctstr *sp)
 {
     int k;
     int *wp;
@@ -87,3 +91,9 @@ fill_update_array(int *bp, struct sctstr *sp)
     wp = get_wp(bp, sp, bud_key[I_MAX + 1]);
     *wp = sp->sct_avail;
 }
+
+struct bp *
+alloc_bp(void)
+{
+    return calloc(WORLD_X * WORLD_Y, sizeof(struct bp));
+}
index 853e694ce8d562568a65462703eab13acd8923c6..43c0b1a3a85d62a8cb45ac993032d6bb63d70ede 100644 (file)
@@ -55,7 +55,7 @@ static int babies(int, int, double, int, int);
  */
 int
 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
-       int *workp, int *bp, int etu)
+       int *workp, struct bp *bp, int etu)
 {
     int work_avail;
     int starved, sctwork;
index ece5cf05cfc91dc7a145edf54a509fd0b27b10b0..a6d3566c899c6b6865f7baf6e48a0d09043be094 100644 (file)
 #include "update.h"
 #include <math.h>
 
-static void landrepair(struct lndstr *, struct natstr *, int *, int);
-static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
+static void landrepair(struct lndstr *, struct natstr *, struct bp *, int);
+static void upd_land(struct lndstr *, int, struct natstr *, struct bp *, int);
 static int feed_land(struct lndstr *, int);
 
 int
-prod_land(int etus, int natnum, int *bp, int build)
+prod_land(int etus, int natnum, struct bp *bp, int build)
                /* build = 1, maintain = 0 */
 {
     struct lndstr *lp;
@@ -104,7 +104,7 @@ prod_land(int etus, int natnum, int *bp, int build)
 
 static void
 upd_land(struct lndstr *lp, int etus,
-        struct natstr *np, int *bp, int build)
+        struct natstr *np, struct bp *bp, int build)
               /* build = 1, maintain = 0 */
 {
     struct lchrstr *lcp;
@@ -214,7 +214,7 @@ upd_land(struct lndstr *lp, int etus,
 }
 
 static void
-landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
+landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
 {
     int delta;
     struct sctstr *sp;
index 5d79042d19f7b1910ad21663fd52be791524e413..65ebb573617fa9f1d510bc6dd94a3387c4e60562 100644 (file)
@@ -58,7 +58,7 @@ update_main(void *unused)
     int etu = etu_per_update;
     int n;
     int x;
-    int *bp;
+    struct bp *bp;
     int cn, cn2, rel;
     struct natstr *cnp;
     struct natstr *np;
@@ -92,7 +92,7 @@ update_main(void *unused)
     memset(air_money, 0, sizeof(air_money));
     memset(sea_money, 0, sizeof(sea_money));
     memset(lnd_money, 0, sizeof(lnd_money));
-    bp = calloc(WORLD_X * WORLD_Y * 7, sizeof(int));
+    bp = alloc_bp();
     for (n = 0; n < MAXNOC; n++) {
        money[n] = 0;
        if (!(np = getnatp(n)))
index 5b733ef2854d559128df7af9efb1f71c402a8cdf..8ccd09b37d560cb17726ffdffbd5839a2d16382c 100644 (file)
@@ -48,7 +48,7 @@
  * Return adjusted build percentage.
  */
 int
-get_materials(struct sctstr *sp, int *bp, int *mvec, int pct)
+get_materials(struct sctstr *sp, struct bp *bp, int *mvec, int pct)
 {
     int i, amt;
 
index 97fdc078d52249048fb1f355345eec9da13c6d55..82e2d91b9edfa101efcd1146f0dbe565aecf0257 100644 (file)
 #include "ship.h"
 #include "update.h"
 
-static void planerepair(struct plnstr *, struct natstr *, int *, int);
-static void upd_plane(struct plnstr *, int, struct natstr *, int *, int);
+static void planerepair(struct plnstr *, struct natstr *, struct bp *, int);
+static void upd_plane(struct plnstr *, int, struct natstr *, struct bp *, int);
 
 int
-prod_plane(int etus, int natnum, int *bp, int buildem)
+prod_plane(int etus, int natnum, struct bp *bp, int buildem)
                 /* Build = 1, maintain =0 */
 {
     struct plnstr *pp;
@@ -88,7 +88,7 @@ prod_plane(int etus, int natnum, int *bp, int buildem)
 
 static void
 upd_plane(struct plnstr *pp, int etus,
-         struct natstr *np, int *bp, int build)
+         struct natstr *np, struct bp *bp, int build)
 {
     struct plchrstr *pcp = &plchr[(int)pp->pln_type];
     int mult, cost, eff;
@@ -125,7 +125,7 @@ upd_plane(struct plnstr *pp, int etus,
 }
 
 static void
-planerepair(struct plnstr *pp, struct natstr *np, int *bp, int etus)
+planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
 {
     int build;
     int mvec[I_MAX + 1];
index 4c5d74a1656ea719d730160c3fb4d267b3f31a30..cd0afb11e9fcda05646ea2226610b60130aaa6cb 100644 (file)
@@ -44,7 +44,7 @@
 #include "update.h"
 
 void
-prepare_sects(int etu, int *bp)
+prepare_sects(int etu, struct bp *bp)
 {
     struct sctstr *sp;
     struct natstr *np;
index 90ed037138112c3a376fc8256ca244239e906113..ccc3637197b67471865ed509c52af7a28a853501 100644 (file)
@@ -257,7 +257,7 @@ decay_fallout(struct sctstr *sp, int etus)
  * Produce for a specific nation
  */
 void
-produce_sect(int natnum, int etu, int *bp, long p_sect[][2])
+produce_sect(int natnum, int etu, struct bp *bp, long p_sect[][2])
 {
     struct sctstr *sp;
     struct natstr *np;
index fd5863f2d52d844bc210ee3671952b83e59660b6..54c18ec3388dbef81c60f0e50c407c2fd6765112 100644 (file)
 #include "ship.h"
 #include "update.h"
 
-static void shiprepair(struct shpstr *, struct natstr *, int *, int);
-static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
+static void shiprepair(struct shpstr *, struct natstr *, struct bp *, int);
+static void upd_ship(struct shpstr *, int, struct natstr *, struct bp *, int);
 static int feed_ship(struct shpstr *, int);
 
 int
-prod_ship(int etus, int natnum, int *bp, int build)
+prod_ship(int etus, int natnum, struct bp *bp, int build)
                /* build = 1, maintain = 0 */
 {
     struct shpstr *sp;
@@ -107,7 +107,7 @@ prod_ship(int etus, int natnum, int *bp, int build)
 
 static void
 upd_ship(struct shpstr *sp, int etus,
-        struct natstr *np, int *bp, int build)
+        struct natstr *np, struct bp *bp, int build)
               /* build = 1, maintain = 0 */
 {
     struct sctstr *sectp;
@@ -266,7 +266,7 @@ upd_ship(struct shpstr *sp, int etus,
  * 8 * 8 * $40 = $2560!
  */
 static void
-shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
+shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
 {
     int delta;
     struct sctstr *sp;