annotate growth.c @ 7:b1e309dc0b98

added growth module (quite static and limited implementation)
author meillo@marmaro.de
date Thu, 15 May 2008 21:42:01 +0200
parents
children 176ee28e7464
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
1 #include <stdio.h>
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
2 #include <stdlib.h>
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
3 #include <mysql.h>
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
4
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
5 #include "db.h"
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
6 #include "game.h"
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
7
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
8
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
9
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
10
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
11 void grow_plants() {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
12 int i;
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
13 unsigned char r[6];
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
14
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
15 /* get weather and last environments to calculate the next one */
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
16 sprintf(query, "select\
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
17 f.id, f.size, f.age, p.size, p.growspeed, p.age\
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
18 from field f\
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
19 join plant p on f.plant_id = p.id\
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
20 where f.game_id = '%d' ",
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
21 gameid);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
22 db_query(query);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
23 result = mysql_store_result(conn);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
24 printf("number of plants to process: %d\n", (int)mysql_num_rows(result));
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
25 while ((row = mysql_fetch_row(result)) != NULL) {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
26 for (i = 0; i < 6; i++) {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
27 r[i] = atoi(row[i]);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
28 }
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
29
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
30 sprintf(query, "update field set \
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
31 size = '%d', age = '%d' \
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
32 where id = '%d' ",
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
33 (r[1] + 1), r[2]+1, r[0]);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
34 db_query(query);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
35
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
36 if (mysql_affected_rows(conn) > 0) {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
37 printf("update successful ++ %s\n", query);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
38 } else {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
39 printf("E: update failed ++ %s\n", query);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
40 }
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
41
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
42 }
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
43 mysql_free_result(result);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
44
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
45
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
46
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
47
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
48 }
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
49
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
50
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
51 int main(int argc, char* argv[]) {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
52 printf(" --> %s\n", argv[0]);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
53
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
54 /* init */
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
55 if (argc != 2) {
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
56 printf("usage: %s <game>\n", argv[0]);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
57 exit(1);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
58 }
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
59 gamename = argv[1];
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
60
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
61 db_connect();
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
62 check_game();
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
63
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
64 grow_plants();
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
65
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
66
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
67 db_close();
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
68
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
69 printf(" --< %s\n", argv[0]);
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
70 return 0;
b1e309dc0b98 added growth module (quite static and limited implementation)
meillo@marmaro.de
parents:
diff changeset
71 }