npc.cpp 15.3 KB
Newer Older
1 2 3 4 5

#include "npc.hpp"

#include <components/esm/loadnpc.hpp>

Marc Zinnschlag's avatar
Marc Zinnschlag committed
6
#include "../mwmechanics/creaturestats.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
7
#include "../mwmechanics/npcstats.hpp"
Marc Zinnschlag's avatar
Marc Zinnschlag committed
8

9
#include "../mwworld/ptr.hpp"
10
#include "../mwworld/actiontalk.hpp"
11 12 13 14
#include "../mwworld/environment.hpp"
#include "../mwworld/world.hpp"

#include "../mwrender/cellimp.hpp"
15

16
#include "../mwmechanics/mechanicsmanager.hpp"
Jason Hooks's avatar
Jason Hooks committed
17
#include <OgreSceneNode.h>
18

19
namespace MWClass
20
{
Marc Zinnschlag's avatar
Marc Zinnschlag committed
21 22 23 24 25 26 27 28
    std::string Npc::getId (const MWWorld::Ptr& ptr) const
    {
        ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
            ptr.get<ESM::NPC>();

        return ref->base->mId;
    }

29 30 31
    void Npc::insertObj (const MWWorld::Ptr& ptr, MWRender::CellRenderImp& cellRender,
        MWWorld::Environment& environment) const
    {
Jason Hooks's avatar
Jason Hooks committed
32
		//Ogre::SceneNode *chest;
33 34
        ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
            ptr.get<ESM::NPC>();
Jason Hooks's avatar
Jason Hooks committed
35 36 37
		
		//Store scenenodes by npc's name + bodypart [0]  ,  npc's name + bodypart    [1]
		//Ex.                 Fargothchest               ,  Fargothneck
38 39 40

        assert (ref->base != NULL);

Jason Hooks's avatar
Jason Hooks committed
41
		std::string hairID = ref->base->hair;
42
        std::string headID = ref->base->head;
Jason Hooks's avatar
Jason Hooks committed
43
		std::string npcName = ref->base->name;
Jason Hooks's avatar
Jason Hooks committed
44
		std::cout << "NPC: " << npcName << "\n";
45 46 47 48 49 50

        //get the part of the bodypart id which describes the race and the gender
        std::string bodyRaceID = headID.substr(0, headID.find_last_of("head_") - 4);
        std::string headModel = "meshes\\" +
            environment.mWorld->getStore().bodyParts.find(headID)->model;

Jason Hooks's avatar
Jason Hooks committed
51 52
		std::string hairModel = "meshes\\" +
            environment.mWorld->getStore().bodyParts.find(hairID)->model;
53

Jason Hooks's avatar
Jason Hooks committed
54 55
        MWRender::Rendering rendering (cellRender, ref->ref);
        
56 57 58 59 60 61

        //TODO: define consts for each bodypart e.g. chest, foot, wrist... and put the parts in the
        // right place
        const ESM::BodyPart *bodyPart =
            environment.mWorld->getStore().bodyParts.search (bodyRaceID + "chest");

Jason Hooks's avatar
Jason Hooks committed
62 63 64 65 66
		//bodyPart->model->
		Ogre::Vector3 pos = Ogre::Vector3( 20, 20, 20);
		Ogre::Vector3 axis = Ogre::Vector3( 0, 0, 1);
		Ogre::Radian angle = Ogre::Radian(0);
		
Jason Hooks's avatar
Jason Hooks committed
67 68
		std::string addresses[6] = {"", "", "", "","", ""};
		std::string addresses2[6] = {"", "", "", "", "", ""};
Jason Hooks's avatar
Jason Hooks committed
69 70 71
		std::string upperleft[5] = {"", "", "", "", ""};
		std::string upperright[5] = {"", "", "", "", ""};
		std::string neckandup[5] = {"", "", "","",""};
Jason Hooks's avatar
Jason Hooks committed
72
		std::string empty[6] = {"", "", "", "","", ""};
Jason Hooks's avatar
Jason Hooks committed
73 74 75 76 77
		int numbers = 0;
		int uppernumbers = 0;
		int neckNumbers = 0;
		
        if (bodyPart){
Jason Hooks's avatar
Jason Hooks committed
78 79
		
           cellRender.insertMesh("meshes\\" + bodyPart->model, pos, axis, angle, npcName + "chest", addresses, numbers, true);   //2 0
Jason Hooks's avatar
Jason Hooks committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		   addresses2[numbers] = npcName + "chest";
		   addresses[numbers++] = npcName + "chest";
		   upperleft[uppernumbers] = npcName + "chest";
		   upperright[uppernumbers++] = npcName + "chest";
		   neckandup[neckNumbers++] = npcName + "chest";
		}
		   //std::cout << "GETTING NPC PART";
		//Orgre::SceneNode test = cellRender.getNpcPart();

		const ESM::BodyPart *upperleg = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "upper leg");
		const ESM::BodyPart *groin = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "groin");
		const ESM::BodyPart *arm = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "upper arm");
		const ESM::BodyPart *neck = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "neck");
		const ESM::BodyPart *knee = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "knee");
		const ESM::BodyPart *ankle = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "ankle");
		const ESM::BodyPart *foot = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "foot");
		const ESM::BodyPart *wrist = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "wrist");
		const ESM::BodyPart *forearm = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "forearm");
