changeset 12:8db6497d6065

merged everything to only one program
author meillo@marmaro.de
date Wed, 23 Jul 2008 15:19:45 +0200
parents 176ee28e7464
children f1d43b52ed36
files Makefile clock.c db.h environment.c game.h growth.c weather.c
diffstat 7 files changed, 33 insertions(+), 211 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Jul 23 11:41:38 2008 +0200
+++ b/Makefile	Wed Jul 23 15:19:45 2008 +0200
@@ -22,6 +22,8 @@
 
 # files
 DEP = db.h game.h
+SRC = main.c clock.c environment.c weather.c growth.c db.c
+OBJ = $(SRC:.c=.o)
 
 
 .PHONY: all
@@ -40,20 +42,11 @@
 .PHONY: build
 build: options ${NAME}
 
-bin/clock: clock.c $(DEP)
-	$(CC) -o $@ $(LDFLAGS) clock.c
 
-bin/weather: weather.c $(DEP)
-	$(CC) -o $@ $(LDFLAGS) weather.c
+$(OBJ): $(SRC) $(DEP)
 
-bin/environment: environment.c $(DEP)
-	$(CC) -o $@ $(LDFLAGS) environment.c
-
-bin/growth: growth.c $(DEP)
-	$(CC) -o $@ $(LDFLAGS) growth.c
-
-
-${NAME}: bin/clock bin/weather bin/environment bin/growth
+${NAME}: $(DEP) $(OBJ)
+	gcc -o $(NAME) $(LDFLAGS) $(OBJ)
 
 
 .PHONY: car
@@ -63,7 +56,7 @@
 .PHONY: strip
 strip: ${NAME}
 	@echo stripping
-	@strip bin/*
+	@strip *
 
 
 .PHONY: changelog
@@ -75,7 +68,7 @@
 .PHONY: clean
 clean:
 	rm -f *.o
-	rm -f bin/*
+	rm -f *
 
 
 #dist: build changelog
--- a/clock.c	Wed Jul 23 11:41:38 2008 +0200
+++ b/clock.c	Wed Jul 23 15:19:45 2008 +0200
@@ -6,13 +6,13 @@
 
 
 
+
 void inc_time() {
 	/* get current time */
 	sprintf(query, "select time from game;");
 	db_query(query);
 	if (sqlite3_step(stmt) == SQLITE_ROW) {
 		gametime = sqlite3_column_int(stmt, 0);
-		printf("gametime: %d\n", gametime);
 	} else {
 		fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
 	}
@@ -20,46 +20,14 @@
 
 	/* increment time */
 	sprintf(query, " update game set time = '%d';", ++gametime);
-	db_query(query);
-	if (sqlite3_step(stmt) == SQLITE_DONE) {
-		printf("time update successful\n");
-		printf("virtual time: %d\n", gametime);
-	} else {
+	if (!db_update(query)) {
 		printf("error: time update failed: %s\n", sqlite3_errmsg(db));
 	}
 	sqlite3_finalize(stmt);
 }
 
 
