summaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: d086cb6e2e7120e0876c54595316770aa79c3a3e (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
#include "game.hpp"

namespace game {

size_t selection_cookie = 1;

entity_t::entity_t(game::state_t *game_, int type_) : world::entity_t(type_)
{
	game = game_;
}

entity_t::~entity_t(void)
{
}

void entity_t::destroy(void)
{
	unlink();
	game->awake_entities.erase(this);
	delete this;
}

void entity_t::place(world::world_t *world_)
{
	bool do_spawn = false;

	if (!world)
		do_spawn = true;

	link(world_);

	if (do_spawn)
		on_spawn();

	if (!ignore_waking)
		wake();
}

void entity_t::place(world::world_t *world, v2f_t x_)
{
	x = x_;
	cmodel.bounds = size + x;
	render_bounds = render_size + x;

	place(world);
}

void entity_t::wake(void)
{
	awake = true;
	wake_time = game->now;
	game->awake_entities.insert(this);

	if (!ignore_waking)
		on_wake();
}

void entity_t::sleep(void)
{
	awake = false;
	game->awake_entities.erase(this);
}

void state_t::start(void)
{
	unit_soldier_t *soldier;

	world.generator = worldgen;
	world.generator_data = (void*)this;

	soldier = new unit_soldier_t(this);
	soldier->place(&world, v2f_t(0.5, 0.5));
	soldier = new unit_soldier_t(this);
	soldier->place(&world, v2f_t(1.5, 0.5));
	soldier = new unit_soldier_t(this);
	soldier->place(&world, v2f_t(2.5, 0.5));
	select(rectf_t(v2f_t(0.5, 0.5), v2f_t(2.5, 0.5)));
}

void state_t::stop(void)
{
}

void state_t::group_say(std::string text)
{
	interface->print(text::get(text::SAY_GROUP) + ": " + text);
}

void state_t::select(rectf_t x)
{
	selection_cookie++;
	selected_units.clear();

	for (world::entity_t *ent : world.get_render_entities(x)) {
		unit_t *unit;

		if (ent->type != ET_UNIT)
			continue;

		unit = (unit_t*)ent;

		if (!unit->controllable)
			continue;

		unit->selected = selection_cookie;
		selected_units.insert(unit);
	}

	if (selected_units.size() == 1)
		(*selected_units.begin())->say(text::get(text::SAY_READY));
	else if (selected_units.size() > 1)
		group_say(text::get(text::SAY_READY_GROUP));
}

void state_t::command(v2f_t x)
{
	v2f_t snap;

	snap[0] = std::round(x[0] - 0.5f) + 0.5f;
	snap[1] = std::round(x[1] - 0.5f) + 0.5f;

	if (selected_units.size() > 1)
		group_say(text::get(text::SAY_MOVING_GROUP));

	for (unit_t *unit : selected_units) {
		if (unit->dead)
			continue;

		if (!unit->controllable)
			continue;

		if (!unit->start_moving(snap))
			unit->say(text::get(text::SAY_NO_PATH));
		else if (selected_units.size() == 1)
			unit->say(text::get(text::SAY_MOVING));
	}
}

void state_t::tick(double now_, double dt_)
{
	union {
		double d;
		uint32_t i;
	} u;

	if (paused)
		return;

	dt = dt_;
	now += dt;

	// FIXME: Is this non-deterministic enough?
	u.d = now;
	dice_prng.seed(dice_prng.next() ^ u.i);
	u.d = dt;
	dice_prng.seed(dice_prng.next() ^ u.i);

	// on_think can insert/erase elements of awake_entities so iterate
	// over a copy of it.
	auto copy = awake_entities;
	for (entity_t *ent : copy)
		ent->on_think();
}

die_t::die_t(size_t sides_)
{
	sides = sides_;
}

die_t::die_t(size_t count_, size_t sides_)
{
	count = count_;
	sides = sides_;
}

die_t::die_t(size_t count_, size_t sides_, size_t bonus_)
{
	count = count_;
	sides = sides_;
	bonus = bonus_;
}

size_t state_t::roll(die_t die)
{
	size_t total = 0;

	for (size_t i = 0; i < die.count; i++)
		total += dice_prng.next() % die.sides + 1;

	return total + die.bonus;
}

bool load_assets(void)
{
	assets::load();
	return true;
}

} //namespace game