cleaned up some things, fixed some mem leaks
[riven-wahrk.git] / src / Hotspot.cpp
blobb839d468ebd7bf54a0975f20e1cda294e6caa405
2 /*
4 * Riven-Wahrk - a reimplementation of the game Riven, by Cyan
5 * Copyright (C) 2009 Tyler Genter <tylergenter@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <iostream>
24 #include "Hotspot.h"
25 #include "Game.h"
28 HsptRecord::HsptRecord (File *file, int addr, Game *game) {
30 blstId = file->readSShort (addr);
31 nameRec = file->readSShort (addr + 2);
32 left = file->readSShort (addr + 4);
33 top = file->readSShort (addr + 6);
34 right = file->readSShort (addr + 8);
35 bottom = file->readSShort (addr + 10);
37 mouseCursor = file->readUShort (addr + 14);
38 index = file->readUShort (addr + 16);
40 isZip = file->readUShort (addr + 20);
41 isEnabled = true;
43 script = new Script (file, addr + 22, game);
44 length = script->getSize ()*2 + 22; // getSize() returns number of shorts, so *2
48 HsptRecord *Hotspot::findRecord (uint16_t blst) {
49 if (records.empty())
50 return NULL;
52 map<int,HsptRecord>::iterator iter = records.end();
54 do {
55 iter--;
56 if (iter->second.blstId == blst)
57 return &(iter->second);
58 } while (iter != records.begin());
59 return NULL;
62 void Hotspot::findRecord (int16_t x, int16_t y) {
63 if (records.empty()) {
64 current = NULL;
65 return;
68 map<int,HsptRecord>::iterator iter = records.end();
70 do {
71 iter--;
72 if (iter->second.isInside(x,y))
73 if ((!iter->second.isZip || (iter->second.isZip && game->zipMode)) &&
74 iter->second.isEnabled) {
75 current = &(iter->second);
76 return;
78 } while (iter != records.begin());
79 current = NULL;
82 void Hotspot::readBlst (int id) {
83 File file (game->getStack(), Resource::BLST, id);
85 uint16_t count = file.readUShort (0);
86 for (int i=0; i<count; i++) {
88 BlstRecord temp;
89 temp.enabled = (file.readUShort (2 + i*6 + 2) == 1);
90 temp.record = findRecord (file.readUShort (2 + i*6 + 4));
91 blst.insert (make_pair (file.readUShort (2 + i*6), temp));
96 Hotspot::Hotspot (Game *gameIn, int id) {
98 game = gameIn;
100 File file (game->getStack(), Resource::HSPT, id);
102 current = NULL;
104 uint16_t count = file.readUShort (0);
106 int offset = 2;
108 for (int i=0; i<count; i++) {
109 HsptRecord temp (&file, offset, game);
110 records.insert (make_pair (temp.getIndex(), temp));
111 offset += temp.getLength();
114 readBlst (id);
118 void Hotspot::mouseMove (int16_t x, int16_t y) {
120 findRecord (x, y);
121 if (!current)
122 return;
124 current->script->runHandler (Handler::MouseWithin);
128 void Hotspot::mouseDown () {
129 if (current)
130 current->script->runHandler (Handler::MouseDown);
133 void Hotspot::mouseUp () {
134 if (current)
135 current->script->runHandler (Handler::MouseUp);