garten

view weather.c @ 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 9bd0a2100694
children 8db6497d6065
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
5 #include "db.h"
6 #include "game.h"
9 enum {
10 Nlast = 2,
11 };
13 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
15 struct weather {
16 float temp;
17 float sun;
18 float rain;
19 float wind;
20 float hum;
21 };
23 float mean_temp[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};
24 float mean_sun[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};
25 float mean_rain[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};
26 float mean_wind[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};
27 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};
30 float rand_limit(float limit) {
31 /* generates random number between -limit and +limit */
32 int r;
34 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
35 return r - limit;
36 }
41 void getlastweather(struct weather lastw[], int nlast) {
42 char query[512];
43 int i;
45 sprintf(query,
46 " select temp, sun, rain, wind, hum from weather "
47 " order by tick desc "
48 " limit %d "
49 , nlast);
50 db_query(query);
51 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
52 lastw[i].temp = sqlite3_column_double(stmt, 0);
53 lastw[i].sun = sqlite3_column_double(stmt, 1);
54 lastw[i].rain = sqlite3_column_double(stmt, 2);
55 lastw[i].wind = sqlite3_column_double(stmt, 3);
56 lastw[i].hum = sqlite3_column_double(stmt, 4);
57 }
58 sqlite3_finalize(stmt);
60 /*
61 result = mysql_store_result(conn);
62 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
63 lastw[i].temp = atof(row[0]);
64 lastw[i].sun = atof(row[1]);
65 lastw[i].rain = atof(row[2]);
66 lastw[i].wind = atof(row[3]);
67 lastw[i].hum = atof(row[4]);
68 }
69 mysql_free_result(result);
70 */
72 }
75 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
76 w->temp = 0.2 * mean_temp[month] + 0.4 * lastw[0].temp + 0.2 * lastw[1].temp + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
77 w->sun = 0.2 * mean_sun[month] + 0.4 * lastw[0].sun + 0.2 * lastw[1].sun + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
78 w->rain = 0.2 * mean_rain[month] + 0.4 * lastw[0].rain + 0.2 * lastw[1].rain + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
79 w->wind = 0.2 * mean_wind[month] + 0.4 * lastw[0].wind + 0.2 * lastw[1].wind + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
80 w->hum = 0.2 * mean_hum[month] + 0.4 * lastw[0].hum + 0.2 * lastw[1].hum + 0.08 * rand_limit(10); // + 0.02 * rand_limit(100);
81 }
84 void setweather(struct weather* w) {
85 char query[512];
86 sprintf(query,
87 " insert into weather "
88 " (tick, temp, sun, rain, wind, hum) "
89 " values ('%d', '%f', '%f', '%f', '%f', '%f') "
90 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
91 db_query(query);
92 puts(query);
93 if (sqlite3_step(stmt) == SQLITE_DONE) {
94 printf("weather successful inserted\n");
95 } else {
96 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
97 }
98 }
101 int main(int argc, char* argv[]) {
102 printf(" --> weather\n");
104 /* init */
105 if (argc != 2) {
106 printf("usage: %s <database>\n", argv[0]);
107 exit(1);
108 }
109 database = argv[1];
111 db_connect();
112 read_time();
114 srand((unsigned int) time(NULL));
117 struct weather w;
118 struct weather lastn[Nlast];
120 getlastweather(lastn, Nlast);
121 genweather(&w, lastn, Nlast, May);
122 setweather(&w);
124 db_close();
126 printf(" --< weather\n");
127 return 0;
128 }