1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 
24 #include "sword1/eventman.h"
25 #include "sword1/sworddefs.h"
26 #include "common/textconsole.h"
27 #include "common/util.h"
28 
29 namespace Sword1 {
30 
EventManager()31 EventManager::EventManager() {
32 	for (uint8 cnt = 0; cnt < TOTAL_EVENT_SLOTS; cnt++)
33 		_eventPendingList[cnt].delay = _eventPendingList[cnt].eventNumber = 0;
34 }
35 
serviceGlobalEventList()36 void EventManager::serviceGlobalEventList() {
37 	for (uint8 slot = 0; slot < TOTAL_EVENT_SLOTS; slot++)
38 		if (_eventPendingList[slot].delay)
39 			_eventPendingList[slot].delay--;
40 }
41 
checkForEvent(Object * compact)42 void EventManager::checkForEvent(Object *compact) {
43 	for (uint8 objCnt = 0; objCnt < O_TOTAL_EVENTS; objCnt++) {
44 		if (compact->o_event_list[objCnt].o_event)
45 			for (uint8 globCnt = 0; globCnt < TOTAL_EVENT_SLOTS; globCnt++) {
46 				if (_eventPendingList[globCnt].delay &&
47 				        (_eventPendingList[globCnt].eventNumber == compact->o_event_list[objCnt].o_event)) {
48 					compact->o_logic = LOGIC_script;      //force into script mode
49 					_eventPendingList[globCnt].delay = 0; //started, so remove from queue
50 					compact->o_tree.o_script_level++;
51 					compact->o_tree.o_script_id[compact->o_tree.o_script_level] =
52 					    compact->o_event_list[objCnt].o_event_script;
53 					compact->o_tree.o_script_pc[compact->o_tree.o_script_level] =
54 					    compact->o_event_list[objCnt].o_event_script;
55 				}
56 			}
57 	}
58 }
59 
eventValid(int32 event)60 bool EventManager::eventValid(int32 event) {
61 	for (uint8 slot = 0; slot < TOTAL_EVENT_SLOTS; slot++)
62 		if ((_eventPendingList[slot].eventNumber == event) &&
63 		        (_eventPendingList[slot].delay))
64 			return true;
65 	return false;
66 }
67 
fnCheckForEvent(Object * cpt,int32 id,int32 pause)68 int EventManager::fnCheckForEvent(Object *cpt, int32 id, int32 pause) {
69 	if (pause) {
70 		cpt->o_pause = pause;
71 		cpt->o_logic = LOGIC_pause_for_event;
72 		return SCRIPT_STOP;
73 	}
74 
75 	for (uint8 objCnt = 0; objCnt < O_TOTAL_EVENTS; objCnt++) {
76 		if (cpt->o_event_list[objCnt].o_event)
77 			for (uint8 globCnt = 0; globCnt < TOTAL_EVENT_SLOTS; globCnt++) {
78 				if (_eventPendingList[globCnt].delay &&
79 				        (_eventPendingList[globCnt].eventNumber == cpt->o_event_list[objCnt].o_event)) {
80 					cpt->o_logic = LOGIC_script;      //force into script mode
81 					_eventPendingList[globCnt].delay = 0; //started, so remove from queue
82 					cpt->o_tree.o_script_level++;
83 					cpt->o_tree.o_script_id[cpt->o_tree.o_script_level] =
84 					    cpt->o_event_list[objCnt].o_event_script;
85 					cpt->o_tree.o_script_pc[cpt->o_tree.o_script_level] =
86 					    cpt->o_event_list[objCnt].o_event_script;
87 					return SCRIPT_STOP;
88 				}
89 			}
90 	}
91 	return SCRIPT_CONT;
92 }
93 
fnIssueEvent(Object * compact,int32 id,int32 event,int32 delay)94 void EventManager::fnIssueEvent(Object *compact, int32 id, int32 event, int32 delay) {
95 	uint8 evSlot = 0;
96 	while (_eventPendingList[evSlot].delay)
97 		evSlot++;
98 	if (evSlot >= TOTAL_EVENT_SLOTS)
99 		error("EventManager ran out of event slots");
100 	_eventPendingList[evSlot].delay = delay;
101 	_eventPendingList[evSlot].eventNumber = event;
102 }
103 
104 } // End of namespace Sword1
105