Advertisement
MjnMixael

Untitled

Apr 29th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.39 KB | None | 0 0
  1. #Conditional Hooks
  2.  
  3. $Application: FS2_Open
  4. $On Gameplay Start: [
  5. --table for delayed loading after ship creation
  6. active_mainloader = {}
  7. ]
  8.  
  9. $Application: FS2_Open
  10. $On Game Init:
  11. [
  12. --[[
  13. ship save/load script by Admiral MS
  14. script uses "," "&" ":" and "§" for data seperation. weapon and subsystem data should not contain any of these or the script failes
  15. see readme for instructions how to use this script
  16. ]]
  17. shipsaveload_version = "b"
  18. --definitions for savefile
  19. path_shipsave = "data/scripts/"
  20. --stuff for status loader
  21. shipclass_statusloader = "Shipsave"
  22. position_statusloader = ba.createVector(-2e5,-1e5,-1e5)
  23.  
  24. --functions
  25. function deleteshipsavefile() --deletes savefile, is called in fred
  26. if (mn.SEXPVariables['filename']:isValid()) then
  27. filename = mn.SEXPVariables['filename'].Value
  28. else
  29. filename = mn.getMissionFilename()
  30. end
  31. if (cf.fileExists(filename,path_shipsave)) then
  32. cf.deleteFile(filename,path_shipsave)
  33. end
  34. end
  35.  
  36. function saveexist() --checks if there is a savefile, is called in fred
  37. if (mn.SEXPVariables['saveexist']:isValid()) then
  38. if (mn.SEXPVariables['filename']:isValid()) then
  39. filename = mn.SEXPVariables['filename'].Value
  40. else
  41. filename = mn.getMissionFilename()
  42. end
  43. local var = mn.SEXPVariables['saveexist']
  44. if (cf.fileExists(filename,path_shipsave) and tostring(var.Type) == "SEXPVAR_TYPE_NUMBER") then
  45. var.Value = 1
  46. else
  47. var.Value = 0
  48. end
  49. end
  50. end
  51.  
  52. function deleteship(shipname) --deletes a ship entry in the savefile, is called in fred
  53. local data = {}
  54. data = loadshipfile()
  55. local pos = 0
  56. if (shipname == "var" and mn.SEXPVariables['loadship']:isValid()) then
  57. shipname = mn.SEXPVariables['loadship'].Value
  58. end
  59. if (data[1][1] ~= -1) then
  60. pos = findship(shipname,data[1])
  61. end
  62. if (pos > 0) then -- removing entry from table
  63. table.remove(data[1],pos)
  64. table.remove(data,pos+1)
  65. local length = #data[1]
  66. saveshipfile(data,length)
  67. else
  68. ba.print("saveload - del: ship '"..name.."' not found in savefile")
  69. end
  70. end
  71.  
  72. function svsp(shipname)
  73. saveship(shipname)
  74. end
  75.  
  76. function saveship(shipname) --main function for saving shipdata, is called in fred
  77. local data = {}
  78. data = loadshipfile()
  79. local pos = 0
  80. if (shipname == "var" and mn.SEXPVariables['loadship']:isValid()) then
  81. shipname = mn.SEXPVariables['loadship'].Value
  82. end
  83. if (data[1][1] ~= -1) then
  84. pos = findship(shipname,data[1])
  85. else
  86. pos = -2
  87. end
  88. local length = 1
  89. if (pos == -2) then
  90. pos = 1
  91. elseif (pos == -1) then
  92. pos = #data[1]+1
  93. length = pos
  94. else
  95. length = #data[1]
  96. end
  97. data[1][pos] = shipname
  98. data[pos+1] = shipdatacollect(shipname)
  99. saveshipfile(data,length)
  100. end
  101.  
  102. function svar(name) --save a sexp-variable
  103. --t=="n" (number) or t=="s" (string)
  104. local data = {}
  105. local t = "s" --SEXPVAR_TYPE_STRING
  106. data = loadshipfile()
  107. local tempb = data[0]
  108. local temp = {}
  109. local temparr = {}
  110. local found = 0
  111. if mn.SEXPVariables[name]:isValid() then
  112. local var = mn.SEXPVariables[name]
  113. local vart = tostring(var.Type)
  114. local val = var.Value
  115. if vart == "SEXPVAR_TYPE_NUMBER" then
  116. t = "n"
  117. val = tonumber(val)
  118. if not val then
  119. ba.print("saveload - svar: data type disagreement in var '"..name.."'")
  120. return
  121. end
  122. else
  123. val = tostring(val)
  124. end
  125. tempb = Split(tempb, "§")
  126. if string.find(tempb[2], ",") then
  127. ba.print(tempb[2])
  128. temp = Split(tempb[2], "&")
  129. found = #temp + 1
  130. for i=1,#temp do
  131. temparr[i] = Split(temp[i], ",")
  132. if temparr[i][1] == name then
  133. found = i
  134. end
  135. end
  136. else
  137. found = 1
  138. end
  139. temparr[found] = {}
  140.  
  141. temparr[found][1] = name
  142. temparr[found][2] = t
  143. temparr[found][3] = tostring(val)
  144. temp[found] = table.concat(temparr[found],",")
  145.  
  146. data[0] = table.concat(temp,"&")
  147.  
  148. saveshipfile(data,#data[1])
  149. else
  150. ba.print("saveload - svar: target variable '"..name.."' not found")
  151. end
  152. end
  153.  
  154. function ldsp(shipname,typ,stat,targetship)
  155. loadship(shipname,typ,stat,targetship)
  156. end
  157.  
  158. function loadship(shipname,typ,stat,targetship) --main function to load shipdata, is called in fred
  159. local data = {}
  160. data = loadshipfile()
  161. local pos = 0
  162. local shipnameactive = shipname
  163. if (shipname == "var" and mn.SEXPVariables['loadship']:isValid()) then
  164. shipnameactive = mn.SEXPVariables['loadship'].Value
  165. end
  166. if (data[1][1] ~= -1) then
  167. pos = findship(shipnameactive,data[1])
  168. end
  169. if (targetship == "var" and mn.SEXPVariables['loadship2']:isValid()) then
  170. targetship = mn.SEXPVariables['loadship2'].Value
  171. end
  172. if (type(targetship) == "string") then
  173. shipnameactive = targetship
  174. end
  175. if (pos > 0) then
  176. shipdatainsert(data[pos+1],shipnameactive,typ,stat,shipname)
  177. end
  178. end
  179.  
  180. function lvar(name,newname)
  181. if not newname then
  182. newname = name
  183. end
  184. local data = {}
  185. local t = "s" --SEXPVAR_TYPE_STRING
  186. data = loadshipfile()
  187. local tempb = data[0]
  188. local temp = {}
  189. local temparr = {}
  190. local found = 0
  191. if mn.SEXPVariables[newname]:isValid() then
  192. local var = mn.SEXPVariables[newname]
  193. local vart = tostring(var.Type)
  194. if vart == "SEXPVAR_TYPE_NUMBER" then
  195. t = "n"
  196. end
  197. tempb = Split(tempb, "§")
  198. if string.find(tempb[2], ",") then
  199. temp = Split(tempb[2], "&")
  200. for i=1,#temp do
  201. temparr[i] = Split(temp[i], ",")
  202. if tostring(temparr[i][1]) == name and temparr[i][2] == t then
  203. found = i
  204. if t =="n" then
  205. temparr[i][3] = tonumber(temparr[i][3])
  206. end
  207. end
  208. end
  209. end
  210.  
  211. if found > 0 then
  212. var.Value = temparr[found][3]
  213. else
  214. ba.print("saveload - lvar: no variable '"..name.."' found or target variable of wrong type")
  215. end
  216. else
  217. ba.print("saveload - lvar: target variable '"..newname.."' not found")
  218. end
  219.  
  220. end
  221.  
  222. function cpsp(shipname,stat,targetname) --copies the data of one ship to another ship without saving, uses all data. Orders will have to wait until after the code freeze
  223. if not shipname or not stat or not targetname then
  224. ba.print("saveload - cpsp: missing arguments")
  225. return
  226. end
  227. if (shipname == "var" and mn.SEXPVariables['loadship']:isValid()) then
  228. shipname = mn.SEXPVariables['loadship'].Value
  229. end
  230. if (targetname == "var" and mn.SEXPVariables['loadship2']:isValid()) then
  231. targetname = mn.SEXPVariables['loadship2'].Value
  232. end
  233. local playertarget = false
  234. if hv.Player.Target and hv.Player.Target:isValid() and hv.Player.Target.Name == shipname then
  235. playertarget = true
  236. end
  237. local shipstring = shipdatacollect(shipname)
  238. shipdatainsert(shipstring,targetname,3,stat,targetname,1)
  239. local ship = mn.Ships[shipname]
  240. local target = mn.Ships[targetname]
  241. if playertarget then
  242. hv.Player.Target = target
  243. end
  244. if ship:isValid() and target:isValid() then
  245. target.Physics = ship.Physics
  246. end
  247. end
  248.  
  249. function saveshipfile(data,length) --stores data into the savefile
  250. if (mn.SEXPVariables['filename']:isValid()) then
  251. filename = mn.SEXPVariables['filename'].Value
  252. else
  253. filename = mn.getMissionFilename()
  254. end
  255. local file = cf.openFile(filename,"w",path_shipsave)
  256. file:write("§"..data[0].."\n")
  257. if data[1][1] == -1 then
  258. file:write("\n")
  259. else
  260. local initblock = table.concat(data[1],",",1,length)
  261. file:write(initblock.."\n")
  262. for i=1,length do
  263. file:write(data[i+1].."\n")
  264. end
  265. end
  266. file:close()
  267. end
  268.  
  269. function loadshipfile() --loads savefile and splits the shiplist
  270. if (mn.SEXPVariables['filename']:isValid()) then
  271. filename = mn.SEXPVariables['filename'].Value
  272. else
  273. filename = mn.getMissionFilename()
  274. end
  275. local data = {}
  276. data[0] = "§"
  277. data[1] = {}
  278. if (cf.fileExists(filename,path_shipsave)) then
  279. local file = cf.openFile(filename,"r",path_shipsave)
  280. local temp = file:read("*l")
  281. if (temp == "") then
  282. data[1][1] = -1
  283. elseif string.find(temp, "§") == 1 then
  284. data[0] = temp
  285. temp = file:read("*l")
  286. if (temp == "") then
  287. data[1][1] = -1
  288. else
  289. data[1] = Split(temp,",")
  290. local n = #data[1]
  291. for i=1,n do
  292. data[i+1] = file:read("*l")
  293. end
  294. end
  295. else
  296. data[1] = Split(temp,",")
  297. local n = #data[1]
  298. for i=1,n do
  299. data[i+1] = file:read("*l")
  300. end
  301. end
  302. file:close()
  303. else
  304. --ba.print("saveload - l: file '"..filename.."' not found")
  305. data[1][1] = -1
  306. end
  307. return data
  308. end
  309.  
  310. function findship(shipname,data) --finds the specified ship in the shiplist
  311. local n = #data
  312. local pos = -1
  313. for i=1,n do
  314. if (data[i] == shipname) then
  315. pos = i
  316. break
  317. end
  318. end
  319. return pos
  320. end
  321.  
  322. function Split(str, sep, maxNb) --splits strings
  323. -- Eliminate bad cases...
  324. if string.find(str, sep) == nil then
  325. return { str }
  326. end
  327. if maxNb == nil or maxNb < 1 then
  328. maxNb = 0 -- No limit
  329. end
  330. local result = {}
  331. local pat = "(.-)" .. sep .. "()"
  332. local nb = 0
  333. local lastPos
  334. for part, pos in string.gmatch(str, pat) do
  335. nb = nb + 1
  336. result[nb] = part
  337. lastPos = pos
  338. if nb == maxNb then break end
  339. end
  340. -- Handle the last field
  341. if nb ~= maxNb then
  342. result[nb + 1] = string.sub(str, lastPos)
  343. end
  344. return result
  345. end
  346.  
  347. function shipdatacollect(shipname) --shipdata collector function including string creation
  348. local ship = mn.Ships[shipname] --ship handle
  349. local shipdata = {}
  350. shipdata[1] = {}
  351. if (ship:isValid() and (ship:hasShipExploded() == 0)) then --ship existent and not exploding
  352. shipdata[1][1] = 1
  353. shipdata[1][2] = ship.Class.Name --ship class
  354. shipdata[1][3] = ship.Team.Name -- team
  355. shipdata[1][4] = ship.HitpointsMax --user defined max hp
  356. shipdata[1][5] = ship.HitpointsLeft --hp left
  357. if (ship.CountermeasureClass:isValid()) then
  358. shipdata[1][6] = ship.CountermeasuresLeft
  359. else
  360. shipdata[1][6] = -1
  361. end
  362. if (ship.AfterburnerFuelMax > 0) then
  363. shipdata[1][7] = ship.AfterburnerFuelLeft
  364. else
  365. shipdata[1][7] = -1
  366. end
  367. if (ship.WeaponEnergyMax > 0) then
  368. shipdata[1][8] = ship.WeaponEnergyLeft
  369. else
  370. shipdata[1][8] = -1
  371. end
  372. shields = ship.Shields
  373. if (shields:isValid()) then --shield data in case there is a shield, otherwise -1
  374. shipdata[1][9] = shields.CombinedMax
  375. shipdata[1][10] = shields[1]
  376. shipdata[1][11] = shields[2]
  377. shipdata[1][12] = shields[3]
  378. shipdata[1][13] = shields[4]
  379. else
  380. shipdata[1][9] = -1
  381. shipdata[1][10] = -1
  382. shipdata[1][11] = -1
  383. shipdata[1][12] = -1
  384. shipdata[1][13] = -1
  385. end
  386. ns = #ship --# subsystems
  387. shipdata[2] = {}
  388. if (ns > 0) then
  389. shipdata[2][1] = ns
  390. for i=1,ns do
  391. shipdata[2][1+(2*i-1)] = ship[i].HitpointsLeft --hp subsystem
  392. shipdata[2][1+2*i] = weaponsdatacollect(ship[i],2) --for turrets, only primaries und secondaries
  393. end
  394. else
  395. shipdata[2][1] = -1 --no subsystems
  396. end
  397. shipdata[3] = weaponsdatacollect(ship,1) --normal weapons, primaries, secondaries, tertiaries
  398. shipdata[4] = {}
  399. for i=1,3 do
  400. shipdata[4][i] = ship.Position[i]
  401. end
  402. for i=1,9 do
  403. shipdata[4][i+3] = ship.Orientation[i]
  404. end
  405. for i=1,3 do
  406. shipdata[4][i+12] = ship.Physics.Velocity[i]
  407. end
  408. for i=1,3 do
  409. shipdata[4][i+15] = ship.Physics.RotationalVelocity[i]
  410. end
  411. else
  412. if (mn.evaluateSEXP("(is-destroyed-delay !0! !"..shipname.."!)")) then
  413. shipdata[1][1] = -2 --ship destroyed
  414. elseif (mn.evaluateSEXP("(has-departed-delay !0! !"..shipname.."!)")) then
  415. shipdata[1][1] = -1 --ship departed
  416. else
  417. shipdata[1][1] = -3 --ship handle invalid, ship-vanish used or something like it
  418. end
  419. end
  420. local savestring = createshipstring(shipdata,shipname)
  421. return savestring
  422. end
  423.  
  424. function shipdatainsert(shipstring,shipname,typ,stat,loadname,loc)
  425. if not loc then
  426. loc = 0
  427. end
  428. local shipdata = {}
  429. local ship = mn.Ships[shipname] --ship handle
  430. local stathold = 0
  431. shipdata = shipstringsplitter(shipstring) -- create shipdata array and fill it
  432. local position_loader = position_statusloader + ba.createVector(math.random(-500,500),math.random(-500,500),math.random(-500,500)) --random position for statusloader
  433. if loc == 1 and shipdata[1][1] == 1 then
  434. if ship.Class.Name ~= shipdata[1][2] then
  435. ship.Class = tb.ShipClasses[shipdata[1][2]]
  436. end
  437. elseif ship:isValid() and shipdata[1][1] == 1 and ship.Class.Name ~= shipdata[1][2] then
  438. ship.Class = tb.ShipClasses[shipdata[1][2]]
  439. stathold = 1
  440. table.insert(active_mainloader,{start_time = mn.getMissionTime(),ship = shipname,statcall = stat,typecall = typ,loadn = loadname})
  441. end
  442. if (ship:isValid() and stathold ==0) then
  443. if (shipdata[1][1] == 1) then --ship was ingame or wasn't arriving while saving
  444. if (ship.Team.Name ~= shipdata[1][3]) then
  445. local nothing = mn.runSEXP("(change-iff !"..shipdata[1][3].."! !"..shipname.."!)")
  446. end
  447. ship.HitpointsMax = shipdata[1][4]
  448. ship.HitpointsLeft = shipdata[1][5]
  449. if (shipdata[1][6] > -1) then
  450. ship.CountermeasuresLeft = shipdata[1][6]
  451. end
  452. if (typ == 2 or typ == "2" or typ == 3 or typ == "3") then
  453. if (ship.AfterburnerFuelMax > 0) then
  454. ship.AfterburnerFuelLeft = shipdata[1][7]
  455. end
  456. if (ship.WeaponEnergyMax > 0) then
  457. ship.WeaponEnergyLeft = shipdata[1][8]
  458. end
  459. shields=ship.Shields
  460. if (shields:isValid() and (shipdata[1][9] > 0)) then --ship has shields and save contains shields
  461. shields.CombinedMax = shipdata[1][9]
  462. shields[1] = shipdata[1][10]
  463. shields[2] = shipdata[1][11]
  464. shields[3] = shipdata[1][12]
  465. shields[4] = shipdata[1][13]
  466. end
  467. end
  468. if (shipdata[2][1] > 0) then --ship has subsystems?
  469. for i=1,shipdata[2][1] do
  470. local turretname = "none"
  471. if ship[i]:getModelName() ~= nil then
  472. turretname = ship[i]:getModelName()
  473. else
  474. turretname = ship[i].Name
  475. end
  476. if ship[i].HitpointsMax > 0 then
  477. local percentageleft = shipdata[2][1+(2*i-1)] / ship[i].HitpointsMax * 100
  478. if percentageleft > 0 then
  479. mn.runSEXP("(set-subsystem-strength !"..shipname.."! !"..turretname.."! !"..percentageleft.."! !true!)")
  480. else
  481. mn.runSEXP("(destroy-subsys-instantly !"..shipname.."! !"..turretname.."!)")
  482. end
  483. end
  484. weaponsinsert(shipdata[2][1+2*i],ship[i],2) --turrets loader, only primaries and secondaries
  485. end
  486. end
  487. weaponsinsert(shipdata[3],ship,1) --ships weapons, primaries, secondaries and tertiaries
  488. if ((typ == 3 or typ == "3") and stathold == 0) then
  489. ship.Position = ba.createVector(shipdata[4][1],shipdata[4][2],shipdata[4][3])
  490. local ori = {}
  491. for i=1,9 do
  492. ori[i] = shipdata[4][i+3]
  493. end
  494. ship.Orientation = ba.createOrientation(ori[1],ori[2],ori[3],ori[4],ori[5],ori[6],ori[7],ori[8],ori[9])
  495. --[[local vec = ba.createVector(0,0,0)
  496. for i=1,3 do
  497. vec[i] = shipdata[4][i+12]
  498. end
  499. ship.Physics.Velocity = vec
  500. for i=1,3 do
  501. vec[i] = shipdata[4][i+15]
  502. end
  503. ship.Physics.RotationalVelocity = vec]]
  504. end
  505. else
  506. if (shipdata[1][1] == -1 and (stat == 3 or stat == "3" or stat == 4 or stat == "4")) then --recreate departed status
  507. mn.runSEXP("(set-departure-info !"..shipname.."! !Hyperspace! !<no anchor>! !0! !false!)")
  508. ship:warpOut(ship)
  509. elseif (shipdata[1][1] == -2 and (stat == 3 or stat == "3" or stat == 4 or stat == "4")) then --recreate destroyed status
  510. mn.runSEXP("(destroy-instantly !"..shipname.."!)")
  511. elseif (stat ~= 2 or stat ~= "2") then --ship-vanish the ship
  512. local nothing = mn.runSEXP("(ship-vanish !"..shipname.."!)")
  513. end
  514. end
  515. elseif ((stat == 4 or stat == "4") and stathold == 0) then
  516. if (shipdata[1][1] == -1) then
  517. ship = mn.createShip(shipname,tb.ShipClasses[shipclass_statusloader],ba.createOrientation(1,0,0,0,1,0,0,0,1),ba.createVector(0,0,0))
  518. mn.runSEXP("(set-departure-info !"..shipname.."! !Hyperspace! !<no anchor>! !0! !false!)")
  519. ship:warpOut(ship)
  520. elseif (shipdata[1][1] == -2) then
  521. ship = mn.createShip(shipname,tb.ShipClasses[shipclass_statusloader],ba.createOrientation(1,0,0,0,1,0,0,0,1),position_loader)
  522. mn.runSEXP("(destroy-instantly !"..shipname.."!)")
  523. else
  524. end
  525. end
  526. end
  527.  
  528. function booltostring(boolValue) --tool function booleans to strings
  529. if (boolValue == true) then
  530. return "1"
  531. elseif (boolValue == false) then
  532. return "0"
  533. else
  534. return "-3"
  535. end
  536. end
  537.  
  538. function stringtobool(stringValue) --tool function to recreate booleans
  539. if (stringValue == "1" or stringValue == 1) then
  540. return true
  541. elseif (stringValue == "0" or stringValue == 0) then
  542. return false
  543. else
  544. return nil
  545. end
  546. end
  547.  
  548. function weaponsdatacollect(object,typ) --weapondata to array
  549. local array = {}
  550. bank = object.PrimaryBanks --handle primaries
  551. array[1] = weaponbankdatacollect(bank)
  552. bank = object.SecondaryBanks --handle secondaries
  553. array[2] = weaponbankdatacollect(bank)
  554. if (typ == 1) then --1=main weapons, 2=turrets
  555. bank = object.TertiaryBanks
  556. array[3] = weaponbankdatacollect(bank)
  557. else
  558. array[3] ={}
  559. array[3][1] = -2
  560. end
  561. return array
  562. end
  563.  
  564. function weaponbankdatacollect(bank) --weaponbankdata to array
  565. local array = {}
  566. if (bank:isValid()) then
  567. n = #bank --number of weapons in bank
  568. if (n > 0) then
  569. array[1] = n
  570. array[2] = booltostring(bank.Linked) --linked
  571. array[3] = booltostring(bank.DualFire) --dual fire
  572. for j=1,n do
  573. array[3+j] = {}
  574. wbank=bank[j] --weaponbank handle
  575. array[3+j][1] = wbank.WeaponClass.Name --weapon class name
  576. array[3+j][2] = booltostring(wbank.Armed) --weapon active
  577. array[3+j][3] = wbank.AmmoMax
  578. if (array[3+j][3] > 0) then
  579. array[3+j][4] = wbank.AmmoLeft
  580. else
  581. array[3+j][4] = -1
  582. end
  583. end
  584. else
  585. array[1]=-1 --no weapons in bank
  586. end
  587. else
  588. array[1]=-2 --invalid bank, should not happen
  589. end
  590. return array
  591. end
  592.  
  593. function weaponsinsert(array,object,typ) --transfers weapondata from array to object
  594. if (array[1][1] > 0) then
  595. bank=object.PrimaryBanks --handle primaries
  596. weaponbankinsert(bank,array[1],1)
  597. end
  598. if (array[2][1] > 0) then
  599. bank=object.SecondaryBanks --handle secondaries
  600. weaponbankinsert(bank,array[2],2)
  601. end
  602. if ((typ == 1) and (array[3][1] > 0)) then --1=main weapons, 2=turrets
  603. bank=object.TertiaryBanks
  604. weaponbankinsert(bank,array[3],0)
  605. end
  606. end
  607.  
  608. function weaponbankinsert(bank,array,typ) --array to weaponbankdata
  609. if (bank:isValid()) then
  610. bank.Linked = stringtobool(array[2]) --linked
  611. bank.DualFire = stringtobool(array[3]) --dual fire
  612. for j=1,array[1] do
  613. wbank=bank[j] --weaponbank handle
  614. if (wbank.WeaponClass.Name ~= array[3+j][1]) then --prevent it from doing unnecessary stuff
  615. wbank.WeaponClass = tb.WeaponClasses[array[3+j][1]] --weapon class
  616. end
  617. if (stringtobool(array[3+j][2])) then
  618. wbank.Armed = stringtobool(array[3+j][2]) --weapon active
  619. end
  620. wbank.AmmoMax = array[3+j][3]
  621. if (array[3+j][3] > 0) then
  622. wbank.AmmoLeft = array[3+j][4]
  623. end
  624. end
  625. end
  626. end
  627.  
  628. function createshipstring(shipdata,name) --shipstrig creation for savefile
  629. local savestring = (shipdata[1][1]..",")
  630. local addstring = ""
  631. if (shipdata[1][1] == 1) then
  632. savestring = (savestring..table.concat(shipdata[1],",",2,13)..",") --basic data
  633. savestring = (savestring..":")
  634. savestring = (savestring..shipdata[2][1].."&") --number of subsystems
  635. if (shipdata[2][1]>0) then
  636. for i=1,shipdata[2][1] do --subsystems and turret data
  637. savestring = (savestring..shipdata[2][1+(2*i-1)].."§")
  638. addstring = createweaponstring(shipdata[2][1+(2*i)])
  639. savestring = (savestring..addstring.."&")
  640. end
  641. end
  642. savestring = (savestring..":")
  643. addstring = createweaponstring(shipdata[3]) --weapons
  644. savestring = (savestring..addstring..":")
  645. savestring = (savestring..table.concat(shipdata[4],",",1,18)) --position and orientation
  646. else
  647. savestring = (savestring..":::")
  648. end
  649. return savestring
  650. end
  651.  
  652. function createweaponstring(array) --weaponstring creation for savefile
  653. local savestring = "" --returns an empty string in any case
  654. for j=1,3 do
  655. if (array[j][1] > 0) then
  656. savestring = (savestring..array[j][1]..","..array[j][2]..","..array[j][3]..",") --number, linked, dualfire
  657. for k=1,array[j][1] do
  658. savestring = (savestring..array[j][3+k][1]..","..array[j][3+k][2]..","..array[j][3+k][3]..",") --weaponclass, ammomax, ammoleft
  659. savestring = (savestring..array[j][3+k][4])
  660. if (j ~= 3) then --splitter wont like me without this
  661. savestring = (savestring..",")
  662. end
  663. end
  664. else
  665. savestring = (savestring..array[j][1])
  666. if (j ~= 3) then --splitter wont like me without this
  667. savestring = (savestring..",")
  668. end
  669. end
  670. end
  671. return savestring
  672. end
  673.  
  674. function shipstringsplitter(shipstring) --splits shipstring into shipdata array
  675. local temp = {} --temporary array
  676. local subtemp = {} --more temporary array
  677. local subtempint = {} --and one more cause i like it
  678. local shipdata = {} --what i actually want to have
  679. temp = Split(shipstring,":")
  680. shipdata[1] = {}
  681. shipdata[1] = Split(temp[1],",")
  682. shipdata[1][1] = tonumber(shipdata[1][1]) --need something like (if string = a number then change string to number) in the split function
  683. if (shipdata[1][1] == 1) then
  684. for m=4,13 do
  685. shipdata[1][m] = tonumber(shipdata[1][m])
  686. end
  687. shipdata[2] = {}
  688. subtemp = Split(temp[2],"&")
  689. shipdata[2][1] = tonumber(subtemp[1])
  690. if (shipdata[2][1] > 0) then
  691. for i=1,shipdata[2][1] do
  692. subtempint = Split(subtemp[i+1],"§")
  693. shipdata[2][1+(2*i-1)] = tonumber(subtempint[1])
  694. shipdata[2][1+(2*i)] = weaponstringsplitter(subtempint[2])
  695. end
  696. end
  697. shipdata[3] = weaponstringsplitter(temp[3])
  698. shipdata[4] = Split(temp[4],",")
  699. for m=1,12 do
  700. shipdata[4][m] = tonumber(shipdata[4][m])
  701. end
  702. end
  703. return shipdata
  704. end
  705.  
  706. function weaponstringsplitter(subtempint) --splits weapondata into array
  707. local array = {}
  708. local temp = {}
  709. local l = 1
  710. local n = 0
  711. temp = Split(subtempint,",")
  712. for j=1,3 do
  713. array[j] = {}
  714. array[j][1] = tonumber(temp[l])
  715. if (array[j][1] > 0) then
  716. array[j][2] = tonumber(temp[l+1])
  717. array[j][3] = tonumber(temp[l+2])
  718. l = l + 3
  719. for k=1,array[j][1] do
  720. array[j][k+3] = {}
  721. array[j][k+3][1] = temp[l]
  722. array[j][k+3][2] = tonumber(temp[l+1])
  723. array[j][k+3][3] = tonumber(temp[l+2])
  724. array[j][k+3][4] = tonumber(temp[l+3])
  725. l = l + 4
  726. end
  727. else
  728. l = l + 1
  729. end
  730. end
  731. return array
  732. end
  733. ]
  734.  
  735. $State: GS_STATE_GAME_PLAY
  736. $On Frame:[
  737. if (table.maxn(active_mainloader)) > 0 then
  738. for i=1,table.maxn(active_mainloader) do
  739. local time_elapsed = mn.getMissionTime() - active_mainloader[i].start_time
  740. if (time_elapsed >= 0.05) then
  741. local targetname = active_mainloader[i].ship
  742. local typecall = active_mainloader[i].typecall
  743. local statcall = active_mainloader[i].statcall
  744. local loadname = active_mainloader[i].loadn
  745. loadship(loadname,typecall,statcall,targetname)
  746. table.remove(active_mainloader,i)
  747. break
  748. end
  749. end
  750. end
  751. ]
  752. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement