# HG changeset patch # User meillo@marmaro.de # Date 1211837136 -7200 # Node ID 9bd0a2100694048eea4d849a159c5ba82b509dba # Parent b1e309dc0b981df7f56fface53565bdafe8e2827 new weather implementation diff -r b1e309dc0b98 -r 9bd0a2100694 weather.c --- a/weather.c Thu May 15 21:42:01 2008 +0200 +++ b/weather.c Mon May 26 23:25:36 2008 +0200 @@ -1,46 +1,87 @@ #include #include #include +#include #include "db.h" #include "game.h" +enum { + Nlast = 2, +}; +enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; -void set_weather() { - int temperature, sun, rain, wind, humidity; +struct weather { + float temp; + float sun; + float rain; + float wind; + float hum; +}; - /* get last weather and calculate the next one * - sprintf(query, "select time from simulation where name = '%s' ", gamename); +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}; +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}; +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}; +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}; +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}; + + +float rand_limit(float limit) { + /* generates random number between -limit and +limit */ + int r; + + r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); + return r - limit; +} + + + + +void getlastweather(struct weather lastw[], int nlast) { + char query[512]; + int i; + + sprintf(query, + " select temp, sun, rain, wind, hum from weather " + " where game_id = '%d' " + " order by tick desc " + " limit %d " + , gameid, nlast); db_query(query); result = mysql_store_result(conn); - if (mysql_num_rows(result)) { - row = mysql_fetch_row(result); - time = atoi(row[0]); + for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { + lastw[i].temp = atof(row[0]); + lastw[i].sun = atof(row[1]); + lastw[i].rain = atof(row[2]); + lastw[i].wind = atof(row[3]); + lastw[i].hum = atof(row[4]); } mysql_free_result(result); - */ - temperature = 20; - sun = 5; - rain = 1; - wind = 10; - humidity = 40; +} - /* set weather */ - sprintf(query, " insert into weather \ - (tick, game_id, temperature, sun, rain, wind, humidity) \ - values ('%d', '%d', '%d', '%d', '%d', '%d', '%d') ", - gametime, gameid, temperature, sun, rain, wind, humidity); +void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { + 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); + 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); + 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); + 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); + 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); +} + + +int setweather(struct weather* w) { + char query[512]; + sprintf(query, + " insert into weather " + " (tick, game_id, temp, sun, rain, wind, hum) " + " values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') " + , gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum); db_query(query); - if (mysql_affected_rows(conn) > 0) { - printf("weather successful inserted\n"); - } else { - printf("E: weather insertion failed\n"); - } - + puts(query); + return mysql_affected_rows(conn); } @@ -57,7 +98,23 @@ db_connect(); check_game(); - set_weather(); + srand((unsigned int) time(NULL)); + + + struct weather w; + struct weather lastn[Nlast]; + + getlastweather(lastn, Nlast); + + genweather(&w, lastn, Nlast, May); + + if (setweather(&w) > 0) { + printf("weather successful inserted\n"); + } else { + printf("E: weather insertion failed\n"); + } + +/* set_weather(); */ db_close();