aboutsummaryrefslogtreecommitdiff
path: root/sword1/eventman.cpp
blob: 0fc60cb881683d3b77717096f1d204716b44bc09 (plain)
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003-2005 The ScummVM 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "sword1/eventman.h"
#include "sword1/sworddefs.h"
#include "common/util.h"

namespace Sword1 {

EventManager::EventManager(void) {
	for (uint8 cnt = 0; cnt < TOTAL_EVENT_SLOTS; cnt++)
		_eventPendingList[cnt].delay = _eventPendingList[cnt].eventNumber = 0;
}

void EventManager::serviceGlobalEventList(void) {
	for (uint8 slot = 0; slot < TOTAL_EVENT_SLOTS; slot++)
		if (_eventPendingList[slot].delay)
			_eventPendingList[slot].delay--;
}

void EventManager::checkForEvent(Object *compact) {
	for (uint8 objCnt = 0; objCnt < O_TOTAL_EVENTS; objCnt++) {
		if (compact->o_event_list[objCnt].o_event)
			for (uint8 globCnt = 0; globCnt < TOTAL_EVENT_SLOTS; globCnt++) {
				if (_eventPendingList[globCnt].delay &&
					(_eventPendingList[globCnt].eventNumber == compact->o_event_list[objCnt].o_event)) {
						compact->o_logic = LOGIC_script;      //force into script mode
						_eventPendingList[globCnt].delay = 0; //started, so remove from queue
						compact->o_tree.o_script_level++;
						compact->o_tree.o_script_id[compact->o_tree.o_script_level] = 
							compact->o_event_list[objCnt].o_event_script;
						compact->o_tree.o_script_pc[compact->o_tree.o_script_level] = 
							compact->o_event_list[objCnt].o_event_script;
				}
			}
	}
}

bool EventManager::eventValid(int32 event) {
	for (uint8 slot = 0; slot < TOTAL_EVENT_SLOTS; slot++)
		if ((_eventPendingList[slot].eventNumber == event) &&
			(_eventPendingList[slot].delay))
			return true;
	return false;
}

int EventManager::fnCheckForEvent(Object *cpt, int32 id, int32 pause) {
	if (pause) {
		cpt->o_pause = pause;
		cpt->o_logic = LOGIC_pause_for_event;
		return SCRIPT_STOP;
	}

	for (uint8 objCnt = 0; objCnt < O_TOTAL_EVENTS; objCnt++) {
		if (cpt->o_event_list[objCnt].o_event)
			for (uint8 globCnt = 0; globCnt < TOTAL_EVENT_SLOTS; globCnt++) {
				if (_eventPendingList[globCnt].delay &&
					(_eventPendingList[globCnt].eventNumber == cpt->o_event_list[objCnt].o_event)) {
						cpt->o_logic = LOGIC_script;      //force into script mode
						_eventPendingList[globCnt].delay = 0; //started, so remove from queue
						cpt->o_tree.o_script_level++;
						cpt->o_tree.o_script_id[cpt->o_tree.o_script_level] = 
							cpt->o_event_list[objCnt].o_event_script;
						cpt->o_tree.o_script_pc[cpt->o_tree.o_script_level] = 
							cpt->o_event_list[objCnt].o_event_script;
						return SCRIPT_STOP;
				}
			}
	}
	return SCRIPT_CONT;
}

void EventManager::fnIssueEvent(Object *compact, int32 id, int32 event, int32 delay) {
	uint8 evSlot = 0;
	while (_eventPendingList[evSlot].delay)
		evSlot++;
	if (evSlot >= TOTAL_EVENT_SLOTS)
		error("EventManager ran out of event slots!");
	_eventPendingList[evSlot].delay = delay;
	_eventPendingList[evSlot].eventNumber = event;
}

} // End of namespace Sword1