garten

annotate weather.c @ 18:5937504619f2

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