view weather.c @ 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 9bd0a2100694
children 8db6497d6065
line wrap: on
line source

#include <stdio.h>
#include <stdlib.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 "
			" order by tick desc "
			" limit %d "
			, nlast);
	db_query(query);
	for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
		lastw[i].temp = sqlite3_column_double(stmt, 0);
		lastw[i].sun = sqlite3_column_double(stmt, 1);
		lastw[i].rain = sqlite3_column_double(stmt, 2);
		lastw[i].wind = sqlite3_column_double(stmt, 3);
		lastw[i].hum = sqlite3_column_double(stmt, 4);
	}
	sqlite3_finalize(stmt);

	/*
	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);
}


void setweather(struct weather* w) {
	char query[512];
	sprintf(query,
			" insert into weather "
			" (tick, temp, sun, rain, wind, hum) "
			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
	db_query(query);
	puts(query);
	if (sqlite3_step(stmt) == SQLITE_DONE) {
		printf("weather successful inserted\n");
	} else {
		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
	}
}


int main(int argc, char* argv[]) {
	printf("  --> weather\n");

	/* init */
	if (argc != 2) {
		printf("usage: %s <database>\n", argv[0]);
		exit(1);
	}
	database = argv[1];

	db_connect();
	read_time();

	srand((unsigned int) time(NULL));


	struct weather w;
	struct weather lastn[Nlast];

	getlastweather(lastn, Nlast);
	genweather(&w, lastn, Nlast, May);
	setweather(&w);

	db_close();

	printf("  --< weather\n");
	return 0;
}