File: ags_collision_detector.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (209 lines) | stat: -rw-r--r-- 6,606 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * of the License, or(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/plugins/ags_collision_detector/ags_collision_detector.h"
#include "ags/shared/util/geometry.h"
#include "common/config-manager.h"

namespace AGS3 {
namespace Plugins {
namespace AGSCollisionDetector {

#define CD_NORMAL 0
#define CD_DEBUG 1

#define CD_OBJECT 0
#define CD_CHARACTER 1

#define CD_SYS_ERROR -2
#define CD_PARAM_ERROR -1
#define CD_NO_COLLISION 0
#define CD_COLLISION 1
#define CD_OK 0
#define CD_IGNORED 1

bool find_overlapping_area(Rect r1, Rect r2, Rect *overlap);

const char *AGSCollisionDetector::AGS_GetPluginName() {
	return "AGS Collision Detector";
}

void AGSCollisionDetector::AGS_EngineStartup(IAGSEngine *engine) {
	PluginBase::AGS_EngineStartup(engine);

	SCRIPT_METHOD(boundingBoxColDetect, AGSCollisionDetector::boundingBoxColDetect);
	SCRIPT_METHOD(dumpInfo, AGSCollisionDetector::dumpInfo);
	SCRIPT_METHOD(resetTransparencyThreshold, AGSCollisionDetector::resetTransparencyThreshold);
	SCRIPT_METHOD(resetTransparentColor, AGSCollisionDetector::resetTransparentColor);
	SCRIPT_METHOD(setDebugMode, AGSCollisionDetector::setDebugMode);
	SCRIPT_METHOD(setTransparencyThreshold, AGSCollisionDetector::setTransparencyThreshold);
	SCRIPT_METHOD(setTransparentColor, AGSCollisionDetector::setTransparentColor);
	SCRIPT_METHOD(spritePixelColDetect, AGSCollisionDetector::spritePixelColDetect);
	SCRIPT_METHOD(spriteSpriteColDetect, AGSCollisionDetector::spriteSpriteColDetect);
}

void AGSCollisionDetector::boundingBoxColDetect(ScriptMethodParams &params) {
	//	PARAMS8(int, id1, int, type1, int, slot1, int, scale1, int, id2, int, type2, int, slot2, int, scale2);

	// TODO
	warning("STUB: AGSCollisionDetector::boundingBoxColDetect is not implemented");
	params._result = CD_NO_COLLISION;
}

void AGSCollisionDetector::dumpInfo(ScriptMethodParams &params) {

	// TODO
	warning("STUB: AGSCollisionDetector::dumpInfo is not implemented");
	params._result = CD_IGNORED;
}

void AGSCollisionDetector::resetTransparencyThreshold(ScriptMethodParams &params) {
	transparencyTreshold = 100;
	params._result = CD_OK;
}

void AGSCollisionDetector::resetTransparentColor(ScriptMethodParams &params) {
	transparentColor.r = 255;
	transparentColor.g = 0;
	transparentColor.b = 255;
	params._result = CD_OK;
}

void AGSCollisionDetector::setDebugMode(ScriptMethodParams &params) {
	//	PARAMS1(int, status);

	// TODO
	warning("STUB: AGSCollisionDetector::setDebugMode is not implemented");
	params._result = CD_OK;
}

void AGSCollisionDetector::setTransparencyThreshold(ScriptMethodParams &params) {
	PARAMS1(int, trans_thresh);

	if ((trans_thresh >= 0) && (trans_thresh <= 100)) {
		transparencyTreshold = trans_thresh;
		params._result = CD_OK;
	} else
		params._result = CD_PARAM_ERROR;
}

void AGSCollisionDetector::setTransparentColor(ScriptMethodParams &params) {
	PARAMS3(int, transparentRed, int, transparentGreen, int, transparentBlue);

	if ((transparentRed < 0) || (transparentRed > 255) ||
		(transparentGreen < 0) || (transparentGreen > 255) ||
		(transparentBlue < 0) || (transparentBlue > 255)) {
		params._result = CD_PARAM_ERROR;
		return;
	} else {
		transparentColor.r = transparentRed;
		transparentColor.g = transparentGreen;
		transparentColor.b = transparentBlue;
		params._result = CD_OK;
	}
}

void AGSCollisionDetector::spritePixelColDetect(ScriptMethodParams &params) {
	// PARAMS5(int, id, int, type, int, slot, int, pixelX, int, pixelY);

	// TODO
	warning("STUB: AGSCollisionDetector::spritePixelColDetect is not implemented");
	params._result = CD_NO_COLLISION;
}

void AGSCollisionDetector::spriteSpriteColDetect(ScriptMethodParams &params) {
	PARAMS6(int, id1, int, type1, int, slot1, int, id2, int, type2, int, slot2);

	int32 spr1_w, spr1_h; // sprite sizes
	int32 spr2_w, spr2_h;

	Rect r1, r2;
	Rect overlap(0, 0, 0, 0);

	BITMAP *sprite1 = _engine->GetSpriteGraphic(slot1);
	BITMAP *sprite2 = _engine->GetSpriteGraphic(slot2);
	_engine->GetBitmapDimensions(sprite1, &spr1_w, &spr1_h, nullptr);
	_engine->GetBitmapDimensions(sprite2, &spr2_w, &spr2_h, nullptr);

	if (type1 == CD_OBJECT) { // object
		if (id1 < _engine->GetNumObjects()) {
			AGSObject *obj1 = _engine->GetObject(id1);
			r1.Left = obj1->x;
			r1.Top = obj1->y - spr1_h;
			r1.Right = obj1->x + spr1_w;
			r1.Bottom = obj1->y;
		}
	} else { // character
		if (id1 < _engine->GetNumCharacters()) {
			AGSCharacter *ch1 = _engine->GetCharacter(id1);
			r1.Left = ch1->x - (spr1_w / 2);
			r1.Top = ch1->y - spr1_h;
			r1.Right = ch1->x + (spr1_w / 2);
			r1.Bottom = ch1->y;
		}
	}

	if (type2 == CD_OBJECT) { // object
		if (id2 < _engine->GetNumObjects()) {
			AGSObject *obj2 = _engine->GetObject(id2);
			r2.Left = obj2->x;
			r2.Top = obj2->y - spr2_h;
			r2.Right = obj2->x + spr2_w;
			r2.Bottom = obj2->y;
		}
	} else { // character
		if (id2 < _engine->GetNumCharacters()) {
			AGSCharacter *ch2 = _engine->GetCharacter(id2);
			r2.Left = ch2->x - (spr2_w / 2);
			r2.Top = ch2->y - spr2_h;
			r2.Right = ch2->x + (spr2_w / 2);
			r2.Bottom = ch2->y;
		}
	}

	// WORKAROUND for bee sprite in Bear Story
	if ((ConfMan.get("gameid") == "bear") && (slot1 == 87)) {
		r1.Bottom -= 9;
	}

	if (find_overlapping_area(r1, r2, &overlap)) {
		params._result = CD_COLLISION;
		return;
	}

	params._result = CD_NO_COLLISION;
}

bool find_overlapping_area(Rect r1, Rect r2, Rect *overlap) {
	overlap->Left = MAX(r1.Left, r2.Left);
	overlap->Top = MAX(r1.Top, r2.Top);
	overlap->Right = MIN(r1.Right, r2.Right);
	overlap->Bottom = MIN(r1.Bottom, r2.Bottom);

	if (overlap->Left >= overlap->Right || overlap->Top >= overlap->Bottom) // degenerate rect means no overlap
		return false;

	return true;
}

} // namespace AGSCollisionDetector
} // namespace Plugins
} // namespace AGS3