summaryrefslogtreecommitdiff
path: root/src/d/d_seafightgame.cpp
blob: a3127abf1eba0740a9335b4817024461ce0830fa (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Generated by dtk
// Translation Unit: d_seafightgame.cpp
//

#include "d/d_seafightgame.h"
#include "SSystem/SComponent/c_math.h"

/* 800C1F90-800C20B0       .text init__20dSeaFightGame_info_cFii */
int dSeaFightGame_info_c::init(int i_bulletNum, int i_scenario) {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            mGrid[i][j] = 0;
        }
    }

    mBulletNum = i_bulletNum;
    mScore = 0;

    switch (i_scenario) {
    case 1:
        mAliveShipNum = 1;
        put_ship(0, 2);
        break;
    case 2:
        mAliveShipNum = 2;
        put_ship(0, 2);
        put_ship(1, 3);
        break;
    case 3:
        mAliveShipNum = 3;
        put_ship(0, 2);
        put_ship(1, 3);
        put_ship(2, 4);
        break;
    }

    mDeadShipNum = 0;
    return 1;
}

/* 800C20B0-800C225C       .text put_ship__20dSeaFightGame_info_cFUcUc */
int dSeaFightGame_info_c::put_ship(u8 i_shipNo, u8 i_shipSize) {
    u8 y;
    u8 x;
    u8 direction;

    do {
        direction = (s16)cM_rndF(1000.0f) % 2;
        y = (int)cM_rndF(8.0f);
        x = (int)cM_rndF(8.0f);
    } while (!checkPutShip(y, x, i_shipSize, direction));

    if (direction == 0) {
        for (int i = 0; i < i_shipSize; i++) {
            mShips[i_shipNo].m_pos[i][0] = y;
            mShips[i_shipNo].m_pos[i][1] = x + i;
            mGrid[y][x + i] = i_shipNo + 102;
        }

        mShips[i_shipNo].field_0xb = y;
        mShips[i_shipNo].field_0xc = x;
        mShips[i_shipNo].field_0xd = 0;
        mShips[i_shipNo].field_0xe = i_shipSize;
    } else {
        for (int i = 0; i < i_shipSize; i++) {
            mShips[i_shipNo].m_pos[i][0] = y + i;
            mShips[i_shipNo].m_pos[i][1] = x;
            mGrid[y + i][x] = i_shipNo + 102;
        }

        mShips[i_shipNo].field_0xb = y;
        mShips[i_shipNo].field_0xc = x;
        mShips[i_shipNo].field_0xd = i_shipSize;
        mShips[i_shipNo].field_0xe = 0;
    }

    mShips[i_shipNo].field_0x8 = i_shipSize;
    mShips[i_shipNo].field_0x9 = i_shipSize;
    return 1;
}

/* 800C225C-800C22FC       .text checkPutShip__20dSeaFightGame_info_cFiiii */
bool dSeaFightGame_info_c::checkPutShip(int i_y, int i_x, int i_shipSize, int i_direction) {
    if (i_direction == 0) {
        for (int i = 0; i < i_shipSize; i++) {
            if (i_y > 7 || i_x > 7 || mGrid[i_y][i_x] > 100) {
                return false;
            }
            i_x++;
        }
    } else {
        for (int i = 0; i < i_shipSize; i++) {
            if (i_y > 7 || i_x > 7 || mGrid[i_y][i_x] > 100) {
                return false;
            }
            i_y++;
        }
    }

    return true;
}

/* 800C22FC-800C239C       .text attack__20dSeaFightGame_info_cFUcUc */
int dSeaFightGame_info_c::attack(u8 i_y, u8 i_x) {
    int rt = -1;

    u8 uvar1 = mGrid[i_y][i_x];
    if (uvar1 == 0) {
        mGrid[i_y][i_x] = 1;
    } else if (uvar1 > 100) {
        rt = uvar1 - 102;

        mShips[rt].field_0x9--;
        if (mShips[rt].field_0x9 == 0) {
            mAliveShipNum--;
            mDeadShipNum++;
        }

        mGrid[i_y][i_x] = 3;
    } else {
        return -2;
    }

    mBulletNum--;
    mScore++;
    return rt;
}