aboutsummaryrefslogtreecommitdiff
path: root/gst/gst.c
blob: 09f3c1cc8c9619bdd5666d478c95fa12fa0683cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>

#include <gst.h>

void gst_init (gamestate *gst) {
    map_init(&gst->map_editor, MAXMAP, MAXMAP, 32);
    army_init(gst->army_bp, &gst->map_editor);
    gst->playernum = 1;
    gst->state = 0;
    gst->starttime = FLT_MAX;
    gst->turn = 0;
    gst->coveredtime = 0;
    gst->turnspeed = 0.35f;
}

void gst_destroy (gamestate *gst) {
    army_destory(gst->army_bp+0);
    map_destroy(&gst->map_editor);
}

void gst_get_maparmy(gamestate *gst, map **m, army **ar) {
    if (gst->state == 0) {
        *m = &gst->map_editor;
        *ar = gst->army_bp+0;
    } 
    if (gst->state == 1) {
        *m = &gst->map_battle;
        *ar = &gst->ar;
    }
}

void gst_tobattle (gamestate *gst) {
    if (gst->playernum == 1) {
        //info_load_army(gst->army_bp+1, "army");
        gst->army_bp[1] = gst->army_bp[0];
        gst->playernum = 2;
    }
    gst->state = 1;
    int maxx = MAXMAP, maxy = MAXMAP;
    int dx = 5, dy = 10;
    int sx = dx*2+maxx, sy = dy+maxy*2;
    map_init(&gst->map_battle, sx, sy, 32);
    army_init(&gst->ar, &gst->map_battle);
    unit u;
    for (int i=0; i<gst->playernum; i++) {
        for (int j=0; j<gst->army_bp[i].uslen; j++) {
            u = gst->army_bp[i].us[j];
            u.owner = i;
            if (i==0) {
                u.gridpos[0] += dx;
                u.gridpos[1] += dy+maxy;
            }
            if (i==1) {
                u.gridpos[0] = maxx-u.gridpos[0]+dx;
                u.gridpos[1] = maxy-u.gridpos[1];
            }
            u.pos[0] = u.gridpos[0]*gst->map_battle.ts;
            u.pos[1] = u.gridpos[1]*gst->map_battle.ts;
            army_spawn(&gst->ar, &gst->map_battle, u);
            gst->ar.us[gst->ar.uslen].owner = i;
        }
    }
    gst->starttime = FLT_MAX;
    gst->turn = 0;
    gst->coveredtime = 0;
}

void gst_process (gamestate *gst, infos *info, float t) {
    if (gst->starttime > t) gst->starttime = t; 
    float t_elapsed = t-gst->starttime;
    if (t_elapsed > gst->coveredtime) {
        gst->coveredtime += gst->turnspeed;
        gst->turn ++;
        map *m; army *ar;
        gst_get_maparmy(gst, &m, &ar);
        army_move(info, ar, m);
        army_fire(info, ar, m);
        army_upkeep(info, ar, m);
    }
}