-int main(int argc, char* argv[]) {
-	printf("  --> clock\n");
-
-	/* init */
-	if (argc != 2) {
-		printf("usage: %s <game>\n", argv[0]);
-		exit(1);
-	}
-	database = argv[1];
-
-	db_connect();
-	read_time();
-
+void worldclock(void) {
 	inc_time();
-
-/*
-	while ((row = mysql_fetch_row(result)) != NULL) {
-		for (i = 0; i < mysql_num_fields(result); i++) {
-			printf("%10s  ", row[i]);
-		}
-		printf("\n");
-	}
-	*/
-
-
-
-
-	db_close();
-
-	printf("  --< clock\n");
-	return 0;
+	printf("gametime: %d\n", gametime);
 }
--- a/db.h	Wed Jul 23 11:41:38 2008 +0200
+++ b/db.h	Wed Jul 23 15:19:45 2008 +0200
@@ -1,7 +1,3 @@
-/*
- * data for the database connection
- */
-
 #include <sqlite3.h>
 
 char* database;
@@ -10,43 +6,7 @@
 sqlite3_stmt* stmt;
 
 
-
-void db_connect() {
-	if (sqlite3_open(database, &db) != SQLITE_OK) {
-		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
-		sqlite3_close(db);
-		exit(1);
-	}
-}
-
-
-void db_close() {
-	sqlite3_close(db);
-}
-
-
-/*
-static int callback(void* NotUsed, int argc, char* argv[], char* azColName[]){
-	NotUsed = NULL;
-	int i;
-	for (i = 0; i < argc; i++) {
-		printf("%s = %s\n", azColName[i], argv[i] ? argv[i]: "NULL");
-	}
-	printf("\n");
-	return 0;
-}
-
-int rc;
-char* zErrMsg = 0;
-rc = sqlite3_exec(db, query, callback, 0, &zErrMsg);
-*/
-
-
-void db_query(char* query) {
-	int error;
-
-	error = sqlite3_prepare(db, query, -1, &stmt, NULL);
-	if (error) {
-		fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
-	}
-}
+void db_connect();
+void db_close();
+sqlite3_stmt* db_query(char* query);
+int db_update(char* query);
--- a/environment.c	Wed Jul 23 11:41:38 2008 +0200
+++ b/environment.c	Wed Jul 23 15:19:45 2008 +0200
@@ -1,12 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
-
 #include "db.h"
 #include "game.h"
 
 
-
-
 void set_environment() {
 	int groundwater, slugs, earthworms;
 
@@ -31,34 +28,13 @@
 			(tick, groundwater, slugs, earthworms) \
 			values ('%d', '%d', '%d', '%d') ",
 			gametime, groundwater, slugs, earthworms);
-	db_query(query);
-	if (sqlite3_step(stmt) == SQLITE_DONE) {
-		printf("environment successful inserted\n");
-	} else {
+	if (!db_update(query)) {
 		printf("error: environment insertion failed: %s\n", sqlite3_errmsg(db));
 	}
 
 }
 
 
-int main(int argc, char* argv[]) {
-	printf("  --> environment\n");
-
-	/* init */
-	if (argc != 2) {
-		printf("usage: %s <database>\n", argv[0]);
-		exit(1);
-	}
-	database = argv[1];
-
-	db_connect();
-	read_time();
-
+void environment(void) {
 	set_environment();
-
-
-	db_close();
-
-	printf("  --< environment\n");
-	return 0;
 }
--- a/game.h	Wed Jul 23 11:41:38 2008 +0200
+++ b/game.h	Wed Jul 23 15:19:45 2008 +0200
@@ -1,15 +1,11 @@
 int gametime;
 
 
-int read_time() {
-	sprintf(query, "select time from game;");
-	db_query(query);
-	if (sqlite3_step(stmt) != SQLITE_ROW) {
-		printf("error: %s\n", sqlite3_errmsg(db));
-		exit(1);
-	}
-	gametime = sqlite3_column_int(stmt, 0);
-	sqlite3_finalize(stmt);
-	return gametime;
-}
+int read_time();
+
 
+void worldclock(void);
+void environment(void);
+void weather(void);
+void growth(void);
+
--- a/growth.c	Wed Jul 23 11:41:38 2008 +0200
+++ b/growth.c	Wed Jul 23 15:19:45 2008 +0200
@@ -1,68 +1,21 @@
 #include <stdio.h>
 #include <stdlib.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 ");
-	db_query(query);
-	/* printf("number of plants to process: %d\n", (int)mysql_num_rows(result)); */
-	while (sqlite3_step(stmt) == SQLITE_ROW) {
-		for (i = 0; i < 6; i++) {
-			r[i] = (char) sqlite3_column_int(stmt, 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 (sqlite3_step(stmt) == SQLITE_DONE) {
-			printf("update successful  ++  %s\n", query);
-		} else {
-			printf("E: update failed  ++  %s\n", sqlite3_errmsg(db));
-		}
-
+	sprintf(query, "update field \
+			set age = age+1, size = size+1 ");
+	stmt = db_query(query);
+	if (!db_update(query)) {
+		printf("E: update failed  ++  %s\n", sqlite3_errmsg(db));
 	}
 	sqlite3_finalize(stmt);
-
-
-
-
 }
 
 
-int main(int argc, char* argv[]) {
-	printf("  --> %s\n", argv[0]);
-
-	/* init */
-	if (argc != 2) {
-		printf("usage: %s <database>\n", argv[0]);
-		exit(1);
-	}
-	database = argv[1];
-
-	db_connect();
-	read_time();
-
+void growth(void) {
 	grow_plants();
-
-
-	db_close();
-
-	printf("  --< %s\n", argv[0]);
-	return 0;
 }
--- a/weather.c	Wed Jul 23 11:41:38 2008 +0200
+++ b/weather.c	Wed Jul 23 15:19:45 2008 +0200
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
-
 #include "db.h"
 #include "game.h"
 
@@ -88,41 +87,18 @@
 			" (tick, temp, sun, rain, wind, hum) "
 			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
 			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
-	db_query(query);
-	puts(query);
-	if (sqlite3_step(stmt) == SQLITE_DONE) {
-		printf("weather successful inserted\n");
-	} else {
+	/* puts(query); */
+	if (!db_update(query)) {
 		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
 	}
 }
 
 
-int main(int argc, char* argv[]) {
-	printf("  --> weather\n");
-
-	/* init */
-	if (argc != 2) {
-		printf("usage: %s <database>\n", argv[0]);
-		exit(1);
-	}
-	database = argv[1];
-
-	db_connect();
-	read_time();
-
-	srand((unsigned int) time(NULL));
-
-
+void weather(void) {
 	struct weather w;
 	struct weather lastn[Nlast];
 
 	getlastweather(lastn, Nlast);
 	genweather(&w, lastn, Nlast, May);
 	setweather(&w);
-
-	db_close();
-
-	printf("  --< weather\n");
-	return 0;
 }