Sign in to follow this  
Homicidal

[C++]NPC Movement in a Text Adventure

Recommended Posts

I am making a text adventure and am trying to make NPCs move around but for some reason, they don't move. The Actor.h File
#include <string>
using namespace std;

class actor {
      
private:
        string name, race, sex, type, description;
        int ws, str, hp, sp, t, maxws, maxstr, maxhp, maxsp, maxt, gold, magic;
        armour worn, held1;
        weapon wield, held2;
	bool hostile;
	int room;
	item items[10];
public:
        void CreateActor(string cname, string crace, string csex, int cws, int cstr, int chp, int csp, int ct, string ctype, bool chostile, int cmagic, int croom)
        {
        name = cname;
        race = crace;
        sex = csex;
        ws = cws;
        str = cstr;
        hp = chp;
        sp = csp;
        t = ct;
	magic = cmagic;
        maxws = cws;
        maxstr = cstr;
        maxhp = chp;
        maxsp = csp;
        maxt = ct;
        gold = 50;
        type = ctype;
        hostile = chostile;
        room = croom;
        }

	void Describe(string desc)
	{description = desc;}

	string GetDescription()
	{return description;}

        string GetName()
        {return name;}
        
        string GetRace()
        {return race;}
        
	string GetSex()
        {return sex;}
        
	string GetType()
        {return type;}
        
        int GetWs()
        {return ws;}
        
        int GetStr()
        {return str;}
        
        int GetHp()
        {return hp;}
        
        int GetSp()
        {return sp;}
        
        int GetT()
        {return t;}
        
        int GetMagic()
        {return magic;}
        
        int GetMaxWs()
        {return maxws;}
        
        int GetMaxStr()
        {return maxstr;}
        
        int GetMaxHp()
        {return maxhp;}
        
        int GetMaxSp()
        {return maxsp;}
        
        int GetMaxT()
        {return maxt;}
        
        int GetMaxMagic()
        {return magic;}
        
        int GetGold()
        {return gold;}
        
        int GetRoom()
        {return room;}
        /********************Armour Stuff**********************/
        string GetWornName()
        {string worn_name = worn.GetName();
        return worn_name;}
        
        int GetWornAv()
        {int worn_av = worn.GetAv();
        return worn_av;}
        
        int GetWornValue()
        {int worn_value = worn.GetValue();
        return worn_value;}
        
        string GetHeld1Name()
        {string held1_name = held1.GetName();
        return held1_name;}
        
        int GetHeld1Av()
        {int held1_av = held1.GetAv();
        return held1_av;}
        
        int GetHeld1Value()
        {int held1_value = held1.GetValue();
        return held1_value;}
        
        int ModWornArmour(database_armour a)
        {
        worn.SetArmour(a);
        return 0;
        }
        
        int ModHeldArmour(database_armour b)
        {
        held1.SetArmour(b);
        return 0;
        }
	/******************************************************/

        /*****************Weapon Stuff*************************/
	    string GetWieldName()
        {string wield_name = wield.GetName();
        return wield_name;}
        
        int GetWieldAp()
        {int wield_ap = wield.GetAp();
        return wield_ap;}
        
        int GetWieldValue()
        {int wield_value = wield.GetValue();
        return wield_value;}
        
        string GetHeld2Name()
        {string held2_name = held2.GetName();
        return held2_name;}
        
        int GetHeld2Ap()
        {int held2_ap = held2.GetAp();
        return held2_ap;}
        
        int GetHeld2Value()
        {int held2_value = held2.GetValue();
        return held2_value;}
        
        int ModWieldWeapon(database_weapon c)
        {
        wield.SetWeapon(c);
        return 0;
        }
        
        int ModHeldWeapon(database_weapon d)
        {
        held2.SetWeapon(d);
        return 0;
        }
	/******************************************************/
        string GetItem1()
	{
	return items[0].GetName();}

        string GetItem2()
	{
	return items[1].GetName();}

        string GetItem3()
	{
	return items[2].GetName();}

        string GetItem4()
	{
	return items[3].GetName();}

        string GetItem5()
	{
	return items[4].GetName();}

        string GetItem6()
	{
	return items[5].GetName();}

        string GetItem7()
	{
	return items[6].GetName();}

        string GetItem8()
	{
	return items[7].GetName();}

        string GetItem9()
	{
	return items[8].GetName();}

        string GetItem10()
	{
	return items[9].GetName();}

	int ModWs(int newws)
        {if (ws <= 10)
        {ws += newws;
        return 0;}
        else
        return 0;}
        
        int ModStr(int newstr)
        {if (str <= 15)
        {str += newstr;
        return 0;}
        else
        return 0;}
        
        int ModHp(int newhp)
        {if (hp <= 80)
        {ws += newhp;
        return 0;}
        else
        return 0;}
        
        int ModSp(int newsp)
        {if (sp <= 150)
        {sp += newsp;
        return 0;}
        else
        return 0;}
        
        int ModT(int newt)
        {if (t <= 15)
        {t += newt;
        return 0;}
        else
        return 0;}
        
