]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/triggers/func/train.qc
Merge branch 'amade/small-fixes' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / train.qc
index dd9eaac860a5efe24dc0f7d09b71569b356f5fc2..c5a6e3d0393edebcbfd249296f6ecd8d78eb3dd4 100644 (file)
@@ -1,5 +1,6 @@
 #include "train.qh"
 .float train_wait_turning;
+.entity future_target;
 void train_next(entity this);
 #ifdef SVQC
 void train_use(entity this, entity actor, entity trigger);
@@ -14,7 +15,7 @@ void train_wait(entity this)
        {
                entity targ, cp;
                vector ang;
-               targ = find(NULL, targetname, this.target);
+               targ = this.future_target;
                if((this.spawnflags & 1) && targ.curvetarget)
                        cp = find(NULL, targetname, targ.curvetarget);
                else
@@ -28,7 +29,7 @@ void train_wait(entity this)
                ang_x = -ang_x; // flip up / down orientation
 
                if(this.wait > 0) // slow turning
-                       SUB_CalcAngleMove(this, ang, TSPEED_TIME, this.SUB_LTIME - time + this.wait, train_wait);
+                       SUB_CalcAngleMove(this, ang, TSPEED_TIME, this.ltime - time + this.wait, train_wait);
                else // instant turning
                        SUB_CalcAngleMove(this, ang, TSPEED_TIME, 0.0000001, train_wait);
                this.train_wait_turning = true;
@@ -41,12 +42,12 @@ void train_wait(entity this)
 #endif
 
 #ifdef SVQC
-       entity tg = find(NULL, targetname, this.target);
+       entity tg = this.future_target;
        if(tg.spawnflags & 4)
        {
                this.use = train_use;
-               SUB_THINK(this, func_null);
-               this.SUB_NEXTTHINK = 0;
+               setthink(this, func_null);
+               this.nextthink = 0;
        }
        else
 #endif
@@ -57,18 +58,39 @@ void train_wait(entity this)
        }
        else
        {
-               SUB_THINK(this, train_next);
-               this.SUB_NEXTTHINK = this.SUB_LTIME + this.wait;
+               setthink(this, train_next);
+               this.nextthink = this.ltime + this.wait;
+       }
+}
+
+entity train_next_find(entity this)
+{
+       if(this.target_random)
+       {
+               RandomSelection_Init();
+               for(entity t = NULL; (t = find(t, targetname, this.target));)
+               {
+                       RandomSelection_AddEnt(t, 1, 0);
+               }
+               return RandomSelection_chosen_ent;
+       }
+       else
+       {
+               return find(NULL, targetname, this.target);
        }
 }
 
 void train_next(entity this)
 {
-       entity targ, cp = NULL;
+       entity targ = NULL, cp = NULL;
        vector cp_org = '0 0 0';
 
-       targ = find(NULL, targetname, this.target);
+       targ = this.future_target;
+
        this.target = targ.target;
+       this.target_random = targ.target_random;
+       this.future_target = train_next_find(targ);
+
        if (this.spawnflags & 1)
        {
                if(targ.curvetarget)
@@ -179,24 +201,28 @@ void train_link(entity this)
 
 void train_use(entity this, entity actor, entity trigger)
 {
-       this.SUB_NEXTTHINK = this.SUB_LTIME + 1;
-       SUB_THINK(this, train_next);
+       this.nextthink = this.ltime + 1;
+       setthink(this, train_next);
        this.use = func_null; // not again
+       if(trigger.target2 && trigger.target2 != "")
+               this.future_target = find(NULL, targetname, trigger.target2);
 }
 
 void func_train_find(entity this)
 {
-       entity targ;
-       targ = find(NULL, targetname, this.target);
+       entity targ = train_next_find(this);
        this.target = targ.target;
+       this.target_random = targ.target_random;
+       // save the future target for later
+       this.future_target = train_next_find(targ);
        if (this.target == "")
                objerror(this, "func_train_find: no next target");
-       SUB_SETORIGIN(this, targ.origin - this.view_ofs);
+       setorigin(this, targ.origin - this.view_ofs);
 
        if(!(this.spawnflags & 4))
        {
-               this.SUB_NEXTTHINK = this.SUB_LTIME + 1;
-               SUB_THINK(this, train_next);
+               this.nextthink = this.ltime + 1;
+               setthink(this, train_next);
        }
 
        train_link(this);