garten
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 diff
1.1 --- a/Makefile Wed Jul 23 11:41:38 2008 +0200 1.2 +++ b/Makefile Wed Jul 23 15:19:45 2008 +0200 1.3 @@ -22,6 +22,8 @@ 1.4 1.5 # files 1.6 DEP = db.h game.h 1.7 +SRC = main.c clock.c environment.c weather.c growth.c db.c 1.8 +OBJ = $(SRC:.c=.o) 1.9 1.10 1.11 .PHONY: all 1.12 @@ -40,20 +42,11 @@ 1.13 .PHONY: build 1.14 build: options ${NAME} 1.15 1.16 -bin/clock: clock.c $(DEP) 1.17 - $(CC) -o $@ $(LDFLAGS) clock.c 1.18 1.19 -bin/weather: weather.c $(DEP) 1.20 - $(CC) -o $@ $(LDFLAGS) weather.c 1.21 +$(OBJ): $(SRC) $(DEP) 1.22 1.23 -bin/environment: environment.c $(DEP) 1.24 - $(CC) -o $@ $(LDFLAGS) environment.c 1.25 - 1.26 -bin/growth: growth.c $(DEP) 1.27 - $(CC) -o $@ $(LDFLAGS) growth.c 1.28 - 1.29 - 1.30 -${NAME}: bin/clock bin/weather bin/environment bin/growth 1.31 +${NAME}: $(DEP) $(OBJ) 1.32 + gcc -o $(NAME) $(LDFLAGS) $(OBJ) 1.33 1.34 1.35 .PHONY: car 1.36 @@ -63,7 +56,7 @@ 1.37 .PHONY: strip 1.38 strip: ${NAME} 1.39 @echo stripping 1.40 - @strip bin/* 1.41 + @strip * 1.42 1.43 1.44 .PHONY: changelog 1.45 @@ -75,7 +68,7 @@ 1.46 .PHONY: clean 1.47 clean: 1.48 rm -f *.o 1.49 - rm -f bin/* 1.50 + rm -f * 1.51 1.52 1.53 #dist: build changelog
2.1 --- a/clock.c Wed Jul 23 11:41:38 2008 +0200 2.2 +++ b/clock.c Wed Jul 23 15:19:45 2008 +0200 2.3 @@ -6,13 +6,13 @@ 2.4 2.5 2.6 2.7 + 2.8 void inc_time() { 2.9 /* get current time */ 2.10 sprintf(query, "select time from game;"); 2.11 db_query(query); 2.12 if (sqlite3_step(stmt) == SQLITE_ROW) { 2.13 gametime = sqlite3_column_int(stmt, 0); 2.14 - printf("gametime: %d\n", gametime); 2.15 } else { 2.16 fprintf(stderr, "error: %s\n", sqlite3_errmsg(db)); 2.17 } 2.18 @@ -20,46 +20,14 @@ 2.19 2.20 /* increment time */ 2.21 sprintf(query, " update game set time = '%d';", ++gametime); 2.22 - db_query(query); 2.23 - if (sqlite3_step(stmt) == SQLITE_DONE) { 2.24 - printf("time update successful\n"); 2.25 - printf("virtual time: %d\n", gametime); 2.26 - } else { 2.27 + if (!db_update(query)) { 2.28 printf("error: time update failed: %s\n", sqlite3_errmsg(db)); 2.29 } 2.30 sqlite3_finalize(stmt); 2.31 } 2.32 2.33 2.34 -int main(int argc, char* argv[]) { 2.35 - printf(" --> clock\n"); 2.36 - 2.37 - /* init */ 2.38 - if (argc != 2) { 2.39 - printf("usage: %s <game>\n", argv[0]); 2.40 - exit(1); 2.41 - } 2.42 - database = argv[1]; 2.43 - 2.44 - db_connect(); 2.45 - read_time(); 2.46 - 2.47 +void worldclock(void) { 2.48 inc_time(); 2.49 - 2.50 -/* 2.51 - while ((row = mysql_fetch_row(result)) != NULL) { 2.52 - for (i = 0; i < mysql_num_fields(result); i++) { 2.53 - printf("%10s ", row[i]); 2.54 - } 2.55 - printf("\n"); 2.56 - } 2.57 - */ 2.58 - 2.59 - 2.60 - 2.61 - 2.62 - db_close(); 2.63 - 2.64 - printf(" --< clock\n"); 2.65 - return 0; 2.66 + printf("gametime: %d\n", gametime); 2.67 }
3.1 --- a/db.h Wed Jul 23 11:41:38 2008 +0200 3.2 +++ b/db.h Wed Jul 23 15:19:45 2008 +0200 3.3 @@ -1,7 +1,3 @@ 3.4 -/* 3.5 - * data for the database connection 3.6 - */ 3.7 - 3.8 #include <sqlite3.h> 3.9 3.10 char* database; 3.11 @@ -10,43 +6,7 @@ 3.12 sqlite3_stmt* stmt; 3.13 3.14 3.15 - 3.16 -void db_connect() { 3.17 - if (sqlite3_open(database, &db) != SQLITE_OK) { 3.18 - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 3.19 - sqlite3_close(db); 3.20 - exit(1); 3.21 - } 3.22 -} 3.23 - 3.24 - 3.25 -void db_close() { 3.26 - sqlite3_close(db); 3.27 -} 3.28 - 3.29 - 3.30 -/* 3.31 -static int callback(void* NotUsed, int argc, char* argv[], char* azColName[]){ 3.32 - NotUsed = NULL; 3.33 - int i; 3.34 - for (i = 0; i < argc; i++) { 3.35 - printf("%s = %s\n", azColName[i], argv[i] ? argv[i]: "NULL"); 3.36 - } 3.37 - printf("\n"); 3.38 - return 0; 3.39 -} 3.40 - 3.41 -int rc; 3.42 -char* zErrMsg = 0; 3.43 -rc = sqlite3_exec(db, query, callback, 0, &zErrMsg); 3.44 -*/ 3.45 - 3.46 - 3.47 -void db_query(char* query) { 3.48 - int error; 3.49 - 3.50 - error = sqlite3_prepare(db, query, -1, &stmt, NULL); 3.51 - if (error) { 3.52 - fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); 3.53 - } 3.54 -} 3.55 +void db_connect(); 3.56 +void db_close(); 3.57 +sqlite3_stmt* db_query(char* query); 3.58 +int db_update(char* query);
4.1 --- a/environment.c Wed Jul 23 11:41:38 2008 +0200 4.2 +++ b/environment.c Wed Jul 23 15:19:45 2008 +0200 4.3 @@ -1,12 +1,9 @@ 4.4 #include <stdio.h> 4.5 #include <stdlib.h> 4.6 - 4.7 #include "db.h" 4.8 #include "game.h" 4.9 4.10 4.11 - 4.12 - 4.13 void set_environment() { 4.14 int groundwater, slugs, earthworms; 4.15 4.16 @@ -31,34 +28,13 @@ 4.17 (tick, groundwater, slugs, earthworms) \ 4.18 values ('%d', '%d', '%d', '%d') ", 4.19 gametime, groundwater, slugs, earthworms); 4.20 - db_query(query); 4.21 - if (sqlite3_step(stmt) == SQLITE_DONE) { 4.22 - printf("environment successful inserted\n"); 4.23 - } else { 4.24 + if (!db_update(query)) { 4.25 printf("error: environment insertion failed: %s\n", sqlite3_errmsg(db)); 4.26 } 4.27 4.28 } 4.29 4.30 4.31 -int main(int argc, char* argv[]) { 4.32 - printf(" --> environment\n"); 4.33 - 4.34 - /* init */ 4.35 - if (argc != 2) { 4.36 - printf("usage: %s <database>\n", argv[0]); 4.37 - exit(1); 4.38 - } 4.39 - database = argv[1]; 4.40 - 4.41 - db_connect(); 4.42 - read_time(); 4.43 - 4.44 +void environment(void) { 4.45 set_environment(); 4.46 - 4.47 - 4.48 - db_close(); 4.49 - 4.50 - printf(" --< environment\n"); 4.51 - return 0; 4.52 }
5.1 --- a/game.h Wed Jul 23 11:41:38 2008 +0200 5.2 +++ b/game.h Wed Jul 23 15:19:45 2008 +0200 5.3 @@ -1,15 +1,11 @@ 5.4 int gametime; 5.5 5.6 5.7 -int read_time() { 5.8 - sprintf(query, "select time from game;"); 5.9 - db_query(query); 5.10 - if (sqlite3_step(stmt) != SQLITE_ROW) { 5.11 - printf("error: %s\n", sqlite3_errmsg(db)); 5.12 - exit(1); 5.13 - } 5.14 - gametime = sqlite3_column_int(stmt, 0); 5.15 - sqlite3_finalize(stmt); 5.16 - return gametime; 5.17 -} 5.18 +int read_time(); 5.19 5.20 + 5.21 +void worldclock(void); 5.22 +void environment(void); 5.23 +void weather(void); 5.24 +void growth(void); 5.25 +
6.1 --- a/growth.c Wed Jul 23 11:41:38 2008 +0200 6.2 +++ b/growth.c Wed Jul 23 15:19:45 2008 +0200 6.3 @@ -1,68 +1,21 @@ 6.4 #include <stdio.h> 6.5 #include <stdlib.h> 6.6 - 6.7 #include "db.h" 6.8 #include "game.h" 6.9 6.10 6.11 - 6.12 - 6.13 void grow_plants() { 6.14 - int i; 6.15 - unsigned char r[6]; 6.16 - 6.17 /* get weather and last environments to calculate the next one */ 6.18 - sprintf(query, "select\ 6.19 - f.id, f.size, f.age, p.size, p.growspeed, p.age\ 6.20 - from field f\ 6.21 - join plant p on f.plant_id = p.id "); 6.22 - db_query(query); 6.23 - /* printf("number of plants to process: %d\n", (int)mysql_num_rows(result)); */ 6.24 - while (sqlite3_step(stmt) == SQLITE_ROW) { 6.25 - for (i = 0; i < 6; i++) { 6.26 - r[i] = (char) sqlite3_column_int(stmt, i); 6.27 - } 6.28 - 6.29 - sprintf(query, "update field set \ 6.30 - size = '%d', age = '%d' \ 6.31 - where id = '%d' ", 6.32 - (r[1] + 1), r[2]+1, r[0]); 6.33 - db_query(query); 6.34 - ; 6.35 - 6.36 - if (sqlite3_step(stmt) == SQLITE_DONE) { 6.37 - printf("update successful ++ %s\n", query); 6.38 - } else { 6.39 - printf("E: update failed ++ %s\n", sqlite3_errmsg(db)); 6.40 - } 6.41 - 6.42 + sprintf(query, "update field \ 6.43 + set age = age+1, size = size+1 "); 6.44 + stmt = db_query(query); 6.45 + if (!db_update(query)) { 6.46 + printf("E: update failed ++ %s\n", sqlite3_errmsg(db)); 6.47 } 6.48 sqlite3_finalize(stmt); 6.49 - 6.50 - 6.51 - 6.52 - 6.53 } 6.54 6.55 6.56 -int main(int argc, char* argv[]) { 6.57 - printf(" --> %s\n", argv[0]); 6.58 - 6.59 - /* init */ 6.60 - if (argc != 2) { 6.61 - printf("usage: %s <database>\n", argv[0]); 6.62 - exit(1); 6.63 - } 6.64 - database = argv[1]; 6.65 - 6.66 - db_connect(); 6.67 - read_time(); 6.68 - 6.69 +void growth(void) { 6.70 grow_plants(); 6.71 - 6.72 - 6.73 - db_close(); 6.74 - 6.75 - printf(" --< %s\n", argv[0]); 6.76 - return 0; 6.77 }
7.1 --- a/weather.c Wed Jul 23 11:41:38 2008 +0200 7.2 +++ b/weather.c Wed Jul 23 15:19:45 2008 +0200 7.3 @@ -1,7 +1,6 @@ 7.4 #include <stdio.h> 7.5 #include <stdlib.h> 7.6 #include <time.h> 7.7 - 7.8 #include "db.h" 7.9 #include "game.h" 7.10 7.11 @@ -88,41 +87,18 @@ 7.12 " (tick, temp, sun, rain, wind, hum) " 7.13 " values ('%d', '%f', '%f', '%f', '%f', '%f') " 7.14 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum); 7.15 - db_query(query); 7.16 - puts(query); 7.17 - if (sqlite3_step(stmt) == SQLITE_DONE) { 7.18 - printf("weather successful inserted\n"); 7.19 - } else { 7.20 + /* puts(query); */ 7.21 + if (!db_update(query)) { 7.22 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db)); 7.23 } 7.24 } 7.25 7.26 7.27 -int main(int argc, char* argv[]) { 7.28 - printf(" --> weather\n"); 7.29 - 7.30 - /* init */ 7.31 - if (argc != 2) { 7.32 - printf("usage: %s <database>\n", argv[0]); 7.33 - exit(1); 7.34 - } 7.35 - database = argv[1]; 7.36 - 7.37 - db_connect(); 7.38 - read_time(); 7.39 - 7.40 - srand((unsigned int) time(NULL)); 7.41 - 7.42 - 7.43 +void weather(void) { 7.44 struct weather w; 7.45 struct weather lastn[Nlast]; 7.46 7.47 getlastweather(lastn, Nlast); 7.48 genweather(&w, lastn, Nlast, May); 7.49 setweather(&w); 7.50 - 7.51 - db_close(); 7.52 - 7.53 - printf(" --< weather\n"); 7.54 - return 0; 7.55 }