Mercurial > garten
view weather.c @ 10:13c6828bd4a5
switched from mysql to sqlite; (+ some cleanups)
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 11:40:45 +0200 |
parents | 9bd0a2100694 |
children | 176ee28e7464 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <mysql.h> #include <time.h> #include "db.h" #include "game.h" enum { Nlast = 2, }; enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; struct weather { float temp; float sun; float rain; float wind; float hum; }; 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); 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); } 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); puts(query); return mysql_affected_rows(conn); } int main(int argc, char* argv[]) { printf(" --> weather\n"); /* init */ if (argc != 2) { printf("usage: %s <game>\n", argv[0]); exit(1); } gamename = argv[1]; db_connect(); check_game(); 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(); printf(" --< weather\n"); return 0; }