Advertisement
Saldor010

suush

Nov 27th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.58 KB | None | 0 0
  1. -- Cold War Simulator by Saldor010
  2.  
  3. local args = {...}
  4. if not fs.exists("cobalt") and (not args[1] or not fs.exists(args[1])) then
  5. term.setTextColor(colors.red)
  6. print("Cobalt could not be found on this machine. Cobalt is required to run this game.")
  7. term.setTextColor(colors.lime)
  8. print("To download cobalt, please press the Y key now.")
  9. term.setTextColor(colors.blue)
  10. print("If you already have cobalt, and we just can't find it, please supply the path as the second argument.")
  11. local ev,p1,p2,p3,p4,p5 = os.pullEvent("char")
  12. if string.lower(p1) == "y" then
  13. shell.run("pastebin","run","h5h4fm3t")
  14. else
  15. term.setTextColor(colors.red)
  16. print("Cancelled installation of Cobalt.")
  17. end
  18. error()
  19. end
  20.  
  21. -- PATCHING THE COBALT SURFACE FILE AUTOMAGICALLY
  22. local surfaceFileToBeReadHANDLE = fs.open("cobalt-lib/surface","r")
  23. local surfaceFileToBeRead = surfaceFileToBeReadHANDLE.readAll()
  24. local newFile = nil
  25. if not string.find(surfaceFileToBeRead,"local create = surface.create") then
  26. local ch = string.find(surfaceFileToBeRead,"function surface.load")
  27. print(ch)
  28. local firstSub = string.sub(surfaceFileToBeRead,1,ch-1)
  29. local secondSub = string.sub(surfaceFileToBeRead,ch)
  30. newFile = firstSub.."local create = surface.create\n\n"..secondSub
  31. end
  32. surfaceFileToBeReadHANDLE.close()
  33. if newFile then
  34. local surfaceFileToBeRewritten = fs.open("cobalt-lib/surface","w")
  35. surfaceFileToBeRewritten.write(newFile)
  36. surfaceFileToBeRewritten.close()
  37. end
  38. -- Done patching
  39.  
  40. local cobalt = dofile(args[1] or "cobalt")
  41. cobalt.ui = dofile("cobalt-ui/init.lua")
  42.  
  43. local StrategicCommandCenterPlaced = false
  44. local StrategicCommandCenterPosition = {
  45. ["X"] = 0,
  46. ["Y"] = 0,
  47. }
  48. local marker = {["x"] = -10,["y"] = -10,["timer"] = 0}
  49. local DEFCON = 5
  50.  
  51. local buildings = {
  52. [1] = {
  53. ["Name"] = "Missile Silo",
  54. ["Cost"] = 500000, -- 500K
  55. ["Icon"] = "M",
  56. }
  57. }
  58.  
  59. local nations = {
  60. ["United States"] = {
  61. ["Population"] = 0,
  62. ["Resources"] = 3000000, -- $3M
  63. ["Image"] = "US.nfp",
  64. ["Color"] = colors.blue,
  65. ["Buildings"] = {},
  66. ["Cities"] = {
  67. ["Los Angeles"] = {
  68. ["X"] = 8,
  69. ["Y"] = 13,
  70. ["Population"] = 500000, --500K
  71. ["Dead"] = 0,
  72. ["Nukes"] = 0,
  73. ["Cruise"] = 0,
  74. ["Icon"] = "@",
  75. },
  76. ["Seattle"] = {
  77. ["X"] = 7,
  78. ["Y"] = 4,
  79. ["Population"] = 400000, --400K
  80. ["Dead"] = 0,
  81. ["Nukes"] = 0,
  82. ["Cruise"] = 0,
  83. ["Icon"] = "@",
  84. },
  85. ["Dallas"] = {
  86. ["X"] = 29,
  87. ["Y"] = 15,
  88. ["Population"] = 300000, --300K
  89. ["Dead"] = 0,
  90. ["Nukes"] = 0,
  91. ["Cruise"] = 0,
  92. ["Icon"] = "@",
  93. },
  94. ["New York"] = {
  95. ["X"] = 45,
  96. ["Y"] = 8,
  97. ["Population"] = 1000000, --1M
  98. ["Dead"] = 0,
  99. ["Nukes"] = 0,
  100. ["Cruise"] = 1,
  101. ["Icon"] = "$",
  102. },
  103. ["Wash D.C."] = {
  104. ["X"] = 43,
  105. ["Y"] = 9,
  106. ["Population"] = 1000000, --1M
  107. ["Dead"] = 0,
  108. ["Nukes"] = 0,
  109. ["Cruise"] = 1,
  110. ["Icon"] = "$",
  111. },
  112. ["Denver"] = {
  113. ["X"] = 19,
  114. ["Y"] = 10,
  115. ["Population"] = 100000, --100K
  116. ["Dead"] = 0,
  117. ["Nukes"] = 0,
  118. ["Cruise"] = 0,
  119. ["Icon"] = "@",
  120. },
  121. ["Minneapolis"] = {
  122. ["X"] = 30,
  123. ["Y"] = 7,
  124. ["Population"] = 100000, --100K
  125. ["Dead"] = 0,
  126. ["Nukes"] = 0,
  127. ["Cruise"] = 0,
  128. ["Icon"] = "@",
  129. },
  130. },
  131. },
  132. ["China"] = {
  133. ["Population"] = 0,
  134. ["Resources"] = 3000000, -- $3M
  135. ["Image"] = "China.nfp",
  136. ["Color"] = colors.orange,
  137. ["Buildings"] = {},
  138. ["Cities"] = {
  139. ["Beijing"] = {
  140. ["X"] = 42,
  141. ["Y"] = 7,
  142. ["Population"] = 1000000, --1M
  143. ["Dead"] = 0,
  144. ["Nukes"] = 0,
  145. ["Cruise"] = 1,
  146. ["Icon"] = "$",
  147. },
  148. ["Shanghai"] = {
  149. ["X"] = 47,
  150. ["Y"] = 12,
  151. ["Population"] = 500000, --500K
  152. ["Dead"] = 0,
  153. ["Nukes"] = 0,
  154. ["Cruise"] = 0,
  155. ["Icon"] = "@",
  156. },
  157. ["Hong Kong"] = {
  158. ["X"] = 43,
  159. ["Y"] = 18,
  160. ["Population"] = 1000000, --1M
  161. ["Dead"] = 0,
  162. ["Nukes"] = 0,
  163. ["Cruise"] = 1,
  164. ["Icon"] = "$",
  165. },
  166. ["Chengdu"] = {
  167. ["X"] = 34,
  168. ["Y"] = 13,
  169. ["Population"] = 300000, --300K
  170. ["Dead"] = 0,
  171. ["Nukes"] = 0,
  172. ["Cruise"] = 0,
  173. ["Icon"] = "@",
  174. },
  175. ["Xi'an"] = {
  176. ["X"] = 36,
  177. ["Y"] = 11,
  178. ["Population"] = 100000, --100K
  179. ["Dead"] = 0,
  180. ["Nukes"] = 0,
  181. ["Cruise"] = 0,
  182. ["Icon"] = "@",
  183. },
  184. ["Urumqi"] = {
  185. ["X"] = 12,
  186. ["Y"] = 5,
  187. ["Population"] = 100000, --100K
  188. ["Dead"] = 0,
  189. ["Nukes"] = 0,
  190. ["Cruise"] = 0,
  191. ["Icon"] = "@",
  192. },
  193. ["Guangzhou"] = {
  194. ["X"] = 42,
  195. ["Y"] = 17,
  196. ["Population"] = 400000, --400K
  197. ["Dead"] = 0,
  198. ["Nukes"] = 0,
  199. ["Cruise"] = 0,
  200. ["Icon"] = "@",
  201. },
  202. },
  203. }
  204. }
  205.  
  206. table.insert(nations["United States"]["Buildings"],{
  207. ["X"] = 29,
  208. ["Y"] = 13,
  209. ["type"] = "command",
  210. ["cruise"] = 0,
  211. ["nukes"] = 0,
  212. ["health"] = 3,
  213. ["Icon"] = "A",
  214. ["Work"] = false,
  215. })
  216.  
  217. table.insert(nations["China"]["Buildings"],{
  218. ["X"] = 36,
  219. ["Y"] = 13,
  220. ["type"] = "command",
  221. ["cruise"] = 0,
  222. ["nukes"] = 0,
  223. ["health"] = 3,
  224. ["Icon"] = "A",
  225. ["Work"] = false,
  226. })
  227.  
  228. -- Stole from here http://www.computercraft.info/forums2/index.php?/topic/10279-question-how-to-get-current-dir/
  229. -- I doubt originalbit cares though, since he's been AFK for.. (checking his forum profile).. Yeah, pretty much a year now
  230. local runningProgram = shell.getRunningProgram()
  231. local programName = fs.getName(runningProgram)
  232. local workingDirectory = runningProgram:sub( 1, #runningProgram - #programName )
  233. print(workingDirectory)
  234. for k,v in pairs(nations) do
  235. local Napop = 0
  236.  
  237. if fs.exists(workingDirectory..v["Image"]) then
  238. v["Image"] = cobalt.surface.load(workingDirectory..v["Image"])
  239. else
  240. error("Missing image : "..workingDirectory..v["Image"])
  241. end
  242.  
  243. for p,b in pairs(v["Cities"]) do
  244. b["DefaultIcon"] = b["Icon"]
  245. Napop = Napop + b["Population"]
  246. end
  247. v["Population"] = Napop -- The population that the nation was at when the game started E.G. Perfectly healthy nation
  248. end
  249.  
  250. local worldMap = nil
  251. if fs.exists(workingDirectory.."worldmap.nfp") then
  252. worldMap = cobalt.surface.load(workingDirectory.."worldmap.nfp")
  253. else
  254. error("Missing image : "..workingDirectory.."worldmap.nfp")
  255. end
  256.  
  257. local nationSelectedForPlaying = "United States"
  258. local nationSelectedForFighting = "China"
  259. local nationSelectedForGUI = "United States"
  260. local nationSelectedForTurn = "United States"
  261.  
  262. local AIPaused = false
  263. local turn = 1
  264. local TURNOVER = false
  265. local turnString = "Turn "..tostring(turn)
  266. local turnStringTICK = 20
  267. local tick = 0
  268. local tickRate = 0.2
  269.  
  270. local gridToggle = false
  271.  
  272. local CommandCenterGUI = false
  273. local ContextSelected = nil
  274. local ContextDisabled = false
  275. local ContextOverride = false
  276. local ContextPanel = cobalt.ui.new({w = 14,h = 6, x = -100, y=-100})
  277. local ContextLabels = {
  278. [1] = ContextPanel:add("text",{text="",wrap="center",foreColour = colors.white, backColour = colors.black,x=1,y=1}),
  279. [2] = ContextPanel:add("text",{text="",wrap="center",foreColour = colors.white, backColour = colors.black,x=1,y=2}),
  280. [3] = ContextPanel:add("text",{text="",wrap="left",foreColour = colors.white, backColour = colors.black,x=1,y=3}),
  281. [4] = ContextPanel:add("text",{text="",wrap="left",foreColour = colors.white, backColour = colors.black,x=1,y=4}),
  282. }
  283. local ContextButtons = {
  284. [1] = ContextPanel:add("button",{wrap="left",y=5,w=12,h=1,text=""}),
  285. [2] = ContextPanel:add("button",{wrap="left",y=6,w=12,h=1,text=""}),
  286. }
  287.  
  288. local ContextPopUpBG = cobalt.ui.new({w="70%",h="70%",marginleft="15%",margintop="15%",backColour=colors.white,backColour=colors.white})
  289. --local ContextPopUpBG2 = ContextPopUpBG:add("text",{w=ContextPopUpBG.w,h=ContextPopUpBG.h,x=0,y=0,text=string.rep(string.rep("-",ContextPopUpBG.w).."\n",ContextPopUpBG.h),wrap="left",foreColour=colors.white})
  290.  
  291. local ContextPopUpTOPLABEL = ContextPopUpBG:add("text",{w=99,h=1,x=0,y=1,text="== Command Center "..string.rep("=",30),foreColour=nations[nationSelectedForGUI]["Color"]})
  292. local ContextPopUpSTATUSLABEL = ContextPopUpBG:add("text",{w=15,h=7,x=1,y=2,text="Doing nothing this turn.",warp="left",foreColour=nations[nationSelectedForGUI]["Color"]})
  293. local ContextPopUpDIVIDER = ContextPopUpBG:add("text",{w=1,h=10,x=16,y=1,text=string.rep("| ",30),foreColour=nations[nationSelectedForGUI]["Color"]})
  294.  
  295. local ContextPopUpSTATUS1 = ContextPopUpBG:add("text",{w=25,h=1,x=1,y=6,text="",warp="left",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  296. local ContextPopUpSTATUS2 = ContextPopUpBG:add("text",{w=25,h=1,x=1,y=8,text="",warp="left",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  297. local ContextPopUpSTATUS3 = ContextPopUpBG:add("text",{w=25,h=1,x=1,y=10,text="",warp="left",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  298.  
  299. local ContextPopUpLabel1 = ContextPopUpBG:add("text",{w=25,h=1,x=35-13,y=2,text="Raise DEFCON",wrap="left",foreColour=nations[nationSelectedForGUI]["Color"],backColour=colors.white})
  300. local ContextPopUpButton1 = ContextPopUpBG:add("button",{w=10,h=1,x=22,y=3,text="Activate",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  301. local ContextPopUpHelp1 = ContextPopUpBG:add("button",{w=0,h=1,x=34,y=3,text="?",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  302.  
  303. local ContextPopUpLabel2 = ContextPopUpBG:add("text",{w=25,h=1,x=35-14,y=5,text="Spy Satellite",wrap="left",foreColour=nations[nationSelectedForGUI]["Color"],backColour=colors.white})
  304. local ContextPopUpButton2 = ContextPopUpBG:add("button",{w=10,h=1,x=22,y=6,text="Activate",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  305. local ContextPopUpHelp2 = ContextPopUpBG:add("button",{w=0,h=1,x=34,y=6,text="?",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  306.  
  307. local ContextPopUpLabel3 = ContextPopUpBG:add("text",{w=25,h=1,x=35-16,y=8,text="Lobby for Funds",wrap="left",foreColour=nations[nationSelectedForGUI]["Color"],backColour=colors.white})
  308. local ContextPopUpButton3 = ContextPopUpBG:add("button",{w=10,h=1,x=22,y=9,text="Activate",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  309. local ContextPopUpHelp3 = ContextPopUpBG:add("button",{w=0,h=1,x=34,y=9,text="?",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  310.  
  311. local ContextPopUpLabel4 = ContextPopUpBG:add("text",{w=25,h=1,x=35-14,y=11,text="UNUSED",wrap="left",foreColour=nations[nationSelectedForGUI]["Color"],backColour=colors.white})
  312. local ContextPopUpButton4 = ContextPopUpBG:add("button",{w=10,h=1,x=22,y=12,text="Activate",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  313. local ContextPopUpHelp4 = ContextPopUpBG:add("button",{w=0,h=1,x=34,y=12,text="?",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  314.  
  315. local ContextPopUpEXIT = ContextPopUpBG:add("button",{w=6,h=1,x=2,y="90%",text="Exit",foreColour=colors.white,backColour=nations[nationSelectedForGUI]["Color"]})
  316.  
  317. ContextPopUpEXIT.onclick = function()
  318. ContextDisabled = false
  319. ContextPopUpBG.state = false
  320. end
  321.  
  322. ContextPopUpBG.state = false
  323.  
  324. local function ContextPopUp(context)
  325. ContextDisabled = true
  326. ContextPanel.x = -100
  327. ContextPanel.y = -100
  328.  
  329. ContextPopUpTOPLABEL.text = "== "..context["topLabel"].." "..string.rep("=",30)
  330.  
  331. if context["info"] then
  332. ContextPopUpSTATUSLABEL.text = context["info"]
  333. else
  334. ContextPopUpSTATUSLABEL.text = ""
  335. end
  336.  
  337. if context["status1"] then
  338. ContextPopUpSTATUS1.text = context["status1"]
  339. else
  340. ContextPopUpSTATUS1.text = ""
  341. end
  342.  
  343. if context["status2"] then
  344. ContextPopUpSTATUS2.text = context["status2"]
  345. else
  346. ContextPopUpSTATUS2.text = ""
  347. end
  348.  
  349. if context["status3"] then
  350. ContextPopUpSTATUS3.text = context["status3"]
  351. else
  352. ContextPopUpSTATUS3.text = ""
  353. end
  354.  
  355. if context["button1"] then
  356. ContextPopUpLabel1.text = context["button1"]["label1"]
  357. ContextPopUpLabel1.x = 34-string.len(context["button1"]["label1"])
  358. ContextPopUpButton1.text = context["button1"]["label2"]
  359. ContextPopUpButton1.onclick = function() context["button1"]["function"]() end
  360. ContextPopUpHelp1.onclick = function() context["button1"]["help"]() end
  361.  
  362. ContextPopUpLabel1.state = "_ALL"
  363. ContextPopUpButton1.state = "_ALL"
  364. ContextPopUpHelp1.state = "_ALL"
  365. else
  366. ContextPopUpLabel1.state = false
  367. ContextPopUpButton1.state = false
  368. ContextPopUpHelp1.state = false
  369. end
  370.  
  371. if context["button2"] then
  372. ContextPopUpLabel2.text = context["button2"]["label1"]
  373. ContextPopUpLabel2.x = 34-string.len(context["button2"]["label1"])
  374. ContextPopUpButton2.text = context["button2"]["label2"]
  375. ContextPopUpButton2.onclick = function() context["button2"]["function"]() end
  376. ContextPopUpHelp2.onclick = function() context["button2"]["help"]() end
  377.  
  378. ContextPopUpLabel2.state = "_ALL"
  379. ContextPopUpButton2.state = "_ALL"
  380. ContextPopUpHelp2.state = "_ALL"
  381. else
  382. ContextPopUpLabel2.state = false
  383. ContextPopUpButton2.state = false
  384. ContextPopUpHelp2.state = false
  385. end
  386.  
  387. if context["button3"] then
  388. ContextPopUpLabel3.text = context["button3"]["label1"]
  389. ContextPopUpLabel3.x = 34-string.len(context["button3"]["label1"])
  390. ContextPopUpButton3.text = context["button3"]["label2"]
  391. ContextPopUpButton3.onclick = function() context["button3"]["function"]() end
  392. ContextPopUpHelp3.onclick = function() context["button3"]["help"]() end
  393.  
  394. ContextPopUpLabel3.state = "_ALL"
  395. ContextPopUpButton3.state = "_ALL"
  396. ContextPopUpHelp3.state = "_ALL"
  397. else
  398. ContextPopUpLabel3.state = false
  399. ContextPopUpButton3.state = false
  400. ContextPopUpHelp3.state = false
  401. end
  402.  
  403. if context["button4"] then
  404. ContextPopUpLabel4.text = context["button4"]["label1"]
  405. ContextPopUpLabel4.x = 34-string.len(context["button4"]["label1"])
  406. ContextPopUpButton4.text = context["button4"]["label2"]
  407. ContextPopUpButton4.onclick = function() context["button4"]["function"]() end
  408. ContextPopUpHelp4.onclick = function() context["button4"]["help"]() end
  409.  
  410. ContextPopUpLabel4.state = "_ALL"
  411. ContextPopUpButton4.state = "_ALL"
  412. ContextPopUpHelp4.state = "_ALL"
  413. else
  414. ContextPopUpLabel4.state = false
  415. ContextPopUpButton4.state = false
  416. ContextPopUpHelp4.state = false
  417. end
  418.  
  419. if not context["color"] then
  420. context["color"] = nations[nationSelectedForGUI]["Color"]
  421. end
  422. ContextPopUpTOPLABEL.foreColour = context["color"]
  423. ContextPopUpSTATUSLABEL.foreColour = context["color"]
  424. ContextPopUpDIVIDER.foreColour = context["color"]
  425. ContextPopUpSTATUS1.foreColour = colors.white
  426. ContextPopUpSTATUS2.foreColour = colors.white
  427. ContextPopUpSTATUS3.foreColour = colors.white
  428. ContextPopUpSTATUS1.backColour = context["color"]
  429. ContextPopUpSTATUS2.backColour = context["color"]
  430. ContextPopUpSTATUS3.backColour = context["color"]
  431. ContextPopUpLabel1.foreColour = context["color"]
  432. ContextPopUpButton1.foreColour = colors.white
  433. ContextPopUpButton1.backColour = context["color"]
  434. ContextPopUpHelp1.backColour = context["color"]
  435. ContextPopUpHelp1.foreColour = colors.white
  436. ContextPopUpLabel2.foreColour = context["color"]
  437. ContextPopUpButton2.foreColour = colors.white
  438. ContextPopUpButton2.backColour = context["color"]
  439. ContextPopUpHelp2.backColour = context["color"]
  440. ContextPopUpHelp2.foreColour = colors.white
  441. ContextPopUpLabel3.foreColour = context["color"]
  442. ContextPopUpButton3.foreColour = colors.white
  443. ContextPopUpButton3.backColour = context["color"]
  444. ContextPopUpHelp3.backColour = context["color"]
  445. ContextPopUpHelp3.foreColour = colors.white
  446. ContextPopUpLabel4.foreColour = context["color"]
  447. ContextPopUpButton4.foreColour = colors.white
  448. ContextPopUpButton4.backColour = context["color"]
  449. ContextPopUpHelp4.backColour = context["color"]
  450. ContextPopUpHelp4.foreColour = colors.white
  451. ContextPopUpEXIT.foreColour = colors.white
  452. ContextPopUpEXIT.backColour = context["color"]
  453.  
  454. ContextPopUpBG.state = "_ALL"
  455. end
  456.  
  457. local function FlashContextPopUp()
  458. ContextPopUpBG.backColour = nations[nationSelectedForGUI]["Color"]
  459. ContextPopUpTOPLABEL.backColour = nations[nationSelectedForGUI]["Color"]
  460. ContextPopUpSTATUSLABEL.backColour = nations[nationSelectedForGUI]["Color"]
  461. ContextPopUpDIVIDER.backColour = nations[nationSelectedForGUI]["Color"]
  462. ContextPopUpLabel1.backColour = nations[nationSelectedForGUI]["Color"]
  463. end
  464.  
  465. local function renderPop(pop)
  466. if not pop then pop = 0 end
  467. if pop >= 1000 then -- K
  468. if pop >= 1000000 then -- M
  469. pop = math.floor(pop / 100000)/10
  470. pop = tostring(pop).."M"
  471. return pop
  472. else
  473. pop = math.floor(pop / 1000)
  474. pop = tostring(pop).."K"
  475. return pop
  476. end
  477. else
  478. return pop
  479. end
  480. end
  481.  
  482. local AlertTimer = 0
  483. local AlertPanel = cobalt.ui.new({w=51,h=3,x=1,y=math.floor(19/2),state=false})
  484. local AlertText = AlertPanel:add("text",{w=51,h=1,y=2,text="",wrap="center"})
  485. local function Alert(text,color,timer)
  486. if not timer then timer = 10 end
  487. if not color then color = nations[nationSelectedForGUI]["Color"] end
  488. AlertPanel.backColour = color
  489. AlertText.backColour = color
  490. AlertText.foreColour = colors.white
  491. AlertText.text = text
  492. AlertPanel.state = "_ALL"
  493. AlertText.state = "_ALL"
  494. AlertTimer = timer
  495. end
  496.  
  497. local Animations = {}
  498.  
  499. local function FireNuke(from,to,playerControlled)
  500. from.Nukes = from.Nukes - 1
  501. ContextOverride = nil
  502. ContextDisabled = true
  503.  
  504. if playerControlled then nationSelectedForGUI = nationSelectedForPlaying else nationSelectedForGUI = nationSelectedForFighting end
  505. table.insert(Animations,{
  506. ["Type"] = "NukeAway",
  507. ["X"] = from.X,
  508. ["Y"] = from.Y,
  509. ["step"] = 0,
  510. ["stepLimit"] = 10,
  511. ["Function"] = function()
  512. if playerControlled then nationSelectedForGUI = nationSelectedForFighting else nationSelectedForGUI = nationSelectedForPlaying end
  513. if to.Cruise > 0 then
  514. table.insert(Animations,{
  515. ["Type"] = "NukeFailure",
  516. ["X"] = to.X,
  517. ["Y"] = to.Y,
  518. ["step"] = 0,
  519. ["stepLimit"] = 10,
  520. ["Function"] = function()
  521. to.Cruise = to.Cruise - 1
  522.  
  523. if playerControlled then ContextDisabled = false for k,v in pairs(nations[nationSelectedForPlaying]["Cities"]) do v["Icon"] = v["DefaultIcon"] end end
  524. if not playerControlled then AIPaused = false end
  525. end
  526. })
  527. else
  528. table.insert(Animations,{
  529. ["Type"] = "NukeSuccess",
  530. ["X"] = to.X,
  531. ["Y"] = to.Y,
  532. ["step"] = 0,
  533. ["stepLimit"] = 20,
  534. ["Function"] = function()
  535. to.Population = to.Population / 2
  536. to.Nukes = math.floor(to.Nukes / 2)
  537.  
  538. if playerControlled then ContextDisabled = false for k,v in pairs(nations[nationSelectedForPlaying]["Cities"]) do v["Icon"] = v["DefaultIcon"] end end
  539. if not playerControlled then AIPaused = false end
  540. end
  541. })
  542. end
  543. end
  544. })
  545. end
  546.  
  547. local ContextMenu = {
  548. ["You"] = { -- Clicking on your own cities
  549. --[[[1] = {
  550. ["Text"] = "Build Nuke",
  551. ["Function"] = function(city)
  552. city.Icon = "N"
  553. city.Work = "BuildNuke"
  554. end,
  555. },
  556. [2] = {
  557. ["Text"] = "Build Cruise",
  558. ["Function"] = function(city)
  559. city.Icon = "C"
  560. city.Work = "BuildCruise"
  561. end,
  562. },]]--
  563. --[[[1] = {
  564. ["Text"] = "Build..",
  565. ["Function"] = function(city)
  566. city.Icon = "N"
  567. city.Work = "BuildNuke"
  568. end,
  569. },]]--
  570. [1] = {
  571. ["Text"] = "View",
  572. ["Function"] = function(cityName,city)
  573. ContextPopUp({
  574. ["topLabel"] = cityName,
  575. ["info"] = "The city bustles with life.",
  576. ["status1"] = "Alive: "..renderPop(city.Population),
  577. ["status2"] = "Dead: "..renderPop(city.Dead),
  578. ["status3"] = "$/Turn: "..renderPop((city["Population"] /(DEFCON/5) ) /5),
  579. ["color"] = nations[nationSelectedForGUI]["Color"],
  580. })
  581. end,
  582. },
  583.  
  584. ["command"] = {
  585. [1] = {
  586. ["Text"] = "View",
  587. ["Function"] = function(baseName,base)
  588. local t = {
  589. ["topLabel"] = "Command Center",
  590. ["button1"] = {
  591. ["label1"] = "Raise DEFCON",
  592. ["label2"] = "Activate",
  593. ["function"] = function()
  594. if DEFCON > 1 then
  595. ContextPopUpSTATUSLABEL.text = "Raising the DEFCON level.."
  596. base.Work = "RaiseDEFCON"
  597. else
  598. ContextPopUpSTATUSLABEL.text = "We're already in a nuclear war!"
  599. end
  600. end,
  601. ["help"] = function()
  602.  
  603. end,
  604. },
  605. }
  606.  
  607. if base.Work == "RaiseDEFCON" then
  608. t["info"] = "Raising the DEFCON level.."
  609. else
  610. t["info"] = "Sitting idle. Assign work for us to do!"
  611. end
  612. ContextPopUp(t)
  613. end,
  614. },
  615. }
  616. },
  617. ["Enemy"] = { -- Clicking on enemy cities
  618. [1] = {
  619. ["Text"] = "Fire Nuke",
  620. ["Function"] = function(cityName,city)
  621. ContextPanel.x = -100
  622. ContextPanel.y = -100
  623. ContextDisabled = true
  624.  
  625. for k,v in pairs(nations[nationSelectedForPlaying]["Cities"]) do
  626. v["Icon"] = tostring(v["Nukes"])
  627. end
  628.  
  629. Alert("Select a friendly city to fire from.",nations[nationSelectedForPlaying]["Color"])
  630.  
  631. ContextOverride = function(city2)
  632. if city2.Nukes > 0 then
  633. FireNuke(city2,city,true)
  634. end
  635. end
  636. end,
  637. }
  638. },
  639. }
  640.  
  641. --[[for k,v in pairs(ContextButtons) do
  642. v.onclick = function()
  643. if nationSelectedForGUI == nationSelectedForPlaying then
  644. if ContextMenu["You"][k] then
  645. ContextMenu["You"][k]["Function"](ContextSelected)
  646. end
  647. else
  648. if ContextMenu["Enemy"][k] then
  649. ContextMenu["Enemy"][k]["Function"](ContextSelected)
  650. end
  651. end
  652. end
  653. end]]--
  654.  
  655. local MainPanel = cobalt.ui.new({w=4,h=1})
  656.  
  657. local SwapViewButton = MainPanel:add("button",{w=4,h=1,text="View",foreColour = colors.white,backColour = colors.grey})
  658. SwapViewButton.onclick = function()
  659. ContextSelected = nil
  660. ContextPanel.x = -100
  661. ContextPanel.y = -100
  662. if nationSelectedForPlaying == nationSelectedForGUI then
  663. nationSelectedForGUI = nationSelectedForFighting
  664. else
  665. nationSelectedForGUI = nationSelectedForPlaying
  666. end
  667. end
  668.  
  669. local MX,MY = 0,0
  670.  
  671. local function checkLegalBuildingSite(x,y)
  672. if nations[nationSelectedForGUI] and nations[nationSelectedForGUI]["Color"] then
  673. if (cobalt.application.view.buffer[((y - 1) * 51 + x) * 3 - 1] == nations[nationSelectedForGUI]["Color"]) or (cobalt.application.view.buffer[((y - 1) * 51 + x) * 3 - 1] == colors.black) or (y >= 18 and x <= 14) then
  674. return false
  675. else
  676. return true
  677. end
  678. end
  679. end
  680.  
  681. local AISteps = {}
  682. --[[local function NextTurn()
  683. -- Enemy AI's turn
  684. AINotDone = true
  685. ContextDisabled = true
  686. nationSelectedForTurn = nationSelectedForFighting
  687. nationSelectedForGUI = nationSelectedForFighting
  688. for k,v in pairs(nations[nationSelectedForFighting]["Cities"]) do
  689. if v.Work == "BuildCruise" then
  690. v.Cruise = v.Cruise + 1
  691. elseif v.Work == "BuildNuke" then
  692. v.Nukes = v.Nukes + 1
  693. end
  694.  
  695. if v.Cruise <= 0 then
  696. v.Work = "BuildCruise"
  697. elseif v.Cruise <= 1 and v.Population > 500000 then
  698. v.Work = "BuildCruise"
  699. else
  700. v.Work = "BuildNuke"
  701. end
  702.  
  703. if v.Nukes > 0 then
  704. local highest = nil
  705. for p,b in pairs(nations[nationSelectedForPlaying]["Cities"]) do
  706. if highest == nil then highest = b else
  707. if b.Population > highest.Population then highest = b end
  708. end
  709. end
  710. FireNuke(v,b,false)
  711. end
  712.  
  713. v.Icon = v.DefaultIcon
  714. end
  715.  
  716. -- Enemy turn over
  717. turn = turn + 1
  718. nationSelectedForTurn = nationSelectedForPlaying
  719. nationSelectedForGUI = nationSelectedForPlaying
  720. ContextDisabled = false
  721.  
  722. for k,v in pairs(nations[nationSelectedForPlaying]["Cities"]) do
  723. if v.Work == "BuildCruise" then
  724. v.Cruise = v.Cruise + 1
  725. elseif v.Work == "BuildNuke" then
  726. v.Nukes = v.Nukes + 1
  727. end
  728. v.Icon = v.DefaultIcon
  729. v.Work = nil
  730. end
  731. end]]--
  732.  
  733. local NextTurnPanel = cobalt.ui.new({w = 14,h = 2, x = 1, y=18,backColour = colors.black})
  734. local NextTurnButton = NextTurnPanel:add("button",{w=9,h=1,y=2,text="Next Turn"})
  735. NextTurnButton.onclick = function()
  736. if ContextPopUpBG.state == "_ALL" then
  737. FlashContextPopUp()
  738. elseif StrategicCommandCenterPlaced then
  739. TURNOVER = true
  740. else
  741. Alert("Build your strategic command center",nations[nationSelectedForPlaying]["Color"])
  742. end
  743. end
  744.  
  745. local CommandCenterPanel = cobalt.ui.new({w=51,h=19,backColour = colors.black,state=false})
  746. local CommandCenterMapBackDrop = CommandCenterPanel:add("panel",{x=2,y=math.floor(19/2)-3,w=27,h=12,backColour=colors.black})
  747.  
  748. local CommandCenterDEFCON5 = CommandCenterPanel:add("panel",{w=3,h=2,x=3,y=3,foreColour = colors.white,backColour = colors.blue})
  749. local CommandCenterDEFCON5Text = CommandCenterDEFCON5:add("text",{w=1,h=1,x=1,y=1,text="5",foreColour=colors.white})
  750.  
  751. local CommandCenterDEFCON4 = CommandCenterPanel:add("panel",{w=3,h=2,x=7,y=3,foreColour = colors.white,backColour = colors.green})
  752. local CommandCenterDEFCON4Text = CommandCenterDEFCON4:add("text",{w=1,h=1,x=1,y=1,text="4",foreColour=colors.white})
  753.  
  754. local CommandCenterDEFCON3 = CommandCenterPanel:add("panel",{w=3,h=2,x=11,y=3,foreColour = colors.white,backColour = colors.orange})
  755. local CommandCenterDEFCON3Text = CommandCenterDEFCON3:add("text",{w=1,h=1,x=1,y=1,text="3",foreColour=colors.white})
  756.  
  757. local CommandCenterDEFCON2 = CommandCenterPanel:add("panel",{w=3,h=2,x=15,y=3,foreColour = colors.white,backColour = colors.red})
  758. local CommandCenterDEFCON2Text = CommandCenterDEFCON2:add("text",{w=1,h=1,x=1,y=1,text="2",foreColour=colors.white})
  759.  
  760. local CommandCenterDEFCON1 = CommandCenterPanel:add("panel",{w=3,h=2,x=19,y=3,foreColour = colors.white,backColour = colors.white})
  761. local CommandCenterDEFCON1Text = CommandCenterDEFCON1:add("text",{w=1,h=1,x=1,y=1,text="1",foreColour=colors.black})
  762.  
  763. local CommandCenterDEFCONLabelPanel = CommandCenterPanel:add("panel",{w=19,h=1,x=3,y=2,foreColour=colors.white,backColour=colors.black})
  764. local CommandCenterDEFCONLabel = CommandCenterDEFCONLabelPanel:add("text",{w=19,h=1,x=1,y=1,text="D E F C O N",foreColour=colors.white,backColour=colors.black,wrap="center"})
  765.  
  766. local CommandCenterBack = CommandCenterPanel:add("button",{w=10,h=1,y=19,text="Back to Map",foreColour = colors.white,backColour = colors.grey})
  767.  
  768. local function CommandCenterGUIUpdate(bool)
  769. if bool then
  770. AlertPanel.state = false
  771. AlertText.state = false
  772. CommandCenterGUI = true
  773. ContextDisabled = true
  774.  
  775. NextTurnPanel.state = false
  776. MainPanel.state = false
  777.  
  778. CommandCenterPanel.state = "_ALL"
  779. CommandCenterMapBackDrop.backColour = nations[nationSelectedForTurn]["Color"]
  780. CommandCenterMapBackDrop.state = "_ALL"
  781. CommandCenterBack.state = "_ALL"
  782.  
  783. CommandCenterDEFCON5.backColour = colors.gray
  784. CommandCenterDEFCON4.backColour = colors.gray
  785. CommandCenterDEFCON3.backColour = colors.gray
  786. CommandCenterDEFCON2.backColour = colors.gray
  787. CommandCenterDEFCON1.backColour = colors.gray
  788.  
  789. CommandCenterDEFCON5Text.backColour = colors.gray
  790. CommandCenterDEFCON4Text.backColour = colors.gray
  791. CommandCenterDEFCON3Text.backColour = colors.gray
  792. CommandCenterDEFCON2Text.backColour = colors.gray
  793. CommandCenterDEFCON1Text.backColour = colors.gray
  794.  
  795. if DEFCON == 5 then
  796. CommandCenterDEFCON5.backColour = colors.blue
  797. CommandCenterDEFCON5Text.backColour = colors.blue
  798. elseif DEFCON == 4 then
  799. CommandCenterDEFCON4.backColour = colors.green
  800. CommandCenterDEFCON4Text.backColour = colors.green
  801. elseif DEFCON == 3 then
  802. CommandCenterDEFCON3.backColour = colors.orange
  803. CommandCenterDEFCON3Text.backColour = colors.orange
  804. elseif DEFCON == 2 then
  805. CommandCenterDEFCON2.backColour = colors.red
  806. CommandCenterDEFCON2Text.backColour = colors.red
  807. elseif DEFCON == 1 then
  808. CommandCenterDEFCON1.backColour = colors.white
  809. CommandCenterDEFCON1Text.backColour = colors.white
  810. end
  811. else
  812. CommandCenterGUI = false
  813. ContextDisabled = false
  814.  
  815. NextTurnPanel.state = "_ALL"
  816. MainPanel.state = "_ALL"
  817.  
  818. CommandCenterPanel.state = false
  819. CommandCenterMapBackDrop.state = false
  820. CommandCenterBack.state = false
  821. end
  822. end
  823.  
  824. CommandCenterBack.onclick = function()
  825. CommandCenterGUIUpdate(false)
  826. end
  827.  
  828. local CommandCenterButton = NextTurnPanel:add("button",{w=14,h=1,text="Command Center",foreColour = colors.white,backColour = colors.grey})
  829. CommandCenterButton.onclick = function()
  830. if StrategicCommandCenterPlaced then
  831. CommandCenterGUIUpdate(true)
  832. else
  833. Alert("Build your strategic command center",nations[nationSelectedForPlaying]["Color"])
  834. end
  835. end
  836.  
  837. local function resolveTurn()
  838. nationSelectedForGUI = nationSelectedForTurn
  839. CommandCenterGUI = false
  840. for k,v in pairs(nations[nationSelectedForTurn]["Buildings"]) do
  841. if v.Work == "BuildCruise" then
  842. v.Cruise = v.Cruise + 1
  843. elseif v.Work == "BuildNuke" then
  844. v.Nukes = v.Nukes + 1
  845. elseif v.Work == "RaiseDEFCON" then
  846. if DEFCON > 1 then
  847. CommandCenterGUI = true
  848. CommandCenterGUIUpdate(true)
  849. cobalt.application.view:clear(" ", cobalt.application.backColour, cobalt.application.foreColour );
  850. cobalt.draw()
  851. cobalt.application.view:render()
  852. sleep(0.5)
  853. DEFCON = DEFCON - 1
  854. CommandCenterGUIUpdate(true)
  855. cobalt.application.view:clear(" ", cobalt.application.backColour, cobalt.application.foreColour );
  856. cobalt.draw()
  857. cobalt.application.view:render()
  858. sleep(0.5)
  859. CommandCenterGUI = false
  860. CommandCenterGUIUpdate(false)
  861. cobalt.application.view:clear(" ", cobalt.application.backColour, cobalt.application.foreColour );
  862. cobalt.draw()
  863. cobalt.application.view:render()
  864.  
  865. if DEFCON <= 1 then v.Work = false end
  866. elseif DEFCON <= 1 then
  867. v.Work = false
  868. end
  869. end
  870. end
  871.  
  872. local incomeThisTurn = 0
  873. for k,v in pairs(nations[nationSelectedForTurn]["Cities"]) do
  874. incomeThisTurn = incomeThisTurn + (v["Population"] /(DEFCON/5) ) /5
  875. end
  876. nations[nationSelectedForTurn]["Resources"] = nations[nationSelectedForTurn]["Resources"] + incomeThisTurn
  877. end
  878.  
  879. local AISteps = {}
  880. function cobalt.update( dt )
  881. tick = tick + dt
  882. if tick >= tickRate then
  883. tick = 0
  884. -- Do a game update
  885.  
  886. turnStringTICK = turnStringTICK - 1
  887. if turnStringTICK <= 0 then
  888. turnStringTICK = 20
  889. if string.sub(turnString,1,1) == "T" then
  890. if nationSelectedForTurn == nationSelectedForPlaying then
  891. turnString = "Your turn"
  892. else
  893. turnString = "Enemy's turn"
  894. end
  895. else
  896. turnString = "Turn "..tostring(turn)
  897. end
  898. end
  899.  
  900. AlertTimer = AlertTimer - 1
  901. if AlertTimer <= 0 then
  902. AlertPanel.state = false
  903. AlertText.state = false
  904. end
  905.  
  906. local deleteAnim = {}
  907. for k,v in pairs(Animations) do
  908. v.step = v.step + 1
  909. if v.step >= v.stepLimit then
  910. v.Function()
  911. table.insert(deleteAnim,k)
  912. end
  913. end
  914. for k,v in pairs(deleteAnim) do
  915. Animations[v] = nil
  916. end
  917.  
  918. if TURNOVER == true then -- Next turn
  919. TURNOVER = false
  920. nationSelectedForTurn = nationSelectedForFighting
  921. end
  922.  
  923. if nationSelectedForTurn == nationSelectedForFighting and AIPaused == false then -- AI step
  924. nationSelectedForGUI = nationSelectedForFighting
  925. if #AISteps == 0 then
  926. resolveTurn()
  927.  
  928. table.insert(AISteps,{}) -- To stop infinite turn cycles
  929.  
  930. --[[for k,v in pairs(nations[nationSelectedForFighting]["Cities"]) do
  931. if v.Cruise <= 0 then
  932. table.insert(AISteps,{
  933. ["BuildCruise"] = v
  934. })
  935. --v.Work = "BuildCruise"
  936. elseif v.Cruise <= 1 and v.Population > 500000 then
  937. table.insert(AISteps,{
  938. ["BuildCruise"] = v
  939. })
  940. --v.Work = "BuildCruise"
  941. else
  942. table.insert(AISteps,{
  943. ["BuildNuke"] = v
  944. })
  945. --v.Work = "BuildNuke"
  946. end
  947.  
  948. if v.Nukes > 0 then
  949. local highest = nil
  950. for p,b in pairs(nations[nationSelectedForPlaying]["Cities"]) do
  951. if highest == nil then highest = b else
  952. if b.Population > highest.Population then highest = b end
  953. end
  954. end
  955. table.insert(AISteps,{
  956. ["FireNuke"] = {
  957. ["from"] = v,
  958. ["to"] = highest
  959. }
  960. })
  961. --FireNuke(v,b,false)
  962. end
  963. end]]--
  964. else
  965. local WorkToDo = AISteps[1]
  966. --[[if WorkToDo["BuildCruise"] then
  967. WorkToDo["BuildCruise"].Work = "BuildCruise"
  968. WorkToDo["BuildCruise"].Icon = "C"
  969. elseif WorkToDo["BuildNuke"] then
  970. WorkToDo["BuildNuke"].Work = "BuildNuke"
  971. WorkToDo["BuildNuke"].Icon = "N"
  972. elseif WorkToDo["FireNuke"] then
  973. AIPaused = true
  974. FireNuke(WorkToDo["FireNuke"]["from"],WorkToDo["FireNuke"]["to"],false)
  975. end]]--
  976.  
  977.  
  978.  
  979. table.remove(AISteps,1)
  980. if #AISteps == 0 then
  981. nationSelectedForGUI = nationSelectedForPlaying
  982. nationSelectedForTurn = nationSelectedForPlaying
  983.  
  984. resolveTurn()
  985. end
  986. end
  987. end
  988. end
  989. end
  990.  
  991. function cobalt.draw()
  992. if CommandCenterGUI then
  993. cobalt.ui.draw()
  994.  
  995. cobalt.application.view:drawSurface(3,math.floor(19/2)-2,worldMap)
  996. else
  997. if nationSelectedForGUI and nations[nationSelectedForGUI] then
  998. local N = nations[nationSelectedForGUI]
  999. cobalt.application.view:drawSurface(1,1,N["Image"])
  1000.  
  1001. if gridToggle then
  1002. for i=1,51 do
  1003. for j=1,19 do
  1004. if checkLegalBuildingSite(i,j) then
  1005. cobalt.graphics.print("+",i,j,nil,N["Color"])
  1006. end
  1007. end
  1008. end
  1009. end
  1010.  
  1011. local Npop = 0
  1012. for k,v in pairs(N["Cities"]) do
  1013. cobalt.graphics.print(v["Icon"],v["X"],v["Y"],N["Color"],colors.white)
  1014. Npop = Npop + v["Population"]
  1015. end
  1016.  
  1017. for k,v in pairs(N["Buildings"]) do
  1018. if v["type"] == "command" then
  1019. cobalt.graphics.print(v["Icon"],v["X"],v["Y"],N["Color"],colors.white)
  1020. end
  1021. end
  1022.  
  1023. cobalt.graphics.center(nationSelectedForGUI.." - "..renderPop(Npop).." - "..renderPop(N["Resources"]).."$",1,0,51,colors.black,colors.white)
  1024. end
  1025.  
  1026. for k,v in pairs(Animations) do
  1027. if v.Type == "NukeAway" then
  1028. cobalt.graphics.print("^",v.X,v.Y-v.step)
  1029. --[[for i=1,v.step do
  1030. cobalt.graphics.print("@",v.X,v.Y-i+1,colors.black,colors.gray)
  1031. end]]--
  1032. elseif v.Type == "NukeSuccess" then
  1033. if v.step <= 10 then
  1034. cobalt.graphics.print("V",v.X,v.Y-10+v.step)
  1035. end
  1036. if v.step > 10 then cobalt.graphics.print("@",v.X,v.Y,colors.red,colors.orange) end
  1037. if v.step > 12 then cobalt.graphics.print("@",v.X-1,v.Y,colors.red,colors.orange) cobalt.graphics.print("@",v.X+1,v.Y,colors.red,colors.orange) cobalt.graphics.print("@",v.X,v.Y-1,colors.red,colors.orange) end
  1038. if v.step > 14 then cobalt.graphics.print("@",v.X,v.Y-2,colors.red,colors.orange) end
  1039. if v.step > 16 then cobalt.graphics.print("@",v.X-1,v.Y-3,colors.red,colors.orange) cobalt.graphics.print("@",v.X,v.Y-3,colors.red,colors.orange) cobalt.graphics.print("@",v.X+1,v.Y-3,colors.red,colors.orange) end
  1040. if v.step > 18 then cobalt.graphics.print("@",v.X-1,v.Y-4,colors.red,colors.orange) cobalt.graphics.print("@",v.X,v.Y-4,colors.red,colors.orange) cobalt.graphics.print("@",v.X+1,v.Y-4,colors.red,colors.orange) end
  1041. elseif v.Type == "NukeFailure" then
  1042. if v.step <= 6 then
  1043. cobalt.graphics.print("V",v.X,v.Y-10+v.step)
  1044. end
  1045. if v.step > 2 and v.step <= 6 then
  1046. cobalt.graphics.print("^",v.X,v.Y-v.step+2)
  1047. end
  1048. if v.step > 6 then
  1049. cobalt.graphics.print("*",v.X,v.Y-4,colors.black,colors.orange)
  1050. end
  1051. end
  1052. end
  1053.  
  1054. cobalt.ui.draw()
  1055.  
  1056. if ContextPopUpBG.backColour == nations[nationSelectedForGUI]["Color"] then
  1057. ContextPopUpBG.backColour = colors.white
  1058. ContextPopUpTOPLABEL.backColour = colors.white
  1059. ContextPopUpSTATUSLABEL.backColour = colors.white
  1060. ContextPopUpDIVIDER.backColour = colors.white
  1061. ContextPopUpLabel1.backColour = colors.white
  1062. end
  1063.  
  1064. cobalt.graphics.print(turnString,52-turnString:len(),1)
  1065.  
  1066. marker["timer"] = marker["timer"] + 1
  1067. if marker["timer"] > 4 then marker["timer"] = 0 end
  1068. if marker["timer"] >= 0 and marker["timer"] <= 2 then
  1069. cobalt.graphics.print("X",marker.x,marker.y,colors.white,colors.black)
  1070. elseif marker["timer"] >= 3 and marker["timer"] <= 4 then
  1071. cobalt.graphics.print("X",marker.x,marker.y,colors.black,colors.white)
  1072. end
  1073.  
  1074. --cobalt.graphics.print(MX..";"..MY,45,19)
  1075. end
  1076. end
  1077.  
  1078. function cobalt.mousepressed( x, y, button )
  1079. MX,MY = x,y
  1080.  
  1081. cobalt.ui.mousepressed(x,y,button)
  1082.  
  1083. if button == 1 then
  1084. if StrategicCommandCenterPlaced then
  1085. if nationSelectedForGUI and nations[nationSelectedForGUI] then
  1086. local N = nations[nationSelectedForGUI]
  1087. local clickOut = true
  1088. if x >= ContextPanel.x and x <= ContextPanel.x+ContextPanel.w-1 and y >= ContextPanel.y and y <= ContextPanel.y+ContextPanel.h-1 then
  1089. clickOut = false
  1090. else
  1091. local cityFound = false
  1092. for k,v in pairs(N["Cities"]) do
  1093. if v["X"] == x and v["Y"] == y then
  1094. ContextPopUp({
  1095. ["topLabel"] = k,
  1096. ["info"] = "The city bustles with life.",
  1097. ["status1"] = "Alive: "..renderPop(v["Population"]),
  1098. ["status2"] = "Dead: "..renderPop(v["Dead"]),
  1099. ["status3"] = "$/Turn: "..renderPop((v["Population"] /(DEFCON/5) ) /5),
  1100. })
  1101. --[[if not ContextDisabled then
  1102. cityFound = true
  1103. ContextSelected = v
  1104. ContextPanel.backColour = nations[nationSelectedForGUI]["Color"]
  1105. ContextLabels[1].text = k
  1106. ContextLabels[2].text = "Pop: "..renderPop(v["Population"])
  1107. ContextLabels[3].text = "Income: "..renderPop( (v["Population"] /(DEFCON/5) ) /5 )
  1108. ContextLabels[4].text = ""
  1109.  
  1110. ContextPanel.backColour = nations[nationSelectedForGUI]["Color"]
  1111. ContextLabels[1].backColour = nations[nationSelectedForGUI]["Color"]
  1112. ContextLabels[2].backColour = nations[nationSelectedForGUI]["Color"]
  1113. ContextLabels[3].backColour = nations[nationSelectedForGUI]["Color"]
  1114. ContextLabels[4].backColour = nations[nationSelectedForGUI]["Color"]
  1115.  
  1116. --ContextLabels[3].text = "Nukes: "..v["Nukes"]
  1117. --ContextLabels[4].text = "Cruise: "..v["Cruise"]
  1118. if nationSelectedForGUI ~= nationSelectedForPlaying then
  1119. for i=1,2 do
  1120. ContextButtons[i]["backColour"] = nations[nationSelectedForGUI]["Color"]
  1121. if ContextMenu["Enemy"][i] then
  1122. ContextButtons[i]["text"] = ContextMenu["Enemy"][i]["Text"]
  1123. else
  1124. ContextButtons[i]["text"] = ""
  1125. end
  1126. ContextButtons[i].onclick = function() ContextMenu["Enemy"][i]["Function"](k,v) end
  1127. end
  1128. else
  1129. for i=1,2 do
  1130. ContextButtons[i]["backColour"] = nations[nationSelectedForGUI]["Color"]
  1131. if ContextMenu["You"][i] then
  1132. ContextButtons[i]["text"] = ContextMenu["You"][i]["Text"]
  1133. else
  1134. ContextButtons[i]["text"] = ""
  1135. end
  1136. ContextButtons[i].onclick = function() ContextMenu["You"][i]["Function"](k,v) end
  1137. end
  1138. end
  1139.  
  1140. local xF = x+1
  1141. local yF = y
  1142.  
  1143. if x+ContextPanel.w >= 51 then xF = x-ContextPanel.w end
  1144. if y+ContextPanel.h >= 19 then yF = y-ContextPanel.h end
  1145.  
  1146. ContextPanel.x = xF
  1147. ContextPanel.y = yF
  1148. clickOut = false
  1149. elseif ContextOverride then
  1150. ContextOverride(v)
  1151. end]]--
  1152. end
  1153. end
  1154. if not cityFound then -- We didn't click on a city.. Maybe we clicked on a military building?
  1155. for k,v in pairs(N["Buildings"]) do
  1156. if v["X"] == x and v["Y"] == y then
  1157. if v["type"] == "command" then
  1158. local t = {
  1159. ["topLabel"] = "Command Center",
  1160. ["button1"] = {
  1161. ["label1"] = "Raise DEFCON",
  1162. ["label2"] = "Activate",
  1163. ["function"] = function()
  1164. if DEFCON > 1 then
  1165. ContextPopUpSTATUSLABEL.text = "Raising the DEFCON level.."
  1166. v["Work"] = "RaiseDEFCON"
  1167. else
  1168. ContextPopUpSTATUSLABEL.text = "We're already in a nuclear war!"
  1169. end
  1170. end,
  1171. ["help"] = function()
  1172.  
  1173. end,
  1174. },
  1175. ["status1"] = "Cond. ",
  1176. }
  1177.  
  1178. if v["health"] == 3 then
  1179. t["status1"] = t["status1"].." Working"
  1180. elseif v["health"] == 2 then
  1181. t["status1"] = t["status1"].." Damaged"
  1182. else
  1183. t["status1"] = t["status1"].." CRITICAL"
  1184. end
  1185.  
  1186. if v["Work"] == "RaiseDEFCON" then
  1187. t["info"] = "Raising the DEFCON level.."
  1188. else
  1189. t["info"] = "Sitting idle. Assign work for us to do!"
  1190. end
  1191. ContextPopUp(t)
  1192. end
  1193. --[[if not ContextDisabled then
  1194. cityFound = true
  1195. ContextSelected = v
  1196. ContextPanel.backColour = nations[nationSelectedForGUI]["Color"]
  1197. if v["type"] == "command" then
  1198. ContextLabels[1].text = "Command"
  1199. ContextLabels[2].text = "Center"
  1200. ContextLabels[3].text = "Health: "..tostring(v["health"])
  1201. ContextLabels[4].text = ""
  1202. end
  1203.  
  1204. ContextPanel.backColour = nations[nationSelectedForGUI]["Color"]
  1205. ContextLabels[1].backColour = nations[nationSelectedForGUI]["Color"]
  1206. ContextLabels[2].backColour = nations[nationSelectedForGUI]["Color"]
  1207. ContextLabels[3].backColour = nations[nationSelectedForGUI]["Color"]
  1208. ContextLabels[4].backColour = nations[nationSelectedForGUI]["Color"]
  1209.  
  1210. --ContextLabels[3].text = "Nukes: "..v["Nukes"]
  1211. --ContextLabels[4].text = "Cruise: "..v["Cruise"]
  1212. if nationSelectedForGUI ~= nationSelectedForPlaying then
  1213. for i=1,2 do
  1214. ContextButtons[i]["backColour"] = nations[nationSelectedForGUI]["Color"]
  1215. if ContextMenu["Enemy"][ v["type"] ] and ContextMenu["Enemy"][ v["type"] ][i] then
  1216. ContextButtons[i]["text"] = ContextMenu["Enemy"][ v["type"] ][i]["Text"]
  1217. else
  1218. ContextButtons[i]["text"] = ""
  1219. end
  1220. ContextButtons[i].onclick = function() ContextMenu["Enemy"][ v["type"] ][i]["Function"](k,v) end
  1221. end
  1222. else
  1223. for i=1,2 do
  1224. ContextButtons[i]["backColour"] = nations[nationSelectedForGUI]["Color"]
  1225. if ContextMenu["You"][ v["type"] ] and ContextMenu["You"][ v["type"] ][i] then
  1226. ContextButtons[i]["text"] = ContextMenu["You"][ v["type"] ][i]["Text"]
  1227. else
  1228. ContextButtons[i]["text"] = ""
  1229. end
  1230. ContextButtons[i].onclick = function() ContextMenu["You"][ v["type"] ][i]["Function"](k,v) end
  1231. end
  1232. end
  1233.  
  1234. local xF = x+1
  1235. local yF = y
  1236.  
  1237. if x+ContextPanel.w >= 51 then xF = x-ContextPanel.w end
  1238. if y+ContextPanel.h >= 19 then yF = y-ContextPanel.h end
  1239.  
  1240. ContextPanel.x = xF
  1241. ContextPanel.y = yF
  1242. clickOut = false
  1243. elseif ContextOverride then
  1244. ContextOverride(v)
  1245. end]]--
  1246. end
  1247. end
  1248. end
  1249. end
  1250. if clickOut then
  1251. ContextSelected = nil
  1252. ContextPanel.x = -100
  1253. ContextPanel.y = -100
  1254. end
  1255. end
  1256. else -- We need to place the strategic command center first
  1257. if nationSelectedForPlaying == nationSelectedForGUI then
  1258. local cityFound = false
  1259. for k,v in pairs(nations[nationSelectedForPlaying]["Cities"]) do
  1260. if v["X"] == x and v["Y"] == y then cityFound = true end
  1261. end
  1262.  
  1263. if cityFound then
  1264. Alert("You can't build in a city. Choose another site.",nations[nationSelectedForPlaying]["Color"],10)
  1265. elseif (StrategicCommandCenterPosition["X"] ~= x) or (StrategicCommandCenterPosition["Y"] ~= y) then
  1266. if checkLegalBuildingSite(x,y) then
  1267. StrategicCommandCenterPosition = {
  1268. ["X"] = x,
  1269. ["Y"] = y,
  1270. }
  1271. --[[table.insert(nations[nationSelectedForPlaying]["Buildings"],{
  1272. ["X"] = x,
  1273. ["Y"] = y,
  1274. ["type"] = "command",
  1275. ["health"] = 3,
  1276. })]]--
  1277. Alert("Please click the site again to confirm build.",nations[nationSelectedForPlaying]["Color"],10)
  1278. marker.x = x
  1279. marker.y = y
  1280. end
  1281. elseif (StrategicCommandCenterPosition["X"] == x) or (StrategicCommandCenterPosition["Y"] == y) then
  1282. table.insert(nations[nationSelectedForPlaying]["Buildings"],{
  1283. ["X"] = x,
  1284. ["Y"] = y,
  1285. ["type"] = "command",
  1286. ["cruise"] = 0,
  1287. ["nukes"] = 0,
  1288. ["health"] = 3,
  1289. ["Icon"] = "A",
  1290. ["Work"] = false,
  1291. })
  1292. marker.x = -10
  1293. marker.y = -10
  1294. StrategicCommandCenterPlaced = true
  1295. end
  1296. end
  1297. end
  1298. elseif button == 2 then
  1299. if StrategicCommandCenterPlaced then
  1300. if not constructionMarker then
  1301. local cityFound = false
  1302. for k,v in pairs(nations[nationSelectedForGUI]["Cities"]) do
  1303. if v["X"] == x and v["Y"] == y then
  1304. cityFound = true
  1305. end
  1306. end
  1307. if not cityFound then
  1308. local buildingFound = false
  1309. for k,v in pairs(nations[nationSelectedForGUI]["Buildings"]) do
  1310. if v["X"] == x and v["Y"] == y then
  1311. buildingFound = true
  1312. end
  1313. end
  1314. if not buildingFound then
  1315. constructionMarker = true
  1316. marker["x"] = x
  1317. marker["y"] = y
  1318. end
  1319. end
  1320. else
  1321. if marker.x == x and marker.y == y then
  1322. local popUpTable = {
  1323. ["topLabel"] = "Construction",
  1324. ["info"] = "Choose something to build.",
  1325. }
  1326. for i,v in ipairs(buildings) do
  1327. popUpTable["button"..i] = {
  1328. ["label2"] = "Build $"..renderPop(v["Cost"]),
  1329. ["label1"] = v["Name"],
  1330. ["function"] = function()
  1331. if nations[nationSelectedForPlaying]["Resources"] >= v["Cost"] then
  1332. nations[nationSelectedForPlaying]["Resources"] = nations[nationSelectedForPlaying]["Resources"] - v["Cost"]
  1333. end
  1334. end,
  1335. }
  1336. end
  1337.  
  1338. ContextPopUp(popUpTable)
  1339.  
  1340. constructionMarker = false
  1341. marker.x = -10
  1342. marker.y = -10
  1343. else
  1344. constructionMarker = false
  1345. marker.x = -10
  1346. marker.y = -10
  1347. end
  1348. end
  1349. else
  1350. Alert("Use the LMB to place your command center.",nations[nationSelectedForPlaying]["Color"],10)
  1351. end
  1352. end
  1353. end
  1354.  
  1355. function cobalt.mousereleased( x, y, button )
  1356. cobalt.ui.mousereleased(x,y,button)
  1357. end
  1358.  
  1359. function cobalt.keypressed( keycode, key )
  1360. if string.lower(key) == "q" then
  1361. if ContextPopUpBG.state == "_ALL" then
  1362. FlashContextPopUp()
  1363. elseif StrategicCommandCenterPlaced then
  1364. if CommandCenterGUI == false then
  1365. CommandCenterGUIUpdate(true)
  1366. else
  1367. CommandCenterGUIUpdate(false)
  1368. end
  1369. else
  1370. AlertPanel.backColour = nations[nationSelectedForPlaying]["Color"]
  1371. AlertText.backColour = nations[nationSelectedForPlaying]["Color"]
  1372. AlertText.foreColour = colors.white
  1373. AlertText.text = "Build your strategic command center"
  1374. AlertPanel.state = "_ALL"
  1375. AlertText.state = "_ALL"
  1376. AlertTimer = 10
  1377. end
  1378. end
  1379.  
  1380. if keycode == 15 then -- tab
  1381. ContextSelected = nil
  1382. ContextPanel.x = -100
  1383. ContextPanel.y = -100
  1384. if nationSelectedForPlaying == nationSelectedForGUI then
  1385. nationSelectedForGUI = nationSelectedForFighting
  1386. else
  1387. nationSelectedForGUI = nationSelectedForPlaying
  1388. end
  1389. end
  1390.  
  1391. if keycode == 57 then -- spacebar
  1392. if ContextPopUpBG.state == "_ALL" then
  1393. FlashContextPopUp()
  1394. elseif StrategicCommandCenterPlaced then
  1395. TURNOVER = true
  1396. else
  1397. AlertPanel.backColour = nations[nationSelectedForPlaying]["Color"]
  1398. AlertText.backColour = nations[nationSelectedForPlaying]["Color"]
  1399. AlertText.foreColour = colors.white
  1400. AlertText.text = "Build your strategic command center"
  1401. AlertPanel.state = "_ALL"
  1402. AlertText.state = "_ALL"
  1403. AlertTimer = 10
  1404. end
  1405. end
  1406.  
  1407. if string.lower(key) == "v" then
  1408. gridToggle = not gridToggle
  1409. end
  1410. end
  1411.  
  1412. function cobalt.keyreleased( keycode, key )
  1413.  
  1414. end
  1415.  
  1416. function cobalt.textinput( t )
  1417.  
  1418. end
  1419.  
  1420. AlertPanel.backColour = nations[nationSelectedForPlaying]["Color"]
  1421. AlertText.backColour = nations[nationSelectedForPlaying]["Color"]
  1422. AlertText.foreColour = colors.white
  1423. AlertText.text = "Build your strategic command center"
  1424. AlertPanel.state = "_ALL"
  1425. AlertText.state = "_ALL"
  1426. AlertTimer = 10
  1427.  
  1428. nations[nationSelectedForPlaying]["Buildings"] = {}
  1429.  
  1430. cobalt.initLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement