hello,
I wont bother you long, but I originally came to this site for help in programming and seem to been distracted by you games forum.
I was wondering if you could tell me if there is an error in the following code:
Code:
///////add unit task//////////////
void Add_unit_Task (
LPUNIT_DOING_TASK newTask // the task to be added
)
{ // added by order of time
////start debug: turn off
// if no other task
if (g_bottom_unit_task == (LPUNIT_DOING_TASK) NULL )
{
g_bottom_unit_task = newTask;
g_top_unit_task= newTask;
newTask->prev = (LPUNIT_DOING_TASK) NULL ;
newTask->next = (LPUNIT_DOING_TASK) NULL ;
return;
}
else if ( (newTask->time) >= (g_bottom_unit_task->time) ) // if at end of list
{
g_bottom_unit_task->next=newTask;
newTask->prev = g_bottom_unit_task ;
newTask->next = (LPUNIT_DOING_TASK) NULL ;
g_bottom_unit_task = newTask;
return;
}
else if ( (newTask->time) < (g_top_unit_task->time) ) // if first of list
{
g_top_unit_task->prev=newTask;
newTask->prev = (LPUNIT_DOING_TASK) NULL ;
newTask->next = g_top_unit_task ;
g_top_unit_task = newTask;
return;
}
else
{ // else in middle
LPUNIT_DOING_TASK temp_task; LPUNIT_DOING_TASK next_task = (LPUNIT_DOING_TASK) NULL;
for ( temp_task=g_top_unit_task; temp_task != (LPUNIT_DOING_TASK)NULL; temp_task=next_task )
{
next_task = temp_task->next;
if ( (newTask->time) < (temp_task->time) )
{
newTask->prev = temp_task->prev ;
newTask->next = temp_task ;
newTask->prev->next = newTask;
temp_task->prev = newTask;
return;
}
}
}
////end debug: turn off
}
this code should place the following double linked node in order of time. It is needed so that only those nodes whose time are up are accessed.
Code:
typedef struct _UNIT_DOING_TASK // struct tasks
{
struct _unit* unit_doing;
struct _unit* link_to_another_unit;
struct _UNIT_DOING_TASK* next;
struct _UNIT_DOING_TASK* prev;
int going_to_x;
int going_to_y;
long start_at_x;
long start_at_y;
short int doing;
DWORD time;
DWORD start_time;
}UNIT_DOING_TASK;
//////////////////////////
typedef UNIT_DOING_TASK* LPUNIT_DOING_TASK;
LPUNIT_DOING_TASK g_bottom_unit_task ;
LPUNIT_DOING_TASK g_top_unit_task ;
the code does not do what it is suposed to (but runs) and is turned off in the program, with the bellow code used to stitch it up (replacing where the //debug turn: off begines and ends. In the above code the unites effectively overshot the hexes (the locotion they must be at at the end of the time) by a margin that seems to increase according to a) the number of nodes in the list and b) the the number that are path finding. Perhaps indicative of an influence in the games latency ie. it works with one node, with ten but not 30, 40 etc, the speed of the time also increases the overshooting.
Code:
//debugfile ("add task\n", 1 ); ///debug: just add to bottom
if (g_top_unit_task == (LPUNIT_DOING_TASK) NULL ) // if no other task
{
g_bottom_unit_task = newTask;
g_top_unit_task= newTask;
newTask->prev = (LPUNIT_DOING_TASK) NULL ;
newTask->next = (LPUNIT_DOING_TASK) NULL ;
return;
}
else // at top of list
{
g_top_unit_task->prev=newTask;
newTask->prev = (LPUNIT_DOING_TASK) NULL ;
newTask->next = g_top_unit_task ;
g_top_unit_task = newTask;
return;
}
this replacement code works but is slow (it must go through the hole list every time it is called which is about 20 times a second.), at the moment it is fine, but my project has ambitions of thousands of LPUNIT_DOING_TASK's in the waiting list, and I forsee this to be a problem erea.
I have done a few linked lists so these are not my first foray into organising them, everything seems in order. Perhaps it is one of those problems that you can't see for looking and need fresh eyes.
these codes file can be found in unit_movement.h and structures.h within the “code file” of the projects downloaded zip file. the project is housed at: ad-infinitum | Download ad-infinitum software for free at SourceForge.net
yours ever,
BB
ps, please do not critasize (as they do in your Games forum) my use of outdated libraries. I use the libraries I use and I am tired of explaining why.