[7f9806]: gemrb / core / Scriptable / Door.cpp  Maximize  Restore  History

Download this file

435 lines (387 with data), 10.8 kB

  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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003-2005 The GemRB Project
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "Scriptable/Door.h"
#include "strrefs.h"
#include "win32def.h"
#include "Audio.h"
#include "DisplayMessage.h"
#include "Game.h"
#include "GameData.h"
#include "Interface.h"
#include "Item.h"
#include "Map.h"
#include "Projectile.h"
#include "Spell.h"
#include "SpriteCover.h"
#include "TileMap.h"
#include "Video.h"
#include "GameScript/GSUtils.h"
#include "GUI/GameControl.h"
#include "Scriptable/InfoPoint.h"
#include "System/StringBuffer.h"
#include <cassert>
#include <cmath>
namespace GemRB {
#define YESNO(x) ( (x)?"Yes":"No")
Door::Door(TileOverlay* Overlay)
: Highlightable( ST_DOOR )
{
tiles = NULL;
tilecount = 0;
Flags = 0;
open = NULL;
closed = NULL;
open_ib = NULL;
oibcount = 0;
closed_ib = NULL;
cibcount = 0;
OpenSound[0] = 0;
CloseSound[0] = 0;
LockSound[0] = 0;
UnLockSound[0] = 0;
overlay = Overlay;
LinkedInfo[0] = 0;
OpenStrRef = (ieDword) -1;
}
Door::~Door(void)
{
if (Flags&DOOR_OPEN) {
if (closed) {
delete( closed );
}
} else {
if (open) {
delete( open );
}
}
if (tiles) {
free( tiles );
}
if (open_ib) {
free( open_ib );
}
if (closed_ib) {
free( closed_ib );
}
}
void Door::ImpedeBlocks(int count, Point *points, unsigned char value)
{
for(int i = 0;i<count;i++) {
unsigned char tmp = area->GetInternalSearchMap(points[i].x, points[i].y) & PATH_MAP_NOTDOOR;
area->SetInternalSearchMap(points[i].x, points[i].y, tmp|value);
}
}
void Door::UpdateDoor()
{
if (Flags&DOOR_OPEN) {
outline = open;
} else {
outline = closed;
}
// update the Scriptable position
Pos.x = outline->BBox.x + outline->BBox.w/2;
Pos.y = outline->BBox.y + outline->BBox.h/2;
unsigned char pmdflags;
if (Flags & DOOR_TRANSPARENT) {
pmdflags = PATH_MAP_DOOR_IMPASSABLE;
}
else {
//both door flags are needed here, one for transparency the other
//is for passability
pmdflags = PATH_MAP_DOOR_OPAQUE|PATH_MAP_DOOR_IMPASSABLE;
}
if (Flags &DOOR_OPEN) {
ImpedeBlocks(cibcount, closed_ib, 0);
ImpedeBlocks(oibcount, open_ib, pmdflags);
}
else {
ImpedeBlocks(oibcount, open_ib, 0);
ImpedeBlocks(cibcount, closed_ib, pmdflags);
}
InfoPoint *ip = area->TMap->GetInfoPoint(LinkedInfo);
if (ip) {
if (Flags&DOOR_OPEN) ip->Flags&=~INFO_DOOR;
else ip->Flags|=INFO_DOOR;
}
}
void Door::ToggleTiles(int State, int playsound)
{
int i;
int state;
if (State) {
state = !closedIndex;
if (playsound && ( OpenSound[0] != '\0' ))
core->GetAudioDrv()->Play( OpenSound );
} else {
state = closedIndex;
if (playsound && ( CloseSound[0] != '\0' ))
core->GetAudioDrv()->Play( CloseSound );
}
for (i = 0; i < tilecount; i++) {
overlay->tiles[tiles[i]]->tileIndex = (ieByte) state;
}
//set door_open as state
Flags = (Flags & ~DOOR_OPEN) | (State == !core->HasFeature(GF_REVERSE_DOOR) );
}
//this is the short name (not the scripting name)
void Door::SetName(const char* name)
{
strnlwrcpy( ID, name, 8 );
}
void Door::SetTiles(unsigned short* Tiles, int cnt)
{
if (tiles) {
free( tiles );
}
tiles = Tiles;
tilecount = cnt;
}
void Door::SetDoorLocked(int Locked, int playsound)
{
if (Locked) {
if (Flags & DOOR_LOCKED) return;
Flags|=DOOR_LOCKED;
if (playsound && ( LockSound[0] != '\0' ))
core->GetAudioDrv()->Play( LockSound );
}
else {
if (!(Flags & DOOR_LOCKED)) return;
Flags&=~DOOR_LOCKED;
if (playsound && ( UnLockSound[0] != '\0' ))
core->GetAudioDrv()->Play( UnLockSound );
}
}
int Door::IsOpen() const
{
int ret = core->HasFeature(GF_REVERSE_DOOR);
if (Flags&DOOR_OPEN) {
ret = !ret;
}
return ret;
}
//also mark actors to fix position
bool Door::BlockedOpen(int Open, int ForceOpen)
{
bool blocked;
int count;
Point *points;
blocked = false;
if (Open) {
count = oibcount;
points = open_ib;
} else {
count = cibcount;
points = closed_ib;
}
//getting all impeded actors flagged for jump
Region rgn;
rgn.w = 16;
rgn.h = 12;
for(int i = 0;i<count;i++) {
Actor** ab;
rgn.x = points[i].x*16;
rgn.y = points[i].y*12;
unsigned char tmp = area->GetInternalSearchMap(points[i].x, points[i].y) & PATH_MAP_ACTOR;
if (tmp) {
int ac = area->GetActorInRect(ab, rgn, false);
while(ac--) {
if (ab[ac]->GetBase(IE_DONOTJUMP)) {
continue;
}
ab[ac]->SetBase(IE_DONOTJUMP, DNJ_JUMP);
blocked = true;
}
if (ab) {
free(ab);
}
}
}
if ((Flags&DOOR_SLIDE) || ForceOpen) {
return false;
}
return blocked;
}
void Door::SetDoorOpen(int Open, int playsound, ieDword ID)
{
if (playsound) {
//the door cannot be blocked when opening,
//but the actors will be pushed
//BlockedOpen will mark actors to be pushed
if (BlockedOpen(Open,0) && !Open) {
//clear up the blocking actors
area->JumpActors(false);
return;
}
area->JumpActors(true);
}
if (Open) {
if (Trapped) {
AddTrigger(TriggerEntry(trigger_opened, ID));
} else {
AddTrigger(TriggerEntry(trigger_harmlessopened, ID));
}
// in PS:T, opening a door does not unlock it
if (!core->HasFeature(GF_REVERSE_DOOR)) {
SetDoorLocked(false,playsound);
}
} else {
if (Trapped) {
AddTrigger(TriggerEntry(trigger_closed, ID));
} else {
AddTrigger(TriggerEntry(trigger_harmlessclosed, ID));
}
}
ToggleTiles(Open, playsound);
//synchronising other data with the door state
UpdateDoor();
area->ActivateWallgroups(open_wg_index, open_wg_count, Flags&DOOR_OPEN);
area->ActivateWallgroups(closed_wg_index, closed_wg_count, !(Flags&DOOR_OPEN));
core->SetEventFlag(EF_TARGETMODE);
}
bool Door::TryUnlock(Actor *actor) {
if (!(Flags&DOOR_LOCKED)) return true;
// don't remove key in PS:T!
bool removekey = !core->HasFeature(GF_REVERSE_DOOR) && Flags&DOOR_KEY;
return Highlightable::TryUnlock(actor, removekey);
}
void Door::TryDetectSecret(int skill, ieDword actorID)
{
if (Type != ST_DOOR) return;
if (Visible()) return;
if (skill > (signed)DiscoveryDiff) {
Flags |= DOOR_FOUND;
core->PlaySound(DS_FOUNDSECRET);
AddTrigger(TriggerEntry(trigger_detected, actorID));
}
}
// return true if the door isn't secret or if it is, but was already discovered
bool Door::Visible()
{
return (!(Flags & DOOR_SECRET) || (Flags & DOOR_FOUND));
}
void Door::SetPolygon(bool Open, Gem_Polygon* poly)
{
if (Open) {
if (open)
delete( open );
open = poly;
} else {
if (closed)
delete( closed );
closed = poly;
}
}
void Door::SetNewOverlay(TileOverlay *Overlay) {
overlay = Overlay;
ToggleTiles(IsOpen(), false);
}
void Highlightable::SetTrapDetected(int x)
{
if(x == TrapDetected)
return;
TrapDetected = x;
if(TrapDetected) {
core->Autopause(AP_TRAP, this);
}
}
void Highlightable::TryDisarm(Actor *actor)
{
if (!Trapped || !TrapDetected) return;
int skill = actor->GetStat(IE_TRAPS);
if (skill/2+core->Roll(1,skill/2,0)>TrapRemovalDiff) {
AddTrigger(TriggerEntry(trigger_disarmed, actor->GetGlobalID()));
//trap removed
Trapped = 0;
displaymsg->DisplayConstantStringName(STR_DISARM_DONE, DMC_LIGHTGREY, actor);
int xp = actor->CalculateExperience(XP_DISARM, actor->GetXPLevel(1));
Game *game = core->GetGame();
game->ShareXP(xp, SX_DIVIDE);
} else {
displaymsg->DisplayConstantStringName(STR_DISARM_FAIL, DMC_LIGHTGREY, actor);
TriggerTrap(skill, actor->GetGlobalID());
}
ImmediateEvent();
}
void Door::TryPickLock(Actor *actor)
{
core->PlaySound(DS_PICKLOCK);
if (LockDifficulty == 100) {
if (OpenStrRef != (ieDword)-1) {
displaymsg->DisplayStringName(OpenStrRef, DMC_BG2XPGREEN, actor, IE_STR_SOUND|IE_STR_SPEECH);
} else {
displaymsg->DisplayConstantStringName(STR_DOOR_NOPICK, DMC_BG2XPGREEN, actor);
}
return;
}
if (actor->GetStat(IE_LOCKPICKING)<LockDifficulty) {
displaymsg->DisplayConstantStringName(STR_LOCKPICK_FAILED, DMC_BG2XPGREEN, actor);
AddTrigger(TriggerEntry(trigger_picklockfailed, actor->GetGlobalID()));
return;
}
SetDoorLocked( false, true);
displaymsg->DisplayConstantStringName(STR_LOCKPICK_DONE, DMC_LIGHTGREY, actor);
AddTrigger(TriggerEntry(trigger_unlocked, actor->GetGlobalID()));
ImmediateEvent();
int xp = actor->CalculateExperience(XP_LOCKPICK, actor->GetXPLevel(1));
Game *game = core->GetGame();
game->ShareXP(xp, SX_DIVIDE);
}
void Door::TryBashLock(Actor *actor)
{
//Get the strength bonus against lock difficulty
int bonus;
if (core->HasFeature(GF_3ED_RULES)) {
bonus = actor->GetAbilityBonus(IE_STR);
} else {
int str = actor->GetStat(IE_STR);
int strEx = actor->GetStat(IE_STREXTRA);
bonus = core->GetStrengthBonus(2, str, strEx); //BEND_BARS_LIFT_GATES
}
unsigned int roll = actor->LuckyRoll(1, 10, bonus, 0);
if(roll < LockDifficulty || LockDifficulty == 100) {
displaymsg->DisplayConstantStringName(STR_DOORBASH_FAIL, DMC_BG2XPGREEN, actor);
return;
}
displaymsg->DisplayConstantStringName(STR_DOORBASH_DONE, DMC_LIGHTGREY, actor);
SetDoorLocked(false, true);
Flags|=DOOR_BROKEN;
//This is ok, bashdoor also sends the unlocked trigger
AddTrigger(TriggerEntry(trigger_unlocked, actor->GetGlobalID()));
ImmediateEvent();
}
void Door::dump() const
{
StringBuffer buffer;
buffer.appendFormatted( "Debugdump of Door %s:\n", GetScriptName() );
buffer.appendFormatted( "Door Global ID: %d\n", GetGlobalID());
buffer.appendFormatted( "Position: %d.%d\n", Pos.x, Pos.y);
buffer.appendFormatted( "Door Open: %s\n", YESNO(IsOpen()));
buffer.appendFormatted( "Door Locked: %s\n", YESNO(Flags&DOOR_LOCKED));
buffer.appendFormatted( "Door Trapped: %s Difficulty: %d\n", YESNO(Trapped), TrapRemovalDiff);
if (Trapped) {
buffer.appendFormatted( "Trap Permanent: %s Detectable: %s\n", YESNO(Flags&DOOR_RESET), YESNO(Flags&DOOR_DETECTABLE) );
}
buffer.appendFormatted( "Secret door: %s (Found: %s)\n", YESNO(Flags&DOOR_SECRET),YESNO(Flags&DOOR_FOUND));
const char *Key = GetKey();
const char *name = "NONE";
if (Scripts[0]) {
name = Scripts[0]->GetName();
}
buffer.appendFormatted( "Script: %s, Key (%s) removed: %s, Dialog: %s\n", name, Key?Key:"NONE", YESNO(Flags&DOOR_KEY), Dialog );
Log(DEBUG, "Door", buffer);
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks