changeset 7:b1e309dc0b98

added growth module (quite static and limited implementation)
author meillo@marmaro.de
date Thu, 15 May 2008 21:42:01 +0200
parents e3679f888813
children 9bd0a2100694
files Makefile growth.c runtick.sh
diffstat 3 files changed, 76 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed May 14 21:59:35 2008 +0200
+++ b/Makefile	Thu May 15 21:42:01 2008 +0200
@@ -49,8 +49,11 @@
 bin/environment: environment.c db.h game.h
 	$(CC) -o $@ $(LDFLAGS) `mysql_config --cflags` environment.c `mysql_config --libs`
 
+bin/growth: growth.c db.h game.h
+	$(CC) -o $@ $(LDFLAGS) `mysql_config --cflags` growth.c `mysql_config --libs`
 
-${NAME}: bin/clock bin/weather bin/environment
+
+${NAME}: bin/clock bin/weather bin/environment bin/growth
 
 
 #${OBJ}: ${DEP}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/growth.c	Thu May 15 21:42:01 2008 +0200
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <mysql.h>
+
+#include "db.h"
+#include "game.h"
+
+
+
+
+void grow_plants() {
+	int i;
+	unsigned char r[6];
+
+	/* get weather and last environments to calculate the next one */
+	sprintf(query, "select\
+			f.id, f.size, f.age, p.size, p.growspeed, p.age\
+			from field f\
+			join plant p on f.plant_id = p.id\
+			where f.game_id = '%d' ",
+			gameid);
+	db_query(query);
+	result = mysql_store_result(conn);
+	printf("number of plants to process: %d\n", (int)mysql_num_rows(result));
+	while ((row = mysql_fetch_row(result)) != NULL) {
+		for (i = 0; i < 6; i++) {
+			r[i] = atoi(row[i]);
+		}
+
+		sprintf(query, "update field set \
+				size = '%d', age = '%d' \
+				where id = '%d' ",
+				(r[1] + 1), r[2]+1, r[0]);
+		db_query(query);
+
+		if (mysql_affected_rows(conn) > 0) {
+			printf("update successful  ++  %s\n", query);
+		} else {
+			printf("E: update failed  ++  %s\n", query);
+		}
+
+	}
+	mysql_free_result(result);
+
+
+
+
+}
+
+
+int main(int argc, char* argv[]) {
+	printf("  --> %s\n", argv[0]);
+
+	/* init */
+	if (argc != 2) {
+		printf("usage: %s <game>\n", argv[0]);
+		exit(1);
+	}
+	gamename = argv[1];
+
+	db_connect();
+	check_game();
+
+	grow_plants();
+
+
+	db_close();
+
+	printf("  --< %s\n", argv[0]);
+	return 0;
+}
--- a/runtick.sh	Wed May 14 21:59:35 2008 +0200
+++ b/runtick.sh	Thu May 15 21:42:01 2008 +0200
@@ -17,7 +17,7 @@
 ./bin/weather $gamename
 ./bin/environment $gamename
 #./bin/market $gamename
-#./bin/growth $gamename
+./bin/growth $gamename
 #./bin/orderexec $gamename
 
 echo "finished backend cycle at `date +%F\ %H:%M:%S`"