shithub: sce

Download patch

ref: 6a5d79c24a3aacf25079421ffe9a2d5e0bd41888
parent: 014f77e085d1bf1e285ee7ef29796b66d0e592e0
author: qwx <qwx@sciops.net>
date: Mon Jul 12 03:08:27 EDT 2021

link mineral objects to resources in db

- fs: add `gather' db entry linking resource and obj
- fs: add richness threshold fields to resources
- sim: set richness as a mobj's state rather than only
choosing a sprite in drw

richness thresholds are specific to minerals, but eh.
setting richness as a state when the mobj is spawned
and changed during gameplay is somewhat arbitrary but
it might be useful state later.

--- a/dat.h
+++ b/dat.h
@@ -151,6 +151,7 @@
 	double turn;
 	Obj **spawn;
 	int nspawn;
+	Resource *res;
 };
 struct Path{
 	Point target;
@@ -212,6 +213,10 @@
 struct Resource{
 	char *name;
 	int init;
+	Obj **obj;
+	int nobj;
+	int *thresh;
+	int nthresh;
 };
 extern Resource resources[Nresource];
 
--- a/drw.c
+++ b/drw.c
@@ -303,16 +303,6 @@
 	}
 }
 
-static int
-stateframe(Mobj *mo)
-{
-	/* FIXME: will be replaced */
-	if(mo->o->f & Fresource){
-		return OSrich;
-	}
-	return mo->state;
-}
-
 static Pic *
 frm(Mobj *mo, int type)
 {
@@ -324,7 +314,7 @@
 	Pics *pp;
 	Pic *p;
 
-	n = stateframe(mo);
+	n = mo->state;
 	if(n < 0 || n > OSend){
 		dprint("frm: %s invalid animation frame %d\n", mo->o->name, n);
 		return nil;
--- a/fs.c
+++ b/fs.c
@@ -311,6 +311,25 @@
 }
 
 static void
+readgather(char **fld, int n, Table *)
+{
+	Obj *o, **os;
+	Resource *r;
+
+	unpack(fld, "ro", &r, &o);
+	if(o->res != nil && o->res != r)
+		sysfatal("readgather %s: obj %s already assigned to %s",
+			r->name, o->name, o->res->name);
+	for(os=r->obj; os<r->obj+r->nobj; os++)
+		if(*os == o)
+			sysfatal("readgather: duplicate entry %s in %s\n", o->name, r->name);
+	n = r->nobj + 1;
+	r->obj = erealloc(r->obj, n * sizeof *r->obj, r->nobj * sizeof *r->obj);
+	r->obj[r->nobj++] = o;
+	o->res = r;
+}
+
+static void
 readspawn(char **fld, int n, Table *)
 {
 	Obj *o, **os, **oe;
@@ -372,15 +391,28 @@
 }
 
 static void
-readresource(char **fld, int, Table *tab)
+readresource(char **fld, int n, Table *tab)
 {
 	Resource *r;
+	int *ts, *te;
 
 	r = resources + tab->row;
 	if(r >= resources + nelem(resources))
 		sysfatal("readresource: out of bounds reference");
 	r->name = estrdup(*fld++);
-	unpack(fld, "d", &r->init);
+	unpack(fld++, "d", &r->init);
+	n -= 2;
+	if(n <= 0)
+		return;
+	if(n > OSend)
+		sysfatal("readresource: invalid number of states %d", n);
+	r->thresh = emalloc(n * sizeof *r->thresh);
+	r->nthresh = n;
+	for(ts=r->thresh, te=ts+n; ts<te; ts++){
+		unpack(fld++, "d", ts);
+		if(*ts < 1)
+			sysfatal("readresource: invalid threshold %d\n", *ts);
+	}
 }
 
 static void
@@ -470,16 +502,18 @@
 	Ttileset,
 	Tmap,
 	Tspr,
+	Tgather,
 };
 Table table[] = {
 	[Tmapobj] {"mapobj", readmapobj, 4, &nobjp},
 	[Tobj] {"obj", readobj, 17, &nobj},
 	[Tattack] {"attack", readattack, 4, &nattack},
-	[Tresource] {"resource", readresource, 2, &nresource},
+	[Tresource] {"resource", readresource, -1, &nresource},
 	[Tspawn] {"spawn", readspawn, -1, nil},
 	[Ttileset] {"tileset", readtileset, 1, nil},
 	[Tmap] {"map", readmap, -1, &mapheight},
 	[Tspr] {"spr", readspr, -1, nil},
+	[Tgather] {"gather", readgather, -1, nil},
 };
 
 static int
--- a/sce/map1.db
+++ b/sce/map1.db
@@ -28,3 +28,5 @@
 mapobj,mutalisk,10,10,2
 # overloaded for now
 mapobj,mineral0,8,2,1500
+mapobj,mineral1,10,2,1500
+mapobj,mineral2,12,2,1500
--- a/sce/sce.db
+++ b/sce/sce.db
@@ -1,6 +1,6 @@
-# resource: name, init
+# resource: name, init, [richness thresholds]
 resource,supply,200
-resource,minerals,50
+resource,minerals,50,750,500,250,1
 resource,vespene gas,0
 # attack: name, dmg, range, cool
 attack,fusion cutter,5,10,15
@@ -13,13 +13,15 @@
 obj,control,0x8,16,12,1500,1,1,10,400,0,1800,,,0,0,0,0
 obj,hatchery,0x8,16,12,1250,1,1,10,300,0,1800,,,0,0,0,0
 obj,mineral0,0x4000,8,4,,,,,,,,,,,,,
+obj,mineral1,0x4000,8,4,,,,,,,,,,,,,
+obj,mineral2,0x4000,8,4,,,,,,,,,,,,,
 # spawn: objname, [obj..]
 spawn,control,scv
 spawn,hatchery,drone,mutalisk
-# gather: name, resource, w, h, [richness thresholds..]
-gather,mineral0,minerals,750,500,250,1
-gather,mineral1,minerals,750,500,250,1
-gather,mineral2,minerals,750,500,250,1
+# gather: obj, resource
+gather,minerals,mineral0
+gather,minerals,mineral1
+gather,minerals,mineral2
 # spr: objname, flags (PF enum), rotations, [frame..]
 spr,scv,0x0000,17,0
 spr,scv,0xc000,17,0
@@ -42,3 +44,19 @@
 spr,mineral0,0xd002,1,2
 spr,mineral0,0x1003,1,3
 spr,mineral0,0xd003,1,3
+spr,mineral1,0x1000,1,0
+spr,mineral1,0xd000,1,0
+spr,mineral1,0x1001,1,1
+spr,mineral1,0xd001,1,1
+spr,mineral1,0x1002,1,2
+spr,mineral1,0xd002,1,2
+spr,mineral1,0x1003,1,3
+spr,mineral1,0xd003,1,3
+spr,mineral2,0x1000,1,0
+spr,mineral2,0xd000,1,0
+spr,mineral2,0x1001,1,1
+spr,mineral2,0xd001,1,1
+spr,mineral2,0x1002,1,2
+spr,mineral2,0xd002,1,2
+spr,mineral2,0x1003,1,3
+spr,mineral2,0xd003,1,3
--- a/sim.c
+++ b/sim.c
@@ -170,13 +170,25 @@
 int
 spawnresource(int x, int y, Obj *o, int amount)
 {
+	int *t, *te;
 	Mobj *mo;
+	Resource *r;
 
 	if((mo = mapspawn(x, y, o)) == nil)
 		return -1;
-	mo->state = -1;
 	mo->team = 0;
 	mo->amount = amount;
+	mo->state = OSrich;
+	r = o->res;
+	for(t=r->thresh, te=t+r->nthresh; t<te; t++){
+		if(amount >= *t)
+			break;
+		mo->state++;
+	}
+	if(mo->state >= OSend){
+		dprint("spawnresource %s %d,%d: invalid state %d\n", o->name, x, y, mo->state);
+		mo->state = OSpoor;
+	}
 	refmobj(mo);
 	return 0;
 }