Jason Hooks's avatar
Jason Hooks committed
98 99 100
		const ESM::BodyPart *hand = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "hand.1st");
		const ESM::BodyPart *hands = environment.mWorld->getStore().bodyParts.search (bodyRaceID + "hands.1st");
		std::cout << "RACE" << bodyRaceID << "\n";
Jason Hooks's avatar
Jason Hooks committed
101 102 103 104 105 106

		Ogre::Vector3 pos2 = Ogre::Vector3( 0, .5, 75);
		std::string upperarmpath[2] = {npcName + "chest", npcName + "upper arm"};
		
        if (groin){
            cellRender.insertMesh("meshes\\" + groin->model, pos2, axis, Ogre::Radian(3.14), npcName + "groin", addresses, numbers);
Jason Hooks's avatar
Jason Hooks committed
107
			std::cout <<"GROIN" << groin->model <<"\n";
Jason Hooks's avatar
Jason Hooks committed
108 109 110 111 112 113
			addresses2[numbers] = npcName + "groin";
			addresses[numbers++] = npcName + "groin";
		}
		
		//addresses[1] = npcName + "groin";
		if(upperleg){
Jason Hooks's avatar
Jason Hooks committed
114
		cellRender.insertMesh ("meshes\\" + upperleg->model, Ogre::Vector3( 6, 0, -14), axis, Ogre::Radian(3.14), npcName + "upper leg", addresses, numbers); //-18
Jason Hooks's avatar
Jason Hooks committed
115 116 117
		cellRender.insertMesh ("meshes\\" + upperleg->model, Ogre::Vector3( -6, 0, -14), axis, Ogre::Radian(0), npcName + "upper leg2", addresses2, numbers);
		addresses2[numbers] = npcName + "upper leg2";
		addresses[numbers++] = npcName + "upper leg";
Jason Hooks's avatar
Jason Hooks committed
118
		cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), addresses, numbers);
