garten

diff weather.c @ 8:9bd0a2100694

new weather implementation
author meillo@marmaro.de
date Mon, 26 May 2008 23:25:36 +0200
parents 0c19ad487f02
children 176ee28e7464
line diff
     1.1 --- a/weather.c	Thu May 15 21:42:01 2008 +0200
     1.2 +++ b/weather.c	Mon May 26 23:25:36 2008 +0200
     1.3 @@ -1,46 +1,87 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <mysql.h>
     1.7 +#include <time.h>
     1.8  
     1.9  #include "db.h"
    1.10  #include "game.h"
    1.11  
    1.12  
    1.13 +enum {
    1.14 +	Nlast = 2,
    1.15 +};
    1.16  
    1.17 +enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
    1.18  
    1.19 -void set_weather() {
    1.20 -	int temperature, sun, rain, wind, humidity;
    1.21 +struct weather {
    1.22 +	float temp;
    1.23 +	float sun;
    1.24 +	float rain;
    1.25 +	float wind;
    1.26 +	float hum;
    1.27 +};
    1.28  
    1.29 -	/* get last weather and calculate the next one *
    1.30 -	sprintf(query, "select time from simulation where name = '%s' ", gamename);
    1.31 +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};
    1.32 +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};
    1.33 +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};
    1.34 +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};
    1.35 +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};
    1.36 +
    1.37 +
    1.38 +float rand_limit(float limit) {
    1.39 +	/* generates random number between -limit and +limit */
    1.40 +	int r;
    1.41 +
    1.42 +	r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) );
    1.43 +	return r - limit;
    1.44 +}
    1.45 +
    1.46 +
    1.47 +
    1.48 +
    1.49 +void getlastweather(struct weather lastw[], int nlast) {
    1.50 +	char query[512];
    1.51 +	int i;
    1.52 +
    1.53 +	sprintf(query,
    1.54 +			" select temp, sun, rain, wind, hum from weather "
    1.55 +			" where game_id = '%d' "
    1.56 +			" order by tick desc "
    1.57 +			" limit %d "
    1.58 +			, gameid, nlast);
    1.59  	db_query(query);
    1.60  	result = mysql_store_result(conn);
    1.61 -	if (mysql_num_rows(result)) {
    1.62 -		row = mysql_fetch_row(result);
    1.63 -		time = atoi(row[0]);
    1.64 +	for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
    1.65 +		lastw[i].temp = atof(row[0]);
    1.66 +		lastw[i].sun = atof(row[1]);
    1.67 +		lastw[i].rain = atof(row[2]);
    1.68 +		lastw[i].wind = atof(row[3]);
    1.69 +		lastw[i].hum = atof(row[4]);
    1.70  	}
    1.71  	mysql_free_result(result);
    1.72 -	*/
    1.73  
    1.74 -	temperature = 20;
    1.75 -	sun = 5;
    1.76 -	rain = 1;
    1.77 -	wind = 10;
    1.78 -	humidity = 40;
    1.79 +}
    1.80  
    1.81  
    1.82 -	/* set weather */
    1.83 -	sprintf(query, " insert into weather \
    1.84 -			(tick, game_id, temperature, sun, rain, wind, humidity) \
    1.85 -			values ('%d', '%d', '%d', '%d', '%d', '%d', '%d') ",
    1.86 -			gametime, gameid, temperature, sun, rain, wind, humidity);
    1.87 +void genweather(struct weather* w, struct weather lastw[], int nlast, int month) {
    1.88 +	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);
    1.89 +	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);
    1.90 +	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);
    1.91 +	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);
    1.92 +	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);
    1.93 +}
    1.94 +
    1.95 +
    1.96 +int setweather(struct weather* w) {
    1.97 +	char query[512];
    1.98 +	sprintf(query,
    1.99 +			" insert into weather "
   1.100 +			" (tick, game_id, temp, sun, rain, wind, hum) "
   1.101 +			" values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') "
   1.102 +			, gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum);
   1.103  	db_query(query);
   1.104 -	if (mysql_affected_rows(conn) > 0) {
   1.105 -		printf("weather successful inserted\n");
   1.106 -	} else {
   1.107 -		printf("E: weather insertion failed\n");
   1.108 -	}
   1.109 -
   1.110 +	puts(query);
   1.111 +	return mysql_affected_rows(conn);
   1.112  }
   1.113  
   1.114  
   1.115 @@ -57,7 +98,23 @@
   1.116  	db_connect();
   1.117  	check_game();
   1.118  
   1.119 -	set_weather();
   1.120 +	srand((unsigned int) time(NULL));
   1.121 +
   1.122 +
   1.123 +	struct weather w;
   1.124 +	struct weather lastn[Nlast];
   1.125 +
   1.126 +	getlastweather(lastn, Nlast);
   1.127 +
   1.128 +	genweather(&w, lastn, Nlast, May);
   1.129 +
   1.130 +	if (setweather(&w) > 0) {
   1.131 +		printf("weather successful inserted\n");
   1.132 +	} else {
   1.133 +		printf("E: weather insertion failed\n");
   1.134 +	}
   1.135 +
   1.136 +/* 	set_weather(); */
   1.137  
   1.138  
   1.139  	db_close();