Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 10a207cff93ea710c9f905fb63f619ad057058eb (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

/*
   client/map.cc
   This file is part of the Osirion project and is distributed under 
   the terms of the GNU General Public License version 2 
*/

#include "audio/audio.h"
#include "core/application.h"
#include "client/map.h"
#include "client/input.h"
#include "client/targets.h"
#include "ui/ui.h"
#include "ui/paint.h"
#include "render/gl.h"
#include "render/textures.h"
#include "render/text.h"

namespace client {

bool is_valid_map_target(core::Entity *entity)
{
	if (entity->serverside()) {
		return false;
	} else if (entity == core::localcontrol()) {
		return false;
	} else if (entity->flag_is_set(core::Entity::ShowOnMap)) {
		return true;
	} else 	if (entity == core::localplayer()->mission_target()) {
		return true;
	} else {
		return false;
	}
}

Map::Map(ui::Widget *parent) : ui::Window(parent)
{
	set_label("map");
	set_border(true);
	set_background(true);
	set_font(ui::root()->font_small());

	//map_target = 0;
	map_hover = 0;
	hide();
}

Map::~Map()
{
}

void Map::hide()
{
	ui::Window::hide();
	map_hover = 0;
	//map_target = 0;
}

void Map::toggle()
{
	if (visible())
		hide();
	else
		show();
}

void Map::draw()
{
	const float margin = font()->width() * 2.0f;
	const float w = width() - margin * 2.0f;
	const float h = height()  - margin * 2.0f;
	const float s = math::min(w, h);
	const float blue = 0.8f;
	const float gridsize=16;

	core::Entity *entity;
	math::Color color;

	math::Vector2f v(global_location());
	math::Vector2f l;

	v.x += margin;
	v.y += margin;
	map_hover = 0;

	if (h > s ) {
		v.y += (h-s) * 0.5f;
	} else {
		//v.x += (w-s) * 0.5f;
	}

	gl::color(0,0,blue);

	gl::begin(gl::Lines);
	for (int i=0; i <= gridsize; i++) {
	
		gl::vertex(v.x, 	v.y + s / gridsize * i);
		gl::vertex(v.x + s, 	v.y + s / gridsize * i);

		gl::vertex(v.x + s / gridsize * i, v.y);
		gl::vertex(v.x + s / gridsize * i, v.y+s);
	}
	gl::end();

	const size_t texture_entity = render::Textures::load("bitmaps/icons/entity_default");
	const size_t texture_globe = render::Textures::load("bitmaps/icons/entity_globe");
	const size_t texture_bright = render::Textures::load("bitmaps/icons/entity_bright");
	
	size_t texture_current = render::Textures::bind(texture_entity);

	v.x += s * 0.5f;
	v.y += s * 0.5f;

	core::Zone *zone = core::localplayer()->zone();

	const math::Vector2f cursor(input::mouse_position_x(), input::mouse_position_y());

	const float r = 12.0f;
	float scale = 2048.0f;
	scale *= 2;

	gl::enable(GL_TEXTURE_2D);
	gl::begin(gl::Quads);

	// draw map icons
	bool valid_target = false;
	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		entity = (*it);

		bool draw_icon = false;
		l.assign(v);

		if (is_valid_map_target(entity)) {
			draw_icon = true;	
			l.x -= s / scale * entity->location().y;
			l.y -= s / scale * entity->location().x;

			if (math::distancesquared(cursor, l) < (r*r)) {
				map_hover = entity->id();
			}

			if (entity == targets::current()) {
				valid_target = true;
				if (core::application()->time() - floorf(core::application()->time()) < 0.5f) {
					draw_icon = false;
				}
			}
		}

		if (entity == core::localcontrol()) {
			draw_icon = false;
		}

		if (draw_icon) {
			if (entity->type() == core::Entity::Globe) {
				if (entity->flag_is_set(core::Entity::Bright)) {
					if (texture_current  != texture_bright) {
						gl::end();
						texture_current = render::Textures::bind(texture_bright);
						gl::begin(gl::Quads);
					}
				} else {
					if (texture_current  != texture_globe) {
						gl::end();
						texture_current = render::Textures::bind(texture_globe);
						gl::begin(gl::Quads);
					}
				}
			} else {
				if (texture_current  != texture_entity) {
					gl::end();
					texture_current = render::Textures::bind(texture_entity);
					gl::begin(gl::Quads);
				}
			}
				
			if (entity == core::localplayer()->mission_target()) {
				color.assign(palette()->mission());
			} else {
				color.assign(entity->color());
			}
			color.a = 1.0f;

			gl::color(color);
			glTexCoord2f(0.0f, 0.0f);
			gl::vertex(l.x-r, l.y-r);
			
			glTexCoord2f(1.0f, 0.0f);
			gl::vertex(l.x+r, l.y-r);
			
			glTexCoord2f(1.0f, 1.0f);
			gl::vertex(l.x+r, l.y+r);
			
			glTexCoord2f(0.0f, 1.0f);
			gl::vertex(l.x-r, l.y+r);
		}
		
 	}

	// draw localcontrol icon
	entity = core::localcontrol();
	l.assign(v);
	l.x -= s / scale * entity->location().y;
	l.y -= s / scale * entity->location().x;
	if (core::application()->time() - floorf(core::application()->time()) < 0.5f) {
		if (texture_current  != texture_entity) {
			gl::end();
			texture_current = render::Textures::bind(texture_entity);
			gl::begin(gl::Quads);
		}

		math::Color color(entity->color());
		color.a = 1.0f;
		gl::color(color);
		glTexCoord2f(0.0f, 0.0f);
		gl::vertex(l.x-r, l.y-r);
			
		glTexCoord2f(1.0f, 0.0f);
		gl::vertex(l.x+r, l.y-r);
		
		glTexCoord2f(1.0f, 1.0f);
		gl::vertex(l.x+r, l.y+r);
		
		glTexCoord2f(0.0f, 1.0f);
		gl::vertex(l.x-r, l.y+r);
	}

	gl::end();
	

	if (targets::current()) {
		l.assign(v);

		if (h > s ) {
			// target information below
			l.x -= s * 0.5f;
			l.y += s * 0.5f;

			l.y += margin;
		} else {
			// target information right
			l.x += s * 0.5f;
			l.y -= s * 0.5f;

			l.x += margin;
		}
	
		render::Text::setcolor('B');
		render::Text::setfont(font()->name().c_str(), font()->width(), font()->height());	
		render::Text::draw(l.x, l.y, targets::current()->name());
		render::Text::setcolor('N');
	}

	gl::disable(GL_TEXTURE_2D);
}

bool Map::on_keypress(const int key, const unsigned int modifier)
{	
	if (key == 512 + SDL_BUTTON_LEFT) {
		if (hover()) {
			core::Entity *target = core::localplayer()->zone()->find_entity(hover());
			if (is_valid_map_target(target)) {
				targets::select_target(target);
			}
		}
		return true;

	} else if (key == SDLK_ESCAPE) {
		if (visible()) {
			hide();
			return true;
		}
	}

	return false;
}

}