1 /*
2 Copyright (C) 2001-2013 The Exult Team
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22 
23 #include "game.h"
24 #include "Jawbone_gump.h"
25 #include "contain.h"
26 #include "gamewin.h"
27 #include "objiter.h"
28 #include "misc_buttons.h"
29 #include "jawbone.h"
30 #include "ignore_unused_variable_warning.h"
31 
32 const int toothx[19] = { 34, 32, 31, 31, 28, 31, 27, 31, 40, 50,
33                          57, 63, 72, 70, 75, 82, 83, 87, 0
34                        };
35 
36 const int toothy[19] = { 19, 30, 37, 44, 52, 57, 66, 77, 82, 84,
37                          80, 71, 69, 61, 50, 42, 36, 32, 0
38                        };
39 
Jawbone_gump(Jawbone_object * cont,int initx,int inity)40 Jawbone_gump::Jawbone_gump(
41     Jawbone_object *cont,   // Jawbone it represents.
42     int initx, int inity        // Coords. on screen.
43 ) : Gump(cont, initx, inity, game->get_shape("gumps/jawbone")),
44 	jawbone(cont) {
45 	set_object_area(TileRect(0, 0, 138, 116), 10, 109);
46 }
47 
48 
add(Game_object * obj,int mx,int my,int sx,int sy,bool dont_check,bool combine)49 bool Jawbone_gump::add(Game_object *obj, int mx, int my, int sx, int sy,
50                       bool dont_check, bool combine) {
51 	ignore_unused_variable_warning(mx, my, sx, sy);
52 	// Jawbone_object handles all the checks required
53 	return jawbone->add(obj, dont_check, combine);
54 }
55 
paint()56 void Jawbone_gump::paint() {
57 	// Paint gump itself
58 	paint_shape(x, y);
59 
60 	// Paint red "checkmark".
61 	paint_elems();
62 
63 	jawbone->find_teeth();
64 
65 	int i;  // Blame MSVC
66 	for (i = 0; i < 9; i++)
67 		if (jawbone->teeth[i])
68 			paint_tooth(i);
69 	for (i = 17; i > 8; i--)
70 		if (jawbone->teeth[i])
71 			paint_tooth(i);
72 }
73 
paint_tooth(int index)74 void Jawbone_gump::paint_tooth(int index) {
75 	ShapeID shape(game->get_shape("gumps/tooth"), index, SF_GUMPS_VGA);
76 
77 	int objx = toothx[index];
78 	int objy = toothy[index];
79 
80 	shape.paint_shape(x + objx, y + objy);
81 }
82 
find_object(int mx,int my)83 Game_object *Jawbone_gump::find_object(int mx, int my) {
84 	jawbone->find_teeth();
85 
86 	// get position relative to gump
87 	mx -= x;
88 	my -= y;
89 
90 	int i;  // Blame MSVC
91 
92 	// reverse of drawing order
93 	for (i = 9; i < 18; i++)
94 		if (jawbone->teeth[i] && on_tooth(mx, my, i)) {
95 			// set correct position (otherwise tooth won't be on mouse cursor)
96 			set_to_spot(jawbone->teeth[i], mx, my);
97 			return jawbone->teeth[i];
98 		}
99 	for (i = 8; i >= 0; i--)
100 		if (jawbone->teeth[i] && on_tooth(mx, my, i)) {
101 			// set correct position (otherwise tooth won't be on mouse cursor)
102 			set_to_spot(jawbone->teeth[i], mx, my);
103 			return jawbone->teeth[i];
104 		}
105 
106 	return nullptr;
107 }
108 
on_tooth(int sx,int sy,int index)109 bool Jawbone_gump::on_tooth(int sx, int sy, int index) {
110 	ShapeID sid(game->get_shape("gumps/tooth"), index, SF_GUMPS_VGA);
111 	Shape_frame *shape = sid.get_shape();
112 
113 	int objx = toothx[index];
114 	int objy = toothy[index];
115 
116 	TileRect r = gwin->get_shape_rect(shape, 0, 0);
117 
118 	return r.has_point(sx - objx, sy - objy) &&
119 	        shape->has_point(sx - objx, sy - objy);
120 }
121 
set_to_spot(Game_object * obj,int sx,int sy)122 void Jawbone_gump::set_to_spot(Game_object *obj, int sx, int sy) {
123 	// Get shape.
124 	Shape_frame *shape = obj->get_shape();
125 
126 	// Height and width
127 	int w = shape->get_width();
128 	int h = shape->get_height();
129 
130 	// Set object's position.
131 	obj->set_shape_pos(sx + shape->get_xleft() - w / 2,
132 	                   sy + shape->get_yabove() - h / 2);
133 }
134 
135