]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/triggers/func/train.qc
Merge branch 'master' into Lyberta/PrintMove
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / train.qc
index 015668328a3c32e0843ab78d7bc7c9181b1e7543..4e9c334562c97cc1bad305ccb2abf279a3d20fa4 100644 (file)
@@ -1,4 +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);
@@ -6,18 +8,18 @@ void train_use(entity this, entity actor, entity trigger);
 void train_wait(entity this)
 {
        SUB_UseTargets(this.enemy, NULL, NULL);
-       this.enemy = world;
+       this.enemy = NULL;
 
        // if turning is enabled, the train will turn toward the next point while waiting
        if(this.platmovetype_turn && !this.train_wait_turning)
        {
                entity targ, cp;
                vector ang;
-               targ = find(world, targetname, this.target);
-               if((this.spawnflags & 1) && targ.curvetarget)
-                       cp = find(world, targetname, targ.curvetarget);
+               targ = this.future_target;
+               if((this.spawnflags & TRAIN_CURVE) && targ.curvetarget)
+                       cp = find(NULL, targetname, targ.curvetarget);
                else
-                       cp = world;
+                       cp = NULL;
 
                if(cp) // bezier curves movement
                        ang = cp.origin - (this.origin - this.view_ofs); // use the origin of the control point of the next path_corner
@@ -27,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;
@@ -40,12 +42,12 @@ void train_wait(entity this)
 #endif
 
 #ifdef SVQC
-       entity tg = find(world, targetname, this.target);
-       if(tg.spawnflags & 4)
+       entity tg = this.future_target;
+       if(tg.spawnflags & TRAIN_NEEDACTIVATION)
        {
                this.use = train_use;
-               SUB_THINK(this, func_null);
-               this.SUB_NEXTTHINK = 0;
+               setthink(this, func_null);
+               this.nextthink = 0;
        }
        else
 #endif
@@ -56,23 +58,44 @@ 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 = world;
+       entity targ = NULL, cp = NULL;
        vector cp_org = '0 0 0';
 
-       targ = find(world, targetname, this.target);
+       targ = this.future_target;
+
        this.target = targ.target;
-       if (this.spawnflags & 1)
+       this.target_random = targ.target_random;
+       this.future_target = train_next_find(targ);
+
+       if (this.spawnflags & TRAIN_CURVE)
        {
                if(targ.curvetarget)
                {
-                       cp = find(world, targetname, targ.curvetarget); // get its second target (the control point)
+                       cp = find(NULL, targetname, targ.curvetarget); // get its second target (the control point)
                        cp_org = cp.origin - this.view_ofs; // no control point found, assume a straight line to the destination
                }
        }
@@ -134,20 +157,12 @@ float train_send(entity this, entity to, float sf)
 
                WriteString(MSG_ENTITY, this.curvetarget);
 
-               WriteCoord(MSG_ENTITY, this.pos1_x);
-               WriteCoord(MSG_ENTITY, this.pos1_y);
-               WriteCoord(MSG_ENTITY, this.pos1_z);
-               WriteCoord(MSG_ENTITY, this.pos2_x);
-               WriteCoord(MSG_ENTITY, this.pos2_y);
-               WriteCoord(MSG_ENTITY, this.pos2_z);
+               WriteVector(MSG_ENTITY, this.pos1);
+               WriteVector(MSG_ENTITY, this.pos2);
 
-               WriteCoord(MSG_ENTITY, this.size_x);
-               WriteCoord(MSG_ENTITY, this.size_y);
-               WriteCoord(MSG_ENTITY, this.size_z);
+               WriteVector(MSG_ENTITY, this.size);
 
-               WriteCoord(MSG_ENTITY, this.view_ofs_x);
-               WriteCoord(MSG_ENTITY, this.view_ofs_y);
-               WriteCoord(MSG_ENTITY, this.view_ofs_z);
+               WriteVector(MSG_ENTITY, this.view_ofs);
 
                WriteAngle(MSG_ENTITY, this.mangle_x);
                WriteAngle(MSG_ENTITY, this.mangle_y);
@@ -178,24 +193,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.use = func_null; // not again
+       this.nextthink = this.ltime + 1;
+       setthink(this, train_next);
+       this.use = func_null; // not again, next target can set it again if needed
+       if(trigger.target2 && trigger.target2 != "")
+               this.future_target = find(NULL, targetname, trigger.target2);
 }
 
 void func_train_find(entity this)
 {
-       entity targ;
-       targ = find(world, 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))
+       if(!(this.spawnflags & TRAIN_NEEDACTIVATION))
        {
-               this.SUB_NEXTTHINK = this.SUB_LTIME + 1;
-               SUB_THINK(this, train_next);
+               this.nextthink = this.ltime + 1;
+               setthink(this, train_next);
        }
 
        train_link(this);
@@ -223,10 +242,10 @@ spawnfunc(func_train)
                return;
        this.effects |= EF_LOWPRECISION;
 
-       if(this.spawnflags & 4)
+       if(this.spawnflags & TRAIN_NEEDACTIVATION)
                this.use = train_use;
 
-       if (this.spawnflags & 2)
+       if (this.spawnflags & TRAIN_TURN)
        {
                this.platmovetype_turn = true;
                this.view_ofs = '0 0 0'; // don't offset a rotating train, origin works differently now
@@ -277,20 +296,12 @@ NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
 
                this.curvetarget = strzone(ReadString());
 
-               this.pos1_x = ReadCoord();
-               this.pos1_y = ReadCoord();
-               this.pos1_z = ReadCoord();
-               this.pos2_x = ReadCoord();
-               this.pos2_y = ReadCoord();
-               this.pos2_z = ReadCoord();
+               this.pos1 = ReadVector();
+               this.pos2 = ReadVector();
 
-               this.size_x = ReadCoord();
-               this.size_y = ReadCoord();
-               this.size_z = ReadCoord();
+               this.size = ReadVector();
 
-               this.view_ofs_x = ReadCoord();
-               this.view_ofs_y = ReadCoord();
-               this.view_ofs_z = ReadCoord();
+               this.view_ofs = ReadVector();
 
                this.mangle_x = ReadAngle();
                this.mangle_y = ReadAngle();
@@ -307,9 +318,10 @@ NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
 
                this.classname = "func_train";
                this.solid = SOLID_BSP;
-               this.movetype = MOVETYPE_PUSH;
+               set_movetype(this, MOVETYPE_PUSH);
                this.drawmask = MASK_NORMAL;
                this.draw = train_draw;
+               if (isnew) IL_PUSH(g_drawables, this);
                this.entremove = trigger_remove_generic;
 
                if(set_platmovetype(this, this.platmovetype))
@@ -322,13 +334,9 @@ NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
                //func_train_find();
 
                // but we will need these
-               //this.move_nextthink = this.move_ltime + 0.1;
-               //this.move_think = train_next;
                train_next(this);
 
-               this.move_movetype = MOVETYPE_PUSH;
-               this.move_origin = this.origin;
-               this.move_angles = this.angles;
+               set_movetype(this, MOVETYPE_PUSH);
                this.move_time = time;
        }