Mathematical optimizations.

This commit is contained in:
Robert MacGregor 2016-01-14 13:53:48 -05:00
parent 3058d538f8
commit 274d858679

View file

@ -4,7 +4,7 @@
// https://github.com/Ragora/T2-DXAI.git // https://github.com/Ragora/T2-DXAI.git
// //
// Copyright (c) 2015 Robert MacGregor // Copyright (c) 2015 Robert MacGregor
// This software is licensed under the MIT license. // This software is licensed under the MIT license.
// Refer to LICENSE.txt for more information. // Refer to LICENSE.txt for more information.
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
@ -12,10 +12,10 @@ function sameSide(%p1, %p2, %a, %b)
{ {
%cp1 = vectorCross(vectorSub(%b, %a), vectorSub(%p1, %a)); %cp1 = vectorCross(vectorSub(%b, %a), vectorSub(%p1, %a));
%cp2 = vectorCross(vectorSub(%b, %a), vectorSub(%p2, %a)); %cp2 = vectorCross(vectorSub(%b, %a), vectorSub(%p2, %a));
if (vectorDot(%cp1, %cp2) >= 0) if (vectorDot(%cp1, %cp2) >= 0)
return true; return true;
return false; return false;
} }
@ -26,17 +26,23 @@ function sameSide(%p1, %p2, %a, %b)
// Param %a: One point of the triangle. // Param %a: One point of the triangle.
// Param %b: One point of the triangle. // Param %b: One point of the triangle.
// Param %c: One point of the triangle. // Param %c: One point of the triangle.
// Return: A boolean representing whether or not the given point resides inside of the // Return: A boolean representing whether or not the given point resides inside of the
// triangle. // triangle.
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
function pointInTriangle(%point, %a, %b, %c) function pointInTriangle(%point, %a, %b, %c)
{ {
if (sameSide(%point, %a, %b, %c) && sameSide(%point, %b, %a, %c) && sameSide(%point, %c, %a, %b)) if (sameSide(%point, %a, %b, %c) && sameSide(%point, %b, %a, %c) && sameSide(%point, %c, %a, %b))
return true; return true;
return false; return false;
} }
function Player::getXFacing(%this)
{
%forward = %this.getForwardVector();
return mAtan(getWord(%forward, 1), getWord(%forward, 0));
}
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// Description: Calculates all the points of the given client's view cone given a maximum // Description: Calculates all the points of the given client's view cone given a maximum
// view distance and returns them in a long string. // view distance and returns them in a long string.
@ -48,60 +54,30 @@ function pointInTriangle(%point, %a, %b, %c)
// TODO: Return in a faster-to-read format: Could try as static GVar names // TODO: Return in a faster-to-read format: Could try as static GVar names
// as the game's scripting environment for the gameplay is single threaded // as the game's scripting environment for the gameplay is single threaded
// and it probably does a hash to store the values. // and it probably does a hash to store the values.
// FIXME: Mathematical optimizations, right now it's a hack because of no // FIXME: The horizontal view cones may be all that's necessary. A player
// reliable way of getting a player's X facing? Also, the horizontal view cones may // height check could be used to help alleviate computational complexity.
// be all that's necessary. A player height check could be used to help alleviate
// computational complexity.
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
function GameConnection::calculateViewCone(%this, %distance) function GameConnection::calculateViewCone(%this, %distance)
{ {
if (!isObject(%this.player) || %this.player.getState() !$= "Move") if (!isObject(%this.player) || %this.player.getState() !$= "Move")
return -1; return -1;
//%xFacing = %this.player.getXFacing(); %xFacing = %this.player.getXFacing();
%halfView = %this.fieldOfView / 2;
%coneOrigin = %this.player.getMuzzlePoint($WeaponSlot); %coneOrigin = %this.player.getMuzzlePoint($WeaponSlot);
%forwardVector = %this.player.getForwardVector(); %halfView = %this.fieldOfView / 2;
%sideVector = vectorCross("0 0 1", %forwardVector); %cos = mCos(%halfView);
%sin = mSin(%halfView);
// Clockwise
//%viewConeClockwise = %xFacing - %halfView; // Translate the horizontal points
%viewConeClockwisePoint = vectorAdd(%coneOrigin, vectorScale(%cos SPC -%sin SPC "0", %this.viewDistance));
// %viewConeClockwisePoint = mCos(%viewConeClockwise) SPC mSin(%viewConeClockwise) SPC "0"; %viewConeCounterClockwisePoint = vectorAdd(%coneOrigin, vectorScale(%cos SPC -%sin SPC "0", %this.viewDistance));
%viewConeClockwisePoint = mCos(-%halfView) SPC mSin(-%halfView) SPC "0";
%viewConeClockwisePoint = vectorScale(%viewConeClockwisePoint, %this.viewDistance);
//%viewConeClockwisePoint = vectorAdd(%viewConeClockwisePoint, %coneOrigin);
// Counter Clockwise
//%viewConeCounterClockwise = %xFacing + %halfView;
//%viewConeCounterClockwisePoint = mCos(%viewConeCounterClockwise) SPC mSin(%viewConeCounterClockwise) SPC "0";
%viewConeCounterClockwisePoint = mCos(%halfView) SPC mSin(%halfView) SPC "0";
%viewConeCounterClockwisePoint = vectorScale(%viewConeCounterClockwisePoint, %this.viewDistance);
//%viewConeCounterClockwisePoint = vectorAdd(%viewConeCounterClockwisePoint, %coneOrigin);
// Offsets
%halfDistance = vectorDist(%viewConeCounterClockwisePoint, %viewConeClockwisePoint) / 2;
%viewConeCounterClockwisePoint = vectorScale(%sideVector, %halfDistance);
%viewConeCounterClockwisePoint = vectorAdd(%coneOrigin, %viewConeCounterClockwisePoint);
%viewConeClockwisePoint = vectorScale(vectorScale(%sideVector, -1), %halfDistance);
%viewConeClockwisePoint = vectorAdd(%coneOrigin, %viewConeClockwisePoint);
// Translate the upper and lower points // Translate the upper and lower points
%viewForwardPoint = vectorScale(%forwardVector, %this.viewDistance); %halfDistance = vectorDist(%viewConeCounterClockwisePoint, %viewConeClockwisePoint) / 2;
%viewConeUpperPoint = vectorAdd(vectorScale("0 0 1", %halfDistance), %viewForwardPoint); %viewConeUpperPoint = vectorAdd(%coneOrigin, "0 0" SPC %halfDistance);
%viewConeUpperPoint = vectorAdd(%coneOrigin, %viewConeUpperPoint); %viewConeLowerPoint = vectorAdd(%coneOrigin, "0 0" SPC -%halfDistance);
%viewConeLowerPoint = vectorAdd(vectorScale("0 0 -1", %halfDistance), %viewForwardPoint);
%viewConeLowerPoint = vectorAdd(%coneOrigin, %viewConeLowerPoint);
// Now cast them forward
%viewConeClockwisePoint = vectorAdd(%viewConeClockwisePoint, vectorScale(%this.player.getForwardVector(), %this.viewDistance));
%viewConeCounterClockwisePoint = vectorAdd(%viewConeCounterClockwisePoint, vectorScale(%this.player.getForwardVector(), %this.viewDistance));
return %coneOrigin SPC %viewConeClockwisePoint SPC %viewConeCounterClockwisePoint SPC %viewConeUpperPoint SPC %viewConeLowerPoint; return %coneOrigin SPC %viewConeClockwisePoint SPC %viewConeCounterClockwisePoint SPC %viewConeUpperPoint SPC %viewConeLowerPoint;
} }
@ -116,17 +92,17 @@ function SimSet::recurse(%this, %result)
{ {
if (!isObject(%result)) if (!isObject(%result))
%result = new SimSet(); %result = new SimSet();
for (%iteration = 0; %iteration < %this.getCount(); %iteration++) for (%iteration = 0; %iteration < %this.getCount(); %iteration++)
{ {
%current = %this.getObject(%iteration); %current = %this.getObject(%iteration);
if (%current.getClassName() $= "SimGroup" || %current.getClassName() $= "SimSet") if (%current.getClassName() $= "SimGroup" || %current.getClassName() $= "SimSet")
%current.recurse(%result); %current.recurse(%result);
else else
%result.add(%current); %result.add(%current);
} }
return %result; return %result;
} }
@ -142,23 +118,23 @@ function GameConnection::getClosestInventory(%this)
{ {
if (!isObject(%this.player)) if (!isObject(%this.player))
return -1; return -1;
%group = nameToID("Team" @ %this.team); %group = nameToID("Team" @ %this.team);
if (!isObject(%group)) if (!isObject(%group))
return -1; return -1;
%teamObjects = %group.recurse(); %teamObjects = %group.recurse();
%closestInventory = -1; %closestInventory = -1;
%closestInventoryDistance = 9999; %closestInventoryDistance = 9999;
for (%iteration = 0; %iteration < %teamObjects.getCount(); %iteration++) for (%iteration = 0; %iteration < %teamObjects.getCount(); %iteration++)
{ {
%current = %teamObjects.getObject(%iteration); %current = %teamObjects.getObject(%iteration);
if (%current.getClassName() $= "StaticShape" && %current.getDatablock().getName() $= "StationInventory") if (%current.getClassName() $= "StaticShape" && %current.getDatablock().getName() $= "StationInventory")
{ {
%inventoryDistance = vectorDist(%current.getPosition(), %this.player.getPosition()); %inventoryDistance = vectorDist(%current.getPosition(), %this.player.getPosition());
if (%inventoryDistance < %closestInventoryDistance) if (%inventoryDistance < %closestInventoryDistance)
{ {
%closestInventoryDistance = %inventoryDistance; %closestInventoryDistance = %inventoryDistance;
@ -166,9 +142,9 @@ function GameConnection::getClosestInventory(%this)
} }
} }
} }
%teamObjects.delete(); %teamObjects.delete();
return %closestInventory; return %closestInventory;
} }
@ -190,27 +166,27 @@ function GameConnection::getObjectsInViewcone(%this, %typeMask, %distance, %perf
%this.fieldOfView = $DXAPI::Bot::DefaultFieldOfView; %this.fieldOfView = $DXAPI::Bot::DefaultFieldOfView;
error("DXAI: Bad field of view value! (" @ %this @ ".fieldOfView > 3.14 || " @ %this @ ".fieldOfView < 0)"); error("DXAI: Bad field of view value! (" @ %this @ ".fieldOfView > 3.14 || " @ %this @ ".fieldOfView < 0)");
} }
if (%this.viewDistance <= 0) if (%this.viewDistance <= 0)
{ {
%this.viewDistance = $DXAPI::Bot::DefaultViewDistance; %this.viewDistance = $DXAPI::Bot::DefaultViewDistance;
error("DXAI: Bad view distance value! (" @ %this @ ".viewDistance <= 0)"); error("DXAI: Bad view distance value! (" @ %this @ ".viewDistance <= 0)");
} }
if (%distance $= "") if (%distance $= "")
%distance = %this.viewDistance; %distance = %this.viewDistance;
%viewCone = %this.calculateViewCone(%distance); %viewCone = %this.calculateViewCone(%distance);
// Extract the results: See TODO above ::calculateViewCone implementation // Extract the results: See TODO above ::calculateViewCone implementation
%coneOrigin = getWords(%viewCone, 0, 2); %coneOrigin = getWords(%viewCone, 0, 2);
%viewConeClockwiseVector = getWords(%viewCone, 3, 5); %viewConeClockwiseVector = getWords(%viewCone, 3, 5);
%viewConeCounterClockwiseVector = getWords(%viewCone, 6, 8); %viewConeCounterClockwiseVector = getWords(%viewCone, 6, 8);
%viewConeUpperVector = getWords(%viewCone, 9, 11); %viewConeUpperVector = getWords(%viewCone, 9, 11);
%viewConeLowerVector = getWords(%viewCone, 12, 14); %viewConeLowerVector = getWords(%viewCone, 12, 14);
%result = new SimSet(); %result = new SimSet();
// Doing a radius search should hopefully be faster than iterating over all objects in MissionCleanup. // Doing a radius search should hopefully be faster than iterating over all objects in MissionCleanup.
// Even if the game did that internally it's definitely faster than doing it in TS // Even if the game did that internally it's definitely faster than doing it in TS
InitContainerRadiusSearch(%coneOrigin, %distance, %typeMask); InitContainerRadiusSearch(%coneOrigin, %distance, %typeMask);
@ -227,9 +203,9 @@ function GameConnection::getObjectsInViewcone(%this, %typeMask, %distance, %perf
else else
{ {
%rayCast = containerRayCast(%coneOrigin, %currentObject.getWorldBoxCenter(), $TypeMasks::AllObjectType, %this.player); %rayCast = containerRayCast(%coneOrigin, %currentObject.getWorldBoxCenter(), $TypeMasks::AllObjectType, %this.player);
%hitObject = getWord(%raycast, 0); %hitObject = getWord(%raycast, 0);
// Since the engine doesn't do raycasts against projectiles & items correctly, we just check if the bot // Since the engine doesn't do raycasts against projectiles & items correctly, we just check if the bot
// hit -nothing- when doing the raycast rather than checking for a hit against the object // hit -nothing- when doing the raycast rather than checking for a hit against the object
%workaroundTypes = $TypeMasks::ProjectileObjectType | $TypeMasks::ItemObjectType; %workaroundTypes = $TypeMasks::ProjectileObjectType | $TypeMasks::ItemObjectType;
@ -238,14 +214,14 @@ function GameConnection::getObjectsInViewcone(%this, %typeMask, %distance, %perf
} }
} }
} }
return %result; return %result;
} }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// Description: Gets a random position somewhere within %distance of the given position. // Description: Gets a random position somewhere within %distance of the given position.
// Param %position: The position to generate a new position around. // Param %position: The position to generate a new position around.
// Param %distance: The maximum distance the new position may be // Param %distance: The maximum distance the new position may be
// Param %raycast: A boolean representing whether or not a raycast should be made from // Param %raycast: A boolean representing whether or not a raycast should be made from
// %position to the randomly chosen location to stop on objects that may be in the way. // %position to the randomly chosen location to stop on objects that may be in the way.
// This is useful for grabbing positions indoors. // This is useful for grabbing positions indoors.
@ -254,16 +230,16 @@ function getRandomPosition(%position, %distance, %raycast)
{ {
// First, we determine a random direction vector // First, we determine a random direction vector
%direction = vectorNormalize(getRandom(0, 10000) SPC getRandom(0, 10000) SPC getRandom(0, 10000)); %direction = vectorNormalize(getRandom(0, 10000) SPC getRandom(0, 10000) SPC getRandom(0, 10000));
// Return the scaled result // Return the scaled result
%result = vectorAdd(%position, vectorScale(%direction, getRandom(0, %distance))); %result = vectorAdd(%position, vectorScale(%direction, getRandom(0, %distance)));
if (!%raycast) if (!%raycast)
return %result; return %result;
%rayCast = containerRayCast(%position, %result, $TypeMasks::AllObjectType, 0); %rayCast = containerRayCast(%position, %result, $TypeMasks::AllObjectType, 0);
%result = getWords(%raycast, 1, 3); %result = getWords(%raycast, 1, 3);
return %result; return %result;
} }
@ -273,7 +249,7 @@ function getRandomPosition(%position, %distance, %raycast)
// getRandomPosition with the raycast setting if all that is necessary is generating a // getRandomPosition with the raycast setting if all that is necessary is generating a
// position relative to the terrain object. // position relative to the terrain object.
// Param %position: The position to generate a new position around. // Param %position: The position to generate a new position around.
// Param %distance: The maximum distance the new position may be // Param %distance: The maximum distance the new position may be
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
function getRandomPositionOnTerrain(%position, %distance) function getRandomPositionOnTerrain(%position, %distance)
{ {
@ -289,8 +265,8 @@ function getRandomPositionOnTerrain(%position, %distance)
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
function vectorMult(%vec1, %vec2) function vectorMult(%vec1, %vec2)
{ {
return (getWord(%vec1, 0) * getWord(%vec2, 0)) SPC return (getWord(%vec1, 0) * getWord(%vec2, 0)) SPC
(getWord(%vec1, 1) * getWord(%vec2, 1)) SPC (getWord(%vec1, 1) * getWord(%vec2, 1)) SPC
(getWord(%vec1, 2) * getWord(%vec2, 2)); (getWord(%vec1, 2) * getWord(%vec2, 2));
} }