annotate weather.c @ 20:17b2bcc42d72 default tip

added check for empty result; minor stuff
author meillo@marmaro.de
date Sun, 27 Jul 2008 21:34:54 +0200
parents 5937504619f2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
1 #include <stdio.h>
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
2 #include <stdlib.h>
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
3 #include <time.h>
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
4 #include "db.h"
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
5
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
6
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
7 enum {
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
8 Nlast = 2,
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
9 };
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
10
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
11 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
12
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
13 struct weather {
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
14 float temp;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
15 float sun;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
16 float rain;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
17 float wind;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
18 float hum;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
19 };
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
20
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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};
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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};
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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};
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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};
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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};
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
26
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
27
17
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
28 /* generates random number between -limit and +limit */
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
29 float rand_limit(float limit) {
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
30 int r;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
31
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
32 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
33 return r - limit;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
34 }
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
35
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
36
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
37
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
38
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
39 void getlastweather(struct weather lastw[], int nlast) {
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
40 char query[512];
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
41 int i;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
42
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
43 sprintf(query,
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
44 " select temp, sun, rain, wind, hum from weather "
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
45 " order by tick desc "
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
46 " limit %d "
17
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
47 , nlast
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
48 );
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
49 stmt = db_query(query);
11
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
50 for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
51 lastw[i].temp = sqlite3_column_double(stmt, 0);
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
52 lastw[i].sun = sqlite3_column_double(stmt, 1);
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
53 lastw[i].rain = sqlite3_column_double(stmt, 2);
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
54 lastw[i].wind = sqlite3_column_double(stmt, 3);
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
55 lastw[i].hum = sqlite3_column_double(stmt, 4);
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
56 }
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
57 sqlite3_finalize(stmt);
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
58 }
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
59
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
60
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
61 void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
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);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
67 }
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
68
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
69
18
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
70 int setweather(struct weather* w) {
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
71 char query[512];
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
72 sprintf(query,
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
73 " insert into weather "
11
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
74 " (tick, temp, sun, rain, wind, hum) "
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
75 " values ('%d', '%f', '%f', '%f', '%f', '%f') "
17
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
76 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
77 );
12
8db6497d6065 merged everything to only one program
meillo@marmaro.de
parents: 11
diff changeset
78 if (!db_update(query)) {
17
5e6c9260913a lots of cleanups and small fixes
meillo@marmaro.de
parents: 12
diff changeset
79 db_error("weather insertion");
18
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
80 return 0;
11
176ee28e7464 switched from mysql to sqlite; (+ some cleanups)
meillo@marmaro.de
parents: 8
diff changeset
81 }
18
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
82 return 1;
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
83 }
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
84
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
85
18
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
86 int weather(void) {
8
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
87 struct weather w;
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
88 struct weather lastn[Nlast];
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
89
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
90 getlastweather(lastn, Nlast);
9bd0a2100694 new weather implementation
meillo@marmaro.de
parents: 3
diff changeset
91 genweather(&w, lastn, Nlast, May);
18
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
92 return setweather(&w);
5937504619f2 rename game.h -> modules.h; added some error handling; and more
meillo@marmaro.de
parents: 17
diff changeset
93 /* FIXME: include all functions in return value */
3
0c19ad487f02 first weather implementation (static values)
meillo@marmaro.de
parents:
diff changeset
94 }