Jump to content

Can anybody slim down my script?


Lash Carver
 Share

You are about to reply to a thread that has been inactive for 3525 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

vector spreadVector = <(0-(rez_spread/2)) + llFrand(rez_spread),(0-(rez_spread/2)) + llFrand(rez_spread),(0-(rez_spread/2)) + llFrand(rez_spread)>;
rotation rotBetween=llRotBetween(<0.01,0.01,0.01>,<0.01,0.01,0.01>+(DEG_TO_RAD*accuracy*(spreadVector*llGetCameraRot())));
rotation modRot = llGetCameraRot() / <rotBetween.x*llPow(-1,llRound(llFrand(1))),rotBetween.y*llPow(-1,llRound(llFrand(1))),rotBetween.z*llPow(-1,llRound(llFrand(1))),rotBetween.s*llPow(-1,llRound(llFrand(1)))>;
llRezObject(bullet,llGetCameraPos() + (rez_offset + spreadVector)*llGetCameraRot(), llRot2Fwd(modRot)*velocity, modRot,100);

 

My Gun script does ALOT of calculations and I was just wondering if anybody can slim it down at all, possibly making it more efficiant for secondlife to read.

 

Any help would be great as always!

Link to comment
Share on other sites

i keep looking at this and hoping that someone else will answer it lol (:

oh well !!

is so much going on here that is hard to know where to start. Would be a bit easier i think if knew what the bounds of rez_spread are ?!?

but anyways, i think i would try to reduce the number of calls to functions and try reduce the number of arithmetics. For example to get started then

in the first line is 3 divisions for the same thing: 0-(rez_spread/2)

I probably move that to a var:

float rez_spread_by_two = 0 - (rez_spread / 2);

so 1 division instead of 3

+

another one to simplify is: llPow(-1,llRound(llFrand(1))

is equivalent to: -1 + 2 * (integer)llFrand(2);

is a heap of calls to llFrand which can be simplified down to 1 call and then use bit mapping equiv similar to above. example:

 

// from thisrotation modRot = llGetCameraRot() / <rotBetween.x*llPow(-1,llRound(llFrand(1))),rotBetween.y*llPow(-1,llRound(llFrand(1))),rotBetween.z*llPow(-1,llRound(llFrand(1))),rotBetween.s*llPow(-1,llRound(llFrand(1)))>;// to thisinteger map = (integer)llFrand(16);rotation modRot = llGetCameraRot() / <  rotBetween.x * (-1 + (map << 1 & 2)),  rotBetween.y * (-1 + (map & 2)),  rotBetween.z * (-1 + (map >> 1 & 2)),  rotBetween.s * (-1 + (map >> 2 & 2))>;  

 

if knew the bounds of rez_spread then could reduce them llFrands in the 1st line down to 1 as well in the same kinda mapping way maybe

Link to comment
Share on other sites

irhapeti's mapping cuts the calculation time for that section by half, so it would bevery beneficial to apply it to the first section as well.  but the spread is very likely to be a float and needs to be adjustable, so bitwise operations are probably not in the cards for that part.  

compared to that my only suggestion is peanuts,  but you should be converting your accuracy to radians when it's set,  not inside your action loop.  saves one multiplication per iteration.

 

float half_spread = rez_spread*-0.5;vector spreadVector = <	llFrand(rez_spread)+half_spread,						llFrand(rez_spread)+half_spread,						llFrand(rez_spread)+half_spread>;//When accuracy is changed, or stored it should be converted to radians at that time//DEG_TO_RAD*accuracyrotation rotBetween=llRotBetween(<0.01,0.01,0.01>,<0.01,0.01,0.01>+(accuracy*(spreadVector*llGetCameraRot())));integer map = (integer)llFrand(16);rotation modRot = llGetCameraRot() / <  rotBetween.x * (-1 + (map << 1 & 2)),  rotBetween.y * (-1 + (map & 2)),  rotBetween.z * (-1 + (map >> 1 & 2)),  rotBetween.s * (-1 + (map >> 2 & 2))>; llRezObject(bullet,llGetCameraPos() + (rez_offset + spreadVector)*llGetCameraRot(), llRot2Fwd(modRot)*velocity, modRot,100);

 

Link to comment
Share on other sites

yes moving the DEG_... multiply out of the loop is good idea

+

maybe Lash will come back and tell us about the bounds of spread_vector. I be kinda interested to know what it is

in the meantime just some thoughts on floating point in these cases

if say the bounds of spread_vector was [0 < 1] then the same int bitshift mapping will work pretty much the same for the first line. However if is [0 < n) where n is some/any float then a little bit more involved like you mention

+

one way for the second case [0 < n) is to still use rand int but then convert to float. example

4 bits for the map we already used for the rotation. Then say 9 bits for each element of the vector. 4 + 9 + 9 + 9 = 31 bits
 
where each 9 bits is represented as a whole number multiplied/divided by the decimal precision (p) relative to number of bits and spread_vector

how many precision bits depends on the use case. But sticking with 9 bits for this example then:

 

integer map = (integer)llFrand(0x80000000);float p = spread_vector / 0x200;  vector a;  // for the 1st line of codesa.x = -spread_vector + 2 * ((map & 0x1FF) * p);a.y = -spread_vector + 2 * ((map >> 9 & 0x1FF) * p);a.z = -spread_vector + 2 * ((map >> 18 & 0x1FF) * p);// the next topmost 4 bits of map being used (shift adjusted)
// for the rotation like in previous examplemap >> 26 & 2;map >> 27 & 2;map >> 28 & 2;map >> 29 & 2;

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3525 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...