shithub: sce

Download patch

ref: 219ea3130072f8e0fed4bd78d3d5eb668f698a13
parent: c0f37dd92b13f2463a38b17e51f5cf26a4a041c0
author: qwx <[email protected]>
date: Sat Nov 20 21:01:14 EST 2021

sim: rudimentary gather command/state

--- a/com.c
+++ b/com.c
@@ -74,6 +74,40 @@
 }
 
 static int
+reqgather(uchar *p, uchar *e)
+{
+	int n;
+	Point click;
+	Mobj reqm, reqt, *mo, *tgt;
+
+	if((n = unpack(p, e, "dldd dd dldd",
+	&reqm.idx, &reqm.uuid, &reqm.x, &reqm.y,
+	&click.x, &click.y,
+	&reqt.idx, &reqt.uuid, &reqt.x, &reqt.y)) < 0)
+		return -1;
+	if((mo = mobjfromreq(&reqm)) == nil)
+		return -1;
+	if((mo->o->f & Fgather) == 0){
+		werrstr("reqgather: object %M not a gatherer", mo);
+		return -1;
+	}
+	if((mo->o->f & Fimmutable) || mo->o->speed == 0.0){
+		werrstr("reqgather: object %M can't move", mo);
+		return -1;
+	}
+	if((tgt = mobjfromreq(&reqt)) == nil)
+		return -1;
+	if(click.x >= nodemapwidth || click.y >= nodemapheight){
+		werrstr("reqgather: invalid location %d,%d", click.x, click.y);
+		return -1;
+	}
+	clearcommands(mo);
+	if(pushgathercommand(click, mo, tgt) < 0)
+		return -1;
+	return n;
+}
+
+static int
 reqmovenear(uchar *p, uchar *e)
 {
 	int n;
@@ -166,10 +200,11 @@
 	while(p < e){
 		type = *p++;
 		switch(type){
-		case Tpause: fn = reqpause; break;
-		case Tmove: fn = reqmove; break;
-		case Tmovenear: fn = reqmovenear; break;
-		case Teom:
+		case CTpause: fn = reqpause; break;
+		case CTmove: fn = reqmove; break;
+		case CTmovenear: fn = reqmovenear; break;
+		case CTgather: fn = reqgather; break;
+		case CTeom:
 			if(p < e)
 				fprint(2, "parsemsg: trailing data\n");
 			return 0;
@@ -247,17 +282,33 @@
 void
 endmsg(Msg *m)
 {
-	packmsg(m, "h", Teom);
+	packmsg(m, "h", CTeom);
 	pack(m->buf, m->buf + Hdrsz, "s", m->sz - Hdrsz);
 }
 
 int
+sendgather(Mobj *mo, Point click, Mobj *tgt)
+{
+	Msg *m;
+
+	m = getclbuf();
+	if(packmsg(m, "h dldd dd dldd", CTgather,
+	mo->idx, mo->uuid, mo->x, mo->y,
+	click.x, click.y,
+	tgt->idx, tgt->uuid, tgt->x, tgt->y) < 0){
+		fprint(2, "sendgather: %r\n");
+		return -1;
+	}
+	return 0;
+}
+
+int
 sendmovenear(Mobj *mo, Point click, Mobj *tgt)
 {
 	Msg *m;
 
 	m = getclbuf();
-	if(packmsg(m, "h dldd dd dldd", Tmovenear,
+	if(packmsg(m, "h dldd dd dldd", CTmovenear,
 	mo->idx, mo->uuid, mo->x, mo->y,
 	click.x, click.y,
 	tgt->idx, tgt->uuid, tgt->x, tgt->y) < 0){
@@ -273,7 +324,7 @@
 	Msg *m;
 
 	m = getclbuf();
-	if(packmsg(m, "h dldd dd", Tmove,
+	if(packmsg(m, "h dldd dd", CTmove,
 	mo->idx, mo->uuid, mo->x, mo->y,
 	tgt.x, tgt.y) < 0){
 		fprint(2, "sendmove: %r\n");
@@ -288,7 +339,7 @@
 	Msg *m;
 
 	m = getclbuf();
-	if(packmsg(m, "h", Tpause) < 0){
+	if(packmsg(m, "h", CTpause) < 0){
 		fprint(2, "sendpause: %r\n");
 		return -1;
 	}
--- a/dat.h
+++ b/dat.h
@@ -95,6 +95,7 @@
 	Fmech = 1<<1,
 	Fair = 1<<2,
 	Fbuild = 1<<3,
+	Fgather = 1<<4,
 	Fresource = 1<<14,
 	Fimmutable = 1<<15,
 };
@@ -114,6 +115,7 @@
 	/* unit */
 	OSidle = OState0,
 	OSmove = OState1,
+	OSgather = OState2,
 
 	/* resource */
 	OSrich = OState0,
@@ -192,10 +194,11 @@
 	Point goal;
 	Mobj *target1;
 	Mobj *target2;
+	vlong tc;
 	int (*initfn)(Mobj*);
 	void (*stepfn)(Mobj*);
 	void (*cleanupfn)(Mobj*);
-	void (*nextfn)(Mobj*);
+	int (*nextfn)(Mobj*);
 };
 struct Mobj{
 	Obj *o;
@@ -256,11 +259,12 @@
 extern int scale;
 
 enum{
-	Tquit = 0x1f,
-	Tpause,
-	Tmove,
-	Tmovenear,
-	Teom,
+	CTquit = 0x1f,
+	CTpause,
+	CTmove,
+	CTmovenear,
+	CTgather,
+	CTeom,
 
 	Nbuf = 4096,
 };
--- a/drw.c
+++ b/drw.c
@@ -56,7 +56,7 @@
 }
 
 void
-select(Point p)
+doselect(Point p)
 {
 	int i;
 
@@ -68,7 +68,7 @@
 }
 
 void
-move(Point p)
+domove(Point p)
 {
 	int i;
 	Point vp;
--- a/fns.h
+++ b/fns.h
@@ -9,6 +9,8 @@
 void	stepsnd(void);
 void	initsnd(void);
 void	linktomap(Mobj*);
+int	pushgathercommand(Point, Mobj*, Mobj*);
+int	pushmove(Mobj*);
 int	pushmovecommand(Point, Mobj*, Mobj*);
 void	resourcestate(Mobj*);
 void	idlestate(Mobj*);
@@ -28,8 +30,8 @@
 void	packcl(char*, ...);
 Msg*	getclbuf(void);
 void	dopan(Point);
-void	select(Point);
-void	move(Point);
+void	doselect(Point);
+void	domove(Point);
 void	compose(int, int, u32int);
 void	redraw(void);
 void	updatefb(void);
--- a/fs.c
+++ b/fs.c
@@ -503,26 +503,26 @@
 }
 
 enum{
-	Tmapobj,
-	Tobj,
-	Tattack,
-	Tresource,
-	Tspawn,
-	Ttileset,
-	Tmap,
-	Tspr,
-	Tgather,
+	TBmapobj,
+	TBobj,
+	TBattack,
+	TBresource,
+	TBspawn,
+	TBtileset,
+	TBmap,
+	TBspr,
+	TBgather,
 };
 Table table[] = {
-	[Tmapobj] {"mapobj", readmapobj, 4, &nobjp},
-	[Tobj] {"obj", readobj, 17, &nobj},
-	[Tattack] {"attack", readattack, 4, &nattack},
-	[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},
+	[TBmapobj] {"mapobj", readmapobj, 4, &nobjp},
+	[TBobj] {"obj", readobj, 17, &nobj},
+	[TBattack] {"attack", readattack, 4, &nattack},
+	[TBresource] {"resource", readresource, -1, &nresource},
+	[TBspawn] {"spawn", readspawn, -1, nil},
+	[TBtileset] {"tileset", readtileset, 1, nil},
+	[TBmap] {"map", readmap, -1, &mapheight},
+	[TBspr] {"spr", readspr, -1, nil},
+	[TBgather] {"gather", readgather, -1, nil},
 };
 
 static int
--- a/mkfile
+++ b/mkfile
@@ -12,6 +12,7 @@
 	pheap.$O\
 	sce.$O\
 	sim.$O\
+	sim.gather.$O\
 	sim.idle.$O\
 	sim.move.$O\
 	sim.resource.$O\
--- a/sce.c
+++ b/sce.c
@@ -219,11 +219,11 @@
 		case Amouse:
 			qlock(&drawlock);	/* just for security */
 			if(me.b & 1)
-				select(me);
+				doselect(me);
 			if(me.b & 2)
 				dopan(me.Δ);
 			if(me.b & 4)
-				move(me);
+				domove(me);
 			qunlock(&drawlock);
 			flushcl();
 			break;
--- a/sce/sce.db
+++ b/sce/sce.db
@@ -7,8 +7,8 @@
 attack,spines,5,10,22
 attack,glave wurm,9,96,30
 # obj: name, flags, w, h, hp, def, vis, cost[3], time, attack[2], speed, accel, halt, turn
-obj,scv,0x3,4,4,60,0,224,1,50,0,20,fusion cutter,,4.92,67,12227,40
-obj,drone,0x1,4,4,40,0,224,1,50,0,20,spines,,4.92,67,12227,40
+obj,scv,0x13,4,4,60,0,224,1,50,0,20,fusion cutter,,4.92,67,12227,40
+obj,drone,0x11,4,4,40,0,224,1,50,0,20,spines,,4.92,67,12227,40
 obj,mutalisk,0x5,4,4,120,0,224,2,100,100,600,glave wurm,glave wurm,6.67,67,21745,40
 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
--- a/sim.c
+++ b/sim.c
@@ -11,6 +11,7 @@
 char *statename[OSend] = {
 	[OSidle] "idle",
 	[OSmove] "moving",
+	[OSgather] "gathering",
 };
 
 static Mobjl mobjl0 = {.l = &mobjl0, .lp = &mobjl0}, *mobjl = &mobjl0;
@@ -71,9 +72,10 @@
 	c = mo->cmds;
 	if(c->cleanupfn != nil)
 		c->cleanupfn(mo);
-	if(c->nextfn != nil)
-		c->nextfn(mo);
-	else
+	if(c->nextfn != nil){
+		c->initfn = c->nextfn;
+		mo->state = OSskymaybe;
+	}else
 		popcommand(mo);
 }
 
--- /dev/null
+++ b/sim.gather.c
@@ -1,0 +1,63 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+/* thanks: https://makingcomputerdothings.com/brood-war-api-the-comprehensive-guide-unit-movement-and-worker-behavios/ */
+/* FIXME: additional bullshit logic */
+
+enum{
+	Tgather = 75,	/* FIXME: 37 for gas, define in db? */
+	Namount = 8,
+};
+
+static void
+cleanup(Mobj *)
+{
+}
+
+static void
+step(Mobj *mo)
+{
+	Command *c;
+
+	c = mo->cmds;
+	if(++c->tc >= Tgather){
+		nextstate(mo);
+		return;
+	}
+	// FIXME: butts
+}
+
+static int
+pushgather(Mobj *mo)
+{
+	Command *c;
+
+	c = mo->cmds;
+	/* FIXME: check if resource still exists? (and amount >0) (needs despawning/death) */
+	c->cleanupfn = cleanup;
+	c->stepfn = step;
+	c->nextfn = nil;
+	c->tc = 0;
+	mo->state = OSgather;
+	return 0;
+}
+
+int
+pushgathercommand(Point goal, Mobj *mo, Mobj *target)
+{
+	Command *c;
+
+	if((c = pushcommand(mo)) == nil){
+		fprint(2, "pushmovecommand: %r\n");
+		return -1;
+	}
+	c->name = "gather";
+	c->initfn = pushmove;
+	c->goal = goal;
+	c->target1 = target;
+	c->nextfn = pushgather;
+	return 0;
+}
--- a/sim.move.c
+++ b/sim.move.c
@@ -286,7 +286,7 @@
 }
 
 int
-newmove(Mobj *mo)
+pushmove(Mobj *mo)
 {
 	Point goal;
 	Command *c;
@@ -312,7 +312,7 @@
 		return -1;
 	}
 	c->name = "move";
-	c->initfn = newmove;
+	c->initfn = pushmove;
 	c->goal = goal;
 	c->target1 = target;
 	c->nextfn = nil;