| 09-26-2007, 03:07 PM | #1 |
Im working on a projectile system i am finally bringing into the open. I would like for people to review my code for efficiency problems or syntactical errors. I am just starting vJASS and hope you guys can help me make my code efficient. Alot of things are still being worked on as well. JASS://constant function Fd takes real v returns real // return (.5*udg_FluidDensity*(v*v)*1*udg_Cd) //endfunction globals real GRAVFORCE = 9.8 // this is the force (in m/s) that gravity incurs on objects real GRAVFORCEINSECONDS = 1 //Say i had gravforce = 19.6 m/2s, then GRAVFORCEINSECONDS would be 2 endglobals library VectorLib //This is a simple force vector, Fx, Fy, Fz. Includes integrated Gravity and Drag (to be added) vectors struct Vector //This is a simple vector, nothing fancy real x //This detirmines where the vector 'is' real y //This detirmines where the vector 'is' real z //This detirmines where the vector 'is' real Fx //These values are counterintuitive from what you might think real Fy //These are based on planes, rather than force, a negative is backwards, thats why there is no directional value real Fz //This way, it requires less math and is easier and less taxing if many events are happening at once //the force values should be speed/sec. method SetX takes real x returns nothing set this.x = x endmethod method SetY takes real y returns nothing set this.y = y endmethod method SetZ takes real z returns nothing set this.z = z endmethod method SetFx takes real m returns nothing set this.Fx = m endmethod method SetFy takes real m returns nothing set this.Fy = m endmethod method SetFz takes real m returns nothing set this.Fz = m endmethod endstruct function VectorNull takes Vector A returns Vector Vector v = Vector.create() v.x = A.x v.y = A.y v.z = A.z v.Fx = 0 v.Fy = 0 v.Fz = 0 return v call Vector.destroy(v) endfunction function VectorNormal takes Vector A returns Vector Vector v = Vector.create() v.x = A.x v.y = A.y v.z = A.z v.Fx = 1 v.Fy = 1 v.Fz = 1 return v call Vector.destroy(v) endfunction function VectorAdd takes Vector A, Vector B returns Vector Vector v = Vector.create() set v.Fx = A.Fx + B.Fx set v.Fy = A.Fy + B.Fy set v.Fz = A.Fz + B.Fz return v call Vector.destroy(v) endfunction function VectorSubtract takes Vector A, Vector B returns Vector Vector v = Vector.create() set v.Fx = A.Fx - B.Fx set v.Fy = A.Fy - B.Fy set v.Fz = A.Fz - B.Fz return v call Vector.destroy(v) endfunction function VectorMultiply takes Vector A, Vector B returns Vector Vector v = Vector.create() set v.Fx = A.Fx * B.Fx set v.Fy = A.Fy * B.Fy set v.Fz = A.Fz * B.Fz return v call Vector.destroy(v) endfunction function VectorDivide takes Vector A, Vector B returns Vector Vector v = Vector.create() set v.Fx = A.Fx / B.Fx set v.Fy = A.Fy / B.Fy set v.Fz = A.Fz / B.Fz return v call Vector.destroy(v) endfunction function GRAVITYINIT takes nothing returns nothing //MUST BE CALLED ON INITIALIZATION, I CANT STRESS THIS ENOUGH. Vector GRAVITYVECTOR = Vector.create() set GRAVITYVECTOR.x = 0 set GRAVITYVECTOR.y = 0 set GRAVITYVECTOR.z = 0 set GRAVITYVECTOR.Fx = 0 set GRAVITYVECTOR.Fy = 0 set GRAVITYVECTOR.Fz = -9.8 endfunction endlibrary struct Projectile private real BaseTerrainLevel // This is used when FollowsTerrainHeight is FALSE, this stands as the base terrain level real Cd //This is the drag coefficient, this effects how air drag effects the projectile real RA //This is the reference area, this is used in calculating drag real ObjectCollisionSize //Say the unit has a proximity effect, this would allow control over that proximity size real Mass // To make a projectile that doesnt fall, set mass to zero private integer RanTimes //dont screw with this value, it controls the Z calculation value of the projectile real Cd // to make a projectile that doesnt slow down, set to zero location TargetPoint unit Target //More or less for homing projectiles unit ProjDummy real TimeStep boolean EffectedByWind //At the moment this does not effect the unit, but when wind is implemented, spells that create wind or natural wind will not effect the projectile, for most objects this should probably be used. boolean HitsCliffs //this could be toggled false so that it floats through cliffs boolean HitsUnits boolean ExplodesOnGround boolean FollowsTerrainHeight //This is a big one, this determines wether the unit's z is based off terrain height, or a base level (the base level is intially based of terrain height, but then becomes independed of terrain height) boolean HitsCaster //If the spell rebounds back by some means, can it hit the caster boolean HitsAllies string CasterCollision //Function name to run on caster collision string AllyCollision //Function name to run on allied collision string UnitCollision //Function name to run on unit collision string GroundCollision //Function name to run on Ground collision string CliffCollision //Function name to run on cliff collision private Vector v method DetectCliffCollision takes nothing returns nothing // Placeholder endmethod method DetectUnitCollision takes nothing returns nothing // Placeholder endmethod method SetFacingAngleToTargetedPoint takes nothing returns nothing call SetUnitFacing(this.ProjDummy,3.14159/180.0 * Atan2(GetLocationY(this.TargetPoint) - GetLocationY(GetUnitLoc(this.ProjDummy)), GetLocationX(this.TargetPoint) - GetLocationX(GetUnitLoc(this.ProjDummy))) endmethod method SetFacingAngleToTargetedUnit takes nothing returns nothing // This allows a 'homing' projectile call SetUnitFacing(this.ProjDummy,3.14159/180.0 * Atan2(GetLocationY(GetUnitLoc(this.Target)) - GetLocationY(GetUnitLoc(this.ProjDummy)), GetLocationX(GetUnitLoc(this.Target)) - GetLocationX(GetUnitLoc(this.ProjDummy))) endmethod method GetGravForce takes nothing returns real return (this.Mass * _GLOBALMASS_) //Placeholder for actual grav formula endmethod method SetNewXYZ takes nothing returns nothing //Doesnt move unit, just sets the values to prepare movement. set v.x = v.x + v.Zx * this.TimeStep set v.y = v.y + v.Zy * this.TimeStep if (this.FollowsTerrainHeight == TRUE) then call VectorAdd(v,_GRAVITY_) set v.z = v.Fz + v.z else // ((z-grav*X)*TS) call VectorAdd(v,_GRAVITY_) set v.Fz = (TERRAINHEIGHT-this.BaseTerrainLevel) set v.z = v.Fz + v.z endif endmethod method MoveToXYZ takes nothing returns nothing call SetUnitPosition(this.ProjDummy,v.x,v.y) call UnitAddAbility(u,'Amrf') call SetUnitFlyHeight(u,v.z,0) call UnitRemoveAbility(u,'Amrf') endmethod method SmartMove takes nothing returns nothing // this pretty much combines MovetoXYZ and SetNewXYZ set v.x = v.x + v.Zx * this.TimeStep set v.y = v.y + v.Zy * this.TimeStep if (this.FollowsTerrainHeight == TRUE) then call VectorAdd(v,_GRAVITY_) set v.z = v.Fz + v.z else // ((z-grav*X)*TS) call VectorAdd(v,_GRAVITY_) set v.Fz = (TERRAINHEIGHT-this.BaseTerrainLevel) set v.z = v.Fz + v.z endif call SetUnitPosition( this.ProjDummy , v.x , v.y ) call UnitAddAbility( u , 'Amrf' ) call SetUnitFlyHeight( u , v.z , 0 ) call UnitRemoveAbility( u , 'Amrf' ) endmethod method Initialization takes real x, real y, real z, real Fx, real Fy, real Fz returns nothing //initializes all the code with fewer lines of code set this.RanTimes = 0 set v = Vector.create() set v.x = x set v.y = y set v.z = z set v.Fx = Fx set v.Fy = Fy set v.Fz = Fz endmethod endstruct |
| 09-27-2007, 02:08 AM | #2 |
Code updated; code is now updated to reflect the most recent version of the code; Fixes incluede: -Fixed a type with methed rather than method -Switched integration to vectors (i think i screwed up vector normal, i only know 2-d vector normals) -Removed excess variables. -Added more comments to explain code Please feel free to suggest fixes or report problems, this has not been tested in WE yet. Thanks in advance -TheSecretArts |
| 09-27-2007, 02:21 AM | #3 |
variable names can't begin with _ or numbers, what's that doing outside the library anyways? |
| 09-27-2007, 03:23 AM | #4 |
ok, forgot about that rule, over the summer, i got in the habit of prefixing and postfixing an underscore to global variables. Those global variables are more 'out there' variables, they really don't belong to a certain library, they are 'owned' by all libraries so to speak. I guess it makes adding on components easier *shrugs* |
| 09-28-2007, 12:17 PM | #5 |
JASS:private Vector v method Initialization takes real x, real y, real z, real Fx, real Fy, real Fz returns nothing //initializes all the code with fewer lines of code set v = Vector.create() set v.x = x set v.y = y set v.z = z set v.Fx = Fx set v.Fy = Fy set v.Fz = Fz endmethod |
| 09-28-2007, 12:48 PM | #6 |
you have to use JASS:
set this.v = Vector.create()
set this.v.x = .... JASS:
set .v = Vector.create()
set .v.x = ....and the other sets accordingly |
| 09-28-2007, 04:19 PM | #7 |
i use it the way i had above elsewhere, and it passed syntax checker there. Thought: Maybe its because i already have a local variable named v, I had that problem earlier, and had to change all variables named "v", I think i overlooked that variable... ill check later today. Edit: Oh, nevermind, i got it..., thanks for the help. |
| 09-28-2007, 07:11 PM | #8 |
i'm not sure if i understood right the issue, but I think this is the mistake: JASS:private struct Vector real x real y real z real Fx real Fy real Fz static method create takes real x, real y, real z, real Fx, real Fy, real Fz returns Vector local Vector v = Vector.allocate() set v.x = x set v.y = y set v.z = z set v.Fx = Fx set v.Fy = Fy set v.Fz = Fz return v endmethod endstruct Now your struct can be called in the way: JASS:function bla takes nothing returns nothing local Vector v = Vector.create(10, 20, 30, 500, 600, 700) // and your code goes here :) endfunction |
| 09-28-2007, 09:52 PM | #9 |
no, it was that i was forgetting that Vector v was a member of struct Projectile. Meaning that the parser thought that i was reffering to a global rather than a struct member and since there were no global vectors named v, it returned an error. |
