1 #include <stdlib.h>
2 #include <string.h>
3 #include <SDL_keycode.h>
4 #include "gamewin.h"
5 #include "actor_init.h"
6 #include "actor_scripts.h"
7 #include "achievements.h"
8 #include "asc.h"
9 #include "buddy.h"
10 #ifdef CLUSTER_INSIDES
11 #include "cluster.h"
12 #endif // CLUSTER_INSIDES
13 #include "console.h"
14 #include "consolewin.h"
15 #include "context_menu.h"
16 #include "cursors.h"
17 #include "dialogues.h"
18 #include "elconfig.h"
19 #include "emotes.h"
20 #include "events.h"
21 #include "eye_candy_wrapper.h"
22 #include "gl_init.h"
23 #include "highlight.h"
24 #include "hud.h"
25 #include "hud_quickbar_window.h"
26 #include "hud_quickspells_window.h"
27 #include "interface.h"
28 #include "manufacture.h"
29 #include "main.h"
30 #include "map.h"
31 #include "minimap.h"
32 #include "missiles.h"
33 #include "multiplayer.h"
34 #include "paste.h"
35 #include "pathfinder.h"
36 #ifdef PAWN
37 #include "pawn/elpawn.h"
38 #endif
39 #include "pm_log.h"
40 #include "questlog.h"
41 #include "reflection.h"
42 #include "shadows.h"
43 #include "special_effects.h"
44 #include "spells.h"
45 #include "sky.h"
46 #ifdef DEBUG
47 #include "filter.h"
48 #include "sound.h"
49 #endif
50 #include "storage.h"
51 #include "tabs.h"
52 #include "textures.h"
53 #include "translate.h"
54 #include "trade.h"
55 #include "url.h"
56 #include "weather.h"
57 
58 // exported
59 int HUD_MARGIN_X = 64;
60 int HUD_MARGIN_Y = 49;
61 float fps_average = 100.0;
62 int have_mouse = 0;
63 int game_root_win = -1;
64 
65 // configuration options exported
66 int use_old_clicker=0;
67 int include_use_cursor_on_animals = 0;
68 int cm_banner_disabled = 0;
69 int auto_disable_ranging_lock = 1;
70 int target_close_clicked_creature = 1;
71 int open_close_clicked_bag = 1;
72 int show_fps = 0;
73 
74 #ifdef  DEBUG
75 extern int e3d_count, e3d_total;    // LRNR:stats testing only
76 #endif  //DEBUG
77 
78 static Uint32 next_fps_time = 0;
79 static int last_count = 0;
80 static int keep_grabbing_mouse = 0;
81 static int ranging_lock = 0;
82 #ifdef NEW_CURSOR
83 static int cursors_tex;
84 #endif // NEW_CURSOR
85 static int fps_center_x = 0;
86 static int fps_default_width = 0;
87 static int action_mode = ACTION_WALK;
88 static int last_action_mode = ACTION_WALK;
89 
90 // Set the game root window action mode
set_gamewin_action_mode(int new_mode)91 void set_gamewin_action_mode(int new_mode)
92 {
93 	action_mode = new_mode;
94 }
95 
96 // save the current mode so it can be restored later
save_gamewin_action_mode(void)97 void save_gamewin_action_mode(void)
98 {
99 	last_action_mode = action_mode;
100 }
101 
102 // return the last saved action mode
retrieve_gamewin_action_mode(void)103 int retrieve_gamewin_action_mode(void)
104 {
105 	return last_action_mode;
106 }
107 
108 // Get the game root window action mode
get_gamewin_action_mode(void)109 int get_gamewin_action_mode(void)
110 {
111 	return action_mode;
112 }
113 
get_fps_default_width(void)114 int get_fps_default_width(void)
115 {
116 	return fps_default_width;
117 }
118 
ranging_lock_is_on(void)119 int ranging_lock_is_on(void)
120 {
121 	return ranging_lock;
122 }
123 
toggle_ranging_lock(void)124 static void toggle_ranging_lock(void)
125 {
126 	ranging_lock = !ranging_lock;
127 	if (ranging_lock)
128 		LOG_TO_CONSOLE(c_green1, ranginglock_enabled_str);
129 	else
130 		LOG_TO_CONSOLE(c_green1, ranginglock_disabled_str);
131 }
132 
check_to_auto_disable_ranging_lock(void)133 void check_to_auto_disable_ranging_lock(void)
134 {
135 	if(ranging_lock && auto_disable_ranging_lock)
136 		toggle_ranging_lock();
137 }
138 
toggle_target_close_clicked_creature(void)139 static void toggle_target_close_clicked_creature(void)
140 {
141 	toggle_OPT_BOOL_by_name("target_close_clicked_creature");
142 	if (target_close_clicked_creature)
143 		LOG_TO_CONSOLE(c_green1, close_click_targetting_on_str);
144 	else
145 		LOG_TO_CONSOLE(c_green1, close_click_targetting_off_str);
146 }
147 
draw_special_cursors(void)148 void draw_special_cursors(void)
149 {
150 	const float RET_WID = 4.0f;
151 	const float RET_LEN = 10.0f;
152 	float ret_x = 0.0, ret_y = 0.0;
153 
154 	float ret_spin,ret_zoom, ret_alpha=0.5f;
155 	float ret_color[4];
156 	float ret_out = 7.0;
157 
158 #ifdef NEW_CURSOR
159 	if (!have_mouse && sdl_cursors) return;
160 #else // NEW_CURSOR
161 	if (!have_mouse) return;
162 #endif // NEW_CURSOR
163 
164 	if(!(SDL_GetWindowFlags(el_gl_window) & SDL_WINDOW_MOUSE_FOCUS)) return;
165 
166 	switch (current_cursor){
167 	case (CURSOR_ATTACK):
168 		ret_zoom = 2.0f;
169 		ret_spin = (cur_time%2000)*360.0f/2000.0f;
170 		ret_color[0]=1.0f;
171 		ret_color[1]=0.0f;
172 		ret_color[2]=0.0f;
173 		ret_color[3]=ret_alpha;
174 		break;
175 	case (CURSOR_WAND):
176 		ret_spin = 0.0f;
177 		ret_zoom = (sin((cur_time%1000)*3.1415/1000.0)+1.0)*6.0;
178 		ret_color[0]=0.0f;
179 		ret_color[1]=0.0f;
180 		ret_color[2]=1.0f;
181 		ret_color[3]=ret_alpha;
182 		ret_out=15.0f;
183 		break;
184 	default:
185 		ret_spin = 45.0f;
186 		ret_zoom = 3.0f;
187 		ret_color[0]=0.0f;
188 		ret_color[1]=1.0f;
189 		ret_color[2]=0.0f;
190 		ret_color[3]=ret_alpha;
191 	}
192 
193 	glPushAttrib(GL_ENABLE_BIT);
194 	glDisable(GL_LIGHTING);
195 	glDisable(GL_DEPTH_TEST);
196 	glEnable(GL_BLEND);
197 	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
198 
199 	glPushMatrix();
200 	glTranslatef(mouse_x, mouse_y, 0.0);
201 #ifdef NEW_CURSOR
202 	glScalef(pointer_size, pointer_size, 1.0);
203 #endif // NEW_CURSOR
204 
205 	//printf("mouse_x=%d mouse_y=%d\n", mouse_x, mouse_y);
206 
207 	if(have_mouse)
208 	{
209 #ifdef NEW_CURSOR
210 		glColor4f(1,1,1,1);
211 		bind_texture(cursors_tex);
212 		if(big_cursors){
213 			float u_start = (32.0f/256.0f) * ((current_cursor + 8) % 8) + 0.5f / 256.0f;
214 			float u_end = u_start + (31.0f/256.0f);
215 			float v_start = (32.0f/256.0f) * ((current_cursor + 8) / 8) + 0.5f / 256.0f;
216 			float v_end = v_start + (31.0f/256.0f);
217 			glBegin(GL_QUADS);
218 			draw_2d_thing(u_start, v_start, u_end, v_end, 0, 0, 32, 32);
219 			glEnd();
220 		} else {
221 			float u_start = (16.0f/256.0f) * (current_cursor % 16) + 0.5f / 256.0f;
222 			float u_end = u_start + (15.0f/256.0f);
223 			float v_start = (16.0f/256.0f) * (current_cursor / 16) + 0.5f / 256.0f;
224 			float v_end = v_start + (15.0f/256.0f);
225 			glBegin(GL_QUADS);
226 			draw_2d_thing(u_start, v_start, u_end, v_end, 10, 10, 26, 26);
227 			glEnd();
228 		}
229 #endif // NEW_CURSOR
230 
231 		glRotatef(ret_spin, 0.0, 0.0, 1.0);
232 		glColor4fv(ret_color);
233 		glDisable(GL_TEXTURE_2D);
234 
235 		ret_x += ret_zoom;
236 		glBegin(GL_TRIANGLES);
237 
238 		glVertex2f(ret_x,ret_y);
239 		glVertex2f(ret_x+RET_LEN,ret_y-RET_WID);
240 		glVertex2f(ret_x+ret_out,ret_y);
241 
242 		glVertex2f(ret_x,ret_y);
243 		glVertex2f(ret_x+RET_LEN,ret_y+RET_WID);
244 		glVertex2f(ret_x+ret_out,ret_y);
245 
246 		ret_x -= ret_zoom*2;
247 
248 		glVertex2f(ret_x,ret_y);
249 		glVertex2f(ret_x-RET_LEN,ret_y-RET_WID);
250 		glVertex2f(ret_x-ret_out,ret_y);
251 
252 		glVertex2f(ret_x,ret_y);
253 		glVertex2f(ret_x-RET_LEN,ret_y+RET_WID);
254 		glVertex2f(ret_x-ret_out,ret_y);
255 
256 		ret_x += ret_zoom;
257 		ret_y -= ret_zoom;
258 
259 		glVertex2f(ret_x,ret_y);
260 		glVertex2f(ret_x-RET_WID,ret_y-RET_LEN);
261 		glVertex2f(ret_x,ret_y-ret_out);
262 
263 		glVertex2f(ret_x,ret_y);
264 		glVertex2f(ret_x+RET_WID,ret_y-RET_LEN);
265 		glVertex2f(ret_x,ret_y-ret_out);
266 
267 		ret_y += ret_zoom*2;
268 
269 		glVertex2f(ret_x,ret_y);
270 		glVertex2f(ret_x-RET_WID,ret_y+RET_LEN);
271 		glVertex2f(ret_x,ret_y+ret_out);
272 
273 		glVertex2f(ret_x,ret_y);
274 		glVertex2f(ret_x+RET_WID,ret_y+RET_LEN);
275 		glVertex2f(ret_x,ret_y+ret_out);
276 
277 		glEnd();
278 		ret_y -= ret_zoom;
279 
280 		glColor4f(0.0,0.0,0.0,ret_alpha);
281 
282 		ret_x += ret_zoom;
283 		glBegin(GL_LINE_LOOP);
284 		glVertex2f(ret_x,ret_y);
285 		glVertex2f(ret_x+RET_LEN,ret_y-RET_WID);
286 		glVertex2f(ret_x+ret_out,ret_y);
287 		glVertex2f(ret_x+RET_LEN,ret_y+RET_WID);
288 		glEnd();
289 		ret_x -= ret_zoom*2;
290 		glBegin(GL_LINE_LOOP);
291 		glVertex2f(ret_x,ret_y);
292 		glVertex2f(ret_x-RET_LEN,ret_y-RET_WID);
293 		glVertex2f(ret_x-ret_out,ret_y);
294 		glVertex2f(ret_x-RET_LEN,ret_y+RET_WID);
295 		glEnd();
296 
297 		ret_x += ret_zoom;
298 		ret_y -= ret_zoom;
299 		glBegin(GL_LINE_LOOP);
300 		glVertex2f(ret_x,ret_y);
301 		glVertex2f(ret_x-RET_WID,ret_y-RET_LEN);
302 		glVertex2f(ret_x,ret_y-ret_out);
303 		glVertex2f(ret_x+RET_WID,ret_y-RET_LEN);
304 		glEnd();
305 
306 		ret_y += ret_zoom*2;
307 		glBegin(GL_LINE_LOOP);
308 		glVertex2f(ret_x,ret_y);
309 		glVertex2f(ret_x-RET_WID,ret_y+RET_LEN);
310 		glVertex2f(ret_x,ret_y+7);
311 		glVertex2f(ret_x+RET_WID,ret_y+RET_LEN);
312 		glEnd();
313 		ret_y -= ret_zoom;
314 	}
315 #ifdef NEW_CURSOR
316 	else
317 #endif // NEW_CURSOR
318 #ifdef NEW_CURSOR
319 	{
320 		if (current_cursor != CURSOR_ARROW){
321 			glColor4fv(ret_color);
322 			glRotatef(-135.0,0,0,1);
323 			glDisable(GL_TEXTURE_2D);
324 			glBegin(GL_TRIANGLES);
325 			glVertex2f(ret_x,ret_y);
326 			glVertex2f(ret_x-RET_LEN,ret_y-RET_WID);
327 			glVertex2f(ret_x-ret_out,ret_y);
328 
329 			glVertex2f(ret_x,ret_y);
330 			glVertex2f(ret_x-RET_LEN,ret_y+RET_WID);
331 			glVertex2f(ret_x-ret_out,ret_y);
332 			glEnd();
333 
334 			glColor4f(0.0,0.0,0.0,ret_alpha);
335 
336 			glBegin(GL_LINE_LOOP);
337 			glVertex2f(ret_x,ret_y);
338 			glVertex2f(ret_x-RET_LEN,ret_y-RET_WID);
339 			glVertex2f(ret_x-ret_out,ret_y);
340 			glVertex2f(ret_x-RET_LEN,ret_y+RET_WID);
341 			glEnd();
342 			glRotatef(135.0,0,0,1);
343 			glEnable(GL_TEXTURE_2D);
344 		}
345 
346 		glColor4f(1,1,1,1);
347 		bind_texture(cursors_tex);
348 		if(big_cursors){
349 			float u_start = (32.0f/256.0f) * ((current_cursor + 8) % 8) + 0.5f / 256.0f;
350 			float u_end = u_start + (31.0f/256.0f);
351 			float v_start = (32.0f/256.0f) * ((current_cursor + 8) / 8) + 0.5f / 256.0f;
352 			float v_end = v_start + (31.0f/256.0f);
353 			glBegin(GL_QUADS);
354 			draw_2d_thing(u_start, v_start, u_end, v_end, 0, 0, 32, 32);
355 			glEnd();
356 		} else {
357 			float u_start = (16.0f/256.0f) * (current_cursor % 16) + 0.5f / 256.0f;
358 			float u_end = u_start + (15.0f/256.0f);
359 			float v_start = (16.0f/256.0f) * (current_cursor / 16) + 0.5f / 256.0f;
360 			float v_end = v_start + (15.0f/256.0f);
361 			glBegin(GL_QUADS);
362 			draw_2d_thing(u_start, v_start, u_end, v_end, 0, 0, 16, 16);
363 			glEnd();
364 		}
365 	}
366 #endif // NEW_CURSOR
367 
368 	glPopMatrix();
369 	glPopAttrib();
370 	reset_material();
371 }
372 
toggle_have_mouse(void)373 void toggle_have_mouse(void)
374 {
375 	have_mouse = !have_mouse;
376 	if(have_mouse){
377 		SDL_SetWindowGrab(el_gl_window, SDL_TRUE);
378 #ifdef NEW_CURSOR
379 		if (sdl_cursors)
380 #endif // NEW_CURSOR
381 			SDL_ShowCursor(0);
382 		if (fol_cam) toggle_follow_cam(&fol_cam);
383 		LOG_TO_CONSOLE (c_red1, "Grab mode: press alt+g again to enter Normal mode.");
384 	} else {
385 		SDL_SetWindowGrab(el_gl_window, SDL_FALSE);
386 #ifdef NEW_CURSOR
387 		if (sdl_cursors)
388 #endif // NEW_CURSOR
389 			SDL_ShowCursor(1);
390 		LOG_TO_CONSOLE (c_red1, "Normal mode: press alt+g again to enter Grab mode.");
391 	}
392 }
393 
toggle_first_person(void)394 static void toggle_first_person(void)
395 {
396 	if (first_person == 0){
397 		//rotate camera where actor is looking at
398 		actor *me = get_our_actor();
399 		if (me)
400 			rz=me->z_rot;
401 		rx=-90;
402 		first_person = 1;
403 		fol_cam = 0;
404 	} else {
405 		first_person = 0;
406 		if (rx < -90) {rx = -90;}
407 	}
408 	++adjust_view;
409 	resize_root_window();
410 	//set_all_intersect_update_needed(main_bbox_tree);
411 }
412 
413 // This is the main part of the old check_cursor_change ()
mouseover_game_handler(window_info * win,int mx,int my)414 static int mouseover_game_handler (window_info *win, int mx, int my)
415 {
416 	SDL_Keymod mod_key_status = SDL_GetModState();
417 
418 	if (hud_mouse_over(win, mx, my))
419 		return 1;
420 
421 	if(object_under_mouse == -1)
422 	{
423 		if(spell_result==2){
424 			elwin_mouse = CURSOR_WAND;
425 		} else {
426 			elwin_mouse = CURSOR_WALK;
427 		}
428 	}
429 
430 	else if (thing_under_the_mouse==UNDER_MOUSE_3D_OBJ && objects_list[object_under_mouse])
431 	{
432 		int range_weapon_equipped;
433 		LOCK_ACTORS_LISTS();
434 		range_weapon_equipped = (your_actor &&
435 								 your_actor->cur_weapon >= BOW_LONG &&
436 								 your_actor->cur_weapon <= BOW_CROSS);
437 		UNLOCK_ACTORS_LISTS();
438 		if(action_mode==ACTION_LOOK)
439 		{
440 			elwin_mouse = CURSOR_EYE;
441 		}
442 		else if(objects_list[object_under_mouse]->flags&OBJ_3D_BAG)
443 		{
444 			elwin_mouse = CURSOR_PICK;
445 		}
446 		else if(action_mode==ACTION_USE)
447 		{
448 			elwin_mouse = CURSOR_USE;
449 		}
450 		else if(action_mode==ACTION_USE_WITEM)
451 		{
452 			elwin_mouse = CURSOR_USE_WITEM;
453 		}
454 		// allow to shoot at 3D objects
455 		else if (range_weapon_equipped &&
456                  (action_mode == ACTION_ATTACK || ((mod_key_status & KMOD_ALT) && (mod_key_status & KMOD_CTRL))))
457 		{
458 			elwin_mouse = CURSOR_ATTACK;
459 		}
460 		//see if the object is a harvestable resource.
461 		else if(objects_list[object_under_mouse]->flags&OBJ_3D_HARVESTABLE)
462 		{
463 			elwin_mouse = CURSOR_HARVEST;
464 		}
465 		//see if the object is an entrable resource.
466 		else if(objects_list[object_under_mouse]->flags&OBJ_3D_ENTRABLE) {
467 			elwin_mouse = CURSOR_ENTER;
468 		}
469 		//hmm, no usefull object, so select walk....
470 		else
471 		{
472 			if(spell_result==2)
473 				elwin_mouse = CURSOR_WAND;
474 			else
475 				elwin_mouse = CURSOR_WALK;
476 		}
477 	}
478 
479 	else if(thing_under_the_mouse==UNDER_MOUSE_NPC)
480 	{
481 		if(action_mode==ACTION_LOOK)
482 		{
483 			elwin_mouse = CURSOR_EYE;
484 		}
485 		else
486 		{
487 			elwin_mouse = CURSOR_TALK;
488 		}
489 	}
490 
491 	else if(thing_under_the_mouse==UNDER_MOUSE_PLAYER)
492 	{
493 		if(action_mode==ACTION_USE)
494 		{
495 			elwin_mouse = CURSOR_USE;
496 		}
497 		else if(action_mode==ACTION_LOOK)
498 		{
499 			elwin_mouse = CURSOR_EYE;
500 		}
501 		else if(action_mode==ACTION_TRADE)
502 		{
503 			elwin_mouse = CURSOR_TRADE;
504 		}
505 		else if((mod_key_status & KMOD_ALT) || action_mode==ACTION_ATTACK)
506 		{
507 			elwin_mouse = CURSOR_ATTACK;
508 		}
509 		else if (spell_result==3 && action_mode==ACTION_WAND)
510 		{
511 			elwin_mouse = CURSOR_WAND;
512 		}
513 		else
514 		{
515 			elwin_mouse = CURSOR_EYE;
516 		}
517 	}
518 
519 	else if(thing_under_the_mouse==UNDER_MOUSE_ANIMAL)
520 	{
521 		if(action_mode==ACTION_USE)
522 		{
523 			elwin_mouse = CURSOR_USE;
524 		}
525 		else if(action_mode==ACTION_LOOK)
526 		{
527 			elwin_mouse = CURSOR_EYE;
528 		}
529 		else if(mod_key_status & KMOD_SHIFT)
530 		{
531 			elwin_mouse = CURSOR_EYE;
532 		}
533 		else if(spell_result==3 && action_mode == ACTION_WAND)
534 		{
535 			elwin_mouse = CURSOR_WAND;
536 		}
537 		else if((mod_key_status & KMOD_ALT) || action_mode==ACTION_ATTACK || (actor_under_mouse && !actor_under_mouse->dead))
538 		{
539 			elwin_mouse = CURSOR_ATTACK;
540 		}
541 	}
542 
543 	// when all fails - walk
544 	else
545 	{
546 		if(spell_result==2) {
547 			elwin_mouse = CURSOR_WAND;
548 		} else {
549 			elwin_mouse = CURSOR_WALK;
550 		}
551 	}
552 
553 	return 1;
554 }
555 
attack_someone(int who_to_attack)556 static void attack_someone(int who_to_attack)
557 {
558 	Uint8 str[10];
559 	str[0] = ATTACK_SOMEONE;
560 	*((int *)(str+1)) = SDL_SwapLE32(who_to_attack);
561 	my_tcp_send (my_socket, str, 5);
562 }
563 
touch_player(int player_to_touch)564 static void touch_player(int player_to_touch)
565 {
566 	Uint8 str[10];
567 	str[0] = TOUCH_PLAYER;
568 	*((int *)(str+1)) = SDL_SwapLE32(player_to_touch);
569 	my_tcp_send (my_socket, str, 5);
570 }
571 
572 // this is the main part of the old check_mouse_click ()
click_game_handler(window_info * win,int mx,int my,Uint32 flags)573 static int click_game_handler(window_info *win, int mx, int my, Uint32 flags)
574 {
575 	int flag_alt = flags & KMOD_ALT;
576 	int flag_ctrl = flags & KMOD_CTRL;
577 	int flag_right = flags & ELW_RIGHT_MOUSE;
578 	int force_walk = (flag_ctrl && flag_right && !flag_alt);
579 	int shift_on = flags & KMOD_SHIFT;
580 	int range_weapon_equipped;
581 
582 	if ((flags & ELW_MOUSE_BUTTON_WHEEL) == ELW_MID_MOUSE)
583 		// Don't handle middle button clicks
584 		return 0;
585 
586 	if (flags & ELW_WHEEL_UP)
587 	{
588 		camera_zoom_speed = (flag_ctrl) ?10 :1;
589 		if (camera_zoom_dir == -1)
590 			camera_zoom_duration += 100;
591 		else
592 			camera_zoom_duration = 100;
593 		camera_zoom_dir = -1;
594 		return 1;
595 	}
596 
597 	if (flags & ELW_WHEEL_DOWN)
598 	{
599 		camera_zoom_speed = (flag_ctrl) ?10 :1;
600 		if (camera_zoom_dir == 1)
601 			camera_zoom_duration += 100;
602 		else
603 			camera_zoom_duration = 100;
604 		camera_zoom_dir = 1;
605 		return 1;
606 	}
607 
608 	if (hud_click(win, mx, my, flags))
609 		return 1;
610 
611 	LOCK_ACTORS_LISTS();
612 	range_weapon_equipped = (your_actor &&
613 							 your_actor->cur_weapon >= BOW_LONG &&
614 							 your_actor->cur_weapon <= BOW_CROSS);
615 	UNLOCK_ACTORS_LISTS();
616 
617 	if (!force_walk)
618 	{
619 		if (flag_right)
620 		{
621 			if (!cm_banner_disabled)
622 			{
623 				/* show the banner control menu if right-clicked and over your actors banner */
624 				static Uint32 reset_cursor_time = 0;
625 				static int cm_activate_when_cursor_is = -1;
626 				extern int cm_mouse_over_banner;
627 				/* activate the menu once in the cursor cycle - start-cursor reset after a couple of seconds inactivity */
628 				if (SDL_GetTicks()-reset_cursor_time > 2000)
629 					cm_activate_when_cursor_is = current_cursor;
630 				if (cm_mouse_over_banner && (current_cursor == cm_activate_when_cursor_is))
631 				{
632 					static size_t cm_id = CM_INIT_VALUE;
633 					if (!cm_valid(cm_id))
634 					{
635 						/* create first time needed */
636 						cm_id = cm_create(cm_banner_menu_str, NULL);
637 						cm_bool_line(cm_id, 0, &view_names, NULL);
638 						cm_bool_line(cm_id, 1, &view_health_bar, NULL);
639 						cm_bool_line(cm_id, 2, &view_hp, NULL);
640 						cm_bool_line(cm_id, 3, &view_ether_bar, NULL);
641 						cm_bool_line(cm_id, 4, &view_ether, NULL);
642 						cm_bool_line(cm_id, 5, &view_mode_instance, "use_view_mode_instance");
643 						cm_bool_line(cm_id, 6, &view_chat_text_as_overtext, NULL);
644 						cm_bool_line(cm_id, 7, &use_alpha_banner, "use_alpha_banner");
645 						cm_bool_line(cm_id, 8, &sit_lock, "sit_lock");
646 						cm_bool_line(cm_id, 9, &ranging_lock, NULL);
647 						cm_bool_line(cm_id, 11, &cm_banner_disabled, "cm_banner_disabled");
648 					}
649 					cm_show_direct(cm_id, -1, -1);
650 					reset_cursor_time = SDL_GetTicks();
651 				}
652 			}
653 			if (item_dragged != -1 || use_item != -1 || object_under_mouse == -1
654 					|| storage_item_dragged != -1
655 					)
656 			{
657 				use_item = -1;
658 				item_dragged = -1;
659 				storage_item_dragged = -1;
660 				action_mode = ACTION_WALK;
661 				return 1;
662 			}
663 			switch (current_cursor)
664 			{
665 				case CURSOR_EYE:
666 					if (thing_under_the_mouse == UNDER_MOUSE_ANIMAL && spell_result==3)
667 						action_mode = ACTION_WAND;
668 					else if (thing_under_the_mouse == UNDER_MOUSE_PLAYER)
669 						action_mode = ACTION_TRADE;
670 					else if (thing_under_the_mouse == UNDER_MOUSE_3D_OBJ){
671 						action_mode = ACTION_USE;
672 					}
673 					else if ((thing_under_the_mouse == UNDER_MOUSE_ANIMAL) && include_use_cursor_on_animals)
674 						action_mode = ACTION_USE;
675 					else
676 						action_mode = ACTION_WALK;
677 					break;
678 				case CURSOR_HARVEST:
679 					action_mode = ACTION_LOOK;
680 					break;
681 				case CURSOR_TRADE:
682 					if(spell_result==3)action_mode=ACTION_WAND;
683 					else action_mode = ACTION_ATTACK;
684 					break;
685 				case CURSOR_USE_WITEM:
686 					if(use_item != -1)
687 						use_item = -1;
688 					else
689 						action_mode = ACTION_WALK;
690 					break;
691 				case CURSOR_WAND:
692 					if(thing_under_the_mouse == UNDER_MOUSE_ANIMAL||thing_under_the_mouse == UNDER_MOUSE_PLAYER)
693 						action_mode = ACTION_ATTACK;
694 					else if(thing_under_the_mouse == UNDER_MOUSE_3D_OBJ)
695 						action_mode = ACTION_LOOK;
696 					break;
697 				case CURSOR_ATTACK:
698 					if(thing_under_the_mouse == UNDER_MOUSE_ANIMAL)
699 						action_mode = ACTION_LOOK;
700 					else
701 						action_mode = ACTION_WALK;
702 					break;
703 				case CURSOR_ENTER:
704 				case CURSOR_PICK:
705 				case CURSOR_WALK:
706 					if(thing_under_the_mouse == UNDER_MOUSE_3D_OBJ)
707 						action_mode = ACTION_LOOK;
708 					else
709 						action_mode = ACTION_WALK;
710 					break;
711 				case CURSOR_USE:
712 					if (range_weapon_equipped)
713 						action_mode = ACTION_ATTACK;
714 					else
715 						action_mode = ACTION_WALK;
716 					break;
717 				case CURSOR_TALK:
718 				case CURSOR_ARROW:
719 				default:
720 					action_mode = ACTION_WALK;
721 					break;
722 			}
723 			return 1;
724 		}
725 	}
726 
727 	// after we test for interface clicks
728 	// alternative drop method...
729 	if (item_dragged >= 0 && item_dragged < ITEM_NUM_ITEMS)
730 	{
731 		Uint8 str[10];
732 
733 		if (flag_right)
734 		{
735 			item_dragged = -1;
736 			return 1;
737 		}
738 
739 		str[0] = DROP_ITEM;
740 		str[1] = item_list[item_dragged].pos;
741 		*((Uint32 *) (str + 2)) = SDL_SwapLE32(item_quantity);
742 		my_tcp_send(my_socket, str, 6);
743 		return 1;
744 	}
745 
746 	if (storage_item_dragged != -1)
747 	{
748 		//TODO: Withdraw from storage, drop on ground...
749 	}
750 
751 	// if we're following a path, stop now if the click was in the main window
752 	if (pf_follow_path && !((mx >= window_width-hud_x) || (my >= window_height-hud_y)))
753 	{
754 		pf_destroy_path();
755 	}
756 
757 	if (force_walk)
758 	{
759 		short x,y;
760 
761 		get_old_world_x_y (&x, &y);
762 		// check to see if the coordinates are OUTSIDE the map
763 		if (y < 0 || x < 0 || x >= tile_map_size_x*6 || y >= tile_map_size_y*6)
764 			return 1;
765 
766 		add_highlight(x, y, HIGHLIGHT_TYPE_WALKING_DESTINATION);
767 
768 		move_to (x, y, 1);
769 		return 1;
770 	}
771 
772 	switch(current_cursor)
773 	{
774 		case CURSOR_EYE:
775 		{
776 			Uint8 str[10];
777 
778 			if (object_under_mouse == -1)
779 				return 1;
780 
781 			if (thing_under_the_mouse == UNDER_MOUSE_PLAYER || thing_under_the_mouse == UNDER_MOUSE_NPC || thing_under_the_mouse == UNDER_MOUSE_ANIMAL)
782 			{
783 #ifdef DEBUG
784 				char log[100];
785 
786 				safe_snprintf(log,sizeof(log),"Actor id: %d",object_under_mouse);
787 				LOG_TO_CONSOLE(c_green1, log);
788 #endif
789 				if (thing_under_the_mouse == UNDER_MOUSE_PLAYER)
790 					achievements_requested(mouse_x, mouse_y, flag_ctrl);
791 				str[0] = GET_PLAYER_INFO;
792 				*((int *)(str+1)) = SDL_SwapLE32((int)object_under_mouse);
793 				my_tcp_send (my_socket, str, 5);
794 				return 1;
795 			}
796 			else if (thing_under_the_mouse == UNDER_MOUSE_3D_OBJ)
797 			{
798 #ifdef DEBUG
799 				char log[100];
800 
801 				safe_snprintf(log,sizeof(log),"Object id: %d",object_under_mouse);
802 				LOG_TO_CONSOLE(c_green1, log);
803 #endif
804 				str[0] = LOOK_AT_MAP_OBJECT;
805 				*((int *)(str+1)) = SDL_SwapLE32((int)object_under_mouse);
806 				my_tcp_send (my_socket, str, 5);
807 				return 1;
808 			}
809 
810 			break;
811 		}
812 
813 		case CURSOR_WAND:
814 		{
815 			if (spell_result==2)
816 			{
817 				short x, y;
818 
819 				if(use_old_clicker)
820 					get_old_world_x_y (&x, &y);
821 				else
822 					get_world_x_y (&x, &y);
823 
824 				// check to see if the coordinates are OUTSIDE the map
825 				if (y < 0 || x < 0 || x >= tile_map_size_x*6 || y >= tile_map_size_y*6)
826 					return 1;
827 
828 				move_to(x,y,0);
829 				return 1;
830 			}
831 			else if (spell_result==3)
832 			{
833 				if (object_under_mouse >= 0 &&
834 					(thing_under_the_mouse == UNDER_MOUSE_ANIMAL ||
835 					 thing_under_the_mouse == UNDER_MOUSE_PLAYER))
836 				{
837 					actor *this_actor = get_actor_ptr_from_id(object_under_mouse);
838 					if(this_actor != NULL)
839 					{
840 						add_highlight(this_actor->x_tile_pos,this_actor->y_tile_pos, HIGHLIGHT_TYPE_SPELL_TARGET);
841 						touch_player((int)object_under_mouse);
842 					}
843 				}
844 			}
845 
846 			break;
847 		}
848 
849 		case CURSOR_TRADE:
850 		{
851 			Uint8 str[10];
852 
853 			if (object_under_mouse == -1)
854 				return 1;
855 			if (thing_under_the_mouse != UNDER_MOUSE_PLAYER)
856 				return 1;
857 			str[0] = TRADE_WITH;
858 			*((int *)(str+1)) = SDL_SwapLE32((int)object_under_mouse);
859 			my_tcp_send (my_socket, str, 5);
860 			return 1;
861 
862 			break;
863 		}
864 
865 		case CURSOR_ATTACK:
866 		{
867 			if (object_under_mouse == -1)
868 				return 1;
869 			if (you_sit && sit_lock && !flag_ctrl){
870 				if(your_actor != NULL)
871 					add_highlight(your_actor->x_tile_pos,your_actor->y_tile_pos, HIGHLIGHT_TYPE_LOCK);
872 				return 1;
873 			}
874 			if (thing_under_the_mouse == UNDER_MOUSE_PLAYER || thing_under_the_mouse == UNDER_MOUSE_NPC || thing_under_the_mouse == UNDER_MOUSE_ANIMAL)
875 			{
876 				if (object_under_mouse>=0){
877 					actor *this_actor = get_actor_ptr_from_id(object_under_mouse);
878 					if(this_actor != NULL)
879 						add_highlight(this_actor->x_tile_pos,this_actor->y_tile_pos, HIGHLIGHT_TYPE_ATTACK_TARGET);
880 				}
881 				attack_someone((int)object_under_mouse);
882 				return 1;
883 			}
884 			else if (range_weapon_equipped && thing_under_the_mouse == UNDER_MOUSE_3D_OBJ)
885 			{
886 				Uint8 str[10];
887 				str[0] = FIRE_MISSILE_AT_OBJECT;
888 				*((int *)(str+1)) = SDL_SwapLE32((int)object_under_mouse);
889 				my_tcp_send(my_socket, str, 5);
890 			}
891 			break;
892 		}
893 
894 		case CURSOR_ENTER:
895 		case CURSOR_USE:
896 		case CURSOR_USE_WITEM:
897 		case CURSOR_TALK:
898 		{
899 			Uint8 str[10];
900 
901 			if (flag_alt && range_weapon_equipped)
902 				return 1;
903 			if (object_under_mouse == -1)
904 				return 1;
905 			if (thing_under_the_mouse == UNDER_MOUSE_PLAYER || thing_under_the_mouse == UNDER_MOUSE_NPC || thing_under_the_mouse == UNDER_MOUSE_ANIMAL)
906 			{
907 				touch_player((int)object_under_mouse);
908 				// clear the previous dialogue entries, so we won't have a left over from some other NPC
909 				if (thing_under_the_mouse == UNDER_MOUSE_NPC)
910 					clear_dialogue_responses();
911 				return 1;
912 			}
913 
914 			str[0] = USE_MAP_OBJECT;
915 			*((int *)(str+1)) = SDL_SwapLE32((int)object_under_mouse);
916 			if (use_item != -1 && current_cursor == CURSOR_USE_WITEM)
917 			{
918 				used_item_counter_action_use(use_item);
919 				*((int *)(str+5)) = SDL_SwapLE32((int)item_list[use_item].pos);
920 				if (!shift_on)
921 				{
922 					use_item = -1;
923 					action_mode = ACTION_WALK;
924 				}
925 			}
926 			else
927 			{
928 				*((int *)(str+5)) = SDL_SwapLE32((int)-1);
929 			}
930 
931 			my_tcp_send (my_socket, str, 9);
932 			return 1;
933 
934 			break;
935 		}
936 
937 		case CURSOR_PICK:
938 		{
939 			if (flag_alt && range_weapon_equipped)
940 				return 1;
941 			if (object_under_mouse == -1)
942 				return 1;
943 			if (thing_under_the_mouse == UNDER_MOUSE_3D_OBJ)
944 			{
945 				open_bag (object_under_mouse);
946 				return 1;
947 			}
948 			break;
949 		}
950 
951 		case CURSOR_HARVEST:
952 		{
953 			Uint8 str[10];
954 
955 			if (flag_alt && range_weapon_equipped)
956 				return 1;
957 			if (object_under_mouse == -1)
958 				return 1;
959 			str[0] = HARVEST;
960 			*((Uint16 *)(str+1)) = SDL_SwapLE16((Uint16)object_under_mouse);
961 			my_tcp_send (my_socket, str, 3);
962 			return 1;
963 			break;
964 		}
965 
966 		case CURSOR_WALK:
967 		default:
968 		{
969 			int is_ranging_locked = 0;
970 			int is_sit_locked = 0;
971 			short x, y;
972 
973 			/* if outside the main window, on the hud, don't walk */
974 			if ((mx >= window_width-hud_x) || (my >= window_height-hud_y))
975 				return 1;
976 
977 			if (flag_alt && range_weapon_equipped)
978 				return 1;
979 
980 			if (ranging_lock_is_on() && range_weapon_equipped)
981 				is_ranging_locked = 1;
982 
983 			if (you_sit && sit_lock && !flag_ctrl)
984 				is_sit_locked = 1;
985 
986 			if(use_old_clicker)
987 				get_old_world_x_y (&x, &y);
988 			else
989 				get_world_x_y (&x, &y);
990 
991 			// check to see if the coordinates are OUTSIDE the map
992 			if (y < 0 || x < 0 || x >= tile_map_size_x*6 || y >= tile_map_size_y*6)
993 				return 1;
994 
995 			if (target_close_clicked_creature)
996 			{
997 				int closest_actor = get_closest_actor(x, y, 0.8f);
998 				if (closest_actor != -1)
999 				{
1000 					actor *this_actor = get_actor_ptr_from_id(closest_actor);
1001 					if (this_actor != NULL)
1002 					{
1003 						if (spell_result == 3)
1004 						{
1005 							add_highlight(this_actor->x_tile_pos,this_actor->y_tile_pos, HIGHLIGHT_TYPE_SPELL_TARGET);
1006 							touch_player(closest_actor);
1007 							return 1;
1008 						}
1009 						else if (!is_ranging_locked && !is_sit_locked)
1010 						{
1011 							add_highlight(this_actor->x_tile_pos, this_actor->y_tile_pos, HIGHLIGHT_TYPE_ATTACK_TARGET);
1012 							attack_someone(closest_actor);
1013 							return 1;
1014 						}
1015 					}
1016 				}
1017 			}
1018 
1019 			if (is_ranging_locked || is_sit_locked)
1020 			{
1021 				if(your_actor != NULL)
1022 					add_highlight(your_actor->x_tile_pos,your_actor->y_tile_pos, HIGHLIGHT_TYPE_LOCK);
1023 				return 1;
1024 			}
1025 
1026 			if (open_close_clicked_bag && find_and_open_closest_bag(x, y, 0.8f))
1027 				return 1;
1028 
1029 			add_highlight(x, y, HIGHLIGHT_TYPE_WALKING_DESTINATION);
1030 
1031 #ifdef DEBUG // FOR DEBUG ONLY!
1032 			if (enable_client_aiming) {
1033 				if (flag_ctrl) {
1034 					float target[3];
1035 
1036 					target[0] = x * 0.5 + 0.25;
1037 					target[1] = y * 0.5 + 0.25;
1038 					target[2] = get_tile_height(x, y) + 1.2f;
1039 
1040 					missiles_aim_at_xyz(yourself, target);
1041 					add_command_to_actor(yourself, aim_mode_reload);
1042 					missiles_fire_a_to_xyz(yourself, target);
1043 				}
1044 				else {
1045 					char in_aim_mode;
1046 					actor *cur_actor = get_actor_ptr_from_id(yourself);
1047 					LOCK_ACTORS_LISTS();
1048 					in_aim_mode = cur_actor->in_aim_mode;
1049 					UNLOCK_ACTORS_LISTS();
1050 					if (in_aim_mode == 1)
1051 						add_command_to_actor(yourself, leave_aim_mode);
1052 					move_to(x, y, 1);
1053 				}
1054 			}
1055 			else
1056 				move_to (x, y, 1);
1057 #else
1058 			move_to (x, y, 1);
1059 #endif // DEBUG
1060 
1061 			return 1;
1062 		}
1063 	}
1064 
1065 	left_click = 2;
1066 	right_click = 2;
1067 	return 1;
1068 }
1069 
1070 // common to console and map windows
display_handling_common(window_info * win)1071 void display_handling_common(window_info *win)
1072 {
1073 	if(special_effects){
1074 		display_special_effects(0);
1075 	}
1076 
1077 	// remember the time stamp to improve FPS quality when switching modes
1078 	next_fps_time=cur_time+1000;
1079 	last_count=0;
1080 
1081 	ec_idle();
1082 
1083 	missiles_update();
1084 	update_camera();
1085 
1086 	draw_delay = 20;
1087 
1088 	check_and_get_console_input(win->window_id);
1089 }
1090 
1091 
1092 // common to console and map windows
return_to_gamewin_common(void)1093 void return_to_gamewin_common(void)
1094 {
1095 	if (keep_grabbing_mouse)
1096 	{
1097 		toggle_have_mouse();
1098 		keep_grabbing_mouse=0;
1099 	}
1100 	hide_window_MW(MW_TABMAP);
1101 	hide_window_MW(MW_CONSOLE);
1102 	show_window (game_root_win);
1103 	show_hud_windows();
1104 }
1105 
draw_ingame_interface(window_info * win)1106 static void draw_ingame_interface(window_info *win)
1107 {
1108 #ifdef OPENGL_TRACE
1109 CHECK_GL_ERRORS();
1110 #endif //OPENGL_TRACE
1111 	draw_hud_interface(win);
1112 	display_spells_we_have();
1113 #ifdef OPENGL_TRACE
1114 CHECK_GL_ERRORS();
1115 #endif //OPENGL_TRACE
1116 }
1117 
1118 
display_game_handler(window_info * win)1119 static int display_game_handler (window_info *win)
1120 {
1121 	static int main_count = 0;
1122 	static int times_FPS_below_3 = 0;
1123 	static int fps[5]={100};
1124 	static int shadows_were_disabled=0;
1125 	static int eye_candy_was_disabled=0;
1126 	unsigned char str[180];
1127 	int i;
1128 	int any_reflection = 0;
1129 	int mouse_rate;
1130 
1131 	if (!have_a_map) return 1;
1132 	if (yourself==-1) return 1; //we don't have ourselves
1133 
1134 	for(i=0; i<max_actors; i++)
1135 	{
1136         	if(actors_list[i] && actors_list[i]->actor_id == yourself)
1137 			break;
1138 	}
1139 	if(i > max_actors) return 1;//we still don't have ourselves
1140 
1141 #ifdef CLUSTER_INSIDES
1142 	current_cluster = get_actor_cluster();
1143 #endif // CLUSTER_INSIDES
1144 
1145 	main_count++;
1146 	last_count++;
1147 
1148 	if (fps[0] < 5)
1149 	{
1150 		mouse_rate = 1;
1151 		read_mouse_now = 1;
1152 	}
1153 	else if (fps[0] < 10)
1154 	{
1155 		mouse_rate = 3;
1156 	}
1157 	else if (fps[0] < 20)
1158 	{
1159 		mouse_rate = 6;
1160 	}
1161 	else if (fps[0] < 30)
1162 	{
1163 		mouse_rate = 10;
1164 	}
1165 	else if (fps[0] < 40)
1166 	{
1167 		mouse_rate = 15;
1168 	}
1169 	else
1170 	{
1171 		mouse_rate = 20;
1172 	}
1173 	if(mouse_rate > mouse_limit) {
1174 		mouse_rate = mouse_limit;
1175 	}
1176 	if (mouse_rate < 5)
1177 	{
1178 		mouse_rate = 5;
1179 	}
1180 	if ((main_count % mouse_rate) == 0)
1181 	{
1182 		read_mouse_now = 1;
1183 	}
1184 	else
1185 	{
1186 		read_mouse_now = 0;
1187 	}
1188 
1189 	// This window is a bit special since it's not fully 2D
1190 	Leave2DMode ();
1191 	glPushMatrix ();
1192 
1193     update_camera();
1194 
1195 	if (new_zoom_level != zoom_level)
1196 	{
1197 		if (new_zoom_level > zoom_level)
1198 			set_all_intersect_update_needed(main_bbox_tree);
1199 		zoom_level = new_zoom_level;
1200 		resize_root_window ();
1201 	}
1202 
1203 	move_camera ();
1204 	save_scene_matrix ();
1205 
1206 	CalculateFrustum ();
1207 	set_click_line();
1208 	any_reflection = find_reflection ();
1209 	CHECK_GL_ERRORS ();
1210 
1211 	reset_under_the_mouse();
1212 
1213 	if (!dungeon){
1214 		draw_global_light ();
1215 	} else {
1216 		draw_dungeon_light ();
1217 	}
1218 
1219 	if (skybox_update_delay < 1)
1220 		skybox_update_colors();
1221 	if (skybox_show_sky)
1222 	{
1223 		skybox_compute_z_position();
1224 		glPushMatrix();
1225 		glTranslatef(0.0, 0.0, skybox_get_z_position());
1226 		skybox_display();
1227 		glPopMatrix();
1228 	}
1229 
1230 	if (use_fog)
1231 		weather_render_fog();
1232 
1233 	// only draw scene lights if inside or it is night
1234 	if (dungeon || !is_day)
1235 	{
1236 		update_scene_lights ();
1237 		draw_lights ();
1238 	}
1239 	CHECK_GL_ERRORS ();
1240 
1241 	if (!dungeon && shadows_on && (is_day || lightning_falling))
1242 	{
1243 		render_light_view();
1244 		CHECK_GL_ERRORS ();
1245 	}
1246 
1247 	if (any_reflection > 1) // there are water tiles to display
1248 	{
1249 		draw_water_background();
1250 		CHECK_GL_ERRORS ();
1251 		if (show_reflection) display_3d_reflection ();
1252 	}
1253 	CHECK_GL_ERRORS ();
1254 	glClear(GL_DEPTH_BUFFER_BIT);
1255 
1256 	missiles_update();
1257 
1258 	if (!is_day)
1259 		weather_init_lightning_light();
1260 
1261 	if (!dungeon && shadows_on && (is_day || lightning_falling))
1262 	{
1263 		glNormal3f(0.0f,0.0f,1.0f);
1264 		if (use_fog && any_reflection) blend_reflection_fog();
1265 		draw_sun_shadowed_scene (any_reflection);
1266 	}
1267 	else
1268 	{
1269 		glNormal3f (0.0f,0.0f,1.0f);
1270 		if (any_reflection) {
1271 			blend_reflection_fog();
1272 			draw_lake_tiles ();
1273 		}
1274 
1275 		setup_cloud_texturing();
1276 		draw_tile_map();
1277 		CHECK_GL_ERRORS ();
1278 		display_2d_objects();
1279 		CHECK_GL_ERRORS();
1280 		anything_under_the_mouse(0, UNDER_MOUSE_NOTHING);
1281 		display_objects();
1282 		display_ground_objects();
1283 		display_actors(1, DEFAULT_RENDER_PASS);
1284 		display_alpha_objects();
1285 		display_blended_objects();
1286 	}
1287 
1288 	glMatrixMode(GL_PROJECTION);
1289 	glPushMatrix();
1290 	glLoadMatrixd(skybox_view);
1291 	glMatrixMode(GL_MODELVIEW);
1292 
1293 	weather_render_lightning();
1294 
1295 	glMatrixMode(GL_PROJECTION);
1296 	glPopMatrix();
1297 	glMatrixMode(GL_MODELVIEW);
1298 
1299 	CHECK_GL_ERRORS ();
1300 
1301 #ifdef DEBUG_TIME
1302 	light_idle();
1303 #endif // DEBUG_TIME
1304 
1305 	ec_idle();
1306 
1307 	CHECK_GL_ERRORS();
1308 
1309 	if (show_weather){
1310 		weather_render();
1311 	}
1312 
1313 	CHECK_GL_ERRORS ();
1314 	//particles should be last, we have no Z writting
1315 	display_particles ();
1316 
1317 	CHECK_GL_ERRORS ();
1318 	//we do this because we don't want the rain/particles to mess with our cursor
1319 
1320 	ec_draw();
1321 	CHECK_GL_ERRORS();
1322 
1323 	missiles_draw();
1324 	CHECK_GL_ERRORS();
1325 
1326 
1327 	last_texture = -1;
1328 
1329 	CHECK_GL_ERRORS ();
1330 
1331 	animate_map_markers();
1332 	display_map_markers();
1333 	display_map_marks();
1334 
1335 	Enter2DMode ();
1336 	//get the FPS, etc
1337 
1338 	if (next_fps_time<cur_time){
1339 		fps[4]=fps[3];
1340 		fps[3]=fps[2];
1341 		fps[2]=fps[1];
1342 		fps[1]=fps[0];
1343 		fps[0]=last_count*1000/(cur_time-next_fps_time+1000);
1344 		last_count=0;
1345 		next_fps_time=cur_time+1000;
1346 		fps_average=(fps[0]+fps[1]+fps[2]+fps[3]+fps[4])/5.0f;
1347 	}
1348 
1349 	if (!no_adjust_shadows)
1350 	{
1351 		if ((fps_average < 5.0f) || (max_fps != limit_fps))
1352 		{
1353 			times_FPS_below_3++;
1354 			if (((times_FPS_below_3 > 10) || (max_fps != limit_fps)) && (shadows_on || use_eye_candy ))
1355 			{
1356 				if (max_fps == limit_fps)
1357 					LOG_TO_CONSOLE(c_red1, (unsigned char*)low_framerate_str);
1358 				times_FPS_below_3 = 0;
1359 				if (shadows_on)
1360 				{
1361 					shadows_on = 0;
1362 					shadows_were_disabled=1;
1363 				}
1364 				if (use_eye_candy)
1365 				{
1366 					use_eye_candy = 0;
1367 					eye_candy_was_disabled = 1;
1368 				}
1369 			}
1370 		}
1371 		else
1372 		{
1373 			times_FPS_below_3 = 0;
1374 
1375 			if(shadows_were_disabled){
1376 				shadows_on = 1;
1377 				shadows_were_disabled=0;
1378 			}
1379 
1380 			if (eye_candy_was_disabled){
1381 				use_eye_candy = 1;
1382 				eye_candy_was_disabled = 0;
1383 			}
1384 		}
1385 	}
1386 	if (show_fps && !console_input_active_at_top())
1387 	{
1388 		int fps_y;
1389 #ifdef	DEBUG
1390 		int y_cnt = 10;
1391 		actor *me = get_our_actor ();
1392 
1393 		glColor3f (1.0f, 1.0f, 1.0f);
1394 		if(me){
1395  			safe_snprintf((char*)str,sizeof(str),"Busy: %i",me->busy);
1396 	 		draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1397 			safe_snprintf((char*)str,sizeof(str),"Command: %i",me->last_command);
1398  			draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1399 			safe_snprintf((char*)str,sizeof(str),"Coords: %-3i %3i",me->x_tile_pos, me->y_tile_pos);
1400  			draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1401 			safe_snprintf((char*)str,sizeof(str),"Coords: %.3g %.3g",me->x_pos, me->y_pos);
1402  			draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1403 		}
1404 
1405 		safe_snprintf((char*)str, sizeof(str), "lights: ambient=(%.2f,%.2f,%.2f,%.2f) diffuse=(%.2f,%.2f,%.2f,%.2f)",
1406 					  ambient_light[0], ambient_light[1], ambient_light[2], ambient_light[3],
1407 					  diffuse_light[0], diffuse_light[1], diffuse_light[2], diffuse_light[3]);
1408 		draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1409 		safe_snprintf((char*)str, sizeof(str), "weather: drops=%d/%d/%d/%d/%d/%d, int=%f, dty=%f, fog=%f",
1410 					  weather_get_drops_count(1),
1411 					  weather_get_drops_count(2),
1412 					  weather_get_drops_count(3),
1413 					  weather_get_drops_count(4),
1414 					  weather_get_drops_count(5),
1415 					  weather_get_drops_count(6),
1416 					  weather_get_intensity(),
1417 					  weather_get_density(),
1418 					  skybox_fog_density);
1419 		draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1420 		//LRNR: stats testing
1421 		safe_snprintf ((char*)str, sizeof(str),"Lights: %i", show_lights);
1422 		draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1423 		safe_snprintf((char*)str, sizeof(str), "E3D:%3d TOT:%3d", e3d_count, e3d_total);
1424 		draw_string_zoomed (0, win->len_y - hud_y - (y_cnt--) * win->default_font_len_y, str, 1, win->current_scale);
1425 		e3d_count= e3d_total= 0;
1426 #else	//DEBUG
1427 		glColor3f (1.0f, 1.0f, 1.0f);
1428 #endif	//DEBUG
1429 
1430 		if (fps_default_width == 0)
1431 		{
1432 			fps_default_width = get_string_width_zoom((const unsigned char*)"FPS: ",
1433 					win->font_category, win->current_scale)
1434 				+ 3 * get_max_digit_width_zoom(UI_FONT, win->current_scale)
1435 				+ win->default_font_max_len_x;
1436 			fps_center_x = win->len_x - hud_x - win->default_font_max_len_x -
1437 				3 * get_max_digit_width_zoom(UI_FONT, win->current_scale);
1438 		}
1439 
1440 		fps_y = 4 * win->current_scale;
1441 		if (max_fps != limit_fps)
1442 			safe_snprintf ((char*)str, sizeof(str), "FPS: -");
1443 		else
1444 			safe_snprintf ((char*)str, sizeof(str), "FPS: %i", fps[0]);
1445 		draw_string_zoomed_centered_around(fps_center_x, fps_y, str, 4, win->current_scale);
1446 
1447 		// Don't display UVP info if its off.  Either your system does not support it or you turned
1448 		// it off so why keep reminding you? Show only if enabled to help with fault diagnostics.
1449 		if (use_animation_program)
1450 		{
1451 			safe_snprintf((char*)str, sizeof(str), "UVP: %d", use_animation_program);
1452 			draw_string_zoomed_centered_around(fps_center_x, fps_y + win->default_font_len_y,
1453 				str, 4, win->current_scale);
1454 		}
1455 	}
1456 	else
1457 		fps_default_width = 0;
1458 	draw_spell_icon_strings(win);
1459 
1460 	CHECK_GL_ERRORS ();
1461 	/* Draw the chat text */
1462 	if (is_chat_shown() && (use_windowed_chat != 2))
1463 	{
1464 		int msg, offset, filter;
1465 		filter = use_windowed_chat == 1 ? current_filter : FILTER_ALL;
1466 		if (find_last_lines_time(&msg, &offset, filter, get_console_text_width()))
1467 		{
1468 			draw_messages(get_tab_bar_x(), get_tab_bar_y() + get_input_at_top_height(), display_text_buffer,
1469 				DISPLAY_TEXT_BUFFER_SIZE, filter, msg, offset, -1,
1470 				get_console_text_width(), 1 + get_text_height(get_lines_to_show(), CHAT_FONT, 1.0),
1471 				CHAT_FONT, 1.0, NULL);
1472 		}
1473 	}
1474 
1475 	anything_under_the_mouse (0, UNDER_MOUSE_NO_CHANGE);
1476 	CHECK_GL_ERRORS ();
1477 
1478 	draw_ingame_interface (win);
1479 
1480 	CHECK_GL_ERRORS ();
1481 
1482 	Leave2DMode ();
1483 
1484 	if(special_effects){
1485 		display_special_effects(1);
1486 	}
1487 	display_highlight_markers();
1488 
1489 	glEnable (GL_LIGHTING);
1490 
1491 	// Return to 2D mode to draw the other windows
1492 	glPopMatrix ();	// restore the state
1493 	Enter2DMode ();
1494 #ifdef OPENGL_TRACE
1495 CHECK_GL_ERRORS();
1496 #endif //OPENGL_TRACE
1497 
1498 	if (!get_show_window(get_id_MW(MW_CHAT)))
1499 		check_and_get_console_input(win->window_id);
1500 
1501 	return 1;
1502 }
1503 
check_quit_or_fullscreen(SDL_Keycode key_code,Uint16 key_mod)1504 int check_quit_or_fullscreen (SDL_Keycode key_code, Uint16 key_mod)
1505 {
1506 	// first, try to see if we pressed Alt+x or Ctrl+q, to quit.
1507 	if (KEY_DEF_CMP(K_QUIT, key_code, key_mod) || KEY_DEF_CMP(K_QUIT_ALT, key_code, key_mod))
1508 	{
1509 		exit_now = 1;
1510 	}
1511 	else if ((key_code == SDLK_RETURN || key_code == SDLK_KP_ENTER) && key_mod & KMOD_ALT)
1512 	{
1513 		toggle_full_screen ();
1514 	}
1515 	else
1516 	{
1517 		return 0;
1518 	}
1519 
1520 	return 1;
1521 }
1522 
key_to_char(Uint32 unikey)1523 Uint8 key_to_char (Uint32 unikey)
1524 {
1525 	return (Uint8)unikey;
1526 }
1527 
string_input(char * text,size_t maxlen,SDL_Keycode key_code,Uint32 key_unicode,Uint16 key_mod)1528 int string_input(char *text, size_t maxlen, SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
1529 {
1530 	size_t len = strlen(text);
1531 #ifndef OSX
1532 	if (key_code == SDLK_BACKSPACE)
1533 #else
1534 	if ((key_code == SDLK_BACKSPACE) || (key_unicode == 127))
1535 #endif
1536 	{
1537 		if (len > 0)
1538 			text[len-1] = '\0';
1539 		return 1;
1540 	}
1541 	if (is_printable (key_unicode))
1542 	{
1543 		if (len < (maxlen-1))
1544 		{
1545 			text[len] = key_unicode;
1546 			text[len+1] = '\0';
1547 		}
1548 		return 1;
1549 	}
1550 	return 0;
1551 }
1552 
hide_all_windows(void)1553 void hide_all_windows(void)
1554 {
1555 	/* Note: We don't watch for if a window is otherwise closed; alt+d to reopen only cares about the last
1556 	 * time it hid windows itself. If you alt+d to reopen windows, manually close them all, and alt+d
1557 	 * again, it'll reopen the same ones.
1558 	 */
1559 	int any_open = 0;
1560 	enum managed_window_enum i;
1561 
1562 	// control the minimap only if it is not pinned
1563 	if (pin_minimap)
1564 		clear_hideable_MW(MW_MINIMAP);
1565 	else
1566 		set_hideable_MW(MW_MINIMAP);
1567 
1568 	// check if any of the windows are shown, we will hide them all if so
1569 	for (i = 0; i < MW_MAX; i++)
1570 	{
1571 		if (!is_hideable_MW(i))
1572 			continue;
1573 		if (get_show_window_MW(i) > 0)
1574 		{
1575 			any_open = 1;
1576 			break;
1577 		}
1578 	}
1579 
1580 	for (i = 0; i < MW_MAX; i++)
1581 	{
1582 		if (!is_hideable_MW(i))
1583 			continue;
1584 		// at least one is shown so we hide them all
1585 		if (any_open)
1586 		{
1587 			// mark any that are open, so we re-open them next time
1588 			if (get_window_showable(get_id_MW(i)) > 0)
1589 			{
1590 				set_was_open_MW(i); // do first so overrideable in handler
1591 				hide_window_MW(i);
1592 				if (i == MW_BAGS)  // too many edge case to reopen a bag so close it fully
1593 					client_close_bag();
1594 			}
1595 			else
1596 				clear_was_open_MW(i);
1597 		}
1598 		// non were shown so we re-open any hidden last time
1599 		else
1600 		{
1601 			if (was_open_MW(i))
1602 				show_window_MW(i);
1603 		}
1604 	}
1605 }
1606 
1607 
toggle_sit_stand()1608 static void toggle_sit_stand()
1609 {
1610 	Uint8 str[4];
1611 	//Send message to server...
1612 	str[0]=SIT_DOWN;
1613 	str[1]=!you_sit;
1614 	my_tcp_send(my_socket,str,2);
1615 }
1616 
switch_action_mode(int mode)1617 void switch_action_mode(int mode)
1618 {
1619 	action_mode = mode;
1620 	set_quickbar_action_mode(mode);
1621 	set_items_action_mode(mode);
1622 }
1623 
1624 
1625 // keypress handler common to all in-game root windows (game_root_win,
1626 // console_root_win, and map_root_win)
keypress_root_common(SDL_Keycode key_code,Uint32 key_unicode,Uint16 key_mod)1627 int keypress_root_common (SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
1628 {
1629 	Uint16 alt_on = key_mod & KMOD_ALT;
1630 	Uint16 ctrl_on = key_mod & KMOD_CTRL;
1631 #ifdef DEBUG
1632 	int i;
1633 	Uint32 _cur_time= SDL_GetTicks();
1634 	Uint16 shift_on = key_mod & KMOD_SHIFT;
1635 #endif
1636 
1637 	if(check_quit_or_fullscreen(key_code, key_mod))
1638 	{
1639 		return 1;
1640 	}
1641 	else if((key_code == SDLK_UP || key_code == SDLK_DOWN) && ctrl_on)
1642 	{
1643 		char *line;
1644 
1645 		if(key_code == SDLK_UP)
1646 		{
1647 			line = history_get_line_up();
1648 		}
1649 		else
1650 		{
1651 			line= history_get_line_down();
1652 		}
1653 		if(line != NULL)
1654 		{
1655 			put_string_in_input_field((unsigned char*)line);
1656 		}
1657 	}
1658 	else if(disconnected && !alt_on && !ctrl_on && !locked_to_console)
1659 	{
1660 		connect_to_server();
1661 	}
1662 	else if (KEY_DEF_CMP(K_PASTE, key_code, key_mod))
1663 	{
1664 		start_paste(NULL);
1665 	}
1666 #ifdef DEBUG
1667 	else if((key_code == SDLK_LEFT) && shift_on && ctrl_on && !alt_on)
1668 	{
1669 		for (i=0; i<ITEM_WEAR_START;i++) {
1670 			item_list[i].cooldown_rate = 60000;
1671 			item_list[i].cooldown_time = _cur_time + 60000;
1672 		}
1673 	}
1674 	else if((key_code == SDLK_DOWN) && shift_on && ctrl_on && !alt_on)
1675 	{
1676 		for(i=0; i<ITEM_WEAR_START;i++) {
1677 			item_list[i].cooldown_time -= 1000;
1678 		}
1679 	}
1680 	else if((key_code == SDLK_UP) && shift_on && ctrl_on && !alt_on)
1681 	{
1682 		for(i=0; i<ITEM_WEAR_START;i++) {
1683 			item_list[i].cooldown_time += 1000;
1684 		}
1685 	}
1686 	else if((key_code == SDLK_t) && shift_on && ctrl_on && !alt_on)
1687 	{
1688 		weather_add_lightning(rand()%5, -camera_x-100+rand()%200, -camera_y-100+rand()%200);
1689 	}
1690 	else if((key_code == SDLK_w) && shift_on && ctrl_on && alt_on)
1691 	{
1692 		if(real_game_minute >= 355) real_game_minute -=355; else real_game_minute +=  5;
1693 		new_minute();
1694 	}
1695 	else if((key_code == SDLK_q) && shift_on && ctrl_on && alt_on)
1696 	{
1697 		if(real_game_minute < 5) real_game_minute +=355; else real_game_minute -=  5;
1698 		new_minute();
1699 	}
1700 	else if((key_code == SDLK_w) && !shift_on && ctrl_on && alt_on)
1701 	{
1702 		if(real_game_minute >= 359) real_game_minute -=359; else real_game_minute +=  1;
1703 		new_minute();
1704 	}
1705 	else if((key_code == SDLK_q) && !shift_on && ctrl_on && alt_on)
1706 	{
1707 		if(real_game_minute < 1) real_game_minute +=359; else real_game_minute -=  1;
1708 		new_minute();
1709 	}
1710 	else if((key_code == SDLK_s) && shift_on && ctrl_on && alt_on)
1711 	{
1712 		skybox_init_defs(NULL);
1713 	}
1714 	else if((key_code == SDLK_f) && shift_on && ctrl_on && alt_on)
1715 	{
1716 		freeze_time = !freeze_time;
1717 		if (freeze_time)
1718 			LOG_TO_CONSOLE(c_green2, "Time freezed!");
1719 		else
1720 		{
1721 			LOG_TO_CONSOLE(c_green2, "Time unfreezed!");
1722 			new_minute();
1723 			new_second();
1724 		}
1725 	}
1726 	else if((key_code == SDLK_HOME) && shift_on && ctrl_on && !alt_on)
1727 	{
1728 		weather_set_area(0, -camera_x, -camera_y, 100.0, 1, 1.0, 10);
1729 	}
1730 	else if ((key_code == SDLK_END) && shift_on && ctrl_on && !alt_on)
1731 	{
1732 		weather_set_area(1, -camera_x, -camera_y, 100.0, 2, 1.0, 10);
1733 	}
1734 	else if((key_code == SDLK_z) && shift_on && ctrl_on && !alt_on)
1735 	{
1736 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_SMALL_MINE, (poor_man ? 6 : 10));
1737 	}
1738 	else if((key_code == SDLK_x) && shift_on && ctrl_on && !alt_on)
1739 	{
1740 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_MEDIUM_MINE, (poor_man ? 6 : 10));
1741 	}
1742 	else if((key_code == SDLK_c) && shift_on && ctrl_on && !alt_on)
1743 	{
1744 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_HIGH_EXPLOSIVE_MINE, (poor_man ? 6 : 10));
1745 	}
1746 	else if((key_code == SDLK_v) && shift_on && ctrl_on && !alt_on)
1747 	{
1748 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_TRAP, (poor_man ? 6 : 10));
1749 	}
1750 	else if((key_code == SDLK_b) && shift_on && ctrl_on && !alt_on)
1751 	{
1752 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_CALTROP, (poor_man ? 6 : 10));
1753 	}
1754 	else if((key_code == SDLK_n) && shift_on && ctrl_on && !alt_on)
1755 	{
1756 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_POISONED_CALTROP, (poor_man ? 6 : 10));
1757 	}
1758 	else if((key_code == SDLK_m) && shift_on && ctrl_on && !alt_on)
1759 	{
1760 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_MANA_BURNER, (poor_man ? 6 : 10));
1761 	}
1762 	else if((key_code == SDLK_j) && shift_on && ctrl_on && !alt_on)
1763 	{
1764 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_MANA_DRAINER, (poor_man ? 6 : 10));
1765 	}
1766 	else if((key_code == SDLK_k) && shift_on && ctrl_on && !alt_on)
1767 	{
1768 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_UNINVIZIBILIZER, (poor_man ? 6 : 10));
1769 	}
1770 	else if((key_code == SDLK_l) && shift_on && ctrl_on && !alt_on)
1771 	{
1772 		ec_create_mine_detonate(your_actor->x_pos + 0.25f, your_actor->y_pos + 0.25f, 0, MINE_TYPE_MAGIC_IMMUNITY_REMOVAL, (poor_man ? 6 : 10));
1773 	}
1774 #endif
1775 #ifdef DEBUG
1776     // scale the current actor
1777 	else if((key_code == SDLK_p) && shift_on && ctrl_on && !alt_on)
1778 	{
1779 		get_our_actor()->scale *= 1.05;
1780 	}
1781 	else if((key_code == SDLK_o) && shift_on && ctrl_on && !alt_on)
1782 	{
1783 		get_our_actor()->scale /= 1.05;
1784 	}
1785 	else if((key_code == SDLK_h) && shift_on && ctrl_on && !alt_on)
1786 	{
1787 		if (get_our_actor())
1788 		{
1789 			if (get_our_actor()->attached_actor < 0)
1790 				add_actor_attachment(get_our_actor()->actor_id, 200);
1791 			else
1792 				remove_actor_attachment(get_our_actor()->actor_id);
1793 		}
1794 	}
1795 #endif // DEBUG
1796 	// use quickbar items & spells
1797 	else if (action_item_keys(key_code, key_mod))
1798 	{
1799 	}
1800 	else if (action_spell_keys(key_code, key_mod))
1801 	{
1802 	}
1803 	else if (KEY_DEF_CMP(K_SUMMONINGMENU, key_code, key_mod))
1804 	{
1805 		int actor_to_touch = get_id_last_summoned();
1806 		if (actor_to_touch >= 0)
1807 			touch_player(actor_to_touch);
1808 	}
1809 	// hide all windows
1810 	else if (KEY_DEF_CMP(K_HIDEWINS, key_code, key_mod))
1811 	{
1812 		hide_all_windows();
1813 	}
1814 	// toggle options
1815 	else if (KEY_DEF_CMP(K_HEALTHBAR, key_code, key_mod))
1816 	{
1817 		view_health_bar = !view_health_bar;
1818 	}
1819 	else if (KEY_DEF_CMP(K_VIEWETHER, key_code, key_mod))
1820 	{
1821 		view_ether = !view_ether;
1822 	}
1823 	else if (KEY_DEF_CMP(K_ETHERBARS, key_code, key_mod))
1824 	{
1825 		view_ether_bar = !view_ether_bar;
1826 	}
1827 	else if (KEY_DEF_CMP(K_VIEWTEXTASOVERTEXT, key_code, key_mod))
1828 	{
1829 		view_chat_text_as_overtext = !view_chat_text_as_overtext;
1830 	}
1831 	else if (KEY_DEF_CMP(K_VIEWNAMES, key_code, key_mod))
1832 	{
1833 		view_names = !view_names;
1834 	}
1835 	else if (KEY_DEF_CMP(K_VIEWHP, key_code, key_mod))
1836 	{
1837 		view_hp = !view_hp;
1838 	}
1839 	else if (KEY_DEF_CMP(K_SHADOWS, key_code, key_mod))
1840 	{
1841 		clouds_shadows = !clouds_shadows;
1842 	}
1843 	else if (KEY_DEF_CMP(K_RANGINGLOCK, key_code, key_mod))
1844 	{
1845 		toggle_ranging_lock();
1846 	}
1847 	// open or close tabbed windows
1848 	else if (KEY_DEF_CMP(K_STATS, key_code, key_mod))
1849 	{
1850 		view_tab(MW_STATS, tab_stats_collection_id, STATS_TAB_STATS);
1851 	}
1852 	else if (KEY_DEF_CMP(K_SESSION, key_code, key_mod))
1853 	{
1854 		view_tab(MW_STATS, tab_stats_collection_id, STATS_TAB_SESSION);
1855 	}
1856 	else if (KEY_DEF_CMP(K_COUNTERS, key_code, key_mod))
1857 	{
1858 		view_tab(MW_STATS, tab_stats_collection_id, STATS_TAB_COUNTERS);
1859 	}
1860 	else if (KEY_DEF_CMP(K_KNOWLEDGE, key_code, key_mod))
1861 	{
1862 		view_tab(MW_STATS, tab_stats_collection_id, STATS_TAB_KNOWLEDGE);
1863 	}
1864 	else if (KEY_DEF_CMP(K_ENCYCLOPEDIA, key_code, key_mod))
1865 	{
1866 		view_tab(MW_HELP, tab_help_collection_id, HELP_TAB_ENCYCLOPEDIA);
1867 	}
1868 	else if (KEY_DEF_CMP(K_HELP, key_code, key_mod))
1869 	{
1870 		view_tab(MW_HELP, tab_help_collection_id, HELP_TAB_HELP);
1871 	}
1872 	else if (KEY_DEF_CMP(K_HELPSKILLS, key_code, key_mod))
1873 	{
1874 		view_tab(MW_HELP, tab_help_collection_id, HELP_TAB_SKILLS);
1875 	}
1876 	else if (KEY_DEF_CMP(K_RULES, key_code, key_mod))
1877 	{
1878 		view_tab(MW_HELP, tab_help_collection_id, HELP_TAB_RULES);
1879 	}
1880 	else if (KEY_DEF_CMP(K_NOTEPAD, key_code, key_mod))
1881 	{
1882 		view_tab(MW_INFO, tab_info_collection_id, INFO_TAB_NOTEPAD);
1883 	}
1884 	else if (KEY_DEF_CMP(K_BROWSERWIN, key_code, key_mod))
1885 	{
1886 		view_tab(MW_INFO, tab_info_collection_id, INFO_TAB_URLWIN);
1887 	}
1888 	// set action modes
1889 	else if (KEY_DEF_CMP(K_WALK, key_code, key_mod))
1890 	{
1891 		switch_action_mode(ACTION_WALK);
1892 	}
1893 	else if (KEY_DEF_CMP(K_LOOK, key_code, key_mod))
1894 	{
1895 		switch_action_mode(ACTION_LOOK);
1896 	}
1897 	else if (KEY_DEF_CMP(K_USE, key_code, key_mod))
1898 	{
1899 		switch_action_mode(ACTION_USE);
1900 	}
1901 	else if (KEY_DEF_CMP(K_AFK, key_code, key_mod))
1902 	{
1903 		if (!afk)
1904 		{
1905 			go_afk ();
1906 			last_action_time = cur_time - afk_time;
1907 		}
1908 		else
1909 		{
1910 			go_ifk ();
1911 		}
1912 	}
1913 	else if (KEY_DEF_CMP(K_TARGET_CLOSE, key_code, key_mod))
1914 	{
1915 		toggle_target_close_clicked_creature();
1916 	}
1917 	else if (KEY_DEF_CMP(K_SIT, key_code, key_mod))
1918 	{
1919 		toggle_sit_stand ();
1920 	}
1921 	else if (KEY_DEF_CMP(K_BROWSER, key_code, key_mod))
1922 	{
1923 		open_last_seen_url();
1924 	}
1925 	else if (key_code == SDLK_ESCAPE)
1926 	{
1927 		root_key_to_input_field (key_code, key_unicode, key_mod);
1928 	}
1929 	else if (KEY_DEF_CMP(K_NEXT_CHAT_TAB, key_code, key_mod))
1930 	{
1931 		next_channel_tab();
1932 	}
1933 	else if (KEY_DEF_CMP(K_PREV_CHAT_TAB, key_code, key_mod))
1934 	{
1935 		prev_channel_tab();
1936 	}
1937 	else if (KEY_DEF_CMP(K_WINDOWS_ON_TOP, key_code, key_mod))
1938 	{
1939 		change_windows_on_top(&windows_on_top);
1940 	}
1941 #ifdef PNG_SCREENSHOT
1942 	else if (KEY_DEF_CMP(K_SCREENSHOT, key_code, key_mod))
1943 	{
1944 		makeScreenShot();
1945 	}
1946 #endif
1947 	else if (KEY_DEF_CMP(K_OPAQUEWIN, key_code, key_mod))
1948 	{
1949 		if (top_SWITCHABLE_OPAQUE_window_drawn != -1)
1950 			windows_list.window[top_SWITCHABLE_OPAQUE_window_drawn].opaque ^= 1;
1951 	}
1952 	else if (KEY_DEF_CMP(K_REPEATSPELL, key_code, key_mod))	// REPEAT spell command
1953 	{
1954 		if ( !get_show_window_MW(MW_TRADE) )
1955 		{
1956 			repeat_spell();
1957 		}
1958 	}
1959 	else
1960 	{
1961 		enum managed_window_enum i;
1962 		for (i = 0; i < MW_MAX; i++)
1963 		{
1964 			if (match_keydef_MW(i, key_code, key_mod))
1965 			{
1966 				view_window(i);
1967 				return 1;
1968 			}
1969 		}
1970 		return 0; // nothing we can handle
1971 	}
1972 
1973 	return 1; // we handled it
1974 }
1975 
text_input_handler(SDL_Keycode key_code,Uint32 key_unicode,Uint16 key_mod)1976 int text_input_handler (SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
1977 {
1978 	Uint8 ch = key_to_char (key_unicode);
1979 
1980 	if (root_key_to_input_field(key_code, key_unicode, key_mod))
1981 	{
1982 		return 1;
1983 	}
1984 	// The following should only be reached when we hit an invalid key
1985 	// combo or for any reason we don't have a valid input widget.
1986 	else if (is_printable (ch) && input_text_line.len < MAX_TEXT_MESSAGE_LENGTH)
1987 	{
1988 		if (put_char_in_buffer(&input_text_line, ch, input_text_line.len))
1989 			check_owned_and_show_console_input(game_root_win);
1990 	}
1991 #ifndef OSX
1992 	else if (key_code == SDLK_BACKSPACE && input_text_line.len > 0)
1993 #else
1994 	else if (((key_code == SDLK_BACKSPACE) || (ch == 127)) && input_text_line.len > 0)
1995 #endif
1996 	{
1997 		input_text_line.len--;
1998 		if (input_text_line.data[input_text_line.len] == '\n'
1999 		|| input_text_line.data[input_text_line.len] == '\r') {
2000 			input_text_line.len--;
2001 		}
2002 		input_text_line.data[input_text_line.len] = '\0';
2003 	}
2004 	else if ((key_code == SDLK_RETURN || key_code == SDLK_KP_ENTER) && input_text_line.len > 0)
2005 	{
2006 		parse_input(input_text_line.data, input_text_line.len);
2007 		add_line_to_history(input_text_line.data, input_text_line.len);
2008 		clear_input_line();
2009 	}
2010 	else
2011 	{
2012 		// no clue what to do with this character
2013 		return 0;
2014 	}
2015 	return 1;
2016 }
2017 
keypress_game_handler(window_info * win,int mx,int my,SDL_Keycode key_code,Uint32 key_unicode,Uint16 key_mod)2018 static int keypress_game_handler (window_info *win, int mx, int my, SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
2019 {
2020 	// first try the keypress handler for all root windows
2021 	if ( keypress_root_common (key_code, key_unicode, key_mod) )
2022 	{
2023 		return 1;
2024 	}
2025 	else if (KEY_DEF_CMP(K_TURNLEFT, key_code, key_mod))
2026 	{
2027 		//Moved delay to my_tcp_send
2028 		Uint8 str[2];
2029 		str[0] = TURN_LEFT;
2030 		my_tcp_send (my_socket, str, 1);
2031 	}
2032 	else if (KEY_DEF_CMP(K_TURNRIGHT, key_code, key_mod))
2033 	{
2034 		Uint8 str[2];
2035 		str[0] = TURN_RIGHT;
2036 		my_tcp_send (my_socket, str, 1);
2037 	}
2038 	else if (KEY_DEF_CMP(K_ADVANCE, key_code, key_mod))
2039 	{
2040 		move_self_forward();
2041 	}
2042 	else if (KEY_DEF_CMP(K_ROTATELEFT, key_code, key_mod))
2043 	{
2044 		camera_rotation_speed = (first_person?-1:1)*normal_camera_rotation_speed / 800.0;
2045 		camera_rotation_deceleration = normal_camera_deceleration*0.5E-3;
2046 		camera_rotation_duration = 800;
2047 		if (fol_cam && !fol_cam_behind)
2048 		{
2049 			hold_camera += camera_kludge - last_kludge;
2050 			last_kludge = camera_kludge;
2051 		}
2052 	}
2053 	else if (KEY_DEF_CMP(K_FROTATELEFT, key_code, key_mod))
2054 	{
2055 		camera_rotation_speed = (first_person?-1:1)*fine_camera_rotation_speed / 200.0;
2056 		camera_rotation_speed /= 4.0;
2057 		camera_rotation_deceleration = normal_camera_deceleration*0.5E-3;
2058 		camera_rotation_duration = 200;
2059 		if (fol_cam && !fol_cam_behind)
2060 		{
2061 			hold_camera += camera_kludge - last_kludge;
2062 			last_kludge = camera_kludge;
2063 		}
2064 	}
2065 	else if (KEY_DEF_CMP(K_ROTATERIGHT, key_code, key_mod))
2066 	{
2067 		camera_rotation_speed = (first_person?1:-1)*normal_camera_rotation_speed / 800.0;
2068 		camera_rotation_deceleration = normal_camera_deceleration*0.5E-3;
2069 		camera_rotation_duration = 800;
2070 		if (fol_cam && !fol_cam_behind)
2071 		{
2072 			hold_camera += camera_kludge - last_kludge;
2073 			last_kludge = camera_kludge;
2074 		}
2075 	}
2076 	else if (KEY_DEF_CMP(K_FROTATERIGHT, key_code, key_mod))
2077 	{
2078 		camera_rotation_speed = (first_person?1:-1)*fine_camera_rotation_speed / 200.0;
2079 		camera_rotation_speed /= 4.0;
2080 		camera_rotation_deceleration = normal_camera_deceleration*0.5E-3;
2081 		camera_rotation_duration = 200;
2082 		if (fol_cam && !fol_cam_behind)
2083 		{
2084 			hold_camera += camera_kludge - last_kludge;
2085 			last_kludge = camera_kludge;
2086 		}
2087 	}
2088 	else if (KEY_DEF_CMP(K_CAMERAUP, key_code, key_mod))
2089 	{
2090 		camera_tilt_speed = -normal_camera_rotation_speed * 0.0005;
2091 		camera_tilt_duration += 100;
2092 		camera_tilt_deceleration = normal_camera_deceleration*0.5E-3;
2093 	}
2094 	else if (KEY_DEF_CMP(K_CAMERADOWN, key_code, key_mod))
2095 	{
2096 		camera_tilt_speed = normal_camera_rotation_speed * 0.0005;
2097 		camera_tilt_duration += 100;
2098 		camera_tilt_deceleration = normal_camera_deceleration*0.5E-3;
2099 	}
2100 	else if (KEY_DEF_CMP(K_ZOOMIN, key_code, key_mod))
2101 	{
2102 		if (camera_zoom_dir == -1)
2103 			camera_zoom_duration += 100;
2104 		else
2105 			camera_zoom_duration = 100;
2106 		camera_zoom_dir = -1;
2107 	}
2108 	if (KEY_DEF_CMP(K_ZOOMOUT, key_code, key_mod))
2109 	{
2110 		if (camera_zoom_dir == 1)
2111 			camera_zoom_duration += 100;
2112 		else
2113 			camera_zoom_duration = 100;
2114 		camera_zoom_dir = 1;
2115 	}
2116 	else if ((KEY_DEF_CMP(K_MAP, key_code, key_mod)) || (KEY_DEF_CMP(K_MARKFILTER, key_code, key_mod)))
2117 	{
2118 		// if K_MARKFILTER pressed, open the map window with the filter active
2119 		if (KEY_DEF_CMP(K_MARKFILTER, key_code, key_mod))
2120 			mark_filter_active = 1;
2121 		if ( switch_to_game_map () )
2122 		{
2123 			if (have_mouse) {toggle_have_mouse(); keep_grabbing_mouse=1;}
2124 			hide_window (game_root_win);
2125 			show_window_MW(MW_TABMAP);
2126 		}
2127 	}
2128 	else if (key_code == SDLK_F6)
2129 	{
2130 		if(!hud_x)
2131 		{
2132 			hud_x = HUD_MARGIN_X;
2133 			hud_y = HUD_MARGIN_Y;
2134 		}
2135 		else
2136 		{
2137 			hud_x=0;
2138 			hud_y=0;
2139 		}
2140 		resize_root_window ();
2141 		resize_all_root_windows(window_width, window_width, window_height, window_height);
2142 	}
2143 	else if (KEY_DEF_CMP(K_FIRST_PERSON, key_code, key_mod))
2144 	{
2145 		toggle_first_person();
2146 	}
2147 	else if (KEY_DEF_CMP(K_GRAB_MOUSE, key_code, key_mod))
2148 	{
2149 		toggle_have_mouse();
2150 	}
2151 	else if (KEY_DEF_CMP(K_EXTEND_CAM, key_code, key_mod))
2152 	{
2153 		toggle_ext_cam(&ext_cam);
2154 	}
2155 #ifdef PAWN
2156 	else if (key_code == SDLK_F8)
2157 	{
2158 		if (object_under_mouse != -1 && thing_under_the_mouse == UNDER_MOUSE_3D_OBJ && objects_list[object_under_mouse])
2159 		{
2160 			run_pawn_map_function("play_with_object_pos", "ii", object_under_mouse, key_mod & KMOD_SHIFT ? 1: 0);
2161 		}
2162 		else
2163 		{
2164 			run_pawn_server_function("pawn_test", "s", "meep!");
2165 		}
2166 	}
2167 #endif
2168 	else if (key_code == SDLK_F8)
2169 	{
2170 		static int ison = 0;
2171 		if ((ison) && (weather_get_intensity() > 0.01))
2172 		{
2173 			weather_set_area(1, -camera_x, -camera_y, 100.0, 2, 0, 0);
2174 			ison = 0;
2175 		}
2176 		else
2177 		{
2178 			weather_set_area(1, -camera_x, -camera_y, 100.0, 2, 1.0, 0);
2179 			ison = 1;
2180 		}
2181 	}
2182 
2183 	else if (key_code == SDLK_F9)
2184 	{
2185 		actor *me = get_actor_ptr_from_id (yourself);
2186 		if (key_mod & KMOD_SHIFT)
2187 			remove_fire_at_tile(me->x_pos * 2, me->y_pos * 2);
2188 		else
2189 			add_fire_at_tile(1, me->x_pos * 2, me->y_pos * 2, get_tile_height(me->x_tile_pos, me->y_tile_pos));
2190 	}
2191 #ifdef DEBUG
2192 	else if (key_code == SDLK_F10)
2193 	{
2194 		if (key_mod & KMOD_SHIFT)
2195 		{
2196 #ifdef NEW_SOUND
2197 			print_sound_types();
2198 			print_sound_samples();
2199 			print_sounds_list();
2200 			print_sound_sources();
2201 #endif //!NEW_SOUND
2202 		}
2203 		else if (key_mod & KMOD_ALT)
2204 		{
2205 			print_filter_list ();
2206 		}
2207 		else
2208 		{
2209 			int iwin;
2210 			widget_list *l;
2211 			for (iwin = 0; iwin < windows_list.num_windows; iwin++)
2212 			{
2213 				printf ("%s: id = %d, order = %d, parent = %d, pos = (%d, %d), cur_pos = (%d, %d), displayed = %d\n", windows_list.window[iwin].window_name, windows_list.window[iwin].window_id, windows_list.window[iwin].order, windows_list.window[iwin].pos_id, windows_list.window[iwin].pos_x, windows_list.window[iwin].pos_y, windows_list.window[iwin].cur_x, windows_list.window[iwin].cur_y, windows_list.window[iwin].displayed);
2214 				for (l = windows_list.window[iwin].widgetlist; l; l = l->next)
2215 					printf ("\t%d\n", l->id);
2216 			}
2217 		}
2218 	}
2219 	else if (key_code == SDLK_F11)
2220 	{
2221 		unload_texture_cache();
2222 	}
2223 	else if (key_code == SDLK_F12)
2224 	{
2225 		dump_texture_cache();
2226 	}
2227 #endif	/* DEBUG */
2228 	// END OF TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2229 	else
2230 	{
2231 		Uint8 ch = key_to_char (key_unicode);
2232 
2233 		if (ch == '`' || KEY_DEF_CMP(K_CONSOLE, key_code, key_mod))
2234 		{
2235 			if (have_mouse) {toggle_have_mouse(); keep_grabbing_mouse=1;}
2236 			hide_window (game_root_win);
2237 			show_window_MW(MW_CONSOLE);
2238 		}
2239 		// see if the common text handler can deal with it
2240 		else if ( !text_input_handler (key_code, key_unicode, key_mod) )
2241 		{
2242 			// nothing we can handle
2243 			return 0;
2244 		}
2245 	}
2246 
2247 	// we handled it, return 1 to let the window manager know
2248 	return 1;
2249 }
2250 
do_keypress(el_key_def key)2251 void do_keypress(el_key_def key)
2252 {
2253 	if (game_root_win >= 0)
2254 	{
2255 		window_info *win = &windows_list.window[game_root_win];
2256 		if (win != NULL)
2257 			keypress_game_handler(win, 0, 0, key.key_code, 0, key.key_mod);
2258 	}
2259 }
2260 
show_game_handler(window_info * win)2261 static int show_game_handler (window_info *win)
2262 {
2263 	init_hud_interface (HUD_INTERFACE_GAME);
2264 	show_hud_windows();
2265 	if (use_windowed_chat == 1)
2266 	{
2267 		if (is_chat_shown())
2268 			display_tab_bar();
2269 		else
2270 			hide_window(tab_bar_win);
2271 	}
2272 	set_all_intersect_update_needed(main_bbox_tree); // redraw the scene
2273 	return 1;
2274 }
2275 
resize_game_root_handler(window_info * win,int width,int height)2276 static int resize_game_root_handler(window_info *win, int width, int height)
2277 {
2278 	if (get_show_window(win->window_id))
2279 	{
2280 		init_hud_interface (HUD_INTERFACE_GAME);
2281 		set_all_intersect_update_needed(main_bbox_tree); // redraw the scene
2282 	}
2283 	// Recalculate FPS width
2284 	fps_default_width = 0;
2285 	return 1;
2286 }
2287 
ui_scale_game_root_handler(window_info * win)2288 static int ui_scale_game_root_handler(window_info* win)
2289 {
2290 	// Recalculate FPS width
2291 	fps_default_width = 0;
2292 	return 1;
2293 }
2294 
change_game_root_font_handler(window_info * win,font_cat cat)2295 static int change_game_root_font_handler(window_info *win, font_cat cat)
2296 {
2297 	if (cat != UI_FONT)
2298 		return 0;
2299 	ui_scale_game_root_handler(win);
2300 	return 1;
2301 }
2302 
create_game_root_window(int width,int height)2303 void create_game_root_window (int width, int height)
2304 {
2305 	if (game_root_win < 0)
2306 	{
2307 		game_root_win = create_window ("Game", -1, -1, 0, 0, width, height, ELW_USE_UISCALE|ELW_TITLE_NONE|ELW_SHOW_LAST);
2308 
2309 		set_window_handler (game_root_win, ELW_HANDLER_DISPLAY, &display_game_handler);
2310 		set_window_handler (game_root_win, ELW_HANDLER_CLICK, &click_game_handler);
2311 		set_window_handler (game_root_win, ELW_HANDLER_MOUSEOVER, &mouseover_game_handler);
2312 		set_window_handler (game_root_win, ELW_HANDLER_KEYPRESS, (int (*)())&keypress_game_handler);
2313 		set_window_handler (game_root_win, ELW_HANDLER_SHOW, &show_game_handler);
2314 		set_window_handler (game_root_win, ELW_HANDLER_AFTER_SHOW, &update_have_display);
2315 		set_window_handler (game_root_win, ELW_HANDLER_HIDE, &update_have_display);
2316 		set_window_handler (game_root_win, ELW_HANDLER_RESIZE, &resize_game_root_handler);
2317 		set_window_handler (game_root_win, ELW_HANDLER_UI_SCALE, &ui_scale_game_root_handler);
2318 		set_window_handler (game_root_win, ELW_HANDLER_FONT_CHANGE, &change_game_root_font_handler);
2319 
2320 		if (!have_console_input())
2321 		{
2322 			if (dark_channeltext == 1)
2323 				set_text_message_color (&input_text_line, 0.6f, 0.6f, 0.6f);
2324 			else if (dark_channeltext == 2)
2325 				set_text_message_color (&input_text_line, 0.16f, 0.16f, 0.16f);
2326 			else
2327 				set_text_message_color (&input_text_line, 1.0f, 1.0f, 1.0f);
2328 			create_console_input(game_root_win, 42, 0, height - get_input_default_height() - hud_y,
2329 				width - hud_x, get_input_default_height(), INPUT_DEFAULT_FLAGS);
2330 		}
2331 		set_console_input_onkey();
2332 		resize_root_window();
2333 
2334 #ifdef NEW_CURSOR
2335 		cursors_tex = load_texture_cached("textures/cursors2.dds", tt_gui);
2336 		//Emajekral's hi-color & big cursor code
2337 		if (!sdl_cursors) SDL_ShowCursor(0);
2338 #endif // NEW_CURSOR
2339 	}
2340 }
2341