Advertisement
scroton

whip code

May 13th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.91 KB | None | 0 0
  1. script "whip" (int f, int spwn, int spawnharm)
  2. {  /* f = which frame, spwn = whether to spawn actors, spawnharm = whether to spawn harming actor at end of whip */
  3.   /* points for quartic bezier curve for x offsets */
  4.   int w_x[7][5] = { /*  p0,     p1,     p2,     p3,     p4 */
  5.                      { 0.0,  -82.0,   43.0,  -60.0,  -27.0 }, /* frame 0 */
  6.                      { 0.0,  -99.0, -157.0,  -42.0,  -32.0 }, /* frame 1 */
  7.                      { 0.0,  -15.0, -147.0,  -52.0,  -86.0 }, /* frame 2 */
  8.                      { 0.0,   58.0,   -7.0,  -55.0,  -94.0 }, /* frame 3 */
  9.                      { 0.0,  139.0,  144.0,   93.0,   77.0 }, /* frame 4 */
  10.                      { 0.0,  138.0,  192.0,  176.0,  187.0 }, /* frame 5 */
  11.                      { 0.0,  212.0,  212.0,  212.0,  212.0 }, /* frame 6 */
  12.                   };
  13.  
  14.   /* points for quartic bezier curve for y offsets */
  15.   int w_y[7][5] = { /*  p0,     p1,     p2,     p3,     p4 */
  16.                      { 0.0,    0.0,   48.0,    0.0,    18.0 }, /* frame 0 */
  17.                      { 0.0,    0.0,   42.0,    0.0,    14.0 }, /* frame 1 */
  18.                      { 0.0,    0.0,   36.0,    0.0,    10.0 }, /* frame 2 */
  19.                      { 0.0,    0.0,   30.0,    0.0,    6.0 }, /* frame 3 */
  20.                      { 0.0,    0.0,   24.0,    0.0,    2.0 }, /* frame 4 */
  21.                      { 0.0,    0.0,   18.0,    0.0,    -2.0 }, /* frame 5 */
  22.                      { 0.0,    0.0,   12.0,    0.0,    -6.0 }, /* frame 6 */
  23.                   };
  24.  
  25.   /* points for quartic bezier curve for z offsets */
  26.   int w_z[7][5] = { /*  p0,     p1,     p2,     p3,     p4 */
  27.                      { 0.0,   -9.0,  -77.0,  -83.0, -121.0 }, /* frame 0 */
  28.                      { 0.0,   91.0,  -58.0,  -12.0,  -61.0 }, /* frame 1 */
  29.                      { 0.0,  110.0,   73.0,    0.0,   29.0 }, /* frame 2 */
  30.                      { 0.0,   57.0,  161.0,   49.0,   70.0 }, /* frame 3 */
  31.                      { 0.0,   20.0,   94.0,   72.0,   96.0 }, /* frame 4 */
  32.                      { 0.0,  -10.0,   27.0,   51.0,   65.0 }, /* frame 5 */
  33.                      { 0.0,    6.0,    6.0,    6.0,    6.0 }, /* frame 6 */
  34.                   };
  35.  
  36.   int w_s[7] = { 0, 0, 0, 0, 0, 0, 0 }; /* scaling for each frame out of 100.0, 0 gets unused */
  37.  
  38.   int s_x, s_y, s_z, s_ang, s_pit, i_amt, f_amt, i, s_tid, stid_c, o_tid, a_tid;
  39.   int s_xoff, s_yoff, s_zoff, xoff, yoff, zoff, off_ang, off_c;
  40.   int b1_xoff, b1_yoff, b1_zoff, b2_xoff, b2_yoff, b2_zoff, b4_xoff, b4_yoff, b4_zoff;
  41.   int t, t_2, t_3, t_4, tm, tm_2, tm_3, tm_4;
  42.   int spawn_x, spawn_y, spawn_z;
  43.   int scale, scale_d, p_num;
  44.   int xp[5], yp[5], zp[5];
  45.  
  46.   s_xoff = 6.0; /*x offset to entire curve */
  47.   s_yoff = 6.0; /*y offset to entire curve */
  48.   s_zoff = 32.0; /*z offset to entire curve */
  49.  
  50.   i_amt = 30; /* how many actors to spawn over the range */
  51.   f_amt = i_amt << 16;
  52.  
  53.   scale_d = 30.0; /* default applied scaling out of 100; 0 is not applied */
  54.  
  55.   /* get shooter player number */
  56.   p_num = PlayerNumber();
  57.  
  58.   /* get shooter tid */
  59.   s_tid = ActivatorTID();
  60.  
  61.   /* check if shooter tid is 0 (it doesn't have one) or there are more actors than the shooter with the tid */
  62.   if(!s_tid || ThingCount(T_NONE,s_tid) > 1){
  63.     o_tid = s_tid; /* save the old tid */
  64.     s_tid = UniqueTID(); /* get a new one */
  65.     Thing_ChangeTID(0, s_tid); /* change the activator's tid to the new one */
  66.     stid_c = 1; } /* flag that we changed it */
  67.  
  68.   /* IF the player is crouching, halve the static z offset */
  69.   if(GetActorViewHeight(s_tid) < GetActorProperty(0, APROP_ViewHeight)){ s_zoff = s_zoff / 2; }
  70.  
  71.   if(w_s[f]){ scale = w_s[f]; } /* apply frame specific scaling, if specified */
  72.   else{ scale = scale_d; } /* if not, then apply default scaling */
  73.  
  74.   s_x = GetActorX(0); /* Get x position of player */
  75.   s_y = GetActorY(0); /* and y position */
  76.   s_z = GetActorZ(0); /* and z position */
  77.   s_ang = GetActorAngle(0); /* and angle */
  78.   s_pit = GetActorPitch(0); /* and pitch */
  79.  
  80.   /* grab the values for the points in the quartic bezier curves from the arrays */
  81.   for(i = 0; i < 5; ++i){
  82.     xp[i] = w_x[f][i];
  83.     yp[i] = w_y[f][i];
  84.     zp[i] = w_z[f][i]; }
  85.  
  86.   if(scale){ /* apply scaling if specified */
  87.     for(i = 0; i < 5; ++i){
  88.       xp[i] = FixedDiv(FixedMul(xp[i], scale), 100.0);
  89.       yp[i] = FixedDiv(FixedMul(yp[i], scale), 100.0);
  90.       zp[i] = FixedDiv(FixedMul(zp[i], scale), 100.0); }}
  91.  
  92.   /* now apply player pitch to static offsets */
  93.   off_ang = VectorAngle(s_xoff, s_zoff) + s_pit;
  94.   off_c = VectorLength(s_xoff, s_zoff);
  95.   s_xoff = FixedMul(cos(off_ang), off_c);
  96.   s_zoff = FixedMul(sin(off_ang), off_c);
  97.  
  98.   /* apply static offsets */
  99.   for(i = 0; i < 5; ++i){
  100.     xp[i] += s_xoff;
  101.     yp[i] += s_yoff;
  102.     zp[i] += s_zoff; }
  103.  
  104.   /* the calculation / spawning / user_var setting loop */
  105.   for(i = 0; i < i_amt; ++i){
  106.     /* calculation of bezier terms for the linear and quartic bezier curves below */
  107.     t = FixedDiv((i << 16), f_amt); /* t ranges from 0 to 1 */
  108.     t_2 = FixedMul(t, t); /* t^2 */
  109.     t_3 = FixedMul(t_2, t); /* t^3 */
  110.     t_4 = FixedMul(t_3, t); /* t^4 */
  111.     tm = (1.0 - t); /* (1 - t) */
  112.     tm_2 = FixedMul(tm, tm); /* (1 - t)^2 */
  113.     tm_3 = FixedMul(tm_2, tm); /* (1 - t)^3 */
  114.     tm_4 = FixedMul(tm_3, tm); /* (1 - t)^4 */
  115.    
  116.     /* linear bezier curves */
  117.     b1_xoff = ( FixedMul(tm, xp[0]) + FixedMul(t, xp[4]) );
  118.     b1_yoff = ( FixedMul(tm, yp[0]) + FixedMul(t, yp[4]) );
  119.     b1_zoff = ( FixedMul(tm, zp[0]) + FixedMul(t, zp[4]) );
  120.  
  121.     /* quadratic bezier curves */
  122.     b2_xoff = ( FixedMul(tm_2, xp[0]) + 2 * FixedMul(FixedMul(tm, t), xp[2]) + FixedMul(t_2, xp[4]) );
  123.     b2_yoff = ( FixedMul(tm_2, yp[0]) + 2 * FixedMul(FixedMul(tm, t), yp[2]) + FixedMul(t_2, yp[4]) );
  124.     b2_zoff = ( FixedMul(tm_2, zp[0]) + 2 * FixedMul(FixedMul(tm, t), zp[2]) + FixedMul(t_2, zp[4]) );
  125.  
  126.     /* quartic bezier curvess */
  127.     b4_xoff = f_m(tm_4, xp[0]) + 4 * f_m(f_m(t, tm_3), xp[1]) + 6 * f_m(f_m(t_2, tm_2), xp[2]) + 4 * f_m(f_m(t_3, tm), xp[3]) + f_m(t_4, xp[4]);
  128.     b4_yoff = f_m(tm_4, yp[0]) + 4 * f_m(f_m(t, tm_3), yp[1]) + 6 * f_m(f_m(t_2, tm_2), yp[2]) + 4 * f_m(f_m(t_3, tm), yp[3]) + f_m(t_4, yp[4]);
  129.     b4_zoff = f_m(tm_4, zp[0]) + 4 * f_m(f_m(t, tm_3), zp[1]) + 6 * f_m(f_m(t_2, tm_2), zp[2]) + 4 * f_m(f_m(t_3, tm), zp[3]) + f_m(t_4, zp[4]);
  130.     /* reminder thet f_m() is FixedMul() and f_d() is FixedDiv() */
  131.    
  132.     if(f == 6){ /* last frame uses linear curves for x an z offsets */
  133.       xoff = b1_xoff;
  134.       yoff = b2_yoff;
  135.       zoff = b1_zoff; }
  136.     else{
  137.       xoff = b4_xoff;
  138.       yoff = b2_yoff;
  139.       zoff = b4_zoff; }
  140.    
  141.     /* now apply player pitch to calculated offsets */
  142.     off_ang = VectorAngle(xoff, zoff) - s_pit;
  143.     off_c = VectorLength(xoff, zoff);
  144.     xoff = FixedMul(cos(off_ang), off_c);
  145.     zoff = FixedMul(sin(off_ang), off_c);
  146.    
  147.     /* calculate the spawning position for anything the script should spawn... */
  148.     if(spwn || /* IF the script should spawn actors instead of just changing their user_vars OR */
  149.       (spawnharm && /* if the script should spawn harming actor at the and of the whip AND */
  150.       (i == (i_amt - 1)))){ /* it's the last iteration of the spawning/calculating loop */
  151.       /* apply offsets, note that this isn't strictly necessary for appearance since they will
  152.          appear where they should because of A_Warp, but it's good to be thorough */   
  153.      
  154.       /* now apply the x offset, if it isn't 0 */
  155.       if(xoff > 0){
  156.         spawn_x = s_x + FixedMul(cos(s_ang), xoff);
  157.         spawn_y = s_y + FixedMul(sin(s_ang), xoff); }
  158.       else if(xoff < 0){
  159.         spawn_x = s_x - FixedMul(cos(s_ang), xoff);
  160.         spawn_y = s_y - FixedMul(sin(s_ang), xoff); }
  161.  
  162.       /* and the y offset, if it isn't 0 */
  163.       if(yoff > 0){
  164.         spawn_x = s_x + FixedMul(cos(FixedAngMod(s_ang - 0.25)), yoff);
  165.         spawn_y = s_y + FixedMul(sin(FixedAngMod(s_ang - 0.25)), yoff); }
  166.       else if(yoff < 0){
  167.         spawn_x = s_x + FixedMul(cos(FixedAngMod(s_ang + 0.25)), yoff);
  168.         spawn_y = s_y + FixedMul(sin(FixedAngMod(s_ang + 0.25)), yoff); }
  169.  
  170.       /* and the z offset */
  171.       spawn_z = s_z + zoff; }
  172.  
  173.     if(spwn){ /* if the script should spawn actors instead of just changing their user_vars */
  174.  
  175.       /* get a new tid to give to the actor */
  176.       a_tid = UniqueTID();
  177.      
  178.       /* then store its in the array */
  179.       arr_tid[p_num][i] = a_tid;
  180.  
  181.       /* and now spawn the actor */
  182.       SpawnForced("TestActor", spawn_x, spawn_y, spawn_z, a_tid, s_ang);
  183.  
  184.       /* set the shooter to be it's target so that A_Warp functions correctly */
  185.       SetActivator(a_tid); /* first you have to set it to the activator to use SetPointer */
  186.       SetPointer(AAPTR_TARGET, s_tid); } /* then change its target to the shooter */
  187.  
  188.     /* change its user vars to the offsets calculated above */
  189.     SetUserVariable(arr_tid[p_num][i],"user_xoff", xoff);
  190.     SetUserVariable(arr_tid[p_num][i],"user_yoff", yoff);
  191.     SetUserVariable(arr_tid[p_num][i],"user_zoff", zoff); }
  192.    
  193.   /* now, after the loop */
  194.  
  195.   if(spawnharm){ /* if the script should spawn harming actor at the and of the whip */
  196.      
  197.     /* get a new tid to give to the actor */
  198.     a_tid = UniqueTID();
  199.      
  200.     /* spawn the harming actor */ /* NOTE: this is not currently in, and just spawns a regular actor */
  201.     SpawnForced("TestActor", spawn_x, spawn_y, spawn_z, a_tid, s_ang);
  202.     /* the spawn variables are still set to ones used in the last iteration of the above loop */
  203.    
  204.     /* set the shooter to be it's target so that it behaves like a normal projectile */
  205.     SetActivator(a_tid); /* first you have to set it to the activator to use SetPointer */
  206.     SetPointer(AAPTR_TARGET, s_tid); } /* then change its target to the shooter */
  207.  
  208.   /* restore the original script activator */
  209.   SetActivator(s_tid); /* not really necessary in this script, but potentially important in functions called by scripts */
  210.  
  211.   /* if we changed the activator's tid above */
  212.   if(stid_c){ Thing_ChangeTID(s_tid, o_tid); } /* change it back to what it was previously */
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement