meillo@3: #include meillo@3: #include meillo@8: #include meillo@3: meillo@3: #include "db.h" meillo@3: #include "game.h" meillo@3: meillo@3: meillo@8: enum { meillo@8: Nlast = 2, meillo@8: }; meillo@3: meillo@8: enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; meillo@3: meillo@8: struct weather { meillo@8: float temp; meillo@8: float sun; meillo@8: float rain; meillo@8: float wind; meillo@8: float hum; meillo@8: }; meillo@3: meillo@8: 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: 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: 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: 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: 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: meillo@8: meillo@8: float rand_limit(float limit) { meillo@8: /* generates random number between -limit and +limit */ meillo@8: int r; meillo@8: meillo@8: r = rand() / ( ((float) RAND_MAX + 1) / (2*limit) ); meillo@8: return r - limit; meillo@8: } meillo@8: meillo@8: meillo@8: meillo@8: meillo@8: void getlastweather(struct weather lastw[], int nlast) { meillo@8: char query[512]; meillo@8: int i; meillo@8: meillo@8: sprintf(query, meillo@8: " select temp, sun, rain, wind, hum from weather " meillo@8: " order by tick desc " meillo@8: " limit %d " meillo@11: , nlast); meillo@3: db_query(query); meillo@11: for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) { meillo@11: lastw[i].temp = sqlite3_column_double(stmt, 0); meillo@11: lastw[i].sun = sqlite3_column_double(stmt, 1); meillo@11: lastw[i].rain = sqlite3_column_double(stmt, 2); meillo@11: lastw[i].wind = sqlite3_column_double(stmt, 3); meillo@11: lastw[i].hum = sqlite3_column_double(stmt, 4); meillo@11: } meillo@11: sqlite3_finalize(stmt); meillo@11: meillo@11: /* meillo@3: result = mysql_store_result(conn); meillo@8: for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) { meillo@8: lastw[i].temp = atof(row[0]); meillo@8: lastw[i].sun = atof(row[1]); meillo@8: lastw[i].rain = atof(row[2]); meillo@8: lastw[i].wind = atof(row[3]); meillo@8: lastw[i].hum = atof(row[4]); meillo@3: } meillo@3: mysql_free_result(result); meillo@11: */ meillo@3: meillo@8: } meillo@3: meillo@3: meillo@8: void genweather(struct weather* w, struct weather lastw[], int nlast, int month) { meillo@8: 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: 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: 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: 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: 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: } meillo@8: meillo@8: meillo@11: void setweather(struct weather* w) { meillo@8: char query[512]; meillo@8: sprintf(query, meillo@8: " insert into weather " meillo@11: " (tick, temp, sun, rain, wind, hum) " meillo@11: " values ('%d', '%f', '%f', '%f', '%f', '%f') " meillo@11: , gametime, w->temp, w->sun, w->rain, w->wind, w->hum); meillo@3: db_query(query); meillo@8: puts(query); meillo@11: if (sqlite3_step(stmt) == SQLITE_DONE) { meillo@11: printf("weather successful inserted\n"); meillo@11: } else { meillo@11: printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db)); meillo@11: } meillo@3: } meillo@3: meillo@3: meillo@3: int main(int argc, char* argv[]) { meillo@3: printf(" --> weather\n"); meillo@3: meillo@3: /* init */ meillo@3: if (argc != 2) { meillo@11: printf("usage: %s \n", argv[0]); meillo@3: exit(1); meillo@3: } meillo@11: database = argv[1]; meillo@3: meillo@3: db_connect(); meillo@11: read_time(); meillo@3: meillo@8: srand((unsigned int) time(NULL)); meillo@8: meillo@8: meillo@8: struct weather w; meillo@8: struct weather lastn[Nlast]; meillo@8: meillo@8: getlastweather(lastn, Nlast); meillo@8: genweather(&w, lastn, Nlast, May); meillo@11: setweather(&w); meillo@3: meillo@3: db_close(); meillo@3: meillo@3: printf(" --< weather\n"); meillo@3: return 0; meillo@3: }