Jason Hooks's avatar
Jason Hooks committed
119 120 121 122 123 124 125 126 127 128 129
		}
		if(knee)
		{
			cellRender.insertMesh ("meshes\\" + knee->model, Ogre::Vector3( 0, -2, -18), axis, Ogre::Radian(0), npcName + "knee", addresses, numbers);
		//cellRender.rotateMesh(Ogre::Vector3(0, 1, 0), Ogre::Radian (1), npcName + "upper arm");
		cellRender.insertMesh ("meshes\\" + knee->model, Ogre::Vector3( 0, -2, -18), axis, Ogre::Radian(0), npcName + "knee2", addresses2, numbers);
		
			addresses2[numbers] = npcName + "knee2";
			addresses[numbers++] = npcName + "knee";
		}
		if(ankle){
Jason Hooks's avatar
Jason Hooks committed
130 131 132
			
			cellRender.insertMesh ("meshes\\" + ankle->model, Ogre::Vector3( 0, -1, -18), axis, Ogre::Radian(0), npcName + "ankle", addresses, numbers);   //-1
			cellRender.insertMesh ("meshes\\" + ankle->model, Ogre::Vector3( 0, -1, -18), axis, Ogre::Radian(0), npcName + "ankle2", addresses2, numbers); //-1
Jason Hooks's avatar
Jason Hooks committed
133 134 135 136 137
			
			addresses2[numbers] = npcName + "ankle2";
			addresses[numbers++] = npcName + "ankle";
		}
		if(foot){
Jason Hooks's avatar
Jason Hooks committed
138 139 140 141 142 143 144 145 146 147 148 149 150
			//std::cout << "RACE" << bodyRaceID << "\n";
			if(bodyRaceID.compare("b_n_khajiit_m_") == 0 || bodyRaceID.compare("b_n_khajiit_f_") == 0)
			{
				std::cout << "BEASTRACE\n";
				cellRender.insertMesh ("meshes\\" + foot->model, Ogre::Vector3( 0, -24, -15), axis, Ogre::Radian(0), npcName + "foot", addresses, numbers);

				cellRender.insertMesh ("meshes\\" + foot->model, Ogre::Vector3( 0, -24, -15), axis, Ogre::Radian(0), npcName + "foot2", addresses2, numbers);
			}
			else{
			cellRender.insertMesh ("meshes\\" + foot->model, Ogre::Vector3( 0, -4, -15), axis, Ogre::Radian(0), npcName + "foot", addresses, numbers);

			cellRender.insertMesh ("meshes\\" + foot->model, Ogre::Vector3( 0, -4, -15), axis, Ogre::Radian(0), npcName + "foot2", addresses2, numbers);
			}
Jason Hooks's avatar
Jason Hooks committed
151 152
			addresses2[numbers] = npcName + "foot2";
			addresses[numbers++] = npcName + "foot";
Jason Hooks's avatar
Jason Hooks committed
153
			//cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), addresses, numbers);
Jason Hooks's avatar
Jason Hooks committed
154 155 156 157
		}
		
		 
		if (arm){
Jason Hooks's avatar
Jason Hooks committed
158 159 160
			//010
			cellRender.insertMesh("meshes\\" + arm->model, Ogre::Vector3(-12.5, 0, 104), Ogre::Vector3(0, 1, 0), Ogre::Radian(-3.14 / 2), npcName + "upper arm", upperleft, uppernumbers);   //1, 0,.75
			 //cellRender.rotateMesh(Ogre::Vector3(1, 0, 0), Ogre::Radian (.45), upperarmpath, 2);                                                                                          //-.5, 0, -.75
Jason Hooks's avatar
Jason Hooks committed
161 162 163
			cellRender.insertMesh("meshes\\" + arm->model, Ogre::Vector3(12.5, 0, 105), Ogre::Vector3(-.5, 0, -.75), Ogre::Radian(3.14), npcName + "upper arm2", upperright, uppernumbers);
			upperleft[uppernumbers] = npcName + "upper arm";
			upperright[uppernumbers++] = npcName + "upper arm2";
Jason Hooks's avatar
Jason Hooks committed
164 165
		    cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), upperleft, uppernumbers);        //1 -1 1
			cellRender.rotateMesh(Ogre::Vector3(0, .1, 0),  Ogre::Radian(3.14/2), upperleft, uppernumbers);
Jason Hooks's avatar
Jason Hooks committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		}

		if (forearm)
		{
		//addresses[1] = npcName + "upper arm";
			cellRender.insertMesh("meshes\\" + forearm->model, Ogre::Vector3(-12.5, 0, 0), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "forearm", upperleft, uppernumbers);
			cellRender.insertMesh("meshes\\" + forearm->model, Ogre::Vector3(-12.5, 0, 0), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "forearm2", upperright, uppernumbers);
			upperleft[uppernumbers] = npcName + "forearm";
			upperright[uppernumbers++] = npcName + "forearm2";
		}
		//else
		//	std::cout << npcName << "has no forearm";
		if (wrist)
		{
			cellRender.insertMesh("meshes\\" + wrist->model, Ogre::Vector3(-9.5, 0, 0), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "wrist", upperleft, uppernumbers);
			cellRender.insertMesh("meshes\\" + wrist->model, Ogre::Vector3(-9.5, 0, 0), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "wrist2", upperright, uppernumbers);
			upperleft[uppernumbers] = npcName + "wrist";
			upperright[uppernumbers++] = npcName + "wrist2";
		}
		

		if(hand)
