The Great Rename (Part 1)
[gemrb.git] / gemrb / plugins / ITMImporter / ITMImporter.cpp
blob5d1b677f62110d78fdc2cdc86835c792f2e6716e
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id$
22 #include "../../includes/win32def.h"
23 #include "../Core/Interface.h"
24 #include "../Core/EffectMgr.h"
25 #include "ITMImporter.h"
27 ITMImp::ITMImp(void)
29 str = NULL;
30 autoFree = false;
33 ITMImp::~ITMImp(void)
35 if (autoFree) {
36 delete str;
38 str = NULL;
41 bool ITMImp::Open(DataStream* stream, bool autoFree)
43 if (stream == NULL) {
44 return false;
46 if (this->autoFree) {
47 delete str;
49 str = stream;
50 this->autoFree = autoFree;
51 char Signature[8];
52 str->Read( Signature, 8 );
53 if (strncmp( Signature, "ITM V1 ", 8 ) == 0) {
54 version = 10;
55 } else if (strncmp( Signature, "ITM V1.1", 8 ) == 0) {
56 version = 11;
57 } else if (strncmp( Signature, "ITM V2.0", 8 ) == 0) {
58 version = 20;
59 } else {
60 printf( "[ITMImporter]: This file is not a valid ITM File\n" );
61 return false;
64 return true;
67 Item* ITMImp::GetItem(Item *s)
69 unsigned int i;
70 ieByte k1,k2,k3,k4;
72 if( !s) {
73 return NULL;
75 str->ReadDword( &s->ItemName );
76 str->ReadDword( &s->ItemNameIdentified );
77 str->ReadResRef( s->ReplacementItem );
78 str->ReadDword( &s->Flags );
79 str->ReadWord( &s->ItemType );
80 str->ReadDword( &s->UsabilityBitmask );
81 str->Read( s->AnimationType,2 ); //intentionally not reading word!
82 for (i=0;i<2;i++) {
83 if (s->AnimationType[i]==' ') {
84 s->AnimationType[i]=0;
87 str->Read( &s->MinLevel, 1 );
88 str->Read( &s->unknown1, 1 );
89 str->Read( &s->MinStrength,1 );
90 str->Read( &s->unknown2, 1 );
91 str->Read( &s->MinStrengthBonus, 1 );
92 str->Read( &k1,1 );
93 str->Read( &s->MinIntelligence, 1 );
94 str->Read( &k2,1 );
95 str->Read( &s->MinDexterity, 1 );
96 str->Read( &k3,1 );
97 str->Read( &s->MinWisdom, 1 );
98 str->Read( &k4,1 );
99 s->KitUsability=(k1<<24) | (k2<<16) | (k3<<8) | k4; //bg2/iwd2 specific
100 str->Read( &s->MinConstitution, 1 );
101 str->Read( &s->WeaProf, 1 ); //bg2 specific
102 str->Read( &s->MinCharisma, 1 );
103 str->Read( &s->unknown3, 1 );
104 str->ReadDword( &s->Price );
105 str->ReadWord( &s->StackAmount );
106 str->ReadResRef( s->ItemIcon );
107 str->ReadWord( &s->LoreToID );
108 str->ReadResRef( s->GroundIcon );
109 str->ReadDword( &s->Weight );
110 str->ReadDword( &s->ItemDesc );
111 str->ReadDword( &s->ItemDescIdentified );
112 str->ReadResRef( s->DescriptionIcon );
113 str->ReadDword( &s->Enchantment );
114 str->ReadDword( &s->ExtHeaderOffset );
115 str->ReadWord( &s->ExtHeaderCount );
116 str->ReadDword( &s->FeatureBlockOffset );
117 str->ReadWord( &s->EquippingFeatureOffset );
118 str->ReadWord( &s->EquippingFeatureCount );
120 s->Dialog[0] = 0;
121 s->DialogName = 0;
122 s->WieldColor = 0xffff;
123 memset( s->unknown, 0, 26 );
125 //skipping header data for iwd2
126 if (version == ITM_VER_IWD2) {
127 str->Read( s->unknown, 16 );
129 //pst data
130 if (version == ITM_VER_PST) {
131 str->ReadResRef( s->Dialog );
132 str->ReadDword( &s->DialogName );
133 ieWord WieldColor;
134 str->ReadWord( &WieldColor );
135 if (s->AnimationType[0]) {
136 s->WieldColor = WieldColor;
138 str->Read( s->unknown, 26 );
139 } else {
140 //all non pst
141 s->DialogName = core->GetItemDialStr(s->Name);
142 core->GetItemDialRes(s->Name, s->Dialog);
144 s->ItemExcl=core->GetItemExcl(s->Name);
146 s->ext_headers = core->GetITMExt( s->ExtHeaderCount );
148 for (i = 0; i < s->ExtHeaderCount; i++) {
149 str->Seek( s->ExtHeaderOffset + i * 56, GEM_STREAM_START );
150 GetExtHeader( s, s->ext_headers + i );
153 //48 is the size of the feature block
154 s->equipping_features = core->GetFeatures( s->EquippingFeatureCount);
156 str->Seek( s->FeatureBlockOffset + 48*s->EquippingFeatureOffset,
157 GEM_STREAM_START );
158 for (i = 0; i < s->EquippingFeatureCount; i++) {
159 GetFeature(s->equipping_features+i);
163 if (!core->IsAvailable( IE_BAM_CLASS_ID )) {
164 printf( "[ITMImporter]: No BAM Importer Available.\n" );
165 return NULL;
167 return s;
170 void ITMImp::GetExtHeader(Item *s, ITMExtHeader* eh)
172 ieByte tmpByte;
173 ieWord ProjectileType;
175 str->Read( &eh->AttackType,1 );
176 str->Read( &eh->IDReq,1 );
177 str->Read( &eh->Location,1 );
178 str->Read( &eh->unknown1,1 );
179 str->ReadResRef( eh->UseIcon );
180 str->Read( &eh->Target,1 );
181 str->Read( &tmpByte,1 );
182 if (!tmpByte) {
183 tmpByte = 1;
185 eh->TargetNumber = tmpByte;
186 str->ReadWord( &eh->Range );
187 str->ReadWord( &ProjectileType );
188 str->ReadWord( &eh->Speed );
189 str->ReadWord( &eh->THAC0Bonus );
190 str->ReadWord( &eh->DiceSides );
191 str->ReadWord( &eh->DiceThrown );
192 //if your compiler doesn't like this, then we need a ReadWordSigned
193 str->ReadWord( (ieWord *) &eh->DamageBonus );
194 str->ReadWord( &eh->DamageType );
195 str->ReadWord( &eh->FeatureCount );
196 str->ReadWord( &eh->FeatureOffset );
197 str->ReadWord( &eh->Charges );
198 str->ReadWord( &eh->ChargeDepletion );
199 str->ReadDword( &eh->RechargeFlags );
200 str->ReadWord( &eh->ProjectileAnimation );
201 //for some odd reasons 0 and 1 are the same
202 if (eh->ProjectileAnimation) {
203 eh->ProjectileAnimation--;
206 unsigned int i; //msvc6.0 can't cope with index variable scope
208 for (i = 0; i < 3; i++) {
209 str->ReadWord( &eh->MeleeAnimation[i] );
211 ieWord tmp;
213 i = 0;
214 str->ReadWord( &tmp ); //arrow
215 if (tmp) i|=PROJ_ARROW;
216 str->ReadWord( &tmp ); //xbow
217 if (tmp) i|=PROJ_BOLT;
218 str->ReadWord( &tmp ); //bullet
219 if (tmp) i|=PROJ_BULLET;
220 //this hack is required for Nordom's crossbow in PST
221 if (!i && (eh->AttackType==ITEM_AT_BOW) ) {
222 i|=PROJ_BOLT;
225 //this hack is required for the practicing arrows in BG1
226 if (!i && (eh->AttackType==ITEM_AT_PROJECTILE) ) {
227 //0->0
228 //1->1
229 //2->2
230 //3->4
231 i|=(1<<ProjectileType)>>1;
233 eh->ProjectileQualifier=i;
235 //48 is the size of the feature block
236 eh->features = core->GetFeatures(eh->FeatureCount);
237 str->Seek( s->FeatureBlockOffset + 48*eh->FeatureOffset, GEM_STREAM_START );
238 for (i = 0; i < eh->FeatureCount; i++) {
239 GetFeature(eh->features+i);
243 void ITMImp::GetFeature(Effect *fx)
245 EffectMgr* eM = ( EffectMgr* ) core->GetInterface( IE_EFF_CLASS_ID );
246 eM->Open( str, false );
247 eM->GetEffect( fx );
248 core->FreeInterface( eM );
251 #include "../../includes/plugindef.h"
253 GEMRB_PLUGIN(0xD913A54, "ITM File Importer")
254 PLUGIN_CLASS(IE_ITM_CLASS_ID, ITMImp)
255 END_PLUGIN()