summaryrefslogtreecommitdiff
path: root/game.cpp
blob: 819b35c777f49294f86c6966b77b47cd1f8df710 (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
#include "game.h"
#include "video.h"
#include "messages.h"

#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

Game* Game::game = NULL;

Game::Game() : socket(io_service) {
	scene = new Scene();

	scene->lua->set_say_func(boost::bind(&Game::say, this, _1));
}

Game::~Game() {
	delete scene;
}

void Game::say(const std::string msg) {
	message::Message m(msg);
	m.send(socket);
}

void Game::run(const std::string host, const unsigned int port) {
	run(host, boost::lexical_cast<std::string>(port));
}

void Game::run(const std::string host, const std::string port) {
	SDL_WarpMouse(video::width/2, video::height/2);
	scene->last_time = SDL_GetTicks();
	scene->update();

	{
		boost::asio::ip::tcp::resolver resolver(io_service);
		boost::asio::ip::tcp::resolver::query query(host, port);
		boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
		socket.connect(boost::asio::ip::tcp::endpoint(*iterator));

		message::Hello h(1);
		h.send(socket);
	}

	async_read();

	unsigned int last = SDL_GetTicks();
	Vector3 last_pos = scene->pos;
	while(scene->running) {
		if(SDL_GetTicks() - last >= 100 && scene->pos != last_pos) {
			message::Pos pos(scene->pos);
			pos.send(socket);
			last = SDL_GetTicks();
		}
		io_service.poll_one();
		scene->events();
		scene->render();

		SDL_Delay(1);
	}
}

void Game::async_read() {
	uint8_t *t = new uint8_t;
	boost::asio::async_read(socket, boost::asio::buffer(t, sizeof(uint8_t)),
			boost::asio::transfer_all(),
			boost::bind(&Game::handle_type, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, t));
}

void Game::handle_type(const boost::system::error_code& error, std::size_t bytes_transferred, uint8_t *type) {
	if(error) {
		std::cerr << error << std::endl;
		delete type;
		scene->running = false;
		return;
	}

	switch(*type) {
		case message::MSG_TYPE_POS:
			handle_pos();
			break;
		case message::MSG_TYPE_CHUNK:
			handle_chunk();
			break;
		case message::MSG_TYPE_MSG:
			handle_message();
			break;
		case message::MSG_TYPE_PLAYER:
			handle_player();
			break;
		case message::MSG_TYPE_OBJECT:
			handle_object();
			break;
		default:
			std::cout << "unknown type: " << (int)*type << std::endl;
	}

	delete type;
	async_read();
}

void Game::handle_pos() {
	message::Pos m;

	m.recv(socket);
	uint32_t id = m.get_id();
	Vector3 pos = m.get_pos();
	if(id == 0) { // my pos
		scene->pos = pos;
	} else { // other player's pos
		for(PlayerList::iterator it = scene->players.begin(); it != scene->players.end(); it++) {
			if((*it)->get_id() == id) {
				(*it)->set_pos(pos);
				break;
			}
		}
	}
}

void Game::handle_chunk() {
	message::Chunk m;

	m.recv(socket);
	int64_t x, y;
	m.get_coords(x, y);

	float *data = m.get_data();
	scene->terrain->tc->add_chunk(data, x, y, Terrain::chunk_size_total, Terrain::chunk_size_total);

	Terrain::Chunk *chunk = NULL;
	try {
		chunk = new Terrain::Chunk(scene->terrain, x, y);
	} catch(...) {
	}
	if(chunk)
		scene->terrain->chunks.push_back(chunk);
}

void Game::handle_message() {
	message::Message m;

	m.recv(socket);
	std::string s = m.get_str();

	scene->chat->add_line(s);
}

void Game::handle_player() {
	message::Player m;

	m.recv(socket);

	uint32_t id = m.get_id();
	Vector3 pos(m.get_pos());
	std::string name = m.get_name();

	scene->players.push_back(Player::p(new Player(id, pos, name)));
}

void Game::handle_object() {
	message::Object m;

	m.recv(socket);

	// type is ignored for now
	uint32_t type = m.get_type();
	Vector3 pos(m.get_pos());

	Terrain::Chunk *chunk = scene->terrain->find_chunk(pos.x, pos.z);
	if(!chunk) {
		std::cerr << "got object for non-existing chunk, discarding" << std::endl;
		return;
	}
	pos.y = chunk->find(pos.x, pos.z)->get_height(pos.x, pos.z);

	int64_t x = pos.x;
	x -= x % Terrain::chunk_size;

	int64_t z = pos.z;
	z -= z % Terrain::chunk_size;

	TerrainCacheObject::p ob = scene->terrain->tc->get_chunk(x, z, Terrain::chunk_size_total, Terrain::chunk_size_total);
	ob->objects.push_back(pos);

	models::ModelManager& model_mgr = models::ModelManager::get_instance();

	chunk->objects.push_back(Terrain::Chunk::ObjectPair(model_mgr.get_model("tree"), pos));
}

Game& Game::get_instance() {
	if(!game)
		game = new Game();
	return *game;
}

void Game::free() {
	if(game) {
		delete game;
		game = NULL;
	}
}