shithub: city

Download patch

ref: 2d226f3ddc911ad4a47d7c986b359075d89f7eea
parent: 24e3f38bb4e102aebca99a107ccbe99dbf4a107f
author: qwx <qwx@sciops.net>
date: Sun Feb 14 23:25:36 EST 2021

sim: upkeep

--- a/dat.h
+++ b/dat.h
@@ -40,6 +40,7 @@
 	char *name;
 	int goods[Gtot];
 };
+extern Resource resources[Rtot];
 
 enum{
 	Btownhall,
@@ -86,8 +87,8 @@
 
 enum{
 	Snull,
-	Swait,
 	Sbuild,
+	Swait,
 	Sproduce,
 };
 struct Tile{
--- a/sim.c
+++ b/sim.c
@@ -11,6 +11,18 @@
 };
 static int stock[Gtot], rstock[Rtot];
 
+int
+max(int a, int b)
+{
+	return a > b ? a : b;
+}
+
+int
+min(int a, int b)
+{
+	return a < b ? a : b;
+}
+
 static void
 spawn(Tile *m, int n)
 {
@@ -31,16 +43,44 @@
 }
 
 static void
+starve(Tile *o)
+{
+	o->state = Swait;
+}
+
+static void
 upkeep(void)
 {
-	Tile **o;
+	int n, r, g, upkeep[nelem(resources)];
+	Tile *o, **ol;
 
 	if(clock % UpkeepΔt != 0 || clock == 0)
 		return;
-	for(o=objhead-1; o>=objs; o--){
-		/* if upkeep can't be kept, stop production immediately
-		 * else decrease stock
-		 */
+	for(ol=objhead-1; ol>=objs; ol--){
+		o = *ol;
+		if(o->state <= Swait)
+			continue;
+		if(o->b == nil)
+			sysfatal("empty active tile");
+		memcpy(upkeep, o->b->upkeep, sizeof upkeep);
+		for(r=0; r<nelem(resources); r++)
+			if(upkeep[r] > rstock[r]){
+				starve(o);
+				goto bail;
+			}
+		for(g=0; g<nelem(goods); g++){
+			r = goods[g].resource;
+			if(upkeep[r] <= 0)
+				continue;
+			n = min(stock[g], upkeep[r]);
+			stock[g] -= n;
+			rstock[r] -= n;
+			upkeep[r] -= n;
+		}
+		for(r=0; r<nelem(upkeep); r++)
+			if(upkeep[r] > 0)
+				sysfatal("upkeep: goods/resources phase error");
+	bail:;
 	}
 }
 
@@ -47,33 +87,35 @@
 static void
 updateobj(void)
 {
-/*
-	iterate through obj stack
+	Tile *o, **ol;
 
-	build
-		call for supplies, start travel towards building site
-		done?
-		start production
-	upgrade
-		finished?
-		start production
-	destroy
-		finished?
-		despawn
-	produce
-		call for supplies
-		enough supplies? else wait until next tick (loop)
-		decrement stocks, start traveling from townhall
-		reached building?
-		increment supplies
-		begin production
-		while producing, if we can call for supplies earlier to restart immediately, do it
-		finished producing?
-		start travel towards building
-		reached building?
-		call for pickup
-		loop
-*/
+	for(ol=objhead-1; ol>=objs; ol--){
+		o = *ol;
+		switch(o->state){
+		case Sbuild:
+			/* 
+			call for supplies, start travel towards building site
+			done?
+			start production
+			*/
+		case Sproduce:
+			/*
+			call for supplies
+			enough supplies? else wait until next tick (loop)
+			decrement stocks, start traveling from townhall
+			reached building?
+			increment supplies
+			begin production
+			while producing, if we can call for supplies earlier to restart immediately, do it
+			finished producing?
+			start travel towards building
+			reached building?
+			call for pickup
+			loop
+			*/
+		default: break;
+		}
+	}
 }
 
 void