Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.38 KB | None | 0 0
  1. #-------------------------------------
  2. #-- Mech Project by shadowscion
  3.  
  4. @name mech_project/mech_spawn
  5. @inputs Camera:wirelink
  6. @outputs Ops Cpu
  7.  
  8. #-- holograms
  9. @persist [Holo_Body Holo_Cockpit Holo_MRLS Seat]:entity
  10.  
  11. #-- mech variables
  12. @persist [Yaw_MechEntity Yaw_Cockpit] [Pitch_MRLS] Sin Cos
  13.  
  14. #-- player inputs
  15. @persist [W A S D Shift Space Mouse1 Mouse2] [Active ActiveN Deactivate] PlayerAim:vector
  16.  
  17. #-- weapons
  18. @persist [MRLS_CanFire MRLS_Wait] [MRLS_Fire MRLS_Sequence MRLS_Tube] MRLS_Active:array MRLS_Data:table MRLS_Pitch
  19. @persist Cannons:array CannonProjectiles:array [RecoilVector]:vector
  20.  
  21. #-- triggers
  22. @trigger none
  23.  
  24. if (first()) {
  25. #include "mech_project/mech_lib"
  26. #include "mech_project/mech_holo"
  27. Seat = entity():isWeldedTo()
  28. }
  29.  
  30. if(Seat:type()!="prop_vehicle_prisoner_pod")
  31. {
  32. print("You must spawn the Chip on a seat")
  33. selfDestructAll()
  34. }
  35.  
  36. if (clk("spawn")) {
  37. local Done = spawn_holograms()
  38. if (Done) {
  39. #-- initialize hologram entities
  40. MechEntity = holoEntity(1)
  41. Holo_Body = holoEntity(2)
  42. Holo_Cockpit = holoEntity(3)
  43. Holo_MRLS = holoEntity(800)
  44.  
  45. holoUnparent(1)
  46. holoUnparent(1000)
  47. holoDisableShading(908,1)
  48.  
  49. Camera["Parent",entity] = MechEntity
  50. Camera["Distance",number] = 0
  51. Camera["Position",vector] = vec(0,0,100)
  52.  
  53. for (I = 1,8) {
  54. local N = 806 + I
  55. local MStartPos = Holo_MRLS:toLocal(holoEntity(N):pos())
  56.  
  57. MRLS_Data[I,table] = table(
  58. "index" = N,
  59. "entity" = holoEntity(N),
  60. "startPos" = MStartPos
  61. )
  62. }
  63.  
  64. #-- initialize mech variables
  65. Yaw_MechEntity = MechEntity:angles():yaw()
  66. Yaw_Cockpit = MechEntity:angles():yaw()
  67. Pitch_MRLS = Holo_Cockpit:toLocal(Holo_MRLS:angles()):pitch()
  68.  
  69. Active = 1
  70. FootLerpRate = 2
  71. FootCycleRate = 0.25
  72.  
  73. MechMoveSpeed = LegLengthBF + LegLengthBF
  74.  
  75. PlayerAim = MechEntity:pos() + MechEntity:forward()*1000
  76. MechHeight = 75
  77.  
  78. #-- initialize mech feet
  79. MechFeet:createFoot("fl",ang(0,45,0):forward()*160):setFootGait(0.00,0.25)
  80. MechFeet:createFoot("fr",ang(0,-45,0):forward()*160):setFootGait(0.50,0.75)
  81. MechFeet:createFoot("rl",ang(0,135,0):forward()*160):setFootGait(0.25,0.50)
  82. MechFeet:createFoot("rr",ang(0,-135,0):forward()*160):setFootGait(0.75,1.00)
  83.  
  84. MechFeet["fl",table]:setFootSound("soundStepTake","npc/dog/dog_pneumatic2.wav",holoEntity(100),0.50,100)
  85. MechFeet["fl",table]:setFootSound("soundStepDone","doors/drawbridge_stop1.wav",holoEntity(101),0.75,200)
  86. MechFeet["fr",table]:setFootSound("soundStepTake","npc/dog/dog_pneumatic2.wav",holoEntity(200),0.50,100)
  87. MechFeet["fr",table]:setFootSound("soundStepDone","doors/drawbridge_stop1.wav",holoEntity(201),0.75,200)
  88. MechFeet["rl",table]:setFootSound("soundStepTake","npc/dog/dog_pneumatic2.wav",holoEntity(300),0.50,100)
  89. MechFeet["rl",table]:setFootSound("soundStepDone","doors/drawbridge_stop1.wav",holoEntity(301),0.75,200)
  90. MechFeet["rr",table]:setFootSound("soundStepTake","npc/dog/dog_pneumatic2.wav",holoEntity(400),0.50,100)
  91. MechFeet["rr",table]:setFootSound("soundStepDone","doors/drawbridge_stop1.wav",holoEntity(401),0.75,200)
  92.  
  93. #-- wire instructions
  94. if (!->Seat | !->Camera) { spawn_help() }
  95.  
  96. #-- start the interval
  97. interval(100)
  98. runOnLast(1)
  99. }
  100. }
  101.  
  102. if (last()) {
  103. Seat:ejectPod()
  104. Seat:deparent()
  105.  
  106. Camera["Activated",number] = 0
  107. }
  108.  
  109. if (clk()) {
  110. dt(90,1)
  111.  
  112. #-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  113. #-- player and active timer
  114. local Player = (->Seat & ->Camera) ? Seat:driver() : noentity()
  115.  
  116. if (!Seat:parent()) {
  117. Seat:propFreeze(1)
  118. Seat:setPos(Holo_Cockpit:toWorld(vec(0,0,-15)))
  119. Seat:setAng(Holo_Cockpit:toWorld(ang(0,-90,0)))
  120.  
  121. rangerFilter(Seat)
  122. rangerPersist(1)
  123.  
  124. if (Holo_Cockpit:toWorld(vec(0,0,-15)):distance(Seat:pos()) < 1) { Seat:parentTo(Holo_Cockpit) }
  125. }
  126.  
  127. #-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  128. #-- player entier/exit seat
  129. if (changed(Player)) {
  130. foreach (K,V:entity = players()) {
  131. holoVisible(909,V,0)
  132. holoVisible(910,V,0)
  133. }
  134.  
  135. if (Player) {
  136. #-- start engine sounds
  137. holoEntity(1):soundPlay("engine1",0,"vehicles/crane/crane_startengine1.wav")
  138.  
  139. #-- reset camera
  140. Camera["Parent",entity] = holoEntity(908)
  141. Camera["Distance",number] = 0
  142. Camera["Position",vector] = vec()
  143. Camera["Activated",number] = 1
  144.  
  145. #-- filter player from all traces
  146. rangerFilter(Player)
  147. rangerPersist(1)
  148.  
  149. holoVisible(909,Player,1)
  150. holoVisible(910,Player,1)
  151.  
  152. Seat:controls_help(10)
  153. }
  154. else {
  155. #-- reset inputs
  156. W = A = S = D = Space = Shift = Mouse1 = Mouse2 = Sin = Cos = Camera["Activated",number] = 0
  157. PlayerAim = MechEntity:pos() + MechEntity:forward()*1000
  158.  
  159. Seat:soundPlay("exit",0,"npc/env_headcrabcanister/hiss.wav",3)
  160. }
  161.  
  162. Deactivate = 0
  163. }
  164.  
  165. #-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  166. #-- active states
  167. Active = clamp(Active + ((Player:isValid()|MRLS_Active:count()|CannonProjectiles:count()) ?: -1)*DT,0,1)
  168. ActiveN = 1 - Active
  169.  
  170. #-- if inactive
  171. if (Active == 0) {
  172. #-- convenient timer to deactivate expensive code when inactive
  173. Deactivate = min(1,Deactivate + DT*0.1)
  174. }
  175.  
  176. #-- if active
  177. elseif (Active == 1) {
  178. if (Player) {
  179. #-- player controls
  180. W = Player:keyForward()
  181. A = Player:keyLeft()
  182. S = Player:keyBack()
  183. D = Player:keyRight()
  184.  
  185. Space = Player:keyJump()
  186. Shift = Player:keySprint()
  187. Mouse1 = Player:keyAttack1()
  188. Mouse2 = Player:keyAttack2()
  189.  
  190. PlayerAim = rangerOffset(32768,Player:shootPos(),Seat:toLocalAxis(Player:eye())):position()
  191.  
  192. if (Player:keyReload()) { Seat:ejectPod() }
  193. }
  194. else {
  195.  
  196. }
  197.  
  198. Sin = sin(CT*150)/max(abs($Yaw_MechEntity) + MechMoveDir:length(),1)
  199. Cos = cos(CT*150)/max(abs($Yaw_MechEntity) + MechMoveDir:length(),1)
  200. }
  201.  
  202. #-- if initializing
  203. if ($Active) {
  204. #-- mix resting height with active height
  205. MechHeight = 40 + (75 - 40)*Active
  206.  
  207. #-- mix resting footpos with active footpos
  208. MechFeet["fl",table]["restPos",vector] = mix(ang(0,45,0):forward()*160,ang(0,15,0):forward()*175,Active)
  209. MechFeet["fr",table]["restPos",vector] = mix(ang(0,-45,0):forward()*160,ang(0,-15,0):forward()*175,Active)
  210. MechFeet["rl",table]["restPos",vector] = mix(ang(0,135,0):forward()*160,ang(0,165,0):forward()*175,Active)
  211. MechFeet["rr",table]["restPos",vector] = mix(ang(0,-135,0):forward()*160,ang(0,-165,0):forward()*175,Active)
  212.  
  213. #-- engine sound volume
  214. soundVolume("engine1",Active)
  215. soundVolume("engine2",Active)
  216.  
  217. if (!Player & Active <= DT) {
  218. soundStop("engine1")
  219. soundStop("engine2")
  220. }
  221.  
  222. local Open = vec(0,0,ActiveN*135)
  223. holoAng(901,Holo_Cockpit:toWorld((quat(ang(90,0,0))*qRotation(-Open)):toAngle()))
  224. holoAng(904,Holo_Cockpit:toWorld((quat(ang(90,0,0))*qRotation(Open)):toAngle()))
  225.  
  226. holoAng(603,holoEntity(602):toWorld(ang(-90 - ActiveN*60,180,0)))
  227. holoAng(703,holoEntity(702):toWorld(ang(-90 - ActiveN*60,180,0)))
  228. }
  229.  
  230. #-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  231. #-- code is running
  232. if (Deactivate != 1) {
  233. #-- cycle feet
  234. FootLerpRate = lerpN(2,2,Shift)
  235. FootCycleRate = lerpN(0.25,0.50,Shift)
  236.  
  237. MechMoveDir = (MechEntity:forward()*(W - S) + MechEntity:right()*(D - A)):normalized()*MechMoveSpeed*!MRLS_Fire
  238. MechFeet:cycleAllFeet()
  239.  
  240. #-- base hologram
  241. holoPos(1,mix(FootAveragePos + vec(0,0,Sin*5 + MechHeight - MRLS_Fire*15) + MechEntity:toWorldAxis(RecoilVector)*3,MechEntity:pos(),0.75))
  242.  
  243. Yaw_MechEntity = approachAngle(Yaw_MechEntity,(PlayerAim - MechEntity:pos()):toAngle():yaw(),FootCycleRate*45*DT*!MRLS_Fire)
  244. holoAng(1,ang(RecoilVector:x(),Yaw_MechEntity - (1 - Cannons[1,number])*15 + (1 - Cannons[2,number])*15,-RecoilVector:y()))
  245.  
  246. #-- cockpit & body holograms
  247. local DirY = MechEntity:toLocalAxis((MechFeet["fl",table]["stepPos",vector] + MechFeet["fr",table]["stepPos",vector])*0.5 - (MechFeet["rl",table]["stepPos",vector] + MechFeet["rr",table]["stepPos",vector])*0.5) #-- pitch dir
  248. local DirX = MechEntity:toLocalAxis((MechFeet["fr",table]["stepPos",vector] + MechFeet["rr",table]["stepPos",vector])*0.5 - (MechFeet["fl",table]["stepPos",vector] + MechFeet["rl",table]["stepPos",vector])*0.5) #-- roll dir
  249.  
  250. local Angle = MechEntity:toWorld(ang(atan(DirY:x(),DirY:z()) - 90 + ActiveN*15 + Cos*5,0,atan(-DirX:y(),DirX:z()) - 90))
  251. holoAng(2,slerp(quat(Angle),quat(Holo_Body),0.5):toAngle())
  252.  
  253. local CDiff = MechEntity:toLocal((PlayerAim - Holo_Cockpit:pos()):toAngle())
  254. Yaw_Cockpit = approachAngle(Yaw_Cockpit,CDiff:yaw(),FootCycleRate*90*DT)#*!MRLS_Fire)
  255. holoAng(3,MechEntity:toWorld(ang(ActiveN*30 + MRLS_Fire*15,Yaw_Cockpit,0)))
  256. holoAng(909,Holo_Cockpit:angles():setYaw(Yaw_MechEntity))
  257.  
  258. #-- leg holograms
  259. mech_project_ik(Holo_Body,MechFeet["fl",table]["stepPos",vector],LegLengthCF,LegLengthBF,100,101,102,103,104)
  260. mech_project_ik(Holo_Body,MechFeet["fr",table]["stepPos",vector],LegLengthCF,LegLengthBF,200,201,202,203,204)
  261. mech_project_ik(Holo_Body,MechFeet["rl",table]["stepPos",vector],LegLengthCR,LegLengthBR,300,301,302,303,304)
  262. mech_project_ik(Holo_Body,MechFeet["rr",table]["stepPos",vector],LegLengthCR,LegLengthBR,400,401,402,403,404)
  263.  
  264. holoEntity(101):hydraulic(114,122)
  265. holoEntity(201):hydraulic(214,222)
  266. holoEntity(301):hydraulic(314,322)
  267. holoEntity(401):hydraulic(414,422)
  268.  
  269. holoEntity(101):hydraulic(133,117)
  270. holoEntity(201):hydraulic(233,217)
  271. holoEntity(301):hydraulic(333,317)
  272. holoEntity(401):hydraulic(433,417)
  273.  
  274. #-- mrls system
  275. if (!MRLS_Sequence) {
  276. MRLS_Fire += clamp((Space ?: 0) - MRLS_Fire,-DT*3,DT*3)
  277.  
  278. MRLS_Pitch = lerpN(-90,clamp(Holo_Cockpit:toLocal((PlayerAim - Holo_MRLS:pos()):toAngle()):pitch(),-90,0),MRLS_Fire)
  279. holoAng(800,Holo_Cockpit:toWorld(ang(MRLS_Pitch,0,0)))
  280. }
  281.  
  282. else {
  283. holoAng(800,Holo_Cockpit:toWorld(ang(lerpN(-45,MRLS_Pitch,MRLS_Fire),0,0)))
  284. }
  285.  
  286. if (!MRLS_CanFire) {
  287. if (CT - MRLS_Wait > 5) {
  288. MRLS_CanFire = 1
  289.  
  290. Holo_MRLS:soundPlay("can_fire_mrls",0,"weapons/physcannon/physcannon_pickup.wav")
  291.  
  292. for (I = 1,8) {
  293. holoAlpha(MRLS_Data[I,table]["index",number],255)
  294. }
  295. }
  296. }
  297. else {
  298. if (!MRLS_Sequence) {
  299. if (MRLS_Fire == 1 & Mouse1) {
  300. MRLS_CanFire = 0
  301. MRLS_Sequence = 8
  302. }
  303. }
  304. }
  305.  
  306. if (MRLS_Sequence) {
  307. MRLS_Sequence = max(MRLS_Sequence - DT*2,0)
  308. MRLS_Tube = ceil(MRLS_Sequence)
  309.  
  310. if (MRLS_Tube > 0) {
  311. local T = MRLS_Data[MRLS_Tube,table]
  312. local V = MRLS_Tube - MRLS_Sequence
  313.  
  314. T["velocity",number] = 5000*V*V*V*DT
  315. T["randvec",vector] = randvec(-0.05,0.05):setZ(0)
  316. }
  317.  
  318. if (changed(MRLS_Tube) & MRLS_Tube > 0) {
  319. local T = MRLS_Data[MRLS_Tube,table]
  320. local N = T["index",number]
  321.  
  322. holoUnparent(N)
  323. holoEntity(N):setTrails(10,1,0.5,"trails/smoke",vec(255,200,200),100)
  324. holoEntity(N):soundPlay("fire_missile_" + MRLS_Tube,1,"weapons/rpg/rocket1.wav")
  325.  
  326. RecoilVector += MechEntity:toLocalAxis(-Holo_MRLS:forward()*5)
  327.  
  328. MRLS_Active:pushNumber(MRLS_Tube)
  329. }
  330. }
  331.  
  332. if (MRLS_Active:count()) {
  333. MRLS_Wait = CT
  334.  
  335. foreach (K,N:number = MRLS_Active) {
  336. local T = MRLS_Data[N,table]
  337. local N = T["index",number]
  338.  
  339. local P = rangerOffset(holoEntity(N):pos(),holoEntity(N):pos() + (T["randvec",vector] + holoEntity(N):up())*T["velocity",number])
  340. if (P:hit()) {
  341. local Temp = propSpawn("models/props_phx/misc/potato_launcher_explosive.mdl",P:position(),1),Temp:propBreak()
  342.  
  343. holoAlpha(N,0)
  344. holoEntity(N):removeTrails()
  345. holoPos(N,Holo_MRLS:toWorld(T["startPos",vector]))
  346. holoAng(N,Holo_MRLS:toWorld(ang(90,0,0)))
  347. holoParent(N,Holo_MRLS)
  348.  
  349. MRLS_Active:remove(K)
  350. }
  351. else {
  352. holoPos(N,P:position())
  353. }
  354. }
  355. }
  356.  
  357. #-- cannon system
  358. for (I = 1,2) {
  359. #-- if firing sequence
  360. if (Cannons[I,number]) {
  361. Cannons[I,number] = max(Cannons[I,number] - DT*2,0)
  362.  
  363. local NextAngle = Cannons[I + 2,number] + (Cannons[I + 2,number] + 120 - Cannons[I + 2,number])*(1 - Cannons[I,number])
  364. local NextQuat = quat(ang(90,0,0))*qRotation(vec(0,0,NextAngle))
  365.  
  366. holoAng(538 + I*100,holoEntity(535 + I*100):toWorld(NextQuat:toAngle()))
  367.  
  368. if (Cannons[I,number] == 0) {
  369. Cannons[I + 2,number] = NextAngle
  370. holoEntity(535 + I*100):soundPlay(I + "cannon_reload",0,"weapons/crossbow/reload1.wav")
  371. }
  372. }
  373.  
  374. #-- if inactive
  375. else {
  376. local Fire = select(I,Mouse1,Mouse2)*!MRLS_Fire
  377. if (Fire) {
  378. local CanShoot = !select(I,Cannons[2,number],Cannons[1,number]) & propCanCreate()
  379. if (CanShoot) {
  380. #-- cannon fire sound
  381. holoEntity(538 + I*100):soundPlay(I + "cannon_fire",0,"weapons/irifle/irifle_fire2.wav")
  382. soundPitch(I + "cannon_fire",65)
  383.  
  384. #-- cannon fire projectile
  385. local P = propSpawn("models/props_phx/misc/potato_launcher_explosive.mdl",holoEntity(535 + I*100):toWorld(vec(30,0,0)),holoEntity(535 + I*100):toWorld(ang(90,0,0)),0)
  386.  
  387. P:setAlpha(0),P:setMass(100),P:setTrails(10,1,0.25,"trails/smoke",vec(255,255,135),200)
  388. P:propFreeze(0)
  389. P:applyForce(P:mass()*holoEntity(535 + I*100):forward()*3000)
  390.  
  391. rangerFilter(P)
  392.  
  393. CannonProjectiles:pushEntity(P)
  394.  
  395. #-- cannon fire recoil
  396. RecoilVector += MechEntity:toLocalAxis(-(PlayerAim - holoEntity(538 + I*100):pos()):normalized()*15)
  397.  
  398. #-- cannon fire sequence
  399. Cannons[I,number] = 1
  400. }
  401. }
  402. }
  403. }
  404.  
  405. #-- recoil divide toward zero
  406. RecoilVector /= 1.5
  407.  
  408. #-- explode cannon projectiles
  409. foreach (K,V:entity = CannonProjectiles) {
  410. local R = rangerOffset(V:pos(),V:pos() + V:vel():normalized()*200)
  411. if (R:hit() | V == noentity()) {
  412. CannonProjectiles:remove(K)
  413. V:propBreak()
  414. }
  415. }
  416.  
  417. #-- left arm holograms
  418. local Diff = Holo_Cockpit:toLocalAxis(PlayerAim - holoEntity(601):pos())
  419. local Dist = min(Diff:length(),60)
  420.  
  421. local Y = atan(Diff:y(),Diff:x())
  422. local P = atan(Diff:x(),Diff:z()) - icos(Dist,30,30)
  423. local Frac = abs(Diff:normalized():y())*45 + 15 + Cannons[1,number]*25 + Cos*5
  424.  
  425. holoAng(600,Holo_Cockpit:toWorld(ang(0,Diff:normalized():y()*45 + Cannons[1,number]*25,-25)))
  426. holoAng(601,Holo_Cockpit:toWorld(ang(P + 180 + Frac + ActiveN*75,Y,180)))
  427. holoAng(602,holoEntity(601):toWorld(ang(-icos(30,30,Dist) + 180 + Frac + ActiveN*15,0,0)))
  428. holoEntity(601):hydraulic(624,633)
  429.  
  430. #-- right arm holograms
  431. local Diff = Holo_Cockpit:toLocalAxis(PlayerAim - holoEntity(701):pos())
  432. local Dist = min(Diff:length(),60)
  433.  
  434. local Y = atan(Diff:y(),Diff:x())
  435. local P = atan(Diff:x(),Diff:z()) - icos(Dist,30,30)
  436. local Frac = abs(Diff:normalized():y())*45 + 15 + Cannons[2,number]*25 + Cos*5
  437.  
  438. holoAng(700,Holo_Cockpit:toWorld(ang(0,Diff:normalized():y()*45 - Cannons[2,number]*25,25)))
  439. holoAng(701,Holo_Cockpit:toWorld(ang(P + 180 + Frac + ActiveN*75,Y,180)))
  440. holoAng(702,holoEntity(701):toWorld(ang(-icos(30,30,Dist) + 180 + Frac + ActiveN*15,0,0)))
  441. holoEntity(701):hydraulic(724,733)
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement