summaryrefslogtreecommitdiff
path: root/game.cpp
blob: 42e8b532e6d73950098812ef1a5ce466a5892b1f (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
#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();
	while(scene->running) {
		if(SDL_GetTicks() - last >= 1000) {
			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_CHUNK:
			handle_chunk();
			break;
		case message::MSG_TYPE_MSG:
			handle_message();
			break;
		case message::MSG_TYPE_PLAYER:
			handle_player();
			break;
		default:
			std::cout << "unknown type: " << (int)*type << std::endl;
	}

	delete type;
	async_read();
}

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)));
}

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