> There are a couple of built-in functions within Cold Fusion
> as well as other languages like Javascript etc. etc. that
> Im curious about.
> Abs()
> Atn() (or otherwise referred to as Atan)
>
> Anyone have a real world example of these within a CF
> App???
Yup here's an entertaining, if not unlong snippit of code...
It's used to help run a virtual ecosystem, the below helps me work out
which target creatures are within the source creatures Field of
Vision. i.e. the sourceCreature can be facing 45 degree (northwest),
and have a FoV of 120 degree, the below code works out the angle
between the source creature and target creature, and then the angle
between that angle and the facing angle (nording) of the source
creature, and checks to see if it falls within the FoV.
Of course I assume the whole thing would be faster in <cfscript> and
using some form of look-up table.
I don't know about "real-world", but this is artificial world :)
-----------------------------
<cfset skipCalc = false>
<cfif targetX EQ sourceX AND targetY GTE sourceY>
<cfset skipCalc = true>
<cfset newAngle = 0>
</cfif>
<cfif targetX EQ sourceX AND targetY LT sourceY>
<cfset skipCalc = true>
<cfset newAngle = 180>
</cfif>
<cfif targetY EQ sourceY AND targetX GT sourceX>
<cfset skipCalc = true>
<cfset newAngle = 90>
</cfif>
<cfif targetY EQ sourceY AND targetX LT sourceX>
<cfset skipCalc = true>
<cfset newAngle = 270>
</cfif>
<cfif skipCalc NEQ true>
<cfset fraction = Evaluate((abs(sourceX - targetX)/abs(sourceY -
targetY)))>
<cfset baseAngle = Evaluate(Atn(fraction) * (180/PI()))>
<cfif targetX GTE sourceX and targetY GTE sourceY>
<cfset newAngle = baseAngle>
</cfif>
<cfif targetX GTE sourceX and targetY LT sourceY>
<cfset newAngle = int(180-baseAngle)>
</cfif>
<cfif targetX LT sourceX and targetY GTE sourceY>
<cfset newAngle = int(360-baseAngle)>
</cfif>
<cfif targetX LT sourceX and targetY LT sourceY>
<cfset newAngle = int(180+baseAngle)>
</cfif>
</cfif>
<!--- check to see if the target creature is within the sources
Fov --->
<cfif abs(sourceNording - newAngle) LT abs(sourceNording -
(newAngle-360))>
<cfset testAngle = abs(sourceNording - newAngle)>
<cfelse>
<cfset testAngle = abs(sourceNording - (newAngle-360))>
</cfif>
<cfif testAngle LTE (sourceFoV/2)>
<!--- Work out the attack strength, i.e. the one we'd inflict the
most damage on, who
isn't too far away --->
<cfset Distance = int(SQR((sourceX - targetX)*(sourceX -
targetX)+(sourceY - targetY)*(sourceY - targetY)))>
<cfset attackStrength = int((128-Distance)*(TvTMod/100)*(TvTMod/100))>
<!--- if there is a attack strength, then this is a better
target --->
<cfif attackStrength GT targetCreatureScore AND Distance LTE
sourceDoV AND Distance LTE int(sourceDistance*5)>
<cfset targetCreatureScore = attackStrength>
<cfset targetCreature = targetID>
<cfset targetCreatureX = targetX>
<cfset targetCreatureY = targetY>
<cfset targetDistance = Distance>
<cfset targetAngle = int(newAngle)>
<cfset sourceTargetTheta = int(testAngle)>
</cfif>
</cfif>
----------------------------
Oh yeah, and for the record, here's the SQL that get's the 10 nearest
target creatures, within the source creatures view distance.
<cfquery name="findCreatures" datasource="miniSphere" maxRows="10">
SELECT id as targetID,
x as targetX,
y as targetY,
creatureType as targetCreatureType,
dmgTvTMod.modify as TvTMod,
dmgAvBMod.modify as AvBMod
FROM creature,
dmgTypeVrsTypeMod dmgTvTMod,
dmgAttackVrsBodyMod dmgAvBMod
WHERE id <> #sourceID#
AND energyLevel > 0
AND dmgTvTMod.defenderTypeID = creatureType
AND dmgTvTMod.attackerTypeID = #sourceCreatureType#
AND dmgAvBMod.bodyTypeID = bodyType
AND dmgAvBMod.attackTypeID = #sourceAttackType#
ORDER BY ((x-#sourceX#)*(x-#sourceX#)+(y-#sourceY#)*(y-#sourceY#))
</cfquery>
Notice the scary ORDER BY bit, any suggestions on how to turn that
into a stored procedure greatly welcomed :)
-------------------------
R'grds,
Dan.
This message is intended only for the use of the person(s) ("the intended
recipient(s)") to whom it is addressed.
It may contain information which is privileged and confidential within the meaning of
the applicable law.
If you are not the intended recipient, please contact the sender as soon as possible.
The views expressed in this communication may not necessarily be the views held by
Live Information Systems Limited.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists