garten

changeset 17:5e6c9260913a

lots of cleanups and small fixes
author meillo@marmaro.de
date Wed, 23 Jul 2008 17:14:38 +0200
parents 3c104b5f5158
children 5937504619f2
files clock.c db.c db.h environment.c game.h growth.c weather.c
diffstat 7 files changed, 50 insertions(+), 44 deletions(-) [+]
line diff
     1.1 --- a/clock.c	Wed Jul 23 16:49:10 2008 +0200
     1.2 +++ b/clock.c	Wed Jul 23 17:14:38 2008 +0200
     1.3 @@ -1,29 +1,29 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 -
     1.7  #include "db.h"
     1.8  #include "game.h"
     1.9  
    1.10  
    1.11 -
    1.12 -
    1.13  void inc_time() {
    1.14  	/* get current time */
    1.15  	sprintf(query, "select time from game;");
    1.16 -	db_query(query);
    1.17 +	stmt = db_query(query);
    1.18  	if (sqlite3_step(stmt) == SQLITE_ROW) {
    1.19  		gametime = sqlite3_column_int(stmt, 0);
    1.20  	} else {
    1.21 -		fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
    1.22 +		db_error("get current time");
    1.23  	}
    1.24  	sqlite3_finalize(stmt);
    1.25  
    1.26  	/* increment time */
    1.27 -	sprintf(query, " update game set time = '%d';", ++gametime);
    1.28 +	sprintf(query,
    1.29 +			"update game"
    1.30 +			"set time = '%d';"
    1.31 +			, ++gametime
    1.32 +			);
    1.33  	if (!db_update(query)) {
    1.34 -		printf("error: time update failed: %s\n", sqlite3_errmsg(db));
    1.35 +		db_error("time update");
    1.36  	}
    1.37 -	sqlite3_finalize(stmt);
    1.38  }
    1.39  
    1.40  
     2.1 --- a/db.c	Wed Jul 23 16:49:10 2008 +0200
     2.2 +++ b/db.c	Wed Jul 23 17:14:38 2008 +0200
     2.3 @@ -45,6 +45,18 @@
     2.4  }
     2.5  
     2.6  
     2.7 +int db_rows(sqlite3_stmt* stmt) {
     2.8 +	int i = 0;
     2.9 +
    2.10 +	sqlite3_reset(stmt);
    2.11 +	while (sqlite3_step(stmt) == SQLITE_ROW) {
    2.12 +		i++;
    2.13 +	}
    2.14 +	sqlite3_reset(stmt);
    2.15 +	return i;
    2.16 +}
    2.17 +
    2.18 +
    2.19  int read_time() {
    2.20  	sprintf(query, "select time from game;");
    2.21  	stmt = db_query(query);
    2.22 @@ -57,3 +69,7 @@
    2.23  	return gametime;
    2.24  }
    2.25  
    2.26 +
    2.27 +void db_error(char* info) {
    2.28 +	fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db));
    2.29 +}
     3.1 --- a/db.h	Wed Jul 23 16:49:10 2008 +0200
     3.2 +++ b/db.h	Wed Jul 23 17:14:38 2008 +0200
     3.3 @@ -1,12 +1,16 @@
     3.4  #include <sqlite3.h>
     3.5  
     3.6  char* database;
     3.7 +int gametime;
     3.8  char query[1024];
     3.9  sqlite3* db;
    3.10  sqlite3_stmt* stmt;
    3.11  
    3.12 +int read_time();
    3.13  
    3.14  void db_connect();
    3.15  void db_close();
    3.16  sqlite3_stmt* db_query(char* query);
    3.17  int db_update(char* query);
    3.18 +int db_rows(sqlite3_stmt* stmt);
    3.19 +void db_error(char* info);
     4.1 --- a/environment.c	Wed Jul 23 16:49:10 2008 +0200
     4.2 +++ b/environment.c	Wed Jul 23 17:14:38 2008 +0200
     4.3 @@ -24,12 +24,17 @@
     4.4  
     4.5  
     4.6  	/* set weather */
     4.7 -	sprintf(query, " insert into environment \
     4.8 -			(tick, groundwater, slugs, earthworms) \
     4.9 -			values ('%d', '%d', '%d', '%d') ",
    4.10 -			gametime, groundwater, slugs, earthworms);
    4.11 +	sprintf(query,
    4.12 +			"insert into environment "
    4.13 +			"(tick, groundwater, slugs, earthworms) "
    4.14 +			"values ('%d', '%d', '%d', '%d') "
    4.15 +			, gametime
    4.16 +			, groundwater
    4.17 +			, slugs
    4.18 +			, earthworms
    4.19 +			);
    4.20  	if (!db_update(query)) {
    4.21 -		printf("error: environment insertion failed: %s\n", sqlite3_errmsg(db));
    4.22 +		db_error("environment insertion");
    4.23  	}
    4.24  
    4.25  }
     5.1 --- a/game.h	Wed Jul 23 16:49:10 2008 +0200
     5.2 +++ b/game.h	Wed Jul 23 17:14:38 2008 +0200
     5.3 @@ -1,9 +1,3 @@
     5.4 -int gametime;
     5.5 -
     5.6 -
     5.7 -int read_time();
     5.8 -
     5.9 -
    5.10  void worldclock(void);
    5.11  void environment(void);
    5.12  void weather(void);
     6.1 --- a/growth.c	Wed Jul 23 16:49:10 2008 +0200
     6.2 +++ b/growth.c	Wed Jul 23 17:14:38 2008 +0200
     6.3 @@ -5,14 +5,13 @@
     6.4  
     6.5  
     6.6  void grow_plants() {
     6.7 -	/* get weather and last environments to calculate the next one */
     6.8 -	sprintf(query, "update field \
     6.9 -			set age = age+1, size = size+1 ");
    6.10 -	stmt = db_query(query);
    6.11 +	sprintf(query,
    6.12 +			"update field "
    6.13 +			"set age = age+1, size = size+1 "
    6.14 +			);
    6.15  	if (!db_update(query)) {
    6.16 -		printf("E: update failed  ++  %s\n", sqlite3_errmsg(db));
    6.17 +		db_error("grow plants");
    6.18  	}
    6.19 -	sqlite3_finalize(stmt);
    6.20  }
    6.21  
    6.22  
     7.1 --- a/weather.c	Wed Jul 23 16:49:10 2008 +0200
     7.2 +++ b/weather.c	Wed Jul 23 17:14:38 2008 +0200
     7.3 @@ -26,8 +26,8 @@
     7.4  float mean_hum[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0};
     7.5  
     7.6  
     7.7 +/* generates random number between -limit and +limit */
     7.8  float rand_limit(float limit) {
     7.9 -	/* generates random number between -limit and +limit */
    7.10  	int r;
    7.11  
    7.12  	r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
    7.13 @@ -45,8 +45,9 @@
    7.14  			" select temp, sun, rain, wind, hum from weather "
    7.15  			" order by tick desc "
    7.16  			" limit %d "
    7.17 -			, nlast);
    7.18 -	db_query(query);
    7.19 +			, nlast
    7.20 +			);
    7.21 +	stmt = db_query(query);
    7.22  	for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
    7.23  		lastw[i].temp = sqlite3_column_double(stmt, 0);
    7.24  		lastw[i].sun = sqlite3_column_double(stmt, 1);
    7.25 @@ -55,19 +56,6 @@
    7.26  		lastw[i].hum = sqlite3_column_double(stmt, 4);
    7.27  	}
    7.28  	sqlite3_finalize(stmt);
    7.29 -
    7.30 -	/*
    7.31 -	result = mysql_store_result(conn);
    7.32 -	for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
    7.33 -		lastw[i].temp = atof(row[0]);
    7.34 -		lastw[i].sun = atof(row[1]);
    7.35 -		lastw[i].rain = atof(row[2]);
    7.36 -		lastw[i].wind = atof(row[3]);
    7.37 -		lastw[i].hum = atof(row[4]);
    7.38 -	}
    7.39 -	mysql_free_result(result);
    7.40 -	*/
    7.41 -
    7.42  }
    7.43  
    7.44  
    7.45 @@ -86,10 +74,10 @@
    7.46  			" insert into weather "
    7.47  			" (tick, temp, sun, rain, wind, hum) "
    7.48  			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
    7.49 -			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
    7.50 -	/* puts(query); */
    7.51 +			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum
    7.52 +			);
    7.53  	if (!db_update(query)) {
    7.54 -		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
    7.55 +		db_error("weather insertion");
    7.56  	}
    7.57  }
    7.58