summaryrefslogtreecommitdiff
path: root/src/game_state.cpp
blob: eb0200517d21c2a9ad0f258f135cf1701c111d94 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "game_state.h"
#include <iostream>
#include <fstream>
#include <limits>
#include <bitset>
#include <cmath>

const int OPENING_LINES = 6;
const int GAME_AREA_LINES = 25;
const int GAME_WIDTH = 19;

void moveToNextChar(int &x, int &y, int &width, char &nextChar, std::istream &mapFile)
{
    if (nextChar == '\n')
    {
        x = 0;
        ++y;
    }
    else
    {
        x += width;
    }
    if (width > 1)
    {
        mapFile.ignore(width-1);
    }
    nextChar = mapFile.get();
}

GameState::GameState(std::istream &&mapFile)
{
    for (int i=0; i<OPENING_LINES; ++i)
    {
        mapFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    char nextChar = mapFile.get();
    for (int x=0, y=0, width=1;
         y < GAME_AREA_LINES && nextChar != EOF;
         moveToNextChar(x, y, width, nextChar, mapFile))
    {
        width = addEntity(x, y, nextChar);
    }
}

int GameState::addEntity(int x, int y, char type)
{
    switch (type)
    {
    case Alien::MAP_CHAR:
        _aliens.push_back(Alien(x,y));
        return 1;
    case EnemyBullet::ALIEN_MAP_CHAR:
    case EnemyBullet::ENEMY_MISSILE_MAP_CHAR:
        _bullets.push_back(EnemyBullet(x,y));
        return 1;
    case PlayerMissile::MAP_CHAR:
        _missiles.push_back(PlayerMissile(x,y));
        return 1;
    case Shield::MAP_CHAR:
        _shields.push_back(Shield(x,y));
        return 1;
    case Spaceship::ENEMY_MAP_CHAR:
	_enemySpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y));
	return 3;
    case Spaceship::PLAYER_MAP_CHAR:
	_playerSpaceship = std::unique_ptr<Spaceship>(new Spaceship(x+1, y));
        return 3;
    case Building::MISSILE_CONTROLLER_CHAR:
	_missileControllers.push_back(Building(x+1, y));
	return 3;
    case Building::ALIEN_FACTORY_CHAR:
	_alienFactories.push_back(Building(x+1, y));
	return 3;
    }
    return 1;
}

void GameState::logState() const
{
    for (auto const& alien : _aliens)
    {
        std::cout << "Alien " << alien.coords() << std::endl;
    }
    for (auto const& bullet : _bullets)
    {
        std::cout << "Enemy Bullet" << bullet.coords() << std::endl;
    }
    for (auto const& missile : _missiles)
    {
        std::cout << "Player Missile" << missile.coords() << std::endl;
    }
    for (auto const& shield : _shields)
    {
        std::cout << "Shield" << shield.coords() << std::endl;
    }
    if (_playerSpaceship)
    {
	std::cout << "Player Spaceship" << _playerSpaceship->coords() << std::endl;
    }
    if (_enemySpaceship)
    {
	std::cout << "Enemy Spaceship" << _enemySpaceship->coords() << std::endl;
    }
}

int getPyramidOffset(int bottomWidth, int x, int y, int resultIfLeft, int resultIfRight)
{
    int currentRowWidth = bottomWidth;
    int currentRowOffset = 0;
    for (int i=0; i<y; ++i)
    {
	currentRowOffset += currentRowWidth;
	currentRowWidth += 2;
    }

    if (x > currentRowWidth/2)
    {
	return resultIfRight;
    }
    else if (x < -currentRowWidth/2)
    {
	return resultIfLeft;
    }
    else
    {
	return currentRowOffset + currentRowWidth/2 + x;
    }
}

int getRectangularOffset(int width, int x, int y)
{
    int currentRowOffset = width*y;
    return currentRowOffset + width/2 + x;
}

std::vector<bool> GameState::toBitArray() const
{
    std::vector<bool> result(172);

    int alienOffset = 0; //Aliens are 0 to 100
    int bulletOffset = 101; //Bullets are 101 to 135
    int shieldOffset = 136; //Shields are 136 to 150
    int missileOffset = 151; //Missile info is 151 to 152
    int positionOffset = 153; //Position is 153 to 168
    int existingBuildingsOffset = 169; //Existing buildings is 169 to 170
    int canBuildOffset = 171; //Can build here is 171

    int playerX = GAME_WIDTH/2;
    int playerY = GAME_AREA_LINES-3;
    if (_playerSpaceship)
    {
	playerX = _playerSpaceship->x();
    }

    
    for (auto const& alien : _aliens)
    {
	if (alien.y() >= playerY || alien.y() < playerY-9)
	{
	    continue;
	}

	result.at(alienOffset + getPyramidOffset(3, alien.x()-playerX, playerY-1-alien.y(), 99, 100)) = true;
    }

    for (auto const& bullet : _bullets)
    {
	if (bullet.y() >= playerY || bullet.y() < playerY-5 || bullet.x() > playerX+3 || bullet.x() < playerX-3)
	{
	    continue;
	}

	result.at(bulletOffset + getRectangularOffset(7, bullet.x()-playerX, playerY-1-bullet.y())) = true;
    }

    for (auto const& shield : _shields)
    {
	if (shield.y() < playerY-3 || shield.x() < playerX-2 || shield.x() > playerX+2)
	{
	    continue;
	}

	result.at(shieldOffset + getRectangularOffset(5, shield.x()-playerX, playerY-1-shield.y())) = true;
    }

    if (_missiles.size() > 0)
    {
	result.at(missileOffset) = true;
    }
    if (_missiles.size() < 1)
    {
	result.at(missileOffset + 1) = true;
    }
    
    if (_playerSpaceship)
    {
	result.at(positionOffset + playerX-2) = true;
    }

    result.at(canBuildOffset) = true;
    for (auto const& missileController : _missileControllers)
    {
	if (missileController.y() < playerY)
	{
	    continue;
	}
	result.at(existingBuildingsOffset + 0) = true;
	if (_missiles.size() < 2)
	{
	    result.at(missileOffset+1) = true;
	}
	if (abs(missileController.x() - playerX) < 3)
	{
	    result.at(canBuildOffset) = false;
	}
    }
    for (auto const& alienFactory : _alienFactories)
    {
	if (alienFactory.y() < playerY)
	{
	    continue;
	}
	result.at(existingBuildingsOffset + 1) = true;
	if (abs(alienFactory.x() - playerX) < 3)
	{
	    result.at(canBuildOffset) = false;
	}
    }
    
    return result;
}