Mercurial > garten
comparison weather.c @ 17:5e6c9260913a
lots of cleanups and small fixes
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 17:14:38 +0200 |
parents | 8db6497d6065 |
children | 5937504619f2 |
comparison
equal
deleted
inserted
replaced
16:3c104b5f5158 | 17:5e6c9260913a |
---|---|
24 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}; | 24 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}; |
25 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}; | 25 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}; |
26 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}; | 26 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}; |
27 | 27 |
28 | 28 |
29 /* generates random number between -limit and +limit */ | |
29 float rand_limit(float limit) { | 30 float rand_limit(float limit) { |
30 /* generates random number between -limit and +limit */ | |
31 int r; | 31 int r; |
32 | 32 |
33 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); | 33 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); |
34 return r - limit; | 34 return r - limit; |
35 } | 35 } |
43 | 43 |
44 sprintf(query, | 44 sprintf(query, |
45 " select temp, sun, rain, wind, hum from weather " | 45 " select temp, sun, rain, wind, hum from weather " |
46 " order by tick desc " | 46 " order by tick desc " |
47 " limit %d " | 47 " limit %d " |
48 , nlast); | 48 , nlast |
49 db_query(query); | 49 ); |
50 stmt = db_query(query); | |
50 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) { | 51 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) { |
51 lastw[i].temp = sqlite3_column_double(stmt, 0); | 52 lastw[i].temp = sqlite3_column_double(stmt, 0); |
52 lastw[i].sun = sqlite3_column_double(stmt, 1); | 53 lastw[i].sun = sqlite3_column_double(stmt, 1); |
53 lastw[i].rain = sqlite3_column_double(stmt, 2); | 54 lastw[i].rain = sqlite3_column_double(stmt, 2); |
54 lastw[i].wind = sqlite3_column_double(stmt, 3); | 55 lastw[i].wind = sqlite3_column_double(stmt, 3); |
55 lastw[i].hum = sqlite3_column_double(stmt, 4); | 56 lastw[i].hum = sqlite3_column_double(stmt, 4); |
56 } | 57 } |
57 sqlite3_finalize(stmt); | 58 sqlite3_finalize(stmt); |
58 | |
59 /* | |
60 result = mysql_store_result(conn); | |
61 for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { | |
62 lastw[i].temp = atof(row[0]); | |
63 lastw[i].sun = atof(row[1]); | |
64 lastw[i].rain = atof(row[2]); | |
65 lastw[i].wind = atof(row[3]); | |
66 lastw[i].hum = atof(row[4]); | |
67 } | |
68 mysql_free_result(result); | |
69 */ | |
70 | |
71 } | 59 } |
72 | 60 |
73 | 61 |
74 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { | 62 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { |
75 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); | 63 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); |
84 char query[512]; | 72 char query[512]; |
85 sprintf(query, | 73 sprintf(query, |
86 " insert into weather " | 74 " insert into weather " |
87 " (tick, temp, sun, rain, wind, hum) " | 75 " (tick, temp, sun, rain, wind, hum) " |
88 " values ('%d', '%f', '%f', '%f', '%f', '%f') " | 76 " values ('%d', '%f', '%f', '%f', '%f', '%f') " |
89 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum); | 77 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum |
90 /* puts(query); */ | 78 ); |
91 if (!db_update(query)) { | 79 if (!db_update(query)) { |
92 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db)); | 80 db_error("weather insertion"); |
93 } | 81 } |
94 } | 82 } |
95 | 83 |
96 | 84 |
97 void weather(void) { | 85 void weather(void) { |