Advertisement
JosephMA

Spacewars

Jul 28th, 2016
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.80 KB | None | 0 0
  1. --[[
  2. Spacewars! Simlulate space in space.
  3. Script Version 1.0.4
  4.  
  5. Changes:
  6. Version 1.0.4:
  7. A better fix for Jacob1's mod.
  8. Version 1.0.3:
  9. Fixed issues with Jacob1's mod.
  10. Version 1.0.2:
  11. Fixed deco bar bug.
  12. Version 1.0.1:
  13. Builder spaceships will now clean up space debris and destroy enemy cities.
  14. Minor performance increase for spaceships.
  15. Ships will now mark and move towards rally points.
  16. ]]--
  17.  
  18. sim.gravityMode(1);
  19. sim.airMode(3);
  20. sim.edgeMode(2);
  21.  
  22. --Elements
  23. local continent;
  24. local ocean;
  25. local planet;
  26. local desert;
  27. local rock;
  28. local lasr;
  29. local city;
  30. local explosion;
  31. local missile;
  32. local ship;
  33. local bomber;
  34. local mothership;
  35. local builder;
  36. local nukeMissile;
  37.  
  38. --Settings
  39. local explosionSpread = 0.5;
  40. local fighterLife = 1;
  41. local mothershipLife = 5;
  42. local destroyerLife = 10;
  43.  
  44. --Variables
  45. local interestPoint = {};
  46. interestPoint[0] = {x = -1, y = -1};
  47. interestPoint[1] = {x = -1, y = -1};
  48. interestPoint[2] = {x = -1, y = -1};
  49. interestPoint[3] = {x = -1, y = -1};
  50. interestPoint[4] = {x = -1, y = -1};
  51. interestPoint[5] = {x = -1, y = -1};
  52.  
  53. --Desert
  54. desert = elements.allocate("JosephMA", "DSRT");
  55. elements.element(desert, elements.element(elements.DEFAULT_PT_SAND));
  56. elements.property(desert, "HighTemperature", 673.15);
  57. elements.property(desert, "HighTemperatureTransition", elements.DEFAULT_PT_LAVA);
  58. elements.property(desert, "Name", "DSRT");
  59. elements.property(desert, "Description", "Deserts baren sandy lands.");
  60. elements.property(desert, "MenuSection", 16);
  61.  
  62. --Rock
  63. rock = elements.allocate("JosephMA", "ROCK");
  64. elements.element(rock, elements.element(elements.DEFAULT_PT_BRCK));
  65. elements.property(rock, "HighTemperature", 873.15);
  66. elements.property(rock, "HighTemperatureTransition", elements.DEFAULT_PT_LAVA);
  67. elements.property(rock, "Name", "ROCK");
  68. elements.property(rock, "Description", "Rocks, stony and rocky.");
  69. elements.property(rock, "MenuSection", 16);
  70.  
  71. --City
  72. city = elements.allocate("JosephMA", "CITY");
  73. elements.element(city, elements.element(elements.DEFAULT_PT_BRCK));
  74. elements.property(city, "Color", 0xFF505070);
  75. elements.property(city, "HighTemperature", 873.15);
  76. elements.property(city, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  77. elements.property(city, "Name", "CITY");
  78. elements.property(city, "Description", "A city people live here.");
  79. elements.property(city, "MenuSection", 16);
  80.  
  81. local function cityUpdate(i, x, y, s, n)
  82.     if sim.partProperty(i, "tmp2") == 0 then
  83.         sim.partProperty(i, "tmp", math.random(0, 5));
  84.         sim.partProperty(i, "tmp2", 1);
  85.     end
  86.     local life = sim.partProperty(i, "life");
  87.     if math.random(1, 500) == 1 then
  88.         local part;
  89.         if math.random(1, 1000) == 1 then
  90.             part = sim.partCreate(-1, x + math.random(-25, 25), y + math.random(-25, 25), destroyer);
  91.         elseif math.random(1, 200) == 1 then
  92.             part = sim.partCreate(-1, x + math.random(-25, 25), y + math.random(-25, 25), mothership);
  93.         elseif math.random(1, 2) == 1 then
  94.             part = sim.partCreate(-1, x + math.random(-25, 25), y + math.random(-25, 25), ship);
  95.         elseif math.random(1, 2) == 1 then
  96.             part = sim.partCreate(-1, x + math.random(-25, 25), y + math.random(-25, 25), bomber);
  97.         else
  98.             part = sim.partCreate(-1, x + math.random(-25, 25), y + math.random(-25, 25), builder);
  99.         end
  100.         sim.partProperty(part, "tmp", sim.partProperty(i, "tmp"));
  101.         sim.partProperty(i, "life", life + 1);
  102.     end
  103. end
  104.  
  105. elements.property(city, "Update", cityUpdate);
  106.  
  107. --Continent
  108. continent = elements.allocate("JosephMA", "CONT");
  109. elements.element(continent, elements.element(elements.DEFAULT_PT_PLNT));
  110. elements.property(continent, "HighTemperature", 473.15);
  111. elements.property(continent, "HighTemperatureTransition", rock);
  112. elements.property(continent, "Name", "CONT");
  113. elements.property(continent, "Description", "Continent makes up planets.");
  114. elements.property(continent, "MenuSection", 16);
  115.  
  116. local function continentUpdate(i, x, y, s, n)
  117.     local life = sim.partProperty(i, "life");
  118.     if life > 0 then
  119.         for r in sim.neighbors(x,y,1,1) do
  120.             if math.random(1, 8) == 1 and sim.partProperty(r, "type") == ocean then
  121.                 sim.partChangeType(r, continent);
  122.                 sim.partProperty(r, "life", life - 1);
  123.             end
  124.         end
  125.     elseif math.random(1, 100000000) == 1 then
  126.         for r in sim.neighbors(x,y,1,1) do
  127.             if sim.partProperty(r, "type") == city then
  128.                 sim.partProperty(i, "tmp", sim.partProperty(r, "tmp"));
  129.                 sim.partProperty(i, "tmp2", sim.partProperty(r, "tmp2"));
  130.                 break;
  131.             end
  132.         end
  133.         sim.partChangeType(i, city);
  134.     end
  135. end
  136.  
  137. elements.property(continent, "Update", continentUpdate);
  138.  
  139. --Ocean
  140. ocean = elements.allocate("JosephMA", "OCEN");
  141. elements.element(ocean, elements.element(elements.DEFAULT_PT_WATR));
  142. elements.property(ocean, "HighTemperature", 473.15);
  143. elements.property(ocean, "HighTemperatureTransition", desert);
  144. elements.property(ocean, "Name", "OCEN");
  145. elements.property(ocean, "Description", "Oceans able to substain life on planets.");
  146. elements.property(ocean, "MenuSection", 16);
  147.  
  148. --Planet
  149. planet = elements.allocate("JosephMA", "PLNE");
  150. elements.element(planet, elements.element(elements.DEFAULT_PT_TTAN));
  151. elements.property(planet, "Name", "PLNE");
  152. elements.property(planet, "MenuSection", elem.SC_SPECIAL);
  153. elements.property(planet, "Description", "Creates a randomized planet.");
  154.  
  155. local function planetUpdate(i, x, y, s, n)
  156.     if math.random(1, 20) == 1 then
  157.         sim.partProperty(i, "life", math.random(0, 10));
  158.         sim.partChangeType(i, continent);
  159.     else
  160.         sim.partChangeType(i, ocean);
  161.     end
  162. end
  163.  
  164. elements.property(planet, "Update", planetUpdate);
  165.  
  166. --Laser
  167. laser = elements.allocate("JosephMA", "LASR");
  168. elements.element(laser, elements.element(elements.DEFAULT_PT_PHOT));
  169. elements.property(laser, "Name", "LASR");
  170. elements.property(laser, "Temperature", 295.15);
  171. elements.property(laser, "Description", "Lasers shoot everything up!");
  172. elements.property(laser, "MenuSection", 16);
  173.  
  174. --Explosion
  175. explosion = elements.allocate("JosephMA", "EXPL");
  176. elements.element(explosion, elements.element(elements.DEFAULT_PT_DUST));
  177. elements.property(explosion, "Name", "EXPL");
  178. elements.property(explosion, "Temperature", 673.15);
  179. elements.property(explosion, "Description", "Explosions, boom!");
  180. elements.property(explosion, "MenuSection", elem.SC_EXPLOSIVE);
  181.  
  182. local function explosionUpdate(i, x, y, s, n)
  183.     --Spawn explosion
  184.     local temp = tpt.get_property("temp", i);
  185.     local life = tpt.get_property("life", i);
  186.     local spread;
  187.    
  188.     if life == 0 then
  189.         spread = explosionSpread;
  190.     else
  191.         spread = life;
  192.     end
  193.    
  194.     sim.partKill(x - 1, y);
  195.     sim.partKill(x + 1, y);
  196.     sim.partKill(x - 2, y);
  197.     sim.partKill(x + 2, y);
  198.     sim.partKill(x, y - 1);
  199.     sim.partKill(x, y + 1);
  200.     sim.partKill(x, y - 2);
  201.     sim.partKill(x, y + 2);
  202.     sim.partKill(x - 1, y - 1);
  203.     sim.partKill(x + 1, y - 1);
  204.     sim.partKill(x - 1, y + 1);
  205.     sim.partKill(x + 1, y + 1);
  206.    
  207.     for t=0, 50, 1
  208.     do
  209.         local part = sim.partCreate(-3, x, y, elements.DEFAULT_PT_EMBR);
  210.         tpt.set_property("temp", temp, part);
  211.         tpt.set_property("life", math.random() * 100, part);
  212.         local angle = math.random() * 2.0 * math.pi/spread;
  213.         local v = (math.random()) * 5.0/spread;
  214.         tpt.set_property("vx", v * math.cos(angle), part);
  215.         tpt.set_property("vy", v * math.sin(angle), part);
  216.     end
  217.    
  218.     sim.partKill(i);
  219.     return 1;
  220. end
  221.  
  222. elements.property(explosion, "Update", explosionUpdate);
  223.  
  224. --Missile
  225. missile = elements.allocate("JosephMA", "MISL");
  226. elements.element(missile, elements.element(elements.DEFAULT_PT_PROT));
  227. elements.property(missile, "Name", "MISL");
  228. elements.property(missile, "Colour", 0x5D5D5C);
  229. elements.property(missile, "Description", "Explodes after a set life time!");
  230. elements.property(missile, "MenuSection", 16);
  231.  
  232. local function missileUpdate(i, x, y, s, n)
  233.     sim.partProperty(i, "life", sim.partProperty(i, "life") - 1);
  234.     if sim.partProperty(i, "life") <= 0 then
  235.         local part = sim.partCreate(-1, x, y, explosion);
  236.         sim.partProperty(part, "life", 1);
  237.         sim.partKill(i);
  238.         return 1;
  239.     end
  240.     --Smoke Trail
  241.     local part = sim.partCreate(-1, x, y, elements.DEFAULT_PT_SMKE);
  242.     sim.partProperty(part, "life", 20);
  243. end
  244.  
  245. elements.property(missile, "Update", missileUpdate);
  246.  
  247. --Ship
  248. ship = elements.allocate("JosephMA", "SHIP");
  249. elements.element(ship, elements.element(elements.DEFAULT_PT_PROT));
  250. elements.property(ship, "HighTemperature", 1273.15);
  251. elements.property(ship, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  252. elements.property(ship, "Name", "SHIP");
  253. elements.property(ship, "Description", "Spacecraft fighter shoots lasers.");
  254. elements.property(ship, "MenuSection", elem.SC_SPECIAL);
  255.  
  256. local function shipUpdate(i, x, y, s, n)
  257.     --tmp is current team
  258.     local tmp = sim.partProperty(i, "tmp");
  259.     local life = tpt.get_property("life", i);
  260.     if life == 0 then
  261.         tpt.set_property("life", fighterLife, i);
  262.         life = fighterLife;
  263.     end
  264.     local rand = math.random(1, 150);
  265.     if rand <= 75 then
  266.         for r in sim.neighbors(x,y,1,1) do
  267.             local partType = sim.partProperty(r, "type");
  268.             if partType == laser and sim.partProperty(r, "tmp") ~= tmp then
  269.                 sim.partKill(r);
  270.                 tpt.set_property("life", life - 1, i);
  271.                 life = life - 1;
  272.                 if life - 1 <= -1 then
  273.                     sim.partKill(i);
  274.                     return 1;
  275.                 end
  276.             end
  277.         end
  278.     end
  279.     --Movement
  280.     if math.random(1, 100) == 1 then
  281.         --Move towards point of interest if it exists
  282.         if interestPoint[tmp].x ~= -1 then
  283.             if interestPoint[tmp].x > x then
  284.                 tpt.set_property("vx", math.random(1, 3), i);
  285.             elseif interestPoint[tmp].x < x then
  286.                 tpt.set_property("vx", math.random(-3, -1), i);
  287.             end
  288.             if interestPoint[tmp].y > y then
  289.                 tpt.set_property("vy", math.random(1, 3), i);
  290.             elseif interestPoint[tmp].y < y then
  291.                 tpt.set_property("vy", math.random(-3, -1), i);
  292.             end
  293.         else
  294.             if math.random(1, 2) == 1 then
  295.                 tpt.set_property("vx", math.random(-3, 3), i);
  296.             else
  297.                 tpt.set_property("vy", math.random(-3, 3), i);
  298.             end
  299.         end
  300.     end
  301.     --Shooting
  302.     if rand == 1 then
  303.         for r in sim.neighbors(x,y,5,5) do
  304.             local partType = sim.partProperty(r, "type");
  305.             if (partType == ship or partType == bomber or partType == city or partType == mothership or parType == destroyer or partType == builder) and sim.partProperty(r, "tmp") ~= tmp then
  306.                 local part = sim.partCreate(-3, x, y, laser);
  307.                 tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  308.                 tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  309.                 tpt.set_property("tmp", tmp, part);
  310.                 tpt.set_property("life", 20, part);
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316. elements.property(ship, "Update", shipUpdate);
  317.  
  318. local function shipGraphics(i, colr, colg, colb)
  319.     local r;
  320.     local g;
  321.     local b;
  322.     local tmp = tpt.get_property("tmp", i);
  323.     if tmp == 0 then
  324.         r = 255;
  325.         g = 0;
  326.         b = 0;
  327.     elseif tmp == 1 then
  328.         r = 0;
  329.         g = 255;
  330.         b = 0;
  331.     elseif tmp == 2 then
  332.         r = 0;
  333.         g = 0;
  334.         b = 255;
  335.     elseif tmp == 3 then
  336.         r = 255;
  337.         g = 204;
  338.         b = 0;
  339.     elseif tmp == 4 then
  340.         r = 255;
  341.         g = 255;
  342.         b = 255;
  343.     elseif tmp == 5 then
  344.         r = 153;
  345.         g = 153;
  346.         b = 153;
  347.     end
  348.     return 1,0x00000001,255,r,g,b,255,255,255,255;
  349. end
  350.  
  351. elements.property(ship, "Graphics", shipGraphics);
  352.  
  353. --Bomber
  354. bomber = elements.allocate("JosephMA", "BMBR");
  355. elements.element(bomber, elements.element(elements.DEFAULT_PT_PROT));
  356. elements.property(bomber, "HighTemperature", 1273.15);
  357. elements.property(bomber, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  358. elements.property(bomber, "Name", "BMBR");
  359. elements.property(bomber, "Description", "Spacecraft bomber shoots missiles.");
  360. elements.property(bomber, "MenuSection", elem.SC_SPECIAL);
  361.  
  362. local function bomberUpdate(i, x, y, s, n)
  363.     --tmp is current team
  364.     local tmp = sim.partProperty(i, "tmp");
  365.     local life = tpt.get_property("life", i);
  366.     if life == 0 then
  367.         tpt.set_property("life", fighterLife, i);
  368.         life = fighterLife;
  369.     end
  370.     local rand = math.random(1, 150);
  371.     if rand <= 75 then
  372.         for r in sim.neighbors(x,y,1,1) do
  373.             local partType = sim.partProperty(r, "type");
  374.             if partType == laser and sim.partProperty(r, "tmp") ~= tmp then
  375.                 sim.partKill(r);
  376.                 tpt.set_property("life", life - 1, i);
  377.                 life = life - 1;
  378.                 if life - 1 <= -1 then
  379.                     sim.partKill(i);
  380.                     return 1;
  381.                 end
  382.             end
  383.         end
  384.     end
  385.     --Movement
  386.     if math.random(1, 100) == 1 then
  387.         --Move towards point of interest if it exists
  388.         if interestPoint[tmp].x ~= -1 then
  389.             if interestPoint[tmp].x > x then
  390.                 tpt.set_property("vx", 1, i);
  391.             elseif interestPoint[tmp].x < x then
  392.                 tpt.set_property("vx", -1, i);
  393.             end
  394.             if interestPoint[tmp].y > y then
  395.                 tpt.set_property("vy", 1, i);
  396.             elseif interestPoint[tmp].y < y then
  397.                 tpt.set_property("vy", -1, i);
  398.             end
  399.         else
  400.             if math.random(1, 2) == 1 then
  401.                 tpt.set_property("vx", math.random(-1, 1), i);
  402.             else
  403.                 tpt.set_property("vy", math.random(-1, 1), i);
  404.             end
  405.         end
  406.     end
  407.     --Shooting
  408.     if rand == 1 then
  409.         for r in sim.neighbors(x,y,5,5) do
  410.             local partType = sim.partProperty(r, "type");
  411.             if (partType == ship or partType == bomber or partType == city or partType == mothership or parType == destroyer or partType == builder) and sim.partProperty(r, "tmp") ~= tmp then
  412.                 local part = sim.partCreate(-3, x, y, missile);
  413.                 tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  414.                 tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  415.                 tpt.set_property("life", 20, part);
  416.                 break;
  417.             end
  418.         end
  419.     end
  420. end
  421.  
  422. elements.property(bomber, "Update", bomberUpdate);
  423.  
  424. local function bomberGraphics(i, colr, colg, colb)
  425.     local r;
  426.     local g;
  427.     local b;
  428.     local tmp = tpt.get_property("tmp", i);
  429.     if tmp == 0 then
  430.         r = 255;
  431.         g = 0;
  432.         b = 0;
  433.     elseif tmp == 1 then
  434.         r = 0;
  435.         g = 255;
  436.         b = 0;
  437.     elseif tmp == 2 then
  438.         r = 0;
  439.         g = 0;
  440.         b = 255;
  441.     elseif tmp == 3 then
  442.         r = 255;
  443.         g = 204;
  444.         b = 0;
  445.     elseif tmp == 4 then
  446.         r = 255;
  447.         g = 255;
  448.         b = 255;
  449.     elseif tmp == 5 then
  450.         r = 153;
  451.         g = 153;
  452.         b = 153;
  453.     end
  454.     return 1,0x00000001,255,r,g,b,255,255,255,255;
  455. end
  456.  
  457. elements.property(bomber, "Graphics", bomberGraphics);
  458.  
  459. --Mothership
  460. mothership = elements.allocate("JosephMA", "MOTH");
  461. elements.element(mothership, elements.element(elements.DEFAULT_PT_PROT));
  462. elements.property(mothership, "HighTemperature", 1273.15);
  463. elements.property(mothership, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  464. elements.property(mothership, "Name", "MOTH");
  465. elements.property(mothership, "Description", "Mothership produces ships and fires fast! Also has nukes onboard");
  466. elements.property(mothership, "MenuSection", elem.SC_SPECIAL);
  467.  
  468. local function mothershipUpdate(i, x, y, s, n)
  469.     local tmp = sim.partProperty(i, "tmp");
  470.     --tmp is current team
  471.     local life = tpt.get_property("life", i);
  472.     if life == 0 then
  473.         tpt.set_property("life", mothershipLife, i);
  474.         life = mothershipLife;
  475.     end
  476.     local rand = math.random(1, 60);
  477.     if rand == 60 then
  478.         --Set rally point
  479.         interestPoint[tmp].x = x;
  480.         interestPoint[tmp].y = y;
  481.     end
  482.     if rand <= 30 then
  483.         for r in sim.neighbors(x,y,1,1) do
  484.             local partType = sim.partProperty(r, "type");
  485.             if partType == laser and sim.partProperty(r, "tmp") ~= tmp then
  486.                 sim.partKill(r);
  487.                 tpt.set_property("life", life - 1, i);
  488.                 life = life - 1;
  489.                 if life - 1 <= -1 then
  490.                     sim.partKill(i);
  491.                     return 1;
  492.                 end
  493.             end
  494.         end
  495.     end
  496.     --Movement
  497.     if math.random(1, 100) == 1 then
  498.         if math.random(1, 2) == 1 then
  499.             tpt.set_property("vx", math.random(-1, 1), i);
  500.         else
  501.             tpt.set_property("vy", math.random(-1, 1), i);
  502.         end
  503.     end
  504.     --Shooting
  505.     if rand == 1 then
  506.         for r in sim.neighbors(x,y,10,10) do
  507.             local partType = sim.partProperty(r, "type");
  508.             if (partType == ship or partType == bomber or partType == city or partType == mothership or parType == destroyer or partType == builder) and sim.partProperty(r, "tmp") ~= tmp then
  509.                 if partType ~= city then
  510.                     local part = sim.partCreate(-3, x, y, laser);
  511.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  512.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  513.                     tpt.set_property("tmp", tmp, part);
  514.                     tpt.set_property("life", 20, part);
  515.                     part = sim.partCreate(-3, x - 1, y, laser);
  516.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  517.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  518.                     tpt.set_property("tmp", tmp, part);
  519.                     tpt.set_property("life", 20, part);
  520.                     part = sim.partCreate(-3, x + 1, y, laser);
  521.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  522.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  523.                     tpt.set_property("tmp", tmp, part);
  524.                     tpt.set_property("life", 20, part);
  525.                     part = sim.partCreate(-3, x, y - 1, laser);
  526.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  527.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  528.                     tpt.set_property("tmp", tmp, part);
  529.                     tpt.set_property("life", 20, part);
  530.                     part = sim.partCreate(-3, x, y + 1, laser);
  531.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  532.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  533.                     tpt.set_property("tmp", tmp, part);
  534.                     tpt.set_property("life", 20, part);
  535.                 else
  536.                     --Destroy or assimilate
  537.                     if math.random(1, 2) == 1 then
  538.                         --sim.partChangeType(r, elements.DEFAULT_PT_BOMB);
  539.                         sim.partChangeType(r, rock);
  540.                     else
  541.                         sim.partProperty(r, "tmp", tmp)
  542.                     end
  543.                 end
  544.             end
  545.         end
  546.     end
  547.     --Create more ships
  548.     if math.random(1, 300) == 1 then
  549.         local part;
  550.         if math.random(1, 2) == 1 then
  551.             part = sim.partCreate(-3, x, y, ship);
  552.         else
  553.             part = sim.partCreate(-3, x , y, bomber);
  554.         end
  555.         sim.partProperty(part, "tmp", tmp);
  556.     end
  557. end
  558.  
  559. elements.property(mothership, "Update", mothershipUpdate);
  560.  
  561. local function mothershipGraphics(i, colr, colg, colb)
  562.     local r;
  563.     local g;
  564.     local b;
  565.     local tmp = tpt.get_property("tmp", i);
  566.     if tmp == 0 then
  567.         r = 255;
  568.         g = 0;
  569.         b = 0;
  570.     elseif tmp == 1 then
  571.         r = 0;
  572.         g = 255;
  573.         b = 0;
  574.     elseif tmp == 2 then
  575.         r = 0;
  576.         g = 0;
  577.         b = 255;
  578.     elseif tmp == 3 then
  579.         r = 255;
  580.         g = 204;
  581.         b = 0;
  582.     elseif tmp == 4 then
  583.         r = 255;
  584.         g = 255;
  585.         b = 255;
  586.     elseif tmp == 5 then
  587.         r = 153;
  588.         g = 153;
  589.         b = 153;
  590.     end
  591.     local x = tpt.get_property("x",i);
  592.     local y = tpt.get_property("y",i);
  593.     graphics.fillRect(x - 2, y - 2, 5, 5, r, g, b);
  594. end
  595.  
  596. elements.property(mothership, "Graphics", mothershipGraphics);
  597.  
  598. --Destroyer
  599. destroyer = elements.allocate("JosephMA", "DSTR");
  600. elements.element(destroyer, elements.element(elements.DEFAULT_PT_PROT));
  601. elements.property(destroyer, "HighTemperature", 1273.15);
  602. elements.property(destroyer, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  603. elements.property(destroyer, "Name", "DSTR");
  604. elements.property(destroyer, "Description", "Destroys all worlds.");
  605. elements.property(destroyer, "MenuSection", elem.SC_SPECIAL);
  606.  
  607. local function destroyerUpdate(i, x, y, s, n)
  608.     local tmp = sim.partProperty(i, "tmp");
  609.     --tmp is current team
  610.     local life = tpt.get_property("life", i);
  611.     if life == 0 then
  612.         tpt.set_property("life", destroyerLife, i);
  613.         life = destroyerLife;
  614.     end
  615.     local rand = math.random(1, 30);
  616.     if rand == 30 then
  617.         --Set rally point
  618.         interestPoint[tmp].x = x;
  619.         interestPoint[tmp].y = y;
  620.     end
  621.     if rand <= 15 then
  622.         for r in sim.neighbors(x,y,1,1) do
  623.             local partType = sim.partProperty(r, "type");
  624.             if partType == laser and sim.partProperty(r, "tmp") ~= tmp then
  625.                 sim.partKill(r);
  626.                 tpt.set_property("life", life - 1, i);
  627.                 life = life - 1;
  628.                 if life - 1 <= -1 then
  629.                     sim.partKill(i);
  630.                     return 1;
  631.                 end
  632.             end
  633.         end
  634.     end
  635.     --Movement
  636.     if math.random(1, 100) == 1 then
  637.         if math.random(1, 2) == 1 then
  638.             tpt.set_property("vx", math.random(-1, 1), i);
  639.         else
  640.             tpt.set_property("vy", math.random(-1, 1), i);
  641.         end
  642.     end
  643.     --Shooting
  644.     if rand == 1 then
  645.         for r in sim.neighbors(x,y,10,10) do
  646.             local partType = sim.partProperty(r, "type");
  647.             if (partType == ship or partType == bomber or partType == city or partType == mothership or parType == destroyer or partType == builder) and sim.partProperty(r, "tmp") ~= tmp then
  648.                 if partType ~= city then
  649.                     --Nuke missile
  650.                     local part = sim.partCreate(-3, x, y, nukeMissile);
  651.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  652.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  653.                     --tpt.set_property("tmp", tmp, part);
  654.                     tpt.set_property("life", 40, part);
  655.                 else
  656.                     --Destroy
  657.                     if math.random(1, 2) == 1 then
  658.                         --sim.partChangeType(r, elements.DEFAULT_PT_BOMB);
  659.                         sim.partChangeType(r, rock);
  660.                     end
  661.                 end
  662.             --[[elseif partType == ocean or partType == continent then
  663.                 --Destroy
  664.                 if math.random(1, 10) == 1 then
  665.                     sim.partChangeType(r, elements.DEFAULT_PT_BOMB);
  666.                 else
  667.                     sim.partProperty(r, "temp", 2773.15);
  668.                 end
  669.             elseif partType == desert then
  670.                 --Obliviate
  671.                 sim.partChangeType(r, elements.DEFAULT_PT_PLSM);]]--
  672.             end
  673.         end
  674.     end
  675.     --Create more ships
  676.     if math.random(1, 300) == 1 then
  677.         local part;
  678.         if math.random(1, 2) == 1 then
  679.             part = sim.partCreate(-3, x, y, ship);
  680.         else
  681.             part = sim.partCreate(-3, x , y, bomber);
  682.         end
  683.         sim.partProperty(part, "tmp", tmp);
  684.     end
  685. end
  686.  
  687. elements.property(destroyer, "Update", destroyerUpdate);
  688.  
  689. local function destroyerGraphics(i, colr, colg, colb)
  690.     local r;
  691.     local g;
  692.     local b;
  693.     local tmp = tpt.get_property("tmp", i);
  694.     if tmp == 0 then
  695.         r = 255;
  696.         g = 0;
  697.         b = 0;
  698.     elseif tmp == 1 then
  699.         r = 0;
  700.         g = 255;
  701.         b = 0;
  702.     elseif tmp == 2 then
  703.         r = 0;
  704.         g = 0;
  705.         b = 255;
  706.     elseif tmp == 3 then
  707.         r = 255;
  708.         g = 204;
  709.         b = 0;
  710.     elseif tmp == 4 then
  711.         r = 255;
  712.         g = 255;
  713.         b = 255;
  714.     elseif tmp == 5 then
  715.         r = 153;
  716.         g = 153;
  717.         b = 153;
  718.     end
  719.     local x = tpt.get_property("x",i);
  720.     local y = tpt.get_property("y",i);
  721.     graphics.fillRect(x - 4, y - 4, 9, 9, r, g, b);
  722. end
  723.  
  724. elements.property(destroyer, "Graphics", destroyerGraphics);
  725.  
  726. --Builder
  727. builder = elements.allocate("JosephMA", "BLDR");
  728. elements.element(builder, elements.element(elements.DEFAULT_PT_PROT));
  729. elements.property(builder, "HighTemperature", 1273.15);
  730. elements.property(builder, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  731. elements.property(builder, "Name", "BLDR");
  732. elements.property(builder, "Description", "Spacecraft builder builds space cities.");
  733. elements.property(builder, "MenuSection", elem.SC_SPECIAL);
  734.  
  735. local function builderUpdate(i, x, y, s, n)
  736.     --tmp is current team
  737.     local life = tpt.get_property("life", i);
  738.     if life == 0 then
  739.         tpt.set_property("life", fighterLife, i);
  740.         life = fighterLife;
  741.     end
  742.     --Damage
  743.     if math.random(1, 2) == 1 then
  744.         for r in sim.neighbors(x,y,1,1) do
  745.             local partType = sim.partProperty(r, "type");
  746.             local tmp = sim.partProperty(i, "tmp");
  747.             if partType == laser and sim.partProperty(r, "tmp") ~= tmp then
  748.                 sim.partKill(r);
  749.                 tpt.set_property("life", life - 1, i);
  750.                 life = life - 1;
  751.                 if life - 1 <= -1 then
  752.                     sim.partKill(i);
  753.                     return 1;
  754.                 end
  755.             --Destroy space debris
  756.             elseif partType == elements.DEFAULT_PT_BRMT or partType == elements.DEFAULT_PT_BMTL then
  757.                 sim.partKill(r);
  758.             --Destroy enemy cities
  759.             elseif partType == city and sim.partProperty(r, "tmp") ~= tmp then
  760.                 sim.partKill(r);
  761.             end
  762.         end
  763.     end
  764.     --Movement
  765.     if math.random(1, 100) == 1 then
  766.         if math.random(1, 2) == 1 then
  767.             tpt.set_property("vx", math.random(-3, 3), i);
  768.         else
  769.             tpt.set_property("vy", math.random(-3, 3), i);
  770.         end
  771.     end
  772.     --Create construction
  773.     if math.random(1, 1000) == 1 then
  774.         --City
  775.         if math.random(1, 100) <= 99 then
  776.             sim.partProperty(i, "tmp2", 1);
  777.             sim.partChangeType(i, city);
  778.         --Obliterator cannon
  779.         else
  780.             sim.partChangeType(i, oblitoraterCannon);
  781.         end
  782.     end
  783. end
  784.  
  785. elements.property(builder, "Update", builderUpdate);
  786.  
  787. local function builderGraphics(i, colr, colg, colb)
  788.     local r;
  789.     local g;
  790.     local b;
  791.     local tmp = tpt.get_property("tmp", i);
  792.     if tmp == 0 then
  793.         r = 255;
  794.         g = 0;
  795.         b = 0;
  796.     elseif tmp == 1 then
  797.         r = 0;
  798.         g = 255;
  799.         b = 0;
  800.     elseif tmp == 2 then
  801.         r = 0;
  802.         g = 0;
  803.         b = 255;
  804.     elseif tmp == 3 then
  805.         r = 255;
  806.         g = 204;
  807.         b = 0;
  808.     elseif tmp == 4 then
  809.         r = 255;
  810.         g = 255;
  811.         b = 255;
  812.     elseif tmp == 5 then
  813.         r = 153;
  814.         g = 153;
  815.         b = 153;
  816.     end
  817.     return 1,0x00000001,255,r,g,b,255,255,255,255;
  818. end
  819.  
  820. elements.property(builder, "Graphics", builderGraphics);
  821.  
  822. --Nuke Missile
  823. nukeMissile = elements.allocate("JosephMA", "MSLE");
  824. elements.element(nukeMissile, elements.element(elements.DEFAULT_PT_PROT));
  825. elements.property(nukeMissile, "Name", "MSLE");
  826. elements.property(nukeMissile, "Colour", 0x5D5D5C);
  827. elements.property(nukeMissile, "Description", "Nuke missile, explodes after a set life time!");
  828. elements.property(nukeMissile, "MenuSection", 16);
  829.  
  830. local function nukeMissileUpdate(i, x, y, s, n)
  831.     sim.partProperty(i, "life", sim.partProperty(i, "life") - 1);
  832.     if sim.partProperty(i, "life") <= 0 then
  833.         local part = sim.partCreate(-1, x, y, explosion);
  834.         sim.partProperty(part, "life", 1);
  835.         --Explode
  836.         for r in sim.neighbors(x,y,20,20) do
  837.             local part = sim.partCreate(-3, sim.partProperty(r, "x"), sim.partProperty(r, "y"), elements.DEFAULT_PT_PLSM);
  838.             sim.partProperty(part, "life", 300);
  839.             sim.partKill(r);
  840.         end
  841.         sim.partKill(i);
  842.         return 1;
  843.     end
  844.     --Smoke Trail
  845.     local part = sim.partCreate(-1, x, y, elements.DEFAULT_PT_SMKE);
  846.     sim.partProperty(part, "life", 20);
  847. end
  848.  
  849. elements.property(nukeMissile, "Update", nukeMissileUpdate);
  850.  
  851. --Obliterator cannon
  852. oblitoraterCannon = elements.allocate("JosephMA", "OBLC");
  853. elements.element(oblitoraterCannon, elements.element(elements.DEFAULT_PT_BRCK));
  854. elements.property(oblitoraterCannon, "HighTemperature", 873.15);
  855. elements.property(oblitoraterCannon, "HighTemperatureTransition", elements.DEFAULT_PT_BRMT);
  856. elements.property(oblitoraterCannon, "Name", "OBLC");
  857. elements.property(oblitoraterCannon, "Description", "Obliterator cannon destroys the universe.");
  858. elements.property(oblitoraterCannon, "MenuSection", elem.SC_SPECIAL);
  859.  
  860. local function oblitoraterCannonUpdate(i, x, y, s, n)
  861.     if math.random(1, 500) == 1 then
  862.         for r in sim.neighbors(x,y,100,100) do
  863.             local partType = sim.partProperty(r, "type");
  864.             local tmp = tpt.get_property("tmp", i);
  865.             if (partType == ship or partType == bomber or partType == city or partType == mothership or parType == destroyer or partType == builder) and sim.partProperty(r, "tmp") ~= tmp then
  866.                 if partType ~= city then
  867.                     --Nuke missile
  868.                     local part = sim.partCreate(-3, x, y, nukeMissile);
  869.                     tpt.set_property("vx", sim.partProperty(r, "x") - x, part);
  870.                     tpt.set_property("vy", sim.partProperty(r, "y") - y, part);
  871.                     --tpt.set_property("tmp", tmp, part);
  872.                     tpt.set_property("life", 65, part);
  873.                 else
  874.                     --Destroy
  875.                     if math.random(1, 50) <= 49 then
  876.                         sim.partChangeType(r, rock);
  877.                     else
  878.                         sim.partChangeType(r, elements.DEFAULT_PT_BOMB);
  879.                     end
  880.                 end
  881.             end
  882.         end
  883.         --Fire missiles
  884.         for t=0, 50, 1
  885.         do
  886.             local part = sim.partCreate(-3, x, y, missile);
  887.             tpt.set_property("life", math.random() * 150, part);
  888.             local angle = math.random() * 2.0 * math.pi/0.5;
  889.             local v = (math.random()) * 5.0/0.5;
  890.             tpt.set_property("vx", v * math.cos(angle), part);
  891.             tpt.set_property("vy", v * math.sin(angle), part);
  892.         end
  893.     end
  894. end
  895.  
  896. elements.property(oblitoraterCannon, "Update", oblitoraterCannonUpdate);
  897.  
  898. local function oblitoraterCannonGraphics(i, colr, colg, colb)
  899.     local r;
  900.     local g;
  901.     local b;
  902.     local tmp = tpt.get_property("tmp", i);
  903.     if tmp == 0 then
  904.         r = 255;
  905.         g = 0;
  906.         b = 0;
  907.     elseif tmp == 1 then
  908.         r = 0;
  909.         g = 255;
  910.         b = 0;
  911.     elseif tmp == 2 then
  912.         r = 0;
  913.         g = 0;
  914.         b = 255;
  915.     elseif tmp == 3 then
  916.         r = 255;
  917.         g = 204;
  918.         b = 0;
  919.     elseif tmp == 4 then
  920.         r = 255;
  921.         g = 255;
  922.         b = 255;
  923.     elseif tmp == 5 then
  924.         r = 153;
  925.         g = 153;
  926.         b = 153;
  927.     end
  928.     tpt.drawline(sim.partProperty(i, "x"), sim.partProperty(i, "y"), sim.partProperty(i, "x") + 5, sim.partProperty(i, "y"), r, g, b)
  929.     return 1,0x00000001,255,r,g,b,255,255,255,255;
  930. end
  931.  
  932. elements.property(oblitoraterCannon, "Graphics", oblitoraterCannonGraphics);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement