One little snag....
The original code this was designed to replace has been redone. Mob::CalculateNewPosition and Mob::CalculateNewPosition2 are no longer identical. Major changes have been made while this discussion was ocurring, making a direct merge of the new code impossible.
It looks to me like many of the ideas have been applied, although not as a merge of the code. Here is what we have now:
Code:
bool Mob::CalculateNewPosition(float x, float y, float z, float speed) {
if(GetID()==0)
return true;
/*if(tar_ndx<20 && tarx==x && tary==y && tarz==z){
tar_ndx++;
x_pos = x_pos + tar_vx*tar_vector;
y_pos = y_pos + tar_vy*tar_vector;
z_pos = z_pos + tar_vz*tar_vector;
return true;
}
else{
tar_ndx=0;
tarx=x;
tary=y;
tarz=z;
}*/
float nx = x_pos;
float ny = y_pos;
float nz = z_pos;
float nh = heading;
// if NPC is rooted
if (speed == 0.0) {
SetHeading(CalculateHeadingToTarget(x, y));
if(moved){
SendPosition();
SetMoving(false);
moved=false;
}
SetRunAnimSpeed(0);
return true;
}
float old_test_vector=test_vector;
tar_vx = x - nx;
tar_vy = y - ny;
tar_vz = z - nz;
if (tar_vx == 0 && tar_vy == 0 && tar_vz == 0)
return false;
pRunAnimSpeed = (int8)(speed*43);
speed *= 2.8;
// --------------------------------------------------------------------------
// 2: get unit vector
// --------------------------------------------------------------------------
test_vector=sqrt (x*x + y*y + z*z);
tar_vector = speed / sqrt (tar_vx*tar_vx + tar_vy*tar_vy + tar_vz*tar_vz);
heading = CalculateHeadingToTarget(x, y);
if (tar_vector >= 1.0) {
x_pos = x;
y_pos = y;
z_pos = z;
}
else {
x_pos = x_pos + tar_vx*tar_vector;
y_pos = y_pos + tar_vy*tar_vector;
z_pos = z_pos + tar_vz*tar_vector;
}
//OP_MobUpdate
if((old_test_vector!=test_vector) || tar_ndx>20){ //send update
tar_ndx=0;
APPLAYER* outapp = NULL;
PlayerPositionUpdateServer_Struct* ppu = NULL;
this->SetMoving(true);
moved=true;
outapp = new APPLAYER(OP_ClientUpdate, sizeof(PlayerPositionUpdateServer_Struct));
ppu = (PlayerPositionUpdateServer_Struct*)outapp->pBuffer;
delta_x=(x_pos-nx);
delta_y=(y_pos-ny);
delta_z=(z_pos-nz);
delta_heading=0;//(heading-nh)*8;
MakeSpawnUpdate(ppu);
ppu->heading*=8;
entity_list.QueueCloseClients(this, outapp, true, 300);
safe_delete(outapp);
}
tar_ndx++;
// now get new heading
SetAppearance(0, false); // make sure they're standing
pLastChange = Timer::GetCurrentTime();
return true;
}
and
Code:
bool Mob::CalculateNewPosition2(float x, float y, float z, float speed) {
if(GetID()==0)
return true;
if(tar_ndx<20 && tarx==x && tary==y && tarz==z){
x_pos = x_pos + tar_vx*tar_vector;
y_pos = y_pos + tar_vy*tar_vector;
z_pos = z_pos + tar_vz*tar_vector;
tar_ndx++;
return true;
}
else{
tar_ndx=0;
tarx=x;
tary=y;
tarz=z;
}
float nx = this->x_pos;
float ny = this->y_pos;
float nz = this->z_pos;
float nh = this->heading;
tar_vx = x - nx;
tar_vy = y - ny;
tar_vz = z - nz;
if (tar_vx == 0 && tar_vy == 0 && tar_vz == 0)
return false;
pRunAnimSpeed = (sint8)(speed*36);
speed *= 46;
// --------------------------------------------------------------------------
// 2: get unit vector
// --------------------------------------------------------------------------
tar_vector = speed / sqrt (tar_vx*tar_vx + tar_vy*tar_vy + tar_vz*tar_vz);
heading = CalculateHeadingToTarget(x, y);
if (tar_vector >= 1.0) {
x_pos = x;
y_pos = y;
z_pos = z;
}
else {
tar_vector/=20;
x_pos = x_pos + tar_vx*tar_vector;
y_pos = y_pos + tar_vy*tar_vector;
z_pos = z_pos + tar_vz*tar_vector;
}
APPLAYER* outapp = NULL;
PlayerPositionUpdateServer_Struct* spu = NULL;
this->SetMoving(true);
moved=true;
outapp = new APPLAYER(OP_ClientUpdate, sizeof(PlayerPositionUpdateServer_Struct));
spu = (PlayerPositionUpdateServer_Struct*)outapp->pBuffer;
delta_x=x_pos-nx;
delta_y=y_pos-ny;
delta_z=z_pos-nz;
delta_heading=heading-nh;
MakeSpawnUpdate(spu);
spu->heading*=8;
entity_list.QueueCloseClients(this, outapp, true, 300);
safe_delete(outapp);
SetAppearance(0, false);
pLastChange = Timer::GetCurrentTime();
return true;
}
I think you can see a lot of the same ideas being applied. One interesting addition is the supression of update packets here, which should help.
So, should we try to merge in the logic from each version? Merge the code anyway? Test both versions and pick the best?
Opinions?