        int ModMagic(int newmagic)
        {if (magic <= 15)
        {magic += newmagic;
        return 0;}
        else
        return 0;}
        
        int ModGold(int newgold)
        {if (gold <= 500)
        {gold += newgold;
        if (gold <= 500)
        return 0;
        else
        {
        gold = 500;
        return 0;}
        }
        else
        return 0;}
        
	int SetRoom(int x)
       	{room = x;
	return 0;}
};
The movement code:
int move(room b)
{
actor a = b.GetX();
room temproom;
string tempstring;
int tempint;
int e1 = b.GetExit1(), e2 = b.GetExit2(), e3 = b.GetExit3(), e4 = b.GetExit4();
int r = a.GetRoom(), s = b.GetId();
int z = random(1, 5);
if (z == 1)
{
if (e1 != 0)
{
while (a.GetRoom() != e1)
{
a.SetRoom(e1);
temproom = IdConversion(e1);
temproom.SetX(a);
b.SetX(temproom.GetX())
return 0;
}
}
else
return 0;
}

if (z == 2)
{
if (e2 != 0)
{
while (a.GetRoom() != e2)
{
a.SetRoom(e2);
temproom = IdConversion(e2);
temproom.SetX(a);
b.SetX(temproom.GetX())
return 0;
}
}
else
return 0;
}

if (z == 3)
{
if (e3 != 0)
{
while (a.GetRoom() != e3)
{
a.SetRoom(e3);
temproom = IdConversion(e3);
temproom.SetX(a);
b.SetX(temproom.GetX())
return 0;
}
}
else
return 0;
}

if (z == 4)
{
if (e4 != 0)
{
while (a.GetRoom() != e4)
{
a.SetRoom(e4);
temproom = IdConversion(e4);
temproom.SetX(a);
b.SetX(temproom.GetX())
return 0;
}
}
else
return 0;
}

if (z == 5)
{
return 0;
}
}
And finally, the room code:
if (player.GetRoom() == 1)
{
cout << "Town Plaza" << endl;
cout << "You are standing in the centre of the Town Plaza." << endl;
cout << "The Armoury is to the north." << endl << endl;
getline(cin,command);
if (command == "n")
{
   player.SetRoom(2);
   cout << endl;
   move(r1)}
     else if (command == "i")
     inventory();
   else if (command == "s")
   cout << "There is a concrete block in the way with a note attached." << endl << endl;
        else if (command == "w")
        cout << "There is a concrete block in the way with a note attached." << endl << endl;
        else if (command == "e")
        cout << "There is a concrete block in the way with a note attached." << endl << endl;
             else if (command == "read note")
             {
             cout << "The road is closed until further notice." << endl;
             cout << "			                  --Homicidal" << endl << endl;
             }
             else if (command == "sc")
             sc();
              else
              cout << "What?" << endl;
   }
Also, how do I make one of those scrolly boxes for code?

Share this post


Link to post
Share on other sites
I think you're going to have to have a time-controlled main while loop with a logic function which now and then will have a random chance of movement for NPCs. It might be worth downloading some source code for a MUD and see how they do things. Your code needs to be much more automated and structured.

Edit: Just looked at your movement code. It's a bit convoluted. Maybe something more like this?:

if( chance of move succeeded )
exit = random exit in room
if( mob can enter room )
mob location = getroomnumber( exit direction )

You can setup a vector of rooms that are numbered, much like ROM does. Each exit is mapped to a room number so everything is interconnected.

Share this post


Link to post
Share on other sites
Quote:
Original post by Homicidal
I am making a text adventure and am trying to make NPCs move around but for some reason, they don't move.


Then why is there all this stuff set up for other data members already? Writing things speculatively is almost always a bad idea. Get your problem solved first, with the data that's necessary for that problem, and then move on to the next thing. As it stands, finding the problem is made harder by all the reams of code for handling everything else (which presumably aren't even properly tested yet themselves) putting space in between the functions and definitions you're actually interested in.

But for what it's worth, have you heard of any of the following concepts?

- constructors
- initialization lists
- pass-by-reference
- meaningful and consistent variable names (if you can manage to type out "description" in full, why not "strength"? I can't even *guess* what 't' is for.)
- functions returning 'void' (er, well, you have one; but then there are also a zillion functions declared to return 'int' that can only ever return 0, and there is no reason to assume that 0 value *means anything* or is *usable* in any way)
- encapsulation (hint: it has nothing to do with accessors and mutators)

[Edited by - Zahlman on August 18, 2008 3:08:35 PM]

Share this post


Link to post
Share on other sites
God, my eyes hurt...

Really... The days are over where we smash together single letter variables, sometimes triple, as names. When other people read your code, it makes it 5x harder for them to really recognize what values are what.

And practice using comments to explain why something is doing something.

Just trying to help you, help us... And ultimately, helping you.

If I were you, I would sit down with a piece of paper and think about the structure of all of this again. Use what some of the previous replies have mentioned. The purpose of writing a console game is so you can learn the structure, and principles that will help you when you move to way more advanced topics.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this