DatAmazingCheese

Tank Movement Chip

Nov 1st, 2016
371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @name Tank Movement Chip
  2. @inputs Pod:wirelink Engine:entity
  3. @outputs EngineActive Throttle GearOut LeftTCGear RightTCGear LeftGearOut RightGearOut LeftBrakeClutch RightBrakeClutch LeftWeldBrake RightWeldBrake
  4. @persist Gear Lgear Rgear GEARTIME Speed Active DualClutch DualGearbox Rev
  5. @persist GearboxAndTransfer BrakePower TurnBrakePower TwoSpeed FourSpeed SixSpeed EightSpeed
  6. @persist Seat:entity WheelCount WheelModel:string Wheels:array TreadSound:string ShiftTime TransferCase
  7. @persist [EngineStartTime EngineStartTimeOffset]:number EngineStartSound:string
  8. @trigger
  9. @persist W A S D Space
  10.  
  11. if(first()|duped()|dupefinished())
  12. {
  13.  
  14. ### Originally created by Dakota ###
  15. ### Edited by DatAmazingCheese to make it more friendly to use and neater ###
  16.  
  17. ################################## HOW TO USE ##################################
  18. #[
  19.  
  20. To use this chip first define your setup in the variables below. Spawn your chip
  21. on the world and place it on your chassis so the arrow is facing forward, then
  22. weld it to your chassis.
  23.  
  24. For wiring,
  25.  
  26. Wire "Pod" to your pod controller, then wire "Engine" to your engine.
  27.  
  28. Wire gear from your gearbox to "GearOut" on the chip and wire throttle from your
  29. engine to "Throttle" on the chip.
  30.  
  31. If you are using a dual clutch, create two weld gates from your sprocket to your
  32. chassis. Then wire active from them to their respective side's "WeldBrake" output.
  33.  
  34. Then wire the respective side's clutch and brake to their respective "BrakeClutch"
  35. output.
  36.  
  37. For sounds you require these addons,
  38.  
  39. http://steamcommunity.com/sharedfiles/filedetails/?id=757675124
  40.  
  41. Change your tread sound to something you like.
  42.  
  43. Change your idle sound to an engine idle sound you like.
  44.  
  45. If you want sounds for reving your engine, goto "acf_wtenginestanks" in your sound
  46. browser and find "engine_load" sounds, copy paste them into the "Rev Sound Func"
  47. so that they are in order.
  48.  
  49. For an engine start sound, find one you like. Listen to it and time how long it takes for
  50. the engine to turn on and make it so it's in 1000s. For example, 2 seconds is 2000.
  51. Then listen how long the sound goes after the engine turns on and set the offset to that.
  52.  
  53. Enjoy!
  54.  
  55. ]#
  56. ############################## MOVEMENT VARIABLES ##############################
  57.  
  58. # Transmission Configuration
  59.  
  60. DualClutch = 1
  61. DualGearbox = 0
  62. GearboxAndTransfer = 0
  63.  
  64. # Gearbox Configuration
  65.  
  66. TwoSpeed = 0
  67. FourSpeed = 0
  68. SixSpeed = 1
  69. EightSpeed = 0
  70.  
  71. # Brake Forces
  72.  
  73. TurnBrakePower = 50
  74. BrakePower = 20
  75.  
  76. # Engine Reving on idle
  77.  
  78. Rev = 0
  79.  
  80. # Shifting speed
  81.  
  82. ShiftTime = 3.5
  83.  
  84. # Sounds
  85.  
  86. TreadSound = "acf_wttracks/tracks_speed_66_3.wav"
  87.  
  88. IdleSound = "acf_wtenginestanks/su 100/su_100_engine_rpm_00.wav"
  89.  
  90. EngineStartSound = "acf_wtenginestanks/su 100/su_100_engine_start.wav"
  91.  
  92. EngineStartTime = 2500
  93.  
  94. EngineStartTimeOffset = 2.5
  95.  
  96. # Wheel Stuff
  97.  
  98. WheelModel = "models/sprops/trans/miscwheels/tank30.mdl"
  99.  
  100. ### Rev Sound Func ###
  101.  
  102. function void playRevSound()
  103. {
  104. local TypeRandomNum2 = randint(4)
  105. local StringBase2 = ""
  106. if(TypeRandomNum2 == 1)
  107. {
  108. StringBase2 = "acf_wtenginestanks/su 100/su_100_engine_load_01.wav"
  109. }
  110. elseif(TypeRandomNum2 == 2)
  111. {
  112. StringBase2 = "acf_wtenginestanks/su 100/su_100_engine_load_02.wav"
  113. }
  114. elseif(TypeRandomNum2 == 3)
  115. {
  116. StringBase2 = "acf_wtenginestanks/su 100/su_100_engine_load_03.wav"
  117. }
  118. elseif(TypeRandomNum2 == 4)
  119. {
  120. StringBase2 = "acf_wtenginestanks/su 100/su_100_engine_load_04.wav"
  121. }
  122.  
  123. Engine:soundPlay(1,0,StringBase2)
  124. soundVolume(1,1)
  125. timer("revtimer",4000)
  126. }
  127.  
  128. ################################################################################
  129.  
  130. ### Declaring ###
  131.  
  132. Tank = entity():getConstraints()
  133.  
  134. Wheels = array()
  135. WheelCount = 1
  136. for(I=1,Tank:count()){if(Tank[I,entity]:model() == WheelModel){Wheels[WheelCount,entity]=Tank[I,entity],WheelCount+=1}}
  137.  
  138. ### Sounds ###
  139.  
  140. entity():isWeldedTo():soundPlay(2,0,IdleSound)
  141. soundVolume(2,0)
  142.  
  143. entity():isWeldedTo():soundPlay(3,0,TreadSound)
  144. soundVolume(3,0)
  145. soundPitch(3,0)
  146.  
  147. ### Lever Sound Func ###
  148.  
  149. function void playLeverSound()
  150. {
  151. local TypeRandomNum = randint(4)
  152. local StringBase = ""
  153. local StringEnd = ".wav"
  154. if(TypeRandomNum == 1)
  155. {
  156. StringBase = "acf_wtsfx/misc/transmission_lever_01"
  157. }
  158. elseif(TypeRandomNum == 2)
  159. {
  160. StringBase = "acf_wtsfx/misc/transmission_lever_02"
  161. }
  162. elseif(TypeRandomNum == 3)
  163. {
  164. StringBase = "acf_wtsfx/misc/transmission_lever_03"
  165. }
  166. elseif(TypeRandomNum == 4)
  167. {
  168. StringBase = "acf_wtsfx/misc/transmission_lever_04"
  169. }
  170. soundPlay(4,0,StringBase+StringEnd)
  171. soundVolume(4,0.7)
  172. }
  173.  
  174. }
  175.  
  176. interval(100)
  177.  
  178. ### Declaring ###
  179.  
  180. ChangeRPM = Engine:acfRedline()*90/100
  181.  
  182. DownChangeRPM = Engine:acfPowerbandMin()
  183.  
  184. IdleRPM = Engine:acfIdleRPM()
  185.  
  186. ### Initializing ###
  187.  
  188. Throttle=0
  189.  
  190. RightBrakeClutch=0
  191.  
  192. LeftBrakeClutch=0
  193.  
  194. ### Declaring ###
  195.  
  196. Seat = Pod["Entity",entity]
  197.  
  198. W = Pod["W",number]
  199. A = Pod["A",number]
  200. S = Pod["S",number]
  201. D = Pod["D",number]
  202. Space = Pod["Space",number]
  203. Alt = Pod["Alt",number]
  204. Active = Pod["Active",number]
  205.  
  206. ### Engine Start ###
  207.  
  208. if(EngineStartTime>0)
  209. {
  210.  
  211. if(changed(Alt) & Alt & Active & EngineActive == 0)
  212. {
  213. timer("start",EngineStartTime)
  214. entity():soundPlay(5,EngineStartTime*0.001+EngineStartTimeOffset,EngineStartSound)
  215. }
  216.  
  217. if(EngineActive == 1 & changed(Alt)&Alt){EngineActive = 0}
  218.  
  219. if(changed(Active)&!Active){EngineActive = 0}
  220.  
  221. if(clk("start"))
  222. {
  223. EngineActive = !EngineActive
  224. }
  225.  
  226. }
  227. elseif(Active)
  228. {
  229. EngineActive = 1
  230. }
  231. else
  232. {
  233. EngineActive = 0
  234. }
  235.  
  236. #if(changed(Active)){reset()}
  237. if(Active==1)
  238. {
  239. if(!W&!S&!A&!D&!Space){Gear=0,Lgear=0,Rgear=0}
  240. if(Space){Gear=0,Lgear=0,Rgear=0}
  241. if(Gear==0){GearOut=1}else{GearOut=Gear}
  242. if(Lgear==0){LeftGearOut=1}else{LeftGearOut=Lgear}
  243. if(Rgear==0){RightGearOut=1}else{RightGearOut=Rgear}
  244. if(Engine:acfRPM()<ChangeRPM/4){Gear=0,Lgear=0,Rgear=0}
  245.  
  246. if(DualClutch==1)
  247. {
  248. ### Gearing ###
  249.  
  250. GEARTIME = GEARTIME - 1
  251. if(S&!W&TwoSpeed){Gear = 2}
  252. if(!S&W&TwoSpeed){Gear = 1}
  253. if(FourSpeed){
  254. if(S&!W){Gear=4}else{
  255. if(W&!S&Gear==4){Gear=1}else{
  256. if(Engine:acfRPM()>ChangeRPM&Gear<=2&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  257. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=3&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}}}
  258. elseif(SixSpeed){
  259. if(S&!W){Gear=6}else{
  260. if(W&!S&Gear==6){Gear=1}else{
  261. if(Engine:acfRPM()>ChangeRPM&Gear<=4&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  262. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=5&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}}}
  263. elseif(EightSpeed){
  264. if(S&!W){Gear=8}else{
  265. if(W&!S&Gear==8){Gear=1}else{
  266. if(Engine:acfRPM()>ChangeRPM&Gear<=6&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  267. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=7&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}}}
  268.  
  269. ### Throttle ###
  270.  
  271. if(W|A|S|D){Throttle=100}
  272.  
  273. ### Turning ###
  274.  
  275. if(A&!Space){RightBrakeClutch = TurnBrakePower}else{RightBrakeClutch = 0}
  276. if(D&!Space){LeftBrakeClutch = TurnBrakePower}else{LeftBrakeClutch = 0}
  277. if(A&!Space){RightWeldBrake = TurnBrakePower}else{RightWeldBrake = 0}
  278. if(D&!Space){LeftWeldBrake = TurnBrakePower}else{LeftWeldBrake = 0}
  279. if(Space){RightBrakeClutch = BrakePower}
  280. if(Space){LeftBrakeClutch = BrakePower}
  281. if(!W&!S&!A&!D&!Space){RightBrakeClutch = BrakePower}
  282. if(!W&!S&!A&!D&!Space){LeftBrakeClutch = BrakePower}
  283. if(!W&!S&!A&!D&!Space&Rev){Throttle = 100}
  284.  
  285. }
  286.  
  287. ################################################################################
  288.  
  289. if(DualGearbox==1)
  290. {
  291. ### Gearing ###
  292.  
  293. GEARTIME = GEARTIME - 1
  294. if(A&TwoSpeed&!W&!S){Lgear=2,Rgear=1}
  295. elseif(D&TwoSpeed&!W&!S){Lgear=1,Rgear=2}
  296. elseif(A&FourSpeed&!W&!S){Lgear=4,Rgear=1}
  297. elseif(D&FourSpeed&!W&!S){Lgear=1,Rgear=4}
  298. elseif(A&SixSpeed&!W&!S){Lgear=6,Rgear=1}
  299. elseif(D&SixSpeed&!W&!S){Lgear=1,Rgear=6}
  300. elseif(A&EightSpeed&!W&!S){Lgear=8,Rgear=1}
  301. elseif(D&EightSpeed&!W&!S){Lgear=1,Rgear=8}
  302. if(!S&W&TwoSpeed){Lgear = 1,Rgear = 1}
  303. if(S&!W&TwoSpeed){Lgear = 2,Rgear = 2}
  304. if(FourSpeed){
  305. if(S&!W){Lgear=4,Rgear=4}else{
  306. if(W&!S&(Lgear==4|Rgear==4)){Rgear=1,Lgear=1}else{
  307. if(Engine:acfRPM()>ChangeRPM&Lgear<=2&Rgear<=2&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear+1,Lgear=Lgear+1}
  308. elseif(Engine:acfRPM()<DownChangeRPM&Lgear<=3&Rgear<=3&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear-1,Lgear=Lgear-1}}}}
  309. if(SixSpeed){
  310. if(S&!W){Lgear=6,Rgear=6}else{
  311. if(W&!S&(Lgear==6|Rgear==6)){Rgear=1,Lgear=1}else{
  312. if(Engine:acfRPM()>ChangeRPM&Lgear<=4&Rgear<=4&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear+1,Lgear=Lgear+1}
  313. elseif(Engine:acfRPM()<DownChangeRPM&Lgear<=5&Rgear<=5&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear-1,Lgear=Lgear-1}}}}
  314. if(EightSpeed){
  315. if(S&!W){Lgear=8,Rgear=8}else{
  316. if(W&!S&(Lgear==8|Rgear==8)){Rgear=1,Lgear=1}else{
  317. if(Engine:acfRPM()>ChangeRPM&Lgear<=6&Rgear<=6&!S&W&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear+1,Lgear=Lgear+1}
  318. elseif(Engine:acfRPM()<DownChangeRPM&Lgear<=7&Rgear<=7&!S&W&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Rgear=Rgear-1,Lgear=Lgear-1}}}}
  319.  
  320. ### Throttle ###
  321. if(W|A|S|D){Throttle=100}
  322.  
  323. ### Turning ###
  324.  
  325. if(D&W|D&S){RightBrakeClutch = TurnBrakePower}
  326. if(A&W|A&S){LeftBrakeClutch = TurnBrakePower}
  327. if(Space){RightBrakeClutch = BrakePower}
  328. if(Space){LeftBrakeClutch = BrakePower}
  329. if(!W&!S&!A&!D&!Space){RightBrakeClutch = BrakePower}
  330. if(!W&!S&!A&!D&!Space){LeftBrakeClutch = BrakePower}
  331. if(!W&!S&!A&!D&!Space&Rev){Throttle = 100}
  332.  
  333. }
  334.  
  335. ################################################################################
  336.  
  337. if(GearboxAndTransfer==1)
  338. {
  339. ### Gearing ###
  340.  
  341. GEARTIME = GEARTIME - 1
  342. if(S&!W){LeftTCGear=2,RightTCGear=2}else{RightTCGear=1,LeftTCGear=1}
  343. if(A&TwoSpeed&!W&!S){LeftTCGear=2,RightTCGear=1,Gear=1}
  344. elseif(D&TwoSpeed&!W&!S){LeftTCGear=1,RightTCGear=2,Gear=1}
  345. elseif(A&FourSpeed&!W&!S){LeftTCGear=2,RightTCGear=1,Gear=1}
  346. elseif(D&FourSpeed&!W&!S){LeftTCGear=1,RightTCGear=2,Gear=1}
  347. elseif(A&SixSpeed&!W&!S){LeftTCGear=2,RightTCGear=1,Gear=1}
  348. elseif(D&SixSpeed&!W&!S){LeftTCGear=1,RightTCGear=2,Gear=1}
  349. elseif(A&EightSpeed&!W&!S){LeftTCGear=2,RightTCGear=1,Gear=1}
  350. elseif(D&EightSpeed&!W&!S){LeftTCGear=1,RightTCGear=2,Gear=1}
  351. if(TwoSpeed){
  352. if(Engine:acfRPM()>ChangeRPM&Gear<=1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  353. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=2&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}
  354. if(FourSpeed){
  355. if(Engine:acfRPM()>ChangeRPM&Gear<=3&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  356. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=4&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}
  357. if(SixSpeed){
  358. if(Engine:acfRPM()>ChangeRPM&Gear<=5&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  359. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=6&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}
  360. if(EightSpeed){
  361. if(Engine:acfRPM()>ChangeRPM&Gear<=7&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear+1}
  362. elseif(Engine:acfRPM()<DownChangeRPM&Gear<=8&Gear>1&GEARTIME<=0){GEARTIME=ShiftTime,Gear=Gear-1}}
  363.  
  364. ### Throttle ###
  365.  
  366. if(W|A|S|D){Throttle=100}
  367.  
  368. ### Turning ###
  369.  
  370. if(D&W|D&S){RightBrakeClutch = TurnBrakePower}
  371. if(A&W|A&S){LeftBrakeClutch = TurnBrakePower}
  372. if(Space){RightBrakeClutch = BrakePower}
  373. if(Space){LeftBrakeClutch = BrakePower}
  374. if(!W&!S&!A&!D&!Space){RightBrakeClutch = BrakePower}
  375. if(!W&!S&!A&!D&!Space){LeftBrakeClutch = BrakePower}
  376. if(!W&!S&!A&!D&!Space&Rev){Throttle = 100}
  377.  
  378. }
  379.  
  380. }
  381. else
  382. {
  383. RightBrakeClutch = BrakePower
  384. LeftBrakeClutch = BrakePower
  385. }
  386.  
  387. ################################################################################
  388.  
  389. if(Active==1)
  390. {
  391.  
  392. ### Track Sounds ###
  393.  
  394. Vel = entity():isWeldedTo():vel()
  395.  
  396. Speed = abs(Vel:x())+abs(Vel:y())
  397.  
  398. if(Speed>30)
  399. {
  400. soundVolume(3,Active*clamp(Speed/250,0,0.5))
  401. soundPitch(3,Active*clamp(Speed/2,25,100))
  402. }
  403.  
  404. ### Dynamic Grip ###
  405.  
  406. if(Speed<=150){Climb=1}
  407. if(Speed>150){Climb=0}
  408.  
  409. if(changed(Climb)&Climb){for(I=1,Wheels:count()){if(Wheels[I,entity]){Wheels[I,entity]:propPhysicalMaterial("phx_tire_normal")}}}
  410. if(changed(Climb)&!Climb){for(I=1,Wheels:count()){if(Wheels[I,entity]){Wheels[I,entity]:propPhysicalMaterial("rubber")}}}
  411.  
  412. ################################################################################
  413.  
  414. ### Gear Sounds ###
  415.  
  416. if(changed(GearOut))
  417. {
  418. playLeverSound()
  419. }
  420.  
  421. ### Rev Sounds ###
  422.  
  423. if(EngineActive & Active)
  424. {
  425. if(changed(W)&W)
  426. {
  427. playRevSound()
  428. }
  429.  
  430. if(clk("revtimer"))
  431. {
  432. soundStop(1)
  433. }
  434. }
  435. else
  436. {
  437. soundStop(1)
  438. }
  439.  
  440. ### Idle Sounds ###
  441.  
  442. if(EngineActive & Active)
  443. {
  444. if(Engine:acfRPM() < IdleRPM*1.2)
  445. {
  446. soundVolume(2,100)
  447. }
  448. else
  449. {
  450. soundVolume(2,0)
  451. }
  452. }
  453. else
  454. {
  455. soundVolume(2,0)
  456. }
  457.  
  458. }
  459. else
  460. {
  461. soundVolume(1,0)
  462. soundVolume(2,0)
  463. soundVolume(3,0)
  464. soundVolume(4,0)
  465. soundVolume(5,0)
  466. }
  467. ################################################################################
RAW Paste Data