C++ Search Class/ finding nearby meshes [SOLVED]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Jgwman
Posts: 12
Joined: Sat Dec 29, 2012 6:26 pm

C++ Search Class/ finding nearby meshes [SOLVED]

Post by Jgwman »

Hello, today I was working on an Irrlicht application which is the beginning of an RPG. I was trying to start programming a very simple AI for opponents, and I decided to try to do it through a class which I made for non-player characters in the game. Basically, I wanted to be able to search this class to find any NPC (object of the class) which could "detect" the player based on three variables- smell, sight, and hearing- and if any NPC's smell, sight, or hearing is greater than the player's (camera's) distance from the NPC and the NPC was aggressive (based on a boolean value) towards the player, it would start moving towards the player. The part which I don't know how to implement is a way to check all the objects of the class to see if their member values were greater than the player's distance, without doing it one NPC at a time (with if statements). I thought I might use the std::find function, but as far as I can see, that only works for vectors/arrays/sets and not classes. Does anyone know how I could implement a system like this, with or without the std::find? I just need to be able to test all NPCs' senses against distance without a ton of if statements. My class code (really a struct):

Code: Select all

 struct NPCSceneNode
  {
      IAnimatedMeshSceneNode* node;
      bool isOpponent;
      int bloodVal;
      int smellVal;
      int sightVal;
      int hearVal;
    NPCSceneNode (IAnimatedMesh* mesh, bool cIsOpponent, core::vector3df cPos, core::vector3df cRot, s32 bFrame, s32 eFrame, int cBlood, int cSmell, int cSight, int cHearing, scene::ISceneManager* k, f32 animSpeed,
                   core::vector3df scale, ITriangleSelector* n) {
 
    isOpponent=cIsOpponent;
    bloodVal=cBlood;
    smellVal=cSmell;
    sightVal=cSight;
    hearVal=cHearing;
    node = k->addAnimatedMeshSceneNode(mesh, 0, ID_NPC);
    node->setFrameLoop(bFrame, eFrame);
    node->setAnimationSpeed(animSpeed);
    node->setScale(scale);
    node->setPosition(cPos);
    node->setRotation(cRot);
    node->setMaterialFlag(video::EMF_BACK_FACE_CULLING, false);
    n = k->createTriangleSelector(node);
    node->setTriangleSelector(n);
    n->drop();
    }
    void setFrameLoop(s32 bFrame, s32 eFrame) {
    node->setFrameLoop(bFrame, eFrame);
    }
    void setAnimationSpeed(int animSpeed) {
    node->setAnimationSpeed(animSpeed);
    }
  };
Thanks in advance.
Last edited by Jgwman on Mon Dec 31, 2012 1:50 am, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C++ Search Class/ finding nearby meshes

Post by CuteAlien »

I don't get you when you write "that only works for vectors/arrays/sets and not classes". In what kind of structure _do_ you save your objects? If you have more than one of them you usually use one of those...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Jgwman
Posts: 12
Joined: Sat Dec 29, 2012 6:26 pm

Re: C++ Search Class/ finding nearby meshes

Post by Jgwman »

I am storing them in classes with each class having ints, bools, etc, as I posted above. I have not had to use any other structure in this particular program (yet) except for the vectors and such involved in the Irrlicht functions for 3d meshes. I haven't gotten very far into the application as a whole yet, but I will need vectors for other objects. What I am saying is that I did not store my NPCs' "sense" values in anything but an int within a class, so I don't no how to check if each member of the class has a high enough "sense" value. Even if I stored the sense values in vectors, then, once I found which element had a high enough "sense" value, how would I tell the program which mesh to move towards the camera? I am also not very clear on the std::find function as it is.
Last edited by Jgwman on Mon Dec 31, 2012 12:15 am, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C++ Search Class/ finding nearby meshes

Post by CuteAlien »

Yeah - but that is only one! What's the point in searching if you only have one?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Jgwman
Posts: 12
Joined: Sat Dec 29, 2012 6:26 pm

Re: C++ Search Class/ finding nearby meshes

Post by Jgwman »

What I am saying is that it would search through the OBJECTS of the class (which of there would be one PER NPC, so more than one) to find any objects with a sense member- smell, sight, or hearing - higher than their distance from the camera. So that is more than one OBJECT to search through its members.
An example- say using the class above, I define two objects (in the finished products it would not be two, it would be one PER NPC):

Code: Select all

NPCSceneNode enemy_1N = NPCSceneNode(enemy_1, true, core::vector3df(-70,-66,-25), core::vector3df(0,-90,0), 1, 24, 100, 0, 200, 50, smgr, 24, core::vector3df(15,15,15), selector);
NPCSceneNode enemy_2N = NPCSceneNode(enemy_2, true, core::vector3df(-60,-66,-35), core::vector3df(0,-90,0), 1, 24, 100, 0, 200, 50, smgr, 24, core::vector3df(15,15,15), selector);
Now I want to search through all of these objects (remember, there would be more than two in the finished game), to get a result similar to of this (distanceFromCamera is a placeholder for finding distance):

Code: Select all

if ((enemy_1N.smellVal>distanceFromCamera || enemy_1N.sightVal>distanceFromCamera || enemy_1N.hearVal>distanceFromCamera) && enemy_1N.isOpponent == true) {
//Tell enemy 1 to advance to camera;
}
if ((enemy_2N.smellVal>distanceFromCamera || enemy_2N.sightVal>distanceFromCamera || enemy_2N.hearVal>distanceFromCamera) && enemy_2N.isOpponent == true) {
//Tell enemy 2 to advance to camera;
}
I would have to do this for all enemies, so I just wondered if there is an easier way to do this than an "if" statement for every enemy. Sorry if I can't explain this well enough.
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: C++ Search Class/ finding nearby meshes

Post by polylux »

I'd highly recommend using containers for storing your enemies, such as

Code: Select all

irr::core::array<NPCSceneNode> enemies;
That way you could do

Code: Select all

enemies.push_back(enemy_1N);
and the like.
Then you do smth like:

Code: Select all

for (u32 i = 0; i < enemies.size(); i++)
{
    f32 distance = enemies[i].node->getPosition().getDistanceFrom(cam->getAbsolutePosition());
    if (smellVal < distance)
    {
        ...
    }
}
Hope that helps.
beer->setMotivationCallback(this);
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: C++ Search Class/ finding nearby meshes

Post by CuteAlien »

Yeah, that's pretty much what I meant above. You usually do use a container. Otherwise you would indeed have to code it for each variable :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Jgwman
Posts: 12
Joined: Sat Dec 29, 2012 6:26 pm

Re: C++ Search Class/ finding nearby meshes

Post by Jgwman »

Thanks alot to both of you! I just have a couple more questions- does the array declared in polylux's first code snippet declare with the type NPCSceneNode, thereby using my class? Would this store all of the objects of my class in an array with all their values of the class members? Also, the other thing I really don't understand is the second snippet. What does push_back do? Thanks again to both of you.
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: C++ Search Class/ finding nearby meshes

Post by polylux »

You can add all instances of NPCSceneNode to that array. This is what "push_back()" does. The array (similar to std's vector) then enables you to iterate through all the objects added to it before, as shown in that for-loop.
Containers being a rather fundamential principle of coding, I'd recommend some further reading.
beer->setMotivationCallback(this);
Jgwman
Posts: 12
Joined: Sat Dec 29, 2012 6:26 pm

Re: C++ Search Class/ finding nearby meshes

Post by Jgwman »

Thanks a lot, I think I understand. I will certainly read some more, though it's mostly Irrlicht's array that I was not sure about.
Post Reply