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 "ultima/ultima8/world/actors/auto_firer_process.h"
25 #include "ultima/ultima8/world/actors/actor.h"
26 #include "ultima/ultima8/audio/audio_process.h"
27 #include "ultima/ultima8/kernel/kernel.h"
28 #include "ultima/ultima8/ultima8.h"
29 #include "ultima/ultima8/world/get_object.h"
30 
31 namespace Ultima {
32 namespace Ultima8 {
33 
DEFINE_RUNTIME_CLASSTYPE_CODE(AutoFirerProcess)34 DEFINE_RUNTIME_CLASSTYPE_CODE(AutoFirerProcess)
35 
36 AutoFirerProcess::AutoFirerProcess() : Process() {
37 	Actor *a = getControlledActor();
38 	if (a)
39 		_itemNum = a->getObjId();
40 	_type = 0x260; // CONSTANT !
41 	_startTicks = Kernel::get_instance()->getTickNum();
42 }
43 
run()44 void AutoFirerProcess::run() {
45 	if (Kernel::get_instance()->getTickNum() > _startTicks + 10) {
46 		Actor *a = getControlledActor();
47 
48 		if (!a) {
49 			terminate();
50 			return;
51 		}
52 
53 		uint16 weaponno = a->getActiveWeapon();
54 		const Item *wpn = getItem(weaponno);
55 		if (wpn && wpn->getShape() == 0x38d && wpn->getShapeInfo()->_weaponInfo) {
56 			const WeaponInfo *info = wpn->getShapeInfo()->_weaponInfo;
57 			int shotsleft;
58 			if (info->_ammoShape) {
59 				shotsleft = wpn->getQuality();
60 			} else if (info->_energyUse) {
61 				shotsleft = a->getMana() / info->_energyUse;
62 			} else {
63 				shotsleft = 1;
64 			}
65 			if (shotsleft > 0) {
66 				int32 x = 0;
67 				int32 y = 0;
68 				int32 z = 0;
69 				a->addFireAnimOffsets(x, y, z);
70 				a->fireWeapon(x, y, z, a->getDir(), info->_damageType, true);
71 
72 				AudioProcess *audioproc = AudioProcess::get_instance();
73 				if (audioproc && info->_sound)
74 					audioproc->playSFX(info->_sound, 0x80, a->getObjId(), 0, false);
75 			}
76 		}
77 		terminate();
78 	}
79 }
80 
saveData(Common::WriteStream * ws)81 void AutoFirerProcess::saveData(Common::WriteStream *ws) {
82 	Process::saveData(ws);
83 	ws->writeUint32LE(_startTicks);
84 }
85 
loadData(Common::ReadStream * rs,uint32 version)86 bool AutoFirerProcess::loadData(Common::ReadStream *rs, uint32 version) {
87 	if (!Process::loadData(rs, version)) return false;
88 
89 	_startTicks = rs->readUint32LE();
90 	return true;
91 }
92 
93 } // End of namespace Ultima8
94 } // End of namespace Ultima
95