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 #include "ultima/ultima8/world/actors/battery_charger_process.h"
24 #include "ultima/ultima8/world/actors/main_actor.h"
25 #include "ultima/ultima8/kernel/kernel.h"
26 #include "ultima/ultima8/world/get_object.h"
27 #include "ultima/ultima8/world/world.h"
28 #include "ultima/ultima8/audio/audio_process.h"
29 
30 
31 namespace Ultima {
32 namespace Ultima8 {
33 
34 DEFINE_RUNTIME_CLASSTYPE_CODE(BatteryChargerProcess)
35 
36 // These SFX IDs are the same in both No Regret and No Remorse.
37 static const uint16 CHARGE_START_SFX = 0xa4;
38 static const uint16 CHARGE_GOING_SFX = 0x10b;
39 
BatteryChargerProcess()40 BatteryChargerProcess::BatteryChargerProcess() : Process() {
41 	MainActor *avatar = dynamic_cast<MainActor *>(getActor(World::get_instance()->getControlledNPCNum()));
42 	if (!avatar) {
43 		_itemNum = 0;
44 		_targetMaxEnergy = 0;
45 	} else {
46 		_itemNum = avatar->getObjId();
47 		_targetMaxEnergy = avatar->getMaxEnergy();
48 		AudioProcess *audio = AudioProcess::get_instance();
49 		if (audio) {
50 			audio->playSFX(CHARGE_START_SFX, 0x80, _itemNum, 1, false);
51 		}
52 	}
53 	_type = 0x254; // CONSTANT!
54 }
55 
run()56 void BatteryChargerProcess::run() {
57 	MainActor *avatar = dynamic_cast<MainActor *>(getActor(World::get_instance()->getControlledNPCNum()));
58 	AudioProcess *audio = AudioProcess::get_instance();
59 
60 	if (!avatar || avatar->isDead() || avatar->getMana() >= _targetMaxEnergy) {
61 		// dead or finished healing or switched to robot
62 		terminate();
63 		if (audio)
64 			audio->stopSFX(CHARGE_START_SFX, _itemNum);
65 		return;
66 	}
67 
68 	if (!audio->isSFXPlayingForObject(CHARGE_GOING_SFX, _itemNum))
69 		audio->playSFX(CHARGE_GOING_SFX, 0x80, _itemNum, 1);
70 
71 	uint16 newEnergy = avatar->getMana() + 25;
72 	if (newEnergy > _targetMaxEnergy)
73 		newEnergy = _targetMaxEnergy;
74 
75 	avatar->setMana(newEnergy);
76 }
77 
I_create(const uint8 * args,unsigned int)78 uint32 BatteryChargerProcess::I_create(const uint8 *args, unsigned int /*argsize*/) {
79 	return Kernel::get_instance()->addProcess(new BatteryChargerProcess());
80 }
81 
saveData(Common::WriteStream * ws)82 void BatteryChargerProcess::saveData(Common::WriteStream *ws) {
83 	Process::saveData(ws);
84 	ws->writeUint16LE(_targetMaxEnergy);
85 }
86 
loadData(Common::ReadStream * rs,uint32 version)87 bool BatteryChargerProcess::loadData(Common::ReadStream *rs, uint32 version) {
88 	if (!Process::loadData(rs, version)) return false;
89 	_targetMaxEnergy = rs->readUint16LE();
90 
91 	return true;
92 }
93 
94 } // End of namespace Ultima8
95 } // End of namespace Ultima
96