Jason Hooks's avatar
Jason Hooks committed
188 189 190
		{
			//std::cout << "WE FOUND A HAND\n";
															//-50, 0, -120
Jason Hooks's avatar
Jason Hooks committed
191 192 193 194 195 196 197 198
			//std::cout << "WE FOUND HANDS\n";
			std::string pass;
			if(hand->model.compare("b\\B_N_Dark Elf_F_Hands.1st.NIF")==0 && bodyRaceID.compare("b_n_dark elf_m_") == 0)
				pass = "b\\B_N_Dark Elf_M_Hands.1st.NIF";	
			else
				pass = hand->model;
			cellRender.insertMesh("meshes\\" + pass, Ogre::Vector3(42, 1, -110), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "hand", upperleft, uppernumbers,false);   //0, 100, -100    0,0,120
			cellRender.insertMesh("meshes\\" + pass, Ogre::Vector3(42, 1, -110), Ogre::Vector3(0, 0,0), Ogre::Radian(3.14), npcName + "hand2", upperright, uppernumbers, false);   //0, 100, -100    0,0,120
Jason Hooks's avatar
Jason Hooks committed
199 200 201 202 203 204 205 206
			upperleft[uppernumbers] = npcName + "hand";
			upperright[uppernumbers++] = npcName + "hand2";
			//cellRender.rotateMesh(Ogre::Vector3(0, 0,0),  Ogre::Radian(3.14), upperleft, uppernumbers);
			cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), upperleft, uppernumbers);
			cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), upperright, uppernumbers);
		}
		if(hands)
		{
Jason Hooks's avatar
Jason Hooks committed
207 208 209 210 211 212 213 214
			std::string pass;
			if(hands->model.compare("b\\B_N_Redguard_F_Hands.1st.nif")==0 && bodyRaceID.compare("b_n_redguard_m_") == 0)
				pass = "b\\B_N_Redguard_M_Hands.1st.nif";
			else if(hands->model.compare("b\\B_N_Imperial_M_Hands.1st.nif") == 0 && bodyRaceID.compare("b_n_nord_m_") == 0)
				pass = "b\\B_N_Nord_M_Hands.1st.nif";
			else
				pass =hands->model;								//-50, 0, -120
			cellRender.insertMesh("meshes\\" + pass, Ogre::Vector3(42, 1,-110), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "hand", upperleft, uppernumbers, false);   //0, 100, -100    42, 0, -110
Jason Hooks's avatar
Jason Hooks committed
215
			//cellRender.insertMesh("meshes\\" + hands->model, Ogre::Vector3(42, 0,110), Ogre::Vector3(1, 0, 0), Ogre::Radian(3.14), npcName + "hand", upperleft, uppernumbers, false);   //0, 100, -100    42, 0, -110
Jason Hooks's avatar
Jason Hooks committed
216
			cellRender.insertMesh("meshes\\" + pass, Ogre::Vector3(42, 1, -110), Ogre::Vector3(0, 0, 0), Ogre::Radian(3.14), npcName + "hand2", upperright, uppernumbers, false);   //0, 100, -100    0,0,120
Jason Hooks's avatar
Jason Hooks committed
217 218 219 220 221
			upperleft[uppernumbers] = npcName + "hand";
			upperright[uppernumbers++] = npcName + "hand2";
			cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), upperleft, uppernumbers);
			cellRender.scaleMesh(Ogre::Vector3(1, -1, 1), upperright, uppernumbers);
		}
Jason Hooks's avatar
Jason Hooks committed
222 223 224 225 226 227 228 229 230 231 232 233

		if(neck)
		{
			cellRender.insertMesh ("meshes\\" + neck->model, Ogre::Vector3( 0, 0, 120), axis, Ogre::Radian(3.14), npcName + "neck", neckandup, neckNumbers);
			neckandup[neckNumbers++] = npcName + "neck";
		}
		cellRender.insertMesh (headModel, Ogre::Vector3( 0, 0, 5), axis, Ogre::Radian(0), npcName + "head", neckandup, neckNumbers);
		neckandup[neckNumbers++] = npcName + "head";
		cellRender.insertMesh (hairModel, Ogre::Vector3( 0, -1, 0), axis, Ogre::Radian(0), npcName + "hair", neckandup, neckNumbers);
		ref->mData.setHandle (rendering.end (ref->mData.isEnabled()));
		
	}
234

