garten

view weather.c @ 15:082566ce7d10

added yet unversioned but needed file db.c
author meillo@marmaro.de
date Wed, 23 Jul 2008 16:45:56 +0200
parents 176ee28e7464
children 5e6c9260913a
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include "db.h"
5 #include "game.h"
8 enum {
9 Nlast = 2,
10 };
12 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
14 struct weather {
15 float temp;
16 float sun;
17 float rain;
18 float wind;
19 float hum;
20 };
22 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};
23 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};
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};
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};
29 float rand_limit(float limit) {
30 /* generates random number between -limit and +limit */
31 int r;
33 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
34 return r - limit;
35 }
40 void getlastweather(struct weather lastw[], int nlast) {
41 char query[512];
42 int i;
44 sprintf(query,
45 " select temp, sun, rain, wind, hum from weather "
46 " order by tick desc "
47 " limit %d "
48 , nlast);
49 db_query(query);
50 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
51 lastw[i].temp = sqlite3_column_double(stmt, 0);
52 lastw[i].sun = sqlite3_column_double(stmt, 1);
53 lastw[i].rain = sqlite3_column_double(stmt, 2);
54 lastw[i].wind = sqlite3_column_double(stmt, 3);
55 lastw[i].hum = sqlite3_column_double(stmt, 4);
56 }
57 sqlite3_finalize(stmt);
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 */
71 }
74 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);
76 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);
77 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);
78 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);
79 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);
80 }
83 void setweather(struct weather* w) {
84 char query[512];
85 sprintf(query,
86 " insert into weather "
87 " (tick, temp, sun, rain, wind, hum) "
88 " values ('%d', '%f', '%f', '%f', '%f', '%f') "
89 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
90 /* puts(query); */
91 if (!db_update(query)) {
92 printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
93 }
94 }
97 void weather(void) {
98 struct weather w;
99 struct weather lastn[Nlast];
101 getlastweather(lastn, Nlast);
102 genweather(&w, lastn, Nlast, May);
103 setweather(&w);
104 }