• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ankhd

Need some advice one managing path request from units

1 post in this topic

The system I'm thinking of using is one that has a std::vector<PathData> that I allocate say 20 elements.
A unit can then requests a path , in doing so it recieves a ticketID that it later uses to poll the path manager for its path.
This ticketID is the array index of the fixed size vector container of paths, each path has a reference count of how many units
are following it. When this count is 0 we can reuse that element. The down side is I have to always search for a empty element
and also do it for evey path request could be heaps due to its a RTS game.
I've created a class PathManager, while doing so I keep saying to my self that there must be a neater way to go about this, and
this leads me to this post. If anyone has any ideas I would love to here them thanks Dan.

//here is my path manager class its only a prototype
[source]
//------------------------------------------------------------------------------------
//this class we fill in when we create a new path
//we keep the start and end cell pointers as reference and a list of path points
//keeps track of how many units are usint this path we can remove them when where done
//can only use a path when valid is true
//------------------------------------------------------------------------------------
class PathData
{
public:
DWORD NumberInuse;//how many units are following this path we remove paths after not in use
std::list<D3DXVECTOR3> path;
cCell *StartCell;
cCell *EndCell;

bool Valid;//if false the path is not yet built
PathData(void)
{
StartCell = NULL;
EndCell = NULL;
Valid = false;//when the path is created true
NumberInuse = 0;
}//end ctor
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
~PathData(void)
{
}//end dctor
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

void AddUser(void){NumberInuse++;}
void RemoveUser(void){NumberInuse--; if(NumberInuse < 0) NumberInuse = 0;}

};//end class PathData
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////




//-----------------------------------------------------------------------------------------
//this class will hold and create paths a path is a PathData we will
//hold a vector of these and the ticket will be the index to the vector of paths
//if the distance from the start and end cells are with in some range of any paths in the list
//it will use that path and not create a new path this way we dont create multiple paths with a group
//selection say
//the size needs to be set so we can find empty slots based on the valid array index
//this way we dont remove elements we reuse them, if there is now room it will try to free
//up a slot first
//the ticket number will not change due to removale
//-----------------------------------------------------------------------------------------
class cRTSPathManager
{
private:
//private members
std::vector<PathData> PathList;//holds created paths the indext(ticket) comes from this lists size

public:
//PUBLIC FUNCTIONS
//-----------------------------------------------------------------------------
//how many path we want to create
//-----------------------------------------------------------------------------
cRTSPathManager(UINT numberpaths);
~cRTSPathManager(void);

//----------------------------------------------------------------------------
//call this to summit a path request it will return a ticket number used to
//Identifie the path, because the path may not be ready straght away
//samepathdistance is used to see is a path is allready created or near the location
//returns -1 on errors
//-----------------------------------------------------------------------------
UINT PathRequest(cCell *StartCell, cCell *EndCell, float samepathdistance);


//------------------------------------------------------------------------------------
//you need to call this when a unit leaves the path to remove its reference
//------------------------------------------------------------------------------------
void RemoveRequest(UINT ticketid);

};//end class cRTSPathManager
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////


//-----------------------------------------------------------------------------
//how many path we want to create
//-----------------------------------------------------------------------------
cRTSPathManager::cRTSPathManager(UINT numberpaths)
{
PathList.resize(numberpaths, PathData());
}//end destructor
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////



//----------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------
cRTSPathManager::~cRTSPathManager(void)
{
}//end destructor
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////





//----------------------------------------------------------------------------
//call this to summit a path request it will return a ticket number used to
//Identifie the path, because the path may not be ready straight away
//samepathdistance is used to see is a path is allready created or near the location
//returns -1 on errors
//-----------------------------------------------------------------------------
UINT cRTSPathManager::PathRequest(cCell *StartCell, cCell *EndCell, float samepathdistance)
{
UINT index = -1;
bool startnodesame = false;
bool endnodesame = false;
//we need to see if there is a path near our start and end
for(UINT ctr =0; ctr < PathList.size(); ctr++)
{
//check distance from the current path cell with the new request to see if we can use this path
//check start cell range
D3DXVECTOR3 t = PathList[ctr].StartCell->Centre - StartCell->Centre;
float dist = D3DXVec3LengthSq(&t);
if(dist <= samepathdistance)
{
//we can use the start
startnodesame = true;
}
//check the end cell
t = PathList[ctr].EndCell->Centre - EndCell->Centre;
dist = D3DXVec3LengthSq(&t);
if(dist <= samepathdistance)
{
//we can use the start
endnodesame = true;
}

//if end node same and start node same is true then we can use this path
if(endnodesame && startnodesame)
{
PathList[ctr].AddUser();
return ctr;//this is our slot
}//end using this path
}//end for all paths memorey slots
///////////////////////////////////////////////////////////////////////

//if we get to here there is no path we can use so we need to find a empty slot
for(UINT ctr =0; ctr < PathList.size(); ctr++)
{
if(PathList[ctr].NumberInuse <= 0)
{
//here is our slot
PathList[ctr].AddUser();
PathList[ctr].path.clear();
PathList[ctr].StartCell = StartCell;
PathList[ctr].EndCell = EndCell;
PathList[ctr].Valid = false;
//create a path here
return ctr;
}
}//end
//if we are here we have a problem we need to free some slots up
for(UINT ctr =0; ctr < PathList.size(); ctr++)
{
if(PathList[ctr].NumberInuse <= 0)
{
//here is our slot
PathList[ctr].Valid = false;
PathList[ctr].path.clear();
PathList[ctr].StartCell = StartCell;
PathList[ctr].EndCell = EndCell;
//we freed this slot add the request
PathList[ctr].AddUser();
//create the path here
return ctr;//this is the next fee slot
}
}//end
//now if we get here we are done for
return index;//error -1

}//end PathRequest
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////





//------------------------------------------------------------------------------------
//you need to call this when a unit leaves the path to remove its reference
//------------------------------------------------------------------------------------
void cRTSPathManager::RemoveRequest(UINT ticketid)
{
UINT Error = -1;
if(Error == ticketid)
return;//not valid
if(ticketid > PathList.size())
return;//error
//ok its valid
PathList[ticketid].RemoveUser();
}//end RemoveRequest
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

[/source]
0

Share this post


Link to post
Share on other sites
Why use a fixed-length array and manage what's being used yourself? You are effectively implementing a poor man's allocator. Try using shared pointers instead (for the reference count), and let the usual allocator do the work for you.
0

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  
Followers 0