function wait_time(t)
   local t0=get_time()
   coroutine.yield()
   while ((get_time()-t0)<t) do
      coroutine.yield()
   end
end

function wait_text(t)
   while (is_text_active(t)) do
      coroutine.yield()
   end
end

function add_vec3(v1,v2)
   local v = { }
   v[1]=v1[1]+v2[1]
   v[2]=v1[2]+v2[2]
   v[3]=v1[3]+v2[3]
   
   return v
end

function sub_vec3(v1,v2)
   local v = { }
   v[1]=v1[1]-v2[1]
   v[2]=v1[2]-v2[2]
   v[3]=v1[3]-v2[3]

   return v
end

function scale_vec3(v1,s)
   local v = { }
   v[1]=v1[1]*s
   v[2]=v1[2]*s
   v[3]=v1[3]*s

   return v
end

function neg_vec3(v1)
   local v = { }
   v[1]=-v1[1]
   v[2]=-v1[2]
   v[3]=-v1[3]

   return v
end

function vec3_sqr_dist(v1,v2)
   local d1=v1[1]-v2[1]
   local d2=v1[2]-v2[2]
   local d3=v1[3]-v2[3]

   return (d1*d1)+(d2*d2)+(d3*d3)
end

function add_vec2(v1,v2)
   local v = { }
   v[1]=v1[1]+v2[1]
   v[2]=v1[2]+v2[2]

   return v
end

function neg_vec2(v1)
   local v = { }
   v[1]=-v1[1]
   v[2]=-v1[2]

   return v
end

local gateway_id=1

function create_gateways(pts)
   if (#pts==2) then
      local p1=get_trigger_point_world_pos(pts[1])
      local p2=get_trigger_point_world_pos(pts[2])

      local gate1=instance_game_sprite("gate"..gateway_id,"gate",p1)
      set_hostile(gate1,false)
      set_shadow(gate1,false)
      bind_script_to_game_sprite(gate1,"scripts/portal.lua")

      gateway_id=gateway_id+1

      local gate2=instance_game_sprite("gate"..gateway_id,"gate",p2)
      set_hostile(gate2,false)
      set_shadow(gate2,false)
      bind_script_to_game_sprite(gate2,"scripts/portal.lua")

      gateway_id=gateway_id+1

      -- Send data
      send_event_to_sprite(gate1,"set_target_gate",{ gate2 })
      send_event_to_sprite(gate2,"set_target_gate",{ gate1 })
   end
end
