home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 January / Gamestar_69_2005-01_dvd.iso / Dema / theprojectv1-0.exe / FCData / Scripts.pak / Scripts / common.lua < prev    next >
Encoding:
Text File  |  2004-07-21  |  29.4 KB  |  1,142 lines

  1. ----------------------------------------------------------------------------------
  2. -- A set of useful vectors, so they don't have to be created on the fly everytime.
  3. -- As references to these are kept please make sure that code using these
  4. -- references does not modify the content.
  5. ----------------------------------------------------------------------------------
  6. g_Vectors =
  7. {
  8.     v000={x=0,y=0,z=0},
  9.     v001={x=0,y=0,z=1},
  10.     v010={x=0,y=1,z=0},
  11.     v011={x=0,y=1,z=1},
  12.     v100={x=1,y=0,z=0},
  13.     v101={x=1,y=0,z=1},
  14.     v110={x=1,y=1,z=0},
  15.     v111={x=1,y=1,z=1},
  16.  
  17.     up = {x=0,y=0,z=1},
  18.     down = {x=0,y=0,z=-1},
  19.  
  20.     temp={x=0,y=0,z=0},
  21.     tempColor={x=0,y=0,z=0},
  22.  
  23.     --PointInsideBBox temp vectors
  24.     temp_v1={x=0,y=0,z=0},
  25.     temp_v2={x=0,y=0,z=0},
  26.     temp_v3={x=0,y=0,z=0},
  27.     temp_v4={x=0,y=0,z=0},
  28. }
  29.  
  30. --------------------------------------------------------------------------------
  31. -- Reads a table from a file, and returns it
  32. --------------------------------------------------------------------------------
  33. -- the file should contain only line of the type:
  34. -- keyname = value
  35. --------------------------------------------------------------------------------
  36. function ReadTableFromFile(szFilename, LineMode)
  37.  
  38.     local hfile = openfile(szFilename, "r");
  39.  
  40.     if (hfile == nil) then
  41.         return
  42.     end
  43.  
  44.     local iEqual;
  45.     local tList = {};
  46.     local szLine = read(hfile, "*l");
  47.     local szProp;
  48.     local szValue;
  49.  
  50.     while (szLine ~= nil) do
  51.     
  52.         if (strlen(szLine) > 0) then
  53.             if (strsub(szLine, -1) == "\n") then
  54.                 szLine = strsub(szLine, 1, strlen(szLine)-1);
  55.             end    
  56.         end
  57.         
  58.         if (strlen(szLine) > 0) then
  59.             if (LineMode) then
  60.                 tinsert(tList, szLine);
  61.             else
  62.                 if (strlen(szLine) > 0) then
  63.     
  64.                     iEqual = strfind(szLine, "=", 1, 1);
  65.     
  66.                     if (iEqual) then
  67.                         szProp = strsub(szLine, 1, iEqual-1);
  68.                         szValue = strsub(szLine, iEqual+1, -1);
  69.     
  70.                         tList[szProp] = szValue;
  71.                     else
  72.                         tList[szLine] = 0;
  73.                     end
  74.                 end
  75.             end
  76.         end
  77.         
  78.         szLine = read(hfile, "*l");
  79.     end
  80.  
  81.     closefile(hfile);
  82.  
  83.     if (LineMode) then
  84.         tList.n = nil;
  85.     end
  86.  
  87.     return tList;
  88. end
  89.  
  90. -------------------------------------------------------
  91. -- Convert seconds to mm:ss string
  92. -------------------------------------------------------
  93. function SecondsToString(iSeconds)
  94.  
  95.     local iMinutes = floor(iSeconds / 60);
  96.  
  97.     return sprintf("%.2d:%.2d", iMinutes, iSeconds - (iMinutes * 60));
  98. end
  99.  
  100. -------------------------------------------------------
  101. -- Broadcast Console Print - use only on the server (verbosity 1)
  102. -------------------------------------------------------
  103. function BroadcastConsolePrint(str)
  104.     -- RCP remote console printout
  105.     Server:BroadcastCommand("RCP "..str);
  106.     if not Client then
  107.         System:LogAlways(str);
  108.     end
  109. end
  110.  
  111.  
  112. --------------------------------------------------------------------------------
  113. -- Get the Localized version of the string
  114. --------------------------------------------------------------------------------
  115. function Localize(Token)
  116.     if (strsub(Token, 1, 1) ~= "@") then
  117.         if Token then
  118.             return "@"..Token;
  119.         end
  120.         return "nil";
  121.     else
  122.         return Token;
  123.     end
  124. end
  125.  
  126. -------------------------------------------------------
  127. -- C like printf
  128. -------------------------------------------------------
  129. function printf(...)
  130.   System:LogToConsole(call(format,arg))
  131. end
  132. -------------------------------------------------------
  133. -- C like sprintf
  134. -------------------------------------------------------
  135. function sprintf(...)
  136.   return call(format,arg)
  137. end
  138.  
  139. -------------------------------------------------------
  140. -- tokenize a string
  141. -- BEWARE: this code doesn't handle special character like @ very well
  142. -- \return a table containing the tokens
  143. -------------------------------------------------------
  144. function tokenize(str)
  145.     local toks={}
  146.     local cmd=gsub(str,"(%S+)",function (s) tinsert(%toks,s) end);
  147.     toks.n=nil;
  148.     return toks;
  149. end
  150.  
  151.  
  152. -------------------------------------------------------
  153. --untokenize a string
  154. --returns a string containing the strings (other types are ignored) connected with " "
  155. -------------------------------------------------------
  156. function untokenize(toktable)
  157.     local fullstring="";
  158.  
  159.     for i,tok in toktable do
  160.         if type(tok)=="string" then
  161.             if fullstring=="" then
  162.                 fullstring=tok;
  163.             else
  164.                 fullstring=fullstring.." "..tok;
  165.             end
  166.         end
  167.     end
  168.  
  169.     return fullstring;
  170. end
  171.  
  172. -------------------------------------------------------
  173. --Clone a table
  174. -------------------------------------------------------
  175. function new(_obj)
  176.     if(type(_obj)=="table") then
  177.         local newInstance={};
  178.         for i,field in _obj do
  179.             if(type(field)=="table") then
  180.                 newInstance[i]=new(field);
  181.             else
  182.                 newInstance[i]=field;
  183.             end
  184.         end
  185.         return newInstance;
  186.     else
  187.         return _obj;
  188.     end
  189. end
  190.  
  191. -------------------------------------------------------
  192. --copy table source into the table dest skipping functions
  193. -------------------------------------------------------
  194. function merge(dest,source,recursive)
  195.     for i,v in source do
  196.         if(type(v)~="function") then
  197.             if(recursive) then
  198.                 if(type(v)=="table")then
  199.                     dest[i]={};
  200.                     merge(dest[i],v,recursive);
  201.                 else
  202.                     dest[i]=v;
  203.                 end
  204.             else
  205.                 dest[i]=v;
  206.             end
  207.         end
  208.     end
  209. end
  210.  
  211. -------------------------------------------------------
  212. --copy table source into the table dest with functions
  213. -------------------------------------------------------
  214. function mergef(dest,source,recursive)
  215.     for i,v in source do
  216.         if(recursive) then
  217.             if(type(v)=="table")then
  218.                 dest[i]={};
  219.                 mergef(dest[i],v,recursive);
  220.             else
  221.                 dest[i]=v;
  222.             end
  223.         else
  224.             dest[i]=v;
  225.         end
  226.     end
  227. end
  228.  
  229. g_dump_tabs=0;
  230. function dump(_class,no_func)
  231.     if not _class then
  232.         System:Log("$2nil");
  233.     else
  234.         local str="";
  235.         for n=0,g_dump_tabs,1 do
  236.             str=str.."  ";
  237.         end
  238.         for i,field in _class do
  239.             if(type(field)=="table") then
  240.                 g_dump_tabs=g_dump_tabs+1;
  241.                 System:Log(str.."$4"..i.."$1= {");
  242.                 dump(field);
  243.                 System:Log(str.."$1}");
  244.                 g_dump_tabs=g_dump_tabs-1;
  245.             else
  246.                 if(type(field)=="number" ) then
  247.                     System:Log("$2"..str.."$6"..i.."$1=$8"..field);
  248.                 elseif(type(field) == "string") then
  249.                     System:Log("$2"..str.."$6"..i.."$1=$8".."\""..field.."\"");
  250.                 else
  251.                     if(not no_func)then
  252.                         if(type(field)=="function")then
  253.                             System:Log("$2"..str.."$5"..i.."()");
  254.                         else
  255.                             System:Log("$2"..str.."$7"..i.."$8<userdata>");
  256.                         end
  257.                     end
  258.                 end
  259.             end
  260.         end
  261.     end
  262. end
  263.  
  264.  
  265. -------------------------------------------------------
  266. -- dump all globals variable (that might take a while - the system checks against circles)
  267. function gdump()
  268.     local referenced={}
  269.     local xdump=function (_class,no_func)
  270.         local str="";
  271.         for n=0,g_dump_tabs,1 do
  272.             str=str.."  ";
  273.         end
  274.         for i,field in _class do
  275.             if(type(field)=="table") then
  276.                 if(not %referenced[field])then
  277.                     g_dump_tabs=g_dump_tabs+1;
  278.                     System:Log(str..i.."= {");
  279.                     local idx=getn(%referenced)+1;
  280.                     %referenced[field]=idx;
  281.                     xdumpa(field);
  282.                     System:Log(str.."}");
  283.                     g_dump_tabs=g_dump_tabs-1;
  284.                 else
  285.                     System:Log(str..i.."=referenced["..tonumber(%referenced[field]).."]");
  286.                 end
  287.             else
  288.                 if(type(field)=="number" ) then
  289.                     System:Log(str..i.."="..field);
  290.                 elseif(type(field) == "string") then
  291.                     System:Log(str..i.."=".."\""..field.."\"");
  292.                 else
  293.                     if(not no_func)then
  294.                         if(type(field)=="function")then
  295.                             System:Log(str..i.."()");
  296.                         else
  297.                             System:Log(str..i.."<userdata>");
  298.                         end
  299.                     end
  300.                 end
  301.             end
  302.         end
  303.     end
  304.     xdumpa=xdump;
  305.     xdump(globals());
  306.     referenced=nil;
  307. end
  308.  
  309. -----------------------------------------------------------------------------
  310. function dotproduct3d(a,b)
  311.    return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
  312. end
  313.  
  314. function LogVec(name,v)
  315.     return format("%s = (%f %f %f)",name,v.x,v.y,v.z);
  316. end
  317.  
  318. function CopyVector(dest,src)
  319.     dest.x=src.x;
  320.     dest.y=src.y;
  321.     dest.z=src.z;
  322. end
  323. ----------------------------------
  324. function SumVectors(a,b)
  325.     return {x=a.x+b.x,y=a.y+b.y,z=a.z+b.z};
  326. end
  327.  
  328. ----------------------------------
  329. function FastSumVectors(dest,a,b)
  330.     dest.x=a.x+b.x;
  331.     dest.y=a.y+b.y;
  332.     dest.z=a.z+b.z;
  333. end
  334.  
  335. ----------------------------------
  336. function DifferenceVectors(a,b)
  337.     return {x=a.x-b.x,y=a.y-b.y,z=a.z-b.z};
  338. end
  339.  
  340. ----------------------------------
  341. function FastDifferenceVectors(dest,a,b)
  342.     dest.x=a.x-b.x;
  343.     dest.y=a.y-b.y;
  344.     dest.z=a.z-b.z;
  345. end
  346.  
  347. ----------------------------------
  348. function ProductVectors(a,b)
  349.     return {x=a.x*b.x,y=a.y*b.y,z=a.z*b.z};
  350. end
  351.  
  352. ----------------------------------
  353. function FastProductVectors(dest,a,b)
  354.     dest.x=a.x*b.x;
  355.     dest.y=a.y*b.y;
  356.     dest.z=a.z*b.z;
  357. end
  358.  
  359. ----------------------------------
  360. function LengthSqVector(a)
  361.     return (a.x * a.x + a.y * a.y + a.z * a.z);
  362. end
  363.  
  364. ----------------------------------
  365. function ScaleVector(a,b)
  366.     return {x=a.x*b,y=a.y*b,z=a.z*b};
  367. end
  368.  
  369. function ScaleVectorInPlace(a,b)
  370.     a.x=a.x*b;
  371.     a.y=a.y*b;
  372.     a.z=a.z*b;
  373. end
  374.  
  375. function ConvertToRadAngles(dest,src)
  376.         local x=rad(src.z+180.0);
  377.         local y=rad((-src.x)+90.0)
  378.         local z=rad(src.y);
  379.  
  380.       dest.x=-sin(y)*sin(x);
  381.       dest.y= sin(y)*cos(x);
  382.       dest.z=-cos(y);
  383. end
  384.  
  385. function ConvertVectorToCameraAngles(dest,src)
  386.  
  387.         local    fForward;
  388.         local    fYaw,fPitch;
  389.  
  390.         local temp = g_Vectors.temp;
  391.         CopyVector( temp,src );
  392.  
  393.         NormalizeVector(temp)
  394.  
  395.  
  396.         --first check for simple case
  397.         if (temp.y==0 and temp.x==0) then
  398.             --looking up/down
  399.             fYaw=0;
  400.             if (temp.z>0)then
  401.                 fPitch=90;
  402.             else
  403.                 fPitch=270;
  404.             end
  405.  
  406.         else
  407.             if (temp.x~=0)then
  408.  
  409.                 fYaw=atan2((temp.y),(temp.x))*(180.0/PI);
  410.  
  411.             else
  412.                 --lokking left/right
  413.                 if (temp.y>0) then
  414.                     fYaw=90;
  415.                 else
  416.                     fYaw=270;
  417.                 end
  418.             end
  419.             if (fYaw<0)then
  420.                 fYaw=fYaw+360;
  421.             end
  422.  
  423.             fForward=sqrt(temp.x*temp.x+temp.y*temp.y);
  424.             fPitch=(atan2(temp.z,fForward)*180.0/PI);
  425.             if (fPitch<0)then
  426.                 fPitch=fPitch+360;
  427.             end
  428.  
  429.         end
  430.  
  431.         --y = -fPitch;
  432.         --x = fYaw;
  433.         --z = 0;
  434.         temp.x=-fPitch;
  435.         temp.y=0; --can't calculate roll without an up vector
  436.         temp.z=fYaw+90;
  437.  
  438.         --clamp again
  439.         if (temp.x>360)then
  440.             temp.x=temp.x-360;
  441.         else
  442.             if(temp.x<-360)then
  443.                 temp.x=temp.x+360;
  444.             end
  445.         end
  446.  
  447.         if (temp.z>360)then
  448.             temp.z=temp.z-360;
  449.         else
  450.             if (temp.z<-360) then
  451.                 temp.z=temp.z+360;
  452.             end
  453.         end
  454.         dest.x=temp.x
  455.         dest.y=temp.y
  456.         dest.z=temp.z
  457.  
  458.     end
  459. ----------------------------------
  460. function ResetEnemies()
  461.     local entities=System:GetEntities();
  462.     for i, entity in entities do
  463.         if (entity.Behaviour) then
  464.             System:LogToConsole("$6Resetting "..entity.GetName().."...");
  465.             entity:OnReset();
  466. --            if (entity.cnt) then
  467. --                entity.cnt.health = 100;
  468. --                entity.AnimationSystemEnabled = 1;
  469. --                entity.EnablePhysics(1);
  470. --            end
  471.         end
  472.     end
  473. end
  474.  
  475. ----------------------------------
  476. function NormalizeVector(a)
  477.     local len=sqrt(LengthSqVector(a));
  478.     local multiplier;
  479.     if(len>0)then
  480.         multiplier=1/len;
  481.     else
  482.         multiplier=0.0001;
  483.     end
  484.     a.x=a.x*multiplier;
  485.     a.y=a.y*multiplier;
  486.     a.z=a.z*multiplier;
  487. end
  488. ----------------------------------
  489. function FastScaleVector(dest,a,b)
  490.     dest.x=a.x*b;
  491.     dest.y=a.y*b;
  492.     dest.z=a.z*b;
  493. end
  494.  
  495. --linear interpolation
  496. ----------------------------------
  497. function LerpColors(a,b,k)
  498.     g_Vectors.tempColor[1] = a[1]+(b[1]-a[1])*k
  499.     g_Vectors.tempColor[2] = a[2]+(b[2]-a[2])*k
  500.     g_Vectors.tempColor[3] = a[3]+(b[3]-a[3])*k
  501.     return g_Vectors.tempColor;
  502. end
  503.  
  504. ----------------------------------
  505. function Lerp(a,b,k)
  506.     return (a + (b - a)*k);
  507. end
  508.  
  509.  
  510. ----------------------------------
  511. function __max(a, b)
  512.     if (a > b) then
  513.         return a;
  514.     else
  515.         return b;
  516.     end
  517. end
  518.  
  519. ----------------------------------
  520. function GetPlayerWeaponInfo( player )
  521.     if (player == nil or player.cnt == nil) then
  522.         return nil;
  523.     end
  524.     local weaponid = player.cnt.weaponid;
  525.     local weaponstate = player.WeaponState;
  526.     if (weaponid~=nil and weaponstate~=nil) then
  527.         if (weaponstate[weaponid] == nil) then
  528.             -- init the weapon
  529.             BasicPlayer.ScriptInitWeapon(player, player.cnt.weapon.name);
  530.         end
  531.         return weaponstate[weaponid];
  532.     end
  533.  
  534.     return nil;
  535. end
  536.  
  537. ----------------------------------
  538. --IsDay=1;
  539. --LockToggle=0;
  540. --OldSun=1;
  541. --function ToggleDayNight( SetToDay, DontChangeSky, DontChangeSun )
  542. --    if (SetToDay) then
  543. --        IsDay=SetToDay;
  544. --    else
  545. --        if (LockToggle==0) then
  546. --            IsDay=1-IsDay;
  547. --        end
  548. --    end
  549. --    if (IsDay~=0) then
  550. --        if ( DontChangeSun == nil ) then
  551. --            e_sun=OldSun;
  552. --        end
  553. --        local    color = {x=1, y=1, z=1};
  554. --        System.SetWorldColor( color );
  555. --          System:SetWorldColorRatio( 1 );
  556. --        System.SetMaxFogDistRatio(1, 1, 1, 1);
  557. --        if ( DontChangeSky == nil ) then
  558. --            System:SetSkyBox("InfRedGal", 0, nil);
  559. --        end
  560. --    else
  561. --        if ( DontChangeSun == nil ) then
  562. --            OldSun=e_sun;
  563. --            e_sun=0;
  564. --        end
  565. --        local    color = {x=.1, y=.1, z=.3};
  566. --        System.SetWorldColor( color );
  567. --          System:SetWorldColorRatio( 0.15 );
  568. --        System.SetMaxFogDistRatio(1, 0, 0, 0);
  569. --        if ( DontChangeSky == nil ) then
  570. --            System:SetSkyBox("NightSky", 0, nil);
  571. --        end
  572. --    end
  573. --end
  574.  
  575. ----------------------------------
  576. function IsMaterialUnpierceble(material)
  577.     if(material==nil)then
  578.         System:LogToConsole("IsMaterialUnpierceble material is nil");
  579.         return 1
  580.     end
  581.     if(material.gameplay_physic==nil)then
  582.         System:LogToConsole("IsMaterialUnpierceble material.gameplay_physic is nil");
  583.         return 1
  584.     end
  585.     return material.gameplay_physic.piercing_resistence;
  586. end
  587.  
  588.  
  589. ----------------------------------
  590. function ExecuteMaterial(pos,normal,material,bSound, target_id, ipart, PlaySoundObj)
  591.  
  592.     if( not pos ) then
  593.         System:Log("\001  ExecuteMaterial with NULL pos <<<<<<< "  );
  594.         do return end;
  595.     end
  596.  
  597.     if(material == nil) then
  598.     --    System.LogToConsole("material is nil");
  599.         do return end;
  600.     end
  601.  
  602.     local particles=material.particles;
  603.     local loadedsounds=material.loadedsounds;
  604.  
  605.     if (loadedsounds == nil) then
  606.         --System:Log("****************/////////////////--------------material , loadedsounds was nil");
  607.         material.loadedsounds={};
  608.         loadedsounds=material.loadedsounds;
  609.     end
  610.  
  611.     local sounds=material.sounds;
  612.     local decal=material.decal;
  613.     local particleEffects=material.particleEffects;
  614.  
  615.     --PARTICLES----------------
  616.     if(particles ~= nil) then
  617.         if((particles.clippable==nil) or (particles.clippable and in_frustum))then
  618.             for i,particle in particles do
  619.                 --System:Log("g_gore="..getglobal("g_gore"));
  620.                 if((not particle.gore) or (getglobal("g_gore")==1) or (getglobal("g_gore")==2)) then
  621.                     Particle:CreateParticle(pos,normal,particle);
  622.                 end
  623.             end
  624.         end
  625.     end
  626.  
  627.     --PARTICLES EFFECTS----------------
  628.     if(particleEffects ~= nil) then
  629.         --System:Log("g_gore="..getglobal("g_gore"));
  630.         if ((not particleEffects.gore) or (getglobal("g_gore")==1) or (getglobal("g_gore")==2)) then
  631.             if (particleEffects.scale) then
  632.                 Particle:SpawnEffect(pos,normal, particleEffects.name,particleEffects.scale );
  633.             else
  634.                 Particle:SpawnEffect(pos,normal, particleEffects.name );
  635.             end
  636.         end
  637.     end
  638.  
  639.     --SOUNDS------------------
  640.     if((sounds ~= nil) and (bSound ~= nil)) then
  641.  
  642.         local nsounds=getn(sounds);
  643.  
  644.         if(nsounds > 0) then
  645.  
  646.             --local sound=sounds[random(1,nsounds)];
  647.  
  648.             -- grab a random position in the table
  649.             -- only do this if we are NOT on min spec
  650.             local slot=1;
  651.             if (tonumber(getglobal("sys_spec")) ~= 0) then
  652.                 slot = random(1,nsounds);
  653.             end
  654.  
  655.             -- check if this sound was already loaded
  656.             local sound=loadedsounds[slot];
  657.  
  658.             if (sound==nil) then
  659.                 -- if not, try to load it
  660.  
  661.                 local sounddesc=sounds[slot];
  662.  
  663.                 --System:Log("///////////////////////////////ExecuteMaterial: loading sound "..sounddesc[1]);
  664.  
  665.                 sound=Sound:Load3DSound(sounddesc[1],sounddesc[2],sounddesc[3],sounddesc[4],sounddesc[5]);
  666.                 -- add it to the table
  667.                 loadedsounds[slot]=sound;
  668.             --else
  669.             --    local sounddesc=sounds[slot];
  670.             --    System:Log("********************************ExecuteMaterial: Sound was found "..sounddesc[1]);
  671.             end
  672.  
  673.  
  674.  
  675.             if (PlaySoundObj==nil) then
  676.                 Sound:SetSoundPosition(sound,pos);
  677.                 Sound:PlaySound(sound,bSound);
  678.             else
  679.                 PlaySoundObj:PlaySound(sound,bSound);
  680.             end
  681.         end
  682.  
  683.     end
  684.  
  685.     --DECAL-------------------
  686.     if(decal ~= nil) then
  687.  
  688.         if(g_gore == "0" and decal.gore ) then return end
  689.  
  690.         local rotation=0;
  691.         local scale=decal.scale;
  692.         local lifetime=decal.lifetime;
  693.         if(decal.random_scale~=nil)then
  694.             scale=scale+((scale*0.01)*random(0,decal.random_scale));
  695.         end
  696.         if(decal.random_rotation~=nil)then
  697.             rotation=random(0,decal.random_rotation);
  698.         end
  699.         if(lifetime==nil)then
  700.             lifetime=16;
  701.         end
  702.  
  703.         Particle:CreateDecal(pos, normal, scale, lifetime, decal.texture, decal.object, rotation);
  704.     end
  705.  
  706. end
  707.  
  708.  
  709.  
  710. ----------------------------------
  711. --
  712. --    for bullets hits
  713. --  passes target and bullet direction to CreateDecal
  714. --    hit.pos, hit.normal, hit.target_material.bullet_hit, hit.play_mat_sound, hit.dir, hit.target_id, hit.ipart
  715. --function ExecuteMaterial2(pos,normal,material,bSound, dir, target_id, ipart)
  716. function ExecuteMaterial2(hit,mat_field)
  717.  
  718.     if (hit == nil) then
  719.         return;
  720.     end
  721.  
  722.     if (hit.target_material == nil) then
  723.         return;
  724.     end
  725.  
  726.     if(hit.target_material[mat_field] == nil) then
  727.     --    System.LogToConsole("material is nil");
  728.         do return end;
  729.     end
  730.     local mat=hit.target_material[mat_field]
  731.     local particles=mat.particles;
  732.     local loadedsounds=mat.loadedsounds;
  733.  
  734.     if (loadedsounds == nil) then
  735.         --System:Log("****************/////////////////--------------material , loadedsounds was nil");
  736.         mat.loadedsounds={};
  737.         loadedsounds=mat.loadedsounds;
  738.     end
  739.  
  740.     local sounds=mat.sounds;
  741.     local decal=mat.decal;
  742.  
  743.     --PARTICLES EFFECTS----------------
  744.     if(not hit.suppressParticleEffect)then
  745.         ExecuteMaterial_Particles(hit,mat_field);
  746.     end
  747.  
  748.     --PARTICLES----------------
  749.     if(particles ~= nil) then
  750.         if((particles.clippable==nil) or (particles.clippable and in_frustum))then
  751.             for i,particle in particles do
  752.                 if((g_gore == "1") or (g_gore=="2") or (not particle.gore) ) then
  753.                     Particle:CreateParticle(hit.pos,hit.normal,particle);
  754.                 end
  755.             end
  756.         end
  757.     end
  758.  
  759.     --SOUNDS------------------
  760.     if((sounds ~= nil) and (hit.play_mat_sound ~= nil)) then
  761.  
  762.         local nsounds=getn(sounds);
  763.  
  764.         if(nsounds > 0) then
  765.  
  766.             --local sound=sounds[random(1,nsounds)]
  767.  
  768.             -- grab a random position in the table
  769.             -- only do this if we are NOT on min-spec
  770.             local slot=1;
  771.             if (tonumber(getglobal("sys_spec")) ~= 0) then
  772.                 slot = random(1,nsounds);
  773.             end
  774.  
  775.             -- check if this sound was already loaded
  776.             local sound=loadedsounds[slot];
  777.             if (sound==nil) then
  778.                 -- if not, try to load it
  779.                 local sounddesc=sounds[slot];
  780.                 --System:Log("////////////////////////////ExecuteMaterial2: loading sound "..sounddesc[1]);
  781.                 sound=Sound:Load3DSound(sounddesc[1],sounddesc[2],sounddesc[3],sounddesc[4],sounddesc[5]);
  782.                 -- add it to the table
  783.                 loadedsounds[slot]=sound;
  784.             --else
  785.             --    local sounddesc=sounds[slot];
  786.             --    System:Log("********************************ExecuteMaterial: Sound was found "..sounddesc[1]);
  787.             end
  788.  
  789.             local AIInfo = sounds[slot][6];
  790.             if (AIInfo) then
  791.             AI:SoundEvent(hit.shooter.id,pos,AIInfo.fRadius,AIInfo.fThreat,AIInfo.fInterest,hit.shooter.id);
  792.             end
  793.  
  794.  
  795.             Sound:SetSoundPosition(sound,hit.pos);
  796.             Sound:PlaySound(sound,hit.play_mat_sound);
  797.         end
  798.  
  799.     end
  800.  
  801. --System:Log("decal >>> ");
  802.  
  803.     --DECAL-------------------
  804.     if(decal ~= nil) then
  805.  
  806. --System:Log("decal >>> ON");
  807.  
  808.         if(g_gore == "0" and decal.gore ) then return end
  809.  
  810.         -- no blood decals on local player in SP game
  811.         if(hit.target and hit.target==_localplayer and (not Game:IsMultiplayer())) then return end
  812.  
  813. --System:Log("decal >>> gore");
  814.  
  815.         local rotation=0;
  816.         local scale=decal.scale;
  817.         local lifetime=decal.lifetime;
  818.         if(decal.random_scale~=nil)then
  819.             scale=scale+((scale*0.01)*random(0,decal.random_scale));
  820.         end
  821.         if(decal.random_rotation~=nil)then
  822.             rotation=random(0,decal.random_rotation);
  823.         end
  824.         if(lifetime==nil)then
  825.             lifetime=16;
  826.         end
  827.  
  828.         if(hit.target_id) then
  829.  
  830. --System:Log("decal >>> ID "..hit.target_id);
  831.  
  832.             if(hit.ipart) then
  833.                 Particle:CreateDecal(hit.pos, hit.normal, scale, lifetime, decal.texture, decal.object, rotation, hit.dir, hit.target_id, hit.ipart);
  834.             else
  835.                 Particle:CreateDecal(hit.pos, hit.normal, scale, lifetime, decal.texture, decal.object, rotation, hit.dir, hit.target_id);
  836.             end
  837.         elseif(hit.targetStat) then
  838.  
  839. --System:Log("decal >>> STAT  ");
  840.             Particle:CreateDecal(hit.pos, hit.normal, scale, lifetime, decal.texture, decal.object, rotation, hit.dir, 0, 0, hit.targetStat);
  841.         else
  842. --System:Log("decal >>> TERRAIN ");
  843.             Particle:CreateDecal(hit.pos, hit.normal, scale, lifetime, decal.texture, decal.object, rotation, hit.dir);
  844.         end
  845.     end
  846.  
  847. end
  848.  
  849.  
  850.  
  851. function ExecuteMaterial_Particles(hit,mat_field)
  852.  
  853.     local mat=hit.target_material[mat_field];
  854.     local particleEffects=mat.particleEffects;
  855.  
  856.     if(particleEffects) then
  857.         if((g_gore == "1") or (g_gore == "2") or (not particleEffects.gore)) then
  858.             if (particleEffects.scale) then
  859.                 Particle:SpawnEffect(hit.pos,hit.normal, particleEffects.name,particleEffects.scale );
  860.             else
  861.                 Particle:SpawnEffect(hit.pos,hit.normal, particleEffects.name );
  862.             end
  863.         end
  864.     end
  865. end
  866.  
  867.  
  868. ----------------------------------
  869. function BroadcastEvent( sender,Event  )
  870.     -- Check if Event Target for this input event exists.
  871.     if (sender.Events) then
  872.         --System:Log( "Events found" );
  873.         local eventTargets = sender.Events[Event];
  874.         if (eventTargets) then
  875.             --System:Log( "Events Targets found" );
  876.             for i, target in eventTargets do
  877.                 local TargetId = target[1];
  878.                 local TargetEvent = target[2];
  879.                 --System:Log( "Target: "..TargetId.."/"..TargetEvent );
  880.  
  881.                 if (TargetId == 0) then
  882.                     -- If TargetId refer to global Mission table.
  883.                     if Mission then
  884.                         local func = Mission["Event_"..TargetEvent];
  885.                         if (func ~= nil) then
  886.                             func( sender )
  887.                         else
  888.                             System:Log( "Mission does not support event "..TargetEvent );
  889.                         end
  890.                     end
  891.                 else
  892.                     -- If TargetId refere to Entity.
  893.                     local entity = System:GetEntity(TargetId);
  894.                     if (entity ~= nil) then
  895.  
  896.                         --local TargetName=entity:GetName();
  897.                         --System:Log( "Entity Named "..TargetName.." Found." );
  898.                         --System:Log( "Calling method: "..TargetName..":Event_"..TargetEvent );
  899.                         local func = entity["Event_"..TargetEvent];
  900.                         if (func ~= nil) then
  901.                             func( entity,sender )
  902. --                        else
  903. --                            System:Log( "Entity "..TargetName.." does not support event "..TargetEvent );
  904.                         end
  905. --                    else
  906. --                        System:Log( "Entity Named "..TargetName.." Not Found." );
  907.                     end
  908.                 end
  909.              end
  910.          end
  911.      end
  912. end
  913.  
  914. ----------------------------------
  915. function count(_table)
  916.     local count=0;
  917.     for idx,i in _table do
  918.         count=count+1;
  919.     end
  920.     return count;
  921. end
  922.  
  923. ----------------------------------
  924. function append(_table, item)
  925.     local i=count(_table) + 1;
  926.     _table[i] = item;
  927. end
  928.  
  929. ----------------------------------
  930. function HideWeapon()
  931.     g_HideWeapons = 1;
  932. end
  933.  
  934. function ShowWeapon()
  935.     g_HideWeapons = nil;
  936. end
  937.  
  938. function DumpEntities()
  939.     local ents=System:GetEntities();
  940.     System:Log("Entities dump");
  941.     for idx,e in ents do
  942.         local pos=e:GetPos();
  943.         local ang=e:GetAngles();
  944.         System:Log("["..tostring(e.id).."]..name="..e:GetName().." clsid="..tostring(e.classid)..format(" pos=%.03f,%.03f,%.03f",pos.x,pos.y,pos.z)..format(" ang=%.03f,%.03f,%.03f",ang.x,ang.y,ang.z));
  945.     end
  946. end
  947.  
  948. --///////////////////////////////////////////////////////////////////////////////////
  949. function SetEntitiesState(state,classid)
  950.     local ents=System:GetEntities();
  951.     System:Log("Entities dump");
  952.     for idx,e in ents do
  953.         if(e.classid==classid)then
  954.             e:GotoState(state);
  955.             local pos=e:GetPos();
  956.             System:Log("["..tostring(e.id).."]..name="..e:GetName().." classid="..tostring(e.classid).." pos="..pos.x..","..pos.y..","..pos.z);
  957.         end
  958.     end
  959. end
  960.  
  961. --///////////////////////////////////////////////////////////////////////////////////
  962. function AIReload()
  963.     Script:ReloadScript("Scripts/AI/aiconfig.lua");
  964. end
  965.  
  966. --///////////////////////////////////////////////////////////////////////////////////
  967. -- useful when you want pass a parameter which might be empty to C++
  968. -- without this call you have to check for existance of the parameter and for nil
  969. -- with it you just need to check for nil
  970. function tonotnil( ValOrNil )
  971.     return ValOrNil;
  972. end
  973.  
  974.  
  975. --///////////////////////////////////////////////////////////////////////////////////
  976. -- \param parameterno 1..
  977. function GetParameterNo( textline, parameterno )
  978.     if textline then
  979.         local tokens=tokenize(textline);
  980.     
  981.         return tokens[parameterno];
  982.     end
  983. end
  984.  
  985. --///////////////////////////////////////////////////////////////////////////////////
  986. CommonCallbacks= {
  987. };
  988.  
  989. --///////////////////////////////////////////////////////////////////////////////////
  990. function CommonCallbacks:OnCollide(hit)
  991.     local mat=Game:GetMaterialBySurfaceID(hit.matId);
  992.     ExecuteMaterial(hit.vPos,hit.vNorm,mat.object_impact,1);
  993.     --System:Log("OK....");
  994. end
  995.  
  996. function GetParticleCollisionStatus(ent)
  997.     local stat=ent:GetParticleCollisionStatus()
  998.     if(stat)then
  999.         --System:Log("stat.target_material="..stat.target_material);
  1000.         stat.target_material=Game:GetMaterialBySurfaceID(stat.target_material);
  1001.  
  1002.         if(stat.target_material.type~="obstruct") then
  1003.             return stat;
  1004.         end
  1005. --System:Log("stat.target_material="..stat.target_material.type);
  1006.         return stat;
  1007.     end
  1008. end
  1009.  
  1010. -----------------------------------------------------------------------------------------
  1011. -- Benchmark functions.
  1012. -----------------------------------------------------------------------------------------
  1013. function StartBenchmark1( name )
  1014.     System:Log( "************ StartBenchmark1 ************ " );
  1015.     --profile = 0;
  1016.     setglobal("profile",0);
  1017.     --demo_num_runs=10;
  1018.     setglobal("demo_num_runs",10);
  1019.     Game:StartDemoPlay( name )
  1020. end
  1021.  
  1022. function StartBenchmark2( name )
  1023.     System:Log( "************ StartBenchmark1 ************ " );
  1024.     --profile = -1;
  1025.     setglobal("profile",-1);
  1026.     --demo_num_runs=10;
  1027.     setglobal("demo_num_runs",10);
  1028.     Game:StartDemoPlay( name )
  1029. end
  1030.  
  1031. --------------------------------------------------------------------------
  1032. ----- globals to manage the number of splash effects ---------------------
  1033. --------------------------------------------------------------------------
  1034. g_curSplashes = 0;
  1035. g_maxSplashes = 8;
  1036. g_lastSplashTime = 0;
  1037. g_SplashDuration = 2;
  1038.  
  1039. -------------------------------------------------------------------
  1040. --return if the point is inside ent:localbbox, bias means how close the point could be to the bbox to be considered inside.
  1041. --the test dont take into account a real box, but an ellipsoid based on entity rotation and localbbox size.
  1042. function PointInsideBBox(point,ent,bias)
  1043.  
  1044.     local epos = ent:GetPos();
  1045.     local deltapos = g_Vectors.temp_v1;
  1046.  
  1047.     deltapos.x = point.x-epos.x;
  1048.     deltapos.y = point.y-epos.y;
  1049.     deltapos.z = point.z-epos.z;
  1050.  
  1051.     --I dont use NormalizeVector() because I need the lenght of the vector
  1052.     local deltalen = sqrt(LengthSqVector(deltapos));
  1053.     local normalizelen = 1.0/deltalen;
  1054.  
  1055.     if  (deltalen <= 0) then normalizelen = 0.0001; end
  1056.  
  1057.     deltapos.x = deltapos.x * normalizelen;
  1058.     deltapos.y = deltapos.y * normalizelen;
  1059.     deltapos.z = deltapos.z * normalizelen;
  1060.  
  1061.     local evecfwd = g_Vectors.temp_v2;
  1062.     local evecright = g_Vectors.temp_v3;
  1063.     local evecup = g_Vectors.temp_v4;
  1064.  
  1065.     merge(evecfwd,ent:GetDirectionVector(0));
  1066.     merge(evecright,ent:GetDirectionVector(1));
  1067.     merge(evecup,ent:GetDirectionVector(2));
  1068.  
  1069.     local dot_fwd = dotproduct3d(evecfwd,deltapos);
  1070.     local dot_right = dotproduct3d(evecright,deltapos);
  1071.     local dot_up = dotproduct3d(evecup,deltapos);
  1072.  
  1073.     local bbox = ent:GetLocalBBox(nil,nil);
  1074.     local max = bbox.max;
  1075.  
  1076.     --Hud:AddMessage("dot_fwd:"..dot_fwd..",dot_right:"..dot_right..",fwd_len:"..max.y..",right_len:"..max.x);
  1077.  
  1078.     local distfrombbox = abs(dot_fwd)*max.y*bias + abs(dot_right)*max.x*bias + abs(dot_up)*max.z*bias;
  1079.  
  1080.     --Hud:AddMessage("distfrombbox:"..distfrombbox..",deltalen:"..deltalen);
  1081.  
  1082.     if (deltalen-distfrombbox < bias) then
  1083.         return 1;
  1084.     else
  1085.         return nil;
  1086.     end
  1087. end
  1088.  
  1089. -----------------------------------------------------------------------------------------
  1090. function CreateEntityLight( entity, radius, r, g, b, lifeTime, pos )
  1091.  
  1092.     local doProjectileLight = tonumber(getglobal("cl_projectile_light"));
  1093.     
  1094.     if(doProjectileLight==0) then return end
  1095.     
  1096.     local lightPos = pos;
  1097.     if(not lightPos) then
  1098.          lightPos = entity:GetPos();
  1099.     end     
  1100.  
  1101.     if (doProjectileLight == 1) then        -- no specular
  1102.         -- vPos, fRadius, DiffR, DiffG, DiffB, DiffA, SpecR, SpecG, SpecB, SpecA, fLifeTime
  1103.         entity:AddDynamicLight(lightPos, radius, r, g, b, 1, 0, 0, 0, 0, lifeTime);
  1104.     elseif (doProjectileLight == 2) then        -- with specular
  1105.         entity:AddDynamicLight(lightPos, radius, r, g, b, 1, r, g, b, 1, lifeTime);
  1106.     end
  1107. end
  1108.  
  1109.  
  1110. -----------------------------------------------------------------------------------------
  1111. function toNumberOrZero( inValue )
  1112.     local ret=tonumber(inValue);
  1113.     
  1114.     if ret then
  1115.         return ret;
  1116.     else
  1117.         return 0;
  1118.     end
  1119. end
  1120.  
  1121. -------------------------------------------------------------------------------------------
  1122. function EntitiesDistSq(ent1,ent2)
  1123.  
  1124.     if (ent1.GetPos==nil or ent2.GetPos==nil) then return 0 end
  1125.     
  1126.     local delta = g_Vectors.temp_v1;
  1127.     
  1128.     CopyVector(delta,ent1:GetPos());
  1129.     
  1130.     local epos = ent2:GetPos();
  1131.                     
  1132.     delta.x = delta.x - epos.x;
  1133.     delta.y = delta.y - epos.y;
  1134.     delta.z = delta.z - epos.z;
  1135.         
  1136.     return (LengthSqVector(delta));
  1137. end
  1138.  
  1139. -------------------------------------------------------------------
  1140. -- gloabl to indicate that a ui reload was requested commited by UI:Reload( ... ), will be reset by UI:Init()
  1141. g_reload_ui = 0;
  1142.