garten

view weather.c @ 19:eb8db0d906de

updated runtick.sh to current program
author meillo@marmaro.de
date Wed, 23 Jul 2008 17:44:15 +0200
parents 5e6c9260913a
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include "db.h"
7 enum {
8 Nlast = 2,
9 };
11 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
13 struct weather {
14 float temp;
15 float sun;
16 float rain;
17 float wind;
18 float hum;
19 };
21 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};
22 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};
23 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_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_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};
28 /* generates random number between -limit and +limit */
29 float rand_limit(float limit) {
30 int r;
32 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
33 return r - limit;
34 }
39 void getlastweather(struct weather lastw[], int nlast) {
40 char query[512];
41 int i;
43 sprintf(query,
44 " select temp, sun, rain, wind, hum from weather "
45 " order by tick desc "
46 " limit %d "
47 , nlast
48 );
49 stmt = 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);
58 }
61 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
62 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->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);
64 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);
65 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);
66 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);
67 }
70 int setweather(struct weather* w) {
71 char query[512];
72 sprintf(query,
73 " insert into weather "
74 " (tick, temp, sun, rain, wind, hum) "
75 " values ('%d', '%f', '%f', '%f', '%f', '%f') "
76 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum
77 );
78 if (!db_update(query)) {
79 db_error("weather insertion");
80 return 0;
81 }
82 return 1;
83 }
86 int weather(void) {
87 struct weather w;
88 struct weather lastn[Nlast];
90 getlastweather(lastn, Nlast);
91 genweather(&w, lastn, Nlast, May);
92 return setweather(&w);
93 /* FIXME: include all functions in return value */
94 }