235 236 237 238 239 240 241 242 243 244
    void Npc::enable (const MWWorld::Ptr& ptr, MWWorld::Environment& environment) const
    {
        environment.mMechanicsManager->addActor (ptr);
    }

    void Npc::disable (const MWWorld::Ptr& ptr, MWWorld::Environment& environment) const
    {
        environment.mMechanicsManager->removeActor (ptr);
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
245 246 247 248 249 250 251 252
    std::string Npc::getName (const MWWorld::Ptr& ptr) const
    {
        ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
            ptr.get<ESM::NPC>();

        return ref->base->name;
    }

253
    MWMechanics::CreatureStats& Npc::getCreatureStats (const MWWorld::Ptr& ptr) const
254 255 256 257 258 259
    {
        if (!ptr.getRefData().getCreatureStats().get())
        {
            boost::shared_ptr<MWMechanics::CreatureStats> stats (
                new MWMechanics::CreatureStats);

260
            ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref = ptr.get<ESM::NPC>();
261 262 263 264 265 266 267 268 269 270 271 272 273

            stats->mAttributes[0].set (ref->base->npdt52.strength);
            stats->mAttributes[1].set (ref->base->npdt52.intelligence);
            stats->mAttributes[2].set (ref->base->npdt52.willpower);
            stats->mAttributes[3].set (ref->base->npdt52.agility);
            stats->mAttributes[4].set (ref->base->npdt52.speed);
            stats->mAttributes[5].set (ref->base->npdt52.endurance);
            stats->mAttributes[6].set (ref->base->npdt52.personality);
            stats->mAttributes[7].set (ref->base->npdt52.luck);
            stats->mDynamic[0].set (ref->base->npdt52.health);
            stats->mDynamic[1].set (ref->base->npdt52.mana);
            stats->mDynamic[2].set (ref->base->npdt52.fatigue);

274 275
            stats->mLevel = ref->base->npdt52.level;

276 277 278 279 280 281
            ptr.getRefData().getCreatureStats() = stats;
        }

        return *ptr.getRefData().getCreatureStats();
    }

Marc Zinnschlag's avatar
Marc Zinnschlag committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    MWMechanics::NpcStats& Npc::getNpcStats (const MWWorld::Ptr& ptr) const
    {
        if (!ptr.getRefData().getNpcStats().get())
        {
            // xxx
            boost::shared_ptr<MWMechanics::NpcStats> stats (
                new MWMechanics::NpcStats);

            ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref = ptr.get<ESM::NPC>();

            if (!ref->base->faction.empty())
            {
                // TODO research how initial rank is stored. The information in loadnpc.hpp are at
                // best very unclear.
                stats->mFactionRank[ref->base->faction] = 0;
            }

299 300 301
            for (int i=0; i<27; ++i)
                stats->mSkill[i].setBase (ref->base->npdt52.skills[i]);

Marc Zinnschlag's avatar
Marc Zinnschlag committed
302 303 304 305 306 307
            ptr.getRefData().getNpcStats() = stats;
        }

        return *ptr.getRefData().getNpcStats();
    }

308 309 310 311
    boost::shared_ptr<MWWorld::Action> Npc::activate (const MWWorld::Ptr& ptr,
        const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
    {
        return boost::shared_ptr<MWWorld::Action> (new MWWorld::ActionTalk (ptr));
312
    }
Marc Zinnschlag's avatar
Marc Zinnschlag committed
313

314 315
    MWWorld::ContainerStore<MWWorld::RefData>& Npc::getContainerStore (const MWWorld::Ptr& ptr)
        const
Marc Zinnschlag's avatar
Marc Zinnschlag committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329
    {
        if (!ptr.getRefData().getContainerStore().get())
        {
            boost::shared_ptr<MWWorld::ContainerStore<MWWorld::RefData> > store (
                new MWWorld::ContainerStore<MWWorld::RefData>);

            // TODO add initial content

            ptr.getRefData().getContainerStore() = store;
        }

        return *ptr.getRefData().getContainerStore();
    }

330 331 332 333 334 335 336 337
    std::string Npc::getScript (const MWWorld::Ptr& ptr) const
    {
        ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
            ptr.get<ESM::NPC>();

        return ref->base->script;
    }

338 339 340 341 342 343 344
    void Npc::registerSelf()
    {
        boost::shared_ptr<Class> instance (new Npc);

        registerClass (typeid (ESM::NPC).name(), instance);
    }
}