tic80-loop.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. function loadHighScores()
  2. result = {}
  3. for i=1,5 do
  4. local nameInt = pmem(i-1)
  5. local i1 = nameInt % 32
  6. local i2 = math.floor(nameInt / 32) % 32
  7. local i3 = math.floor(nameInt / 1024) % 32
  8. local score = pmem(i)
  9. if (score ~= 0) then
  10. table.insert(result, {
  11. name = {numToChar(i1), numToChar(i2), numToChar(i3)},
  12. score = score
  13. })
  14. end
  15. end
  16. highScores = result
  17. sortHighScores()
  18. end
  19. function saveHighScores()
  20. sortHighScores()
  21. for i=1,5 do
  22. if (highScores[i] ~= nil) then
  23. local i1 = charToNum(highScores[i].name[1])
  24. local i2 = charToNum(highScores[i].name[2])
  25. local i3 = charToNum(highScores[i].name[3])
  26. local nameInt = i3 * 1024 + i2 * 32 + i1
  27. pmem(i-1, nameInt)
  28. pmem(i, highScores[i].score)
  29. end
  30. end
  31. end
  32. function renderHighScoreEntry()
  33. if (newHighScore == nil) then return end
  34. print(newHighScore.name[1], 100, 50, 12)
  35. print(newHighScore.name[2], 110, 50, 12)
  36. print(newHighScore.name[3], 120, 50, 12)
  37. print(newHighScore.score, 150, 50, 12)
  38. print("OK", 110, 100, 12)
  39. if (newHighScore.y == 1) then
  40. if (newHighScore.x == 1) then
  41. print("_", 100, 54, 12)
  42. elseif (newHighScore.x == 2) then
  43. print("_", 110, 54, 12)
  44. else
  45. print("_", 120, 54, 12)
  46. end
  47. else
  48. print("_", 110, 104, 12)
  49. end
  50. end
  51. function printPopUpBackground(stackAmount)
  52. local iMax = 26
  53. local jMax = 13
  54. for i=1,iMax do
  55. for j=1,jMax do
  56. local sprKey
  57. if (i == 1 and j == 1) then
  58. sprKey = 432
  59. elseif (i == 1 and j == jMax) then
  60. sprKey = 464
  61. elseif (i == iMax and j == 1) then
  62. sprKey = 434
  63. elseif (i == iMax and j == jMax) then
  64. sprKey = 466
  65. elseif (i == 1) then
  66. sprKey = 448
  67. elseif (j == 1) then
  68. sprKey = 433
  69. elseif (i == iMax) then
  70. sprKey = 450
  71. elseif (j == jMax) then
  72. sprKey = 465
  73. else
  74. sprKey = 449
  75. end
  76. spr(sprKey, i*8 + stackAmount * 4 - 4, j*8 + stackAmount * 4 - 4)
  77. end
  78. end
  79. end
  80. function getCommoditySprite(code)
  81. if (code >= 1 and code <= 8) then
  82. return 2 * code + 254
  83. else
  84. return 2 * code + 270
  85. end
  86. end
  87. function getPlanetSprite(code)
  88. return code + 435
  89. end
  90. function renderPage(pageId)
  91. if (page == nil or newHighScore ~= nil) then return end
  92. local lrArrowHeight
  93. if (page.level == 1) then
  94. lrArrowHeight = 114
  95. elseif (page.level == 2) then
  96. lrArrowHeight = 104
  97. else
  98. lrArrowHeight = 94
  99. end
  100. spr(481, 40, lrArrowHeight, 0, 1, 0, 0, 1, 1)
  101. spr(480, 54, lrArrowHeight, 0, 1, 0, 0, 1, 1)
  102. spr(480, 160, lrArrowHeight, 0, 1, 1, 0, 1, 1)
  103. local maxOptions = #page.options
  104. for i=maxOptions, 1, -1 do
  105. local optionList = page.options[i]
  106. if (i == 1) then
  107. print(optionList[page.selections[i]], 68, 116, 12)
  108. elseif (i == 2) then
  109. print(optionList[page.selections[i]], 68, 106, 12)
  110. elseif (i == 3) then
  111. print(optionList[page.selections[i]], 68, 96, 12)
  112. end
  113. end
  114. if (warp == nil) then
  115. print("Planet: " .. player.planet.name, 150, 10, 12)
  116. end
  117. if (page.id == "Commodities") then
  118. local commodityId = getSelection(page, 3)
  119. local commodity = commodityList[commodityId]
  120. print("Stock: " .. player.planet.commodities[commodityId], 10, 10, 12)
  121. print("Unit Cost: " .. math.floor(player.planet.commodityRates[commodityId] * commodity.baseCost), 10, 20, 12)
  122. print("In Inventory: " .. player.commodities[commodityId], 10, 30, 12)
  123. print("Credits: " .. string.format("%d", math.floor(player.credits)), 10, 40, 12)
  124. spr(getCommoditySprite(commodity.code), 138, 60, 0, 1, 0, 0, 2, 2)
  125. elseif (page.id == "Planets") then
  126. local id = getSelection(page, 3)
  127. local targetPlanet = planetList[id]
  128. local distance = calculatePlanetDistance(player.planet, targetPlanet)
  129. print("Distance: " .. string.format("%d", math.ceil(distance)) .. "m Miles", 10, 10, 12)
  130. print("Fuel: " .. tostring(player.ship.fuel) .. " / " .. tostring(player.ship.maxFuel), 10, 20, 12)
  131. print("Fuel Cost: " .. string.format("%d", math.ceil(distance / 10)), 10, 30, 12)
  132. for i, planetId in ipairs(getNeighbors(player.planet.name)) do
  133. local relativeX1, relativeY1 = getDistortedRelativeCoordinates(player.planet, sun)
  134. local relativeX2, relativeY2 = getDistortedRelativeCoordinates(planetList[planetId], sun)
  135. line(108 + math.floor(relativeX1), 52 - math.floor(relativeY1), 108 + math.floor(relativeX2), 52 - math.floor(relativeY2), 12)
  136. end
  137. spr(getPlanetSprite(0), 104, 48, 0, 1, 0, 0, 1, 1)
  138. for i, planet in ipairs(planetList) do
  139. local relativeX, relativeY = getDistortedRelativeCoordinates(planet, sun)
  140. if (relativeX > -112 and relativeX < 112 and relativeY > -28 and relativeY < 28) then
  141. spr(getPlanetSprite(planet.code), 104 + math.floor(relativeX), 48 - math.floor(relativeY), 0, 1, 0, 0, 1, 1)
  142. end
  143. if (targetPlanet == planet) then
  144. spr(447, 104 + math.floor(relativeX), 48 - math.floor(relativeY), 0, 1, 0, 0, 1, 1)
  145. end
  146. end
  147. elseif (page.id == "Services") then
  148. local serviceId = getSelection(page, 2)
  149. local serviceCost = serviceList[serviceId].cost
  150. print("Hull Condition: " .. string.format("%d", math.floor(100 * player.ship.hullHealth / player.ship.hullStrength)) .. "%", 10, 10, 12)
  151. print("Fuel: " .. tostring(player.ship.fuel) .. " / " .. tostring(player.ship.maxFuel), 10, 20, 12)
  152. print("Service Cost: " .. tostring(serviceCost), 10, 30, 12)
  153. print("Credits: " .. string.format("%d", math.floor(player.credits)), 10, 40, 12)
  154. elseif (page.id == "Shipyard") then
  155. local shipId = getSelection(page, 3)
  156. local ship = shipList[shipId]
  157. local cost = shipList[shipId].cost
  158. local tradeInCredit = shipResaleFactor * player.ship.cost
  159. print("Current ship: " .. player.ship.model, 10, 10, 12)
  160. print("Current ship trade-in credit: " .. string.format("%d", tradeInCredit), 10, 20, 12)
  161. print("New ship cost: " .. string.format("%d", cost), 10, 30, 12)
  162. if (tradeInCredit - cost <= 0) then
  163. print("Total cost: " .. string.format("%d", cost - tradeInCredit), 10, 40, 12)
  164. else
  165. print("Total credit: " .. string.format("%d", tradeInCredit - cost), 10, 40, 12)
  166. end
  167. print("Credits: " .. player.credits, 10, 50, 12)
  168. elseif (page.id == "Pirate Encounter" or page.id == "Cop Encounter" and warp ~= nil) then
  169. local intDistance = 10 * (warp.maxIters - warp.iter + 1)
  170. print("Distance to destination: " .. string.format("%d", intDistance) .. "m Miles", 10, 10, 12)
  171. print("Hull Condition: " .. string.format("%d", math.floor(100 * player.ship.hullHealth / player.ship.hullStrength)) .. "%", 10, 20, 12)
  172. print("Surrender Cost: " .. string.format("%d", math.floor(player.credits * 0.9)), 10, 30, 12)
  173. end
  174. end
  175. function renderPopUps()
  176. if (popUps == nil or newHighScore ~= nil) then return end
  177. for i, popUpItem in ipairs(popUps) do
  178. printPopUpBackground(i)
  179. for j, lineItem in ipairs(popUpItem) do
  180. print(lineItem, 8 + i * 4, 10 * j + i * 4 - 2, 0)
  181. end
  182. end
  183. end
  184. t=0
  185. function TIC()
  186. if (not gameLoaded) then
  187. loadHighScores()
  188. newGame()
  189. gameLoaded = true
  190. end
  191. if (exitGame) then
  192. exit()
  193. end
  194. cls(0)
  195. renderPage()
  196. renderPopUps()
  197. renderHighScoreEntry()
  198. if (warp ~= nil and page == nil and (popUps == nil or #popUps == 0) and newHighScore == nil) then
  199. continueWarp()
  200. elseif btnp(0) then
  201. onButtonPress("up")
  202. elseif btnp(1) then
  203. onButtonPress("down")
  204. elseif btnp(2) then
  205. onButtonPress("left")
  206. elseif btnp(3) then
  207. onButtonPress("right")
  208. elseif btnp(4) then
  209. onButtonPress("a")
  210. elseif btnp(5) then
  211. onButtonPress("b")
  212. elseif btnp(6) then
  213. onButtonPress("x")
  214. elseif btnp(7) then
  215. onButtonPress("y")
  216. end
  217. t=t+1
  218. end