ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

costumeemi.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/endian.h"
00024 
00025 #include "engines/grim/debug.h"
00026 #include "engines/grim/costume.h"
00027 #include "engines/grim/grim.h"
00028 #include "engines/grim/resource.h"
00029 #include "engines/grim/actor.h"
00030 #include "engines/grim/emi/costumeemi.h"
00031 #include "engines/grim/emi/modelemi.h"
00032 #include "engines/grim/emi/skeleton.h"
00033 #include "engines/grim/emi/costume/emihead.h"
00034 #include "engines/grim/emi/costume/emianim_component.h"
00035 #include "engines/grim/emi/costume/emiluavar_component.h"
00036 #include "engines/grim/emi/costume/emiluacode_component.h"
00037 #include "engines/grim/emi/costume/emimesh_component.h"
00038 #include "engines/grim/emi/costume/emiskel_component.h"
00039 #include "engines/grim/emi/costume/emisprite_component.h"
00040 #include "engines/grim/emi/costume/emitexi_component.h"
00041 
00042 namespace Grim {
00043 
00044 EMICostume::EMICostume(const Common::String &fname, Actor *owner, Costume *prevCost) :
00045         Costume(fname, owner, prevCost), _wearChore(nullptr), _emiSkel(nullptr) {
00046 }
00047 
00048 void EMICostume::load(Common::SeekableReadStream *data) {
00049     Common::Array<Component *> components;
00050 
00051     _numChores = data->readUint32LE();
00052     _chores = new Chore *[_numChores];
00053     for (int i = 0; i < _numChores; i++) {
00054         uint32 nameLength;
00055         Component *prevComponent = nullptr;
00056         nameLength = data->readUint32LE();
00057         assert(nameLength < 32);
00058 
00059         char name[32];
00060         data->read(name, nameLength);
00061         float length = data->readFloatLE();
00062         int numTracks = data->readUint32LE();
00063 
00064         if (length == 1000)
00065             length = -1.0f;
00066         else
00067             length *= 1000;
00068 
00069         EMIChore *chore = new EMIChore(name, i, this, (int)length, numTracks);
00070         _chores[i] = chore;
00071 
00072         for (int k = 0; k < numTracks; k++) {
00073             int componentNameLength = data->readUint32LE();
00074 
00075             char *componentName = new char[componentNameLength];
00076             data->read(componentName, componentNameLength);
00077 
00078             data->readUint32LE();
00079             int parentID = data->readUint32LE();
00080             if (parentID == -1 && _prevCostume) {
00081                 // However, only the first item can actually share the
00082                 // node hierarchy with the previous costume, so flag
00083                 // that component so it knows what to do
00084                 if (i == 0)
00085                     parentID = -2;
00086                 prevComponent = _prevCostume->getComponent(0);
00087                 // Make sure that the component is valid
00088                 if (!prevComponent->isComponentType('M', 'M', 'D', 'L'))
00089                     prevComponent = nullptr;
00090             }
00091             // Actually load the appropriate component
00092             Component *component = loadEMIComponent(parentID < 0 ? nullptr : components[parentID], parentID, componentName, prevComponent);
00093             if (component) {
00094                 component->setCostume(this);
00095                 component->init();
00096                 chore->addComponent(component);
00097             }
00098 
00099             components.push_back(component);
00100 
00101             ChoreTrack &track = chore->_tracks[k];
00102             track.numKeys = data->readUint32LE();
00103             track.keys = new TrackKey[track.numKeys];
00104             track.component = component;
00105             track.compID = -1; // -1 means "look at .component"
00106 
00107             for (int j = 0; j < track.numKeys; j++) {
00108                 float time, value;
00109                 time = data->readFloatLE();
00110                 value = data->readFloatLE();
00111                 track.keys[j].time = (int)(time * 1000);
00112                 length = MAX(length, time * 1000);
00113                 track.keys[j].value = (int)value;
00114             }
00115             delete[] componentName;
00116         }
00117     }
00118 
00119     _numComponents = components.size();
00120     _components = new Component *[_numComponents];
00121     for (int i = 0; i < _numComponents; ++i) {
00122         _components[i] = components[i];
00123     }
00124 
00125     _head = new EMIHead(this);
00126 }
00127 
00128 void EMICostume::playChore(int num, uint msecs) {
00129     // FIXME: Original EMI can play multiple instances of a chore at the same time.
00130     EMIChore *chore = static_cast<EMIChore *>(_chores[num]);
00131     if (chore->isWearChore()) {
00132         setWearChore(chore);
00133     }
00134     Costume::playChore(num, msecs);
00135 }
00136 
00137 void EMICostume::playChoreLooping(int num, uint msecs) {
00138     // FIXME: Original EMI can play multiple instances of a chore at the same time.
00139     EMIChore *chore = static_cast<EMIChore *>(_chores[num]);
00140     if (chore->isWearChore()) {
00141         setWearChore(chore);
00142     }
00143     Costume::playChoreLooping(num, msecs);
00144 }
00145 
00146 Component *EMICostume::loadEMIComponent(Component *parent, int parentID, const char *name, Component *prevComponent) {
00147     assert(name[0] == '!');
00148     ++name;
00149 
00150     char type[5];
00151     tag32 tag = 0;
00152     memcpy(&tag, name, 4);
00153     memcpy(&type, name, 4);
00154     type[4] = 0;
00155     tag = FROM_BE_32(tag);
00156 
00157     name += 4;
00158 
00159     if (tag == MKTAG('m', 'e', 's', 'h')) {
00160         return new EMIMeshComponent(parent, parentID, name, prevComponent, tag, this);
00161     } else if (tag == MKTAG('s', 'k', 'e', 'l')) {
00162         return new EMISkelComponent(parent, parentID, name, prevComponent, tag);
00163     } else if (tag == MKTAG('t', 'e', 'x', 'i')) {
00164         return new EMITexiComponent(parent, parentID, name, prevComponent, tag);
00165     } else if (tag == MKTAG('a', 'n', 'i', 'm')) {
00166         return new EMIAnimComponent(parent, parentID, name, prevComponent, tag);
00167     } else if (tag == MKTAG('l', 'u', 'a', 'c')) {
00168         return new EMILuaCodeComponent(parent, parentID, name, prevComponent, tag);
00169     } else if (tag == MKTAG('l', 'u', 'a', 'v')) {
00170         return new EMILuaVarComponent(parent, parentID, name, prevComponent, tag);
00171     } else if (tag == MKTAG('s', 'p', 'r', 't')) {
00172         return new EMISpriteComponent(parent, parentID, name, prevComponent, tag);
00173     } else if (tag == MKTAG('s', 'h', 'a', 'd')) {
00174         Debug::warning(Debug::Costumes, "Actor::loadComponentEMI Implement SHAD-handling: %s" , name);
00175     } else if (tag == MKTAG('a', 'w', 'g', 't')) {
00176         Debug::warning(Debug::Costumes, "Actor::loadComponentEMI Implement AWGT-handling: %s" , name);
00177     } else if (tag == MKTAG('s', 'n', 'd', '2')) {
00178         // ignore, this is a leftover from an earlier engine.
00179     } else {
00180         error("Actor::loadComponentEMI missing tag: %s for %s", name, type);
00181     }
00182 
00183     return nullptr;
00184 }
00185 
00186 void EMICostume::draw() {
00187     bool drewMesh = false;
00188     for (Common::List<Chore*>::iterator it = _playingChores.begin(); it != _playingChores.end(); ++it) {
00189         Chore *c = (*it);
00190         if (!c->_playing)
00191             continue;
00192         for (int i = 0; i < c->_numTracks; ++i) {
00193             if (c->_tracks[i].component) {
00194                 c->_tracks[i].component->draw();
00195                 if (c->_tracks[i].component->isComponentType('m', 'e', 's', 'h'))
00196                     drewMesh = true;
00197             }
00198         }
00199     }
00200 
00201     if (_wearChore && !drewMesh) {
00202         _wearChore->getMesh()->draw();
00203     }
00204 }
00205 
00206 int EMICostume::update(uint time) {
00207     for (Common::List<Chore*>::iterator i = _playingChores.begin(); i != _playingChores.end(); ++i) {
00208         Chore *c = *i;
00209         c->update(time);
00210 
00211         for (int t = 0; t < c->_numTracks; ++t) {
00212             if (c->_tracks[t].component) {
00213                 c->_tracks[t].component->update(time);
00214             }
00215         }
00216 
00217         if (!c->isPlaying()) {
00218             i = _playingChores.erase(i);
00219             --i;
00220         }
00221     }
00222 
00223     return 0;
00224 }
00225 
00226 void EMICostume::saveState(SaveGame *state) const {
00227     Costume::saveState(state);
00228 
00229     for (int i = 0; i < _numChores; ++i) {
00230         EMIChore *chore = (EMIChore *)_chores[i];
00231         state->writeLESint32(chore->getId());
00232     }
00233 
00234     state->writeLESint32(_wearChore ? _wearChore->getChoreId() : -1);
00235 }
00236 
00237 bool EMICostume::restoreState(SaveGame *state) {
00238     bool ret = Costume::restoreState(state);
00239     if (ret) {
00240         if (state->saveMinorVersion() >= 11) {
00241             EMIChore::Pool &pool = EMIChore::getPool();
00242             for (int i = 0; i < _numChores; ++i) {
00243                 EMIChore *chore = (EMIChore *)_chores[i];
00244                 int id = state->readLESint32();
00245                 pool.removeObject(chore->getId());
00246                 EMIChore* oldChore = pool.getObject(id);
00247                 if (oldChore) {
00248                     pool.removeObject(id);
00249                     oldChore->setId(chore->getId());
00250                     pool.addObject(oldChore);
00251                 }
00252                 chore->setId(id);
00253                 pool.addObject(chore);
00254             }
00255         }
00256 
00257         if (state->saveMinorVersion() < 13) {
00258             // Used to be active texture IDs for materials. Materials are now
00259             // managed by the owner Actor of this Costume.
00260             for (uint i = 0; i < _materials.size(); ++i) {
00261                 state->readLESint32();
00262             }
00263         }
00264 
00265         int id = state->readLESint32();
00266         if (id >= 0) {
00267             EMIChore *chore = static_cast<EMIChore *>(_chores[id]);
00268             setWearChore(chore);
00269         }
00270     }
00271     return ret;
00272 }
00273 
00274 Material *EMICostume::findMaterial(const Common::String &name) {
00275     return _owner->findMaterial(name);
00276 }
00277 
00278 Material *EMICostume::loadMaterial(const Common::String &name, bool clamp) {
00279     MaterialPtr mat = _owner->loadMaterial(name, clamp);
00280     if (mat) {
00281         // Save a reference to the material, so it will not be freed during the
00282         // lifetime of this costume.
00283         if (Common::find(_materials.begin(), _materials.end(), mat) == _materials.end())
00284             _materials.push_back(mat);
00285     }
00286     return mat;
00287 }
00288 
00289 void EMICostume::setWearChore(EMIChore *chore) {
00290     if (chore != _wearChore) {
00291         _wearChore = chore;
00292 
00293         if (_emiSkel) {
00294             _emiSkel->reset();
00295         }
00296         _emiSkel = chore->getSkeleton();
00297     }
00298 }
00299 
00300 void EMICostume::setHead(const char *joint, const Math::Vector3d &offset) {
00301     static_cast<EMIHead *>(_head)->setJoint(joint, offset);
00302 }
00303 
00304 void EMICostume::setHeadLimits(float yawRange, float maxPitch, float minPitch) {
00305     static_cast<EMIHead *>(_head)->setLimits(yawRange, maxPitch, minPitch);
00306 }
00307 
00308 EMIModel *EMICostume::getEMIModel() const {
00309     if (!_wearChore)
00310         return nullptr;
00311     return _wearChore->getMesh()->_obj;
00312 }
00313 
00314 EMIModel *EMICostume::getEMIModel(int num) const {
00315     if (num >= _numChores) {
00316         return nullptr;
00317     }
00318     EMIChore *chore = static_cast<EMIChore *>(_chores[num]);
00319     if (chore == nullptr) {
00320         return nullptr;
00321     }
00322     EMIMeshComponent *mesh = chore->getMesh();
00323     if (mesh == nullptr) {
00324         return nullptr;
00325     }
00326     return mesh->_obj;
00327 }
00328 
00329 } // end of namespace Grim


Generated on Sat Apr 20 2019 05:02:15 for ResidualVM by doxygen 1.7.1
curved edge   curved edge