Mercurial > garten
annotate weather.c @ 19:eb8db0d906de
updated runtick.sh to current program
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 17:44:15 +0200 |
parents | 5937504619f2 |
children |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
8 | 3 #include <time.h> |
3 | 4 #include "db.h" |
5 | |
6 | |
8 | 7 enum { |
8 Nlast = 2, | |
9 }; | |
10 | |
11 enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; | |
12 | |
13 struct weather { | |
14 float temp; | |
15 float sun; | |
16 float rain; | |
17 float wind; | |
18 float hum; | |
19 }; | |
20 | |
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}; | |
3 | 26 |
27 | |
17 | 28 /* generates random number between -limit and +limit */ |
8 | 29 float rand_limit(float limit) { |
30 int r; | |
31 | |
32 r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); | |
33 return r - limit; | |
34 } | |
35 | |
36 | |
37 | |
3 | 38 |
8 | 39 void getlastweather(struct weather lastw[], int nlast) { |
40 char query[512]; | |
41 int i; | |
42 | |
43 sprintf(query, | |
44 " select temp, sun, rain, wind, hum from weather " | |
45 " order by tick desc " | |
46 " limit %d " | |
17 | 47 , nlast |
48 ); | |
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 | 58 } |
3 | 59 |
60 | |
8 | 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 } | |
68 | |
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 | 71 char query[512]; |
72 sprintf(query, | |
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 | 76 , gametime, w->temp, w->sun, w->rain, w->wind, w->hum |
77 ); | |
12 | 78 if (!db_update(query)) { |
17 | 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 | 83 } |
84 | |
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 | 87 struct weather w; |
88 struct weather lastn[Nlast]; | |
89 | |
90 getlastweather(lastn, Nlast); | |
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 | 94 } |