Waffle3z

mod_classic

Jun 17th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.55 KB | None | 0 0
  1. local seed              = os.time()
  2. local neutrals, homes   = 24,   1
  3. local homeprod, ships   = 100,  100
  4. local prodmin, prodmax  = 15,   100
  5. local costmin, costmax  = 0,    50
  6. local botrate, speed    = .25,  1
  7. local crash, solid      = false, true
  8. local botvsbot, show    = false, false
  9. local spawnside = "random"
  10. local freezeplay = false
  11.  
  12. local running = false
  13. local bot1time, bot2time = 0, 0
  14. local botmemory, bot2memory = {}, {}
  15. local neutral, player, enemy;
  16. local freezetime, fleetrecord = 0, {}
  17. function init_game()
  18.     g2.game_reset()
  19.     bot1time, bot2time = 0, 0
  20.     botmemory, bot2memory = {}, {}
  21.     freezetime, fleetrecord = 0, {}
  22.     math.randomseed(seed)
  23.     g2.ticks = speed
  24.     running = true
  25.    
  26.     neutral = g2.new_user("neutral",0x555555)
  27.     player  = g2.new_user("player", 0x0000ff)
  28.     enemy   = g2.new_user("enemy",  0xff0000)
  29.     if crash then
  30.         player.fleet_crash  = 100
  31.         enemy.fleet_crash   = 100
  32.     end
  33.    
  34.     neutral.user_neutral = 1
  35.     neutral.ships_production_enabled = 0
  36.     if show then player.ui_ships_show_mask = 0xf end
  37.     g2.player = player
  38.  
  39.     local planets = neutrals+homes*2
  40.     local w = math.sqrt(planets)*480/4
  41.     local h = math.sqrt(planets)*230/4
  42.  
  43.    
  44.     for i = 1, (neutrals+neutrals%2)/2 do
  45.         local prod, ships = math.random(prodmin, prodmax), math.random(costmin, costmax)
  46.         local x, y = math.random()*w, math.random()*h
  47.         g2.new_planet(neutral,   x,   y, prod, ships)
  48.         g2.new_planet(neutral, w-x, h-y, prod, ships)
  49.     end
  50.    
  51.     local a = math.random()
  52.     for i = 1, homes do
  53.         local x = w*(1+math.cos(a*math.pi*2))*.5
  54.         local y = h*(1+math.sin(a*math.pi*2))*.5
  55.         if spawnside == "right" then
  56.             if x*2 < w then x = w-x end
  57.         elseif spawnside == "left" then
  58.             if x*2 > w then x = w-x end
  59.         end
  60.         g2.new_planet(player,  x,   y, homeprod, ships)
  61.         g2.new_planet(enemy, w-x, h-y, homeprod, ships)
  62.         a = a + .5/homes
  63.     end
  64.    
  65.     g2.planets_settle()
  66.    
  67.     for _, p in pairs(g2.search("planet")) do
  68.         p.has_collide = solid
  69.     end
  70. end
  71.  
  72. function _bots_data()
  73.     local r = g2.search("user OR planet OR fleet")
  74.     local res = {}
  75.     for _,o in ipairs(r) do
  76.         local _type = nil
  77.         if o.has_user then res[o.n] = {
  78.             n = o.n, is_user = true,
  79.             team = o.user_team_n,
  80.             neutral = o.user_neutral,
  81.             }
  82.         elseif o.has_planet then local u = o:owner() ; res[o.n] = {
  83.             n = o.n, is_planet = true,
  84.             x = o.position_x, y = o.position_y, r = o.planet_r,
  85.             ships = o.ships_value, production = o.ships_production,
  86.             owner = o.owner_n, team = u.user_team_n, neutral = u.user_neutral,
  87.             }
  88.         elseif o.has_fleet then local u = o:owner() ;
  89.             local sync_id = tostring(o.sync_id) ; res[sync_id] = {
  90.             _n = o.n, n = sync_id, is_fleet = true,
  91.             x = o.position_x, y = o.position_y, r = o.fleet_r,
  92.             ships = o.fleet_ships,
  93.             owner = o.owner_n, team = u.user_team_n,
  94.             target = o.fleet_target,
  95.             }
  96.         end
  97.     end
  98.     return res
  99. end
  100.  
  101. local behavior1 = { -- enemy bot behavior
  102.     speed = 0.25; -- moves per second
  103.     tunnels = "on"; -- "on", "off", "only"
  104.     percent = false; -- force a certain percent
  105.     redirects = true; -- bot is allowed to redirect
  106.     multiselect = true; -- bot is allowed to select multiple things each move
  107.     unlimited = false; -- bot is not limited to human controls
  108. }
  109. local behavior2 = { -- auto bot behavior
  110.     speed = 0.25;
  111.     tunnels = "on";
  112.     percent = false;
  113.     redirects = true;
  114.     multiselect = true;
  115.     unlimited = false;
  116. }
  117.  
  118. function bot(params, behavior)
  119.     local abs, floor, ceil, max, min, HUGE, random, PI, sqrt, sort = math.abs, math.floor, math.ceil, math.max, math.min, math.huge, math.random, math.pi, math.sqrt, table.sort
  120.     local function send(p,f,t)return{percent=p,from=f.ships and{f.n}or f,to=t.n}end
  121.     local function redirect(f,t)return{from=f.ships and{f.n}or f,to=t.n}end
  122.     local function distance(a,b)local dx,dy=b.x-a.x,b.y-a.y return sqrt(dx*dx+dy*dy)end
  123.     local G,USER,memory=params.items,params.user,params.memory
  124.     local FIRST=not memory.init
  125.     if FIRST then memory.init=true end
  126.     local uteam=G[USER].team
  127.     local homes,eteam=memory.homes,memory.eteam
  128.     if not homes then
  129.         homes={}
  130.         for _,v in pairs(G)do
  131.             if v.is_planet and not v.neutral then
  132.                 local o=v.team
  133.                 local h=homes[o]
  134.                 if not h or h.production<v.production then
  135.                     homes[o]=v
  136.                 end
  137.             end
  138.         end
  139.         for k,v in pairs(homes)do if k~=uteam then memory.eteam,eteam=k,k end end
  140.         memory.homes=homes
  141.     end
  142.     local home,ehome=homes[uteam],homes[eteam]
  143.     local ships,total,tprod,myprod=0,0,0,0
  144.     local data={planets={},neutral={},myplanets={},myteam={},eplanets={},others={},fleets={},myfleets={},efleets={},mystuff={}}
  145.     for _,p in pairs(G)do
  146.         local o,s=p.team,p.ships
  147.         if not p.is_user and not p.neutral then
  148.             if o==uteam then
  149.                 ships=ships+s
  150.             end
  151.             total=total+s
  152.         end
  153.         if p.is_planet then
  154.             data.planets[#data.planets+1]=p
  155.             if p.neutral then
  156.                 data.neutral[#data.neutral+1]=p
  157.                 data.others[#data.others+1]=p
  158.             else
  159.                 tprod=tprod+p.production
  160.                 if o==uteam then
  161.                     data.myteam[#data.myteam+1]=p
  162.                     if p.owner==USER then
  163.                         myprod=myprod+p.production
  164.                         data.myplanets[#data.myplanets+1]=p
  165.                         data.mystuff[#data.mystuff+1]=p
  166.                     end
  167.                 else
  168.                     data.others[#data.others+1]=p
  169.                     data.eplanets[#data.eplanets+1]=p
  170.                 end
  171.             end
  172.         elseif p.is_fleet then
  173.             data.fleets[#data.fleets+1]=p
  174.             if p.team==uteam then
  175.                 data.myfleets[#data.myfleets+1]=p
  176.                 data.mystuff[#data.mystuff+1]=p
  177.             else
  178.                 data.efleets[#data.efleets+1]=p
  179.             end
  180.         end
  181.     end
  182.     local planets,control=data.myplanets,ships/total
  183.     if FIRST and home and #data.neutral~=0 then
  184.         local function path(f,t,set)
  185.             local ft=distance(f,t)
  186.             for i=1,#set do local p=set[i]
  187.                 if p~=f and p~=t then
  188.                     local pt=distance(p,t)
  189.                     if pt<ft then
  190.                         local fp=distance(f,p)
  191.                         if fp<ft and(pt+fp-p.r*2)<ft then
  192.                             t,ft=p,fp
  193.                         end
  194.                     end
  195.                 end
  196.             end
  197.             return t
  198.         end
  199.         local function recovertime(a,b)
  200.             local r=b.ships-a.ships
  201.             if r<0 then
  202.                 return distance(a,b)/20+b.ships*50/b.production
  203.             elseif a.is_planet then
  204.                 return distance(a,b)/20+b.ships*50/b.production+r*50/a.production
  205.             else
  206.                 return HUGE
  207.             end
  208.         end
  209.         local function pathlength(f,t,set)
  210.             local d=0
  211.             for i=1,20 do
  212.                 if f==t then break end
  213.                 local F=path(f,t,set)
  214.                 d=d+distance(f,F)
  215.                 f=F
  216.             end
  217.             return d
  218.         end
  219.         local eeval,enemy=HUGE
  220.         for _,v in pairs(data.eplanets)do
  221.             local n=distance(v,home)
  222.             if eeval>n then eeval,enemy=n,v end
  223.         end
  224.         if not enemy then
  225.             local eeval=-HUGE
  226.             for _,v in pairs(data.others)do
  227.                 local n=distance(v,home)
  228.                 if n>eeval then eeval,enemy=n,v end
  229.             end
  230.         end
  231.         local p,expand=data.neutral,{}
  232.         local rt={}for _,v in pairs(p)do rt[v]=recovertime(home,v)end
  233.         sort(p,function(a,b)return rt[a]<rt[b]end)
  234.         local c=home.ships
  235.         local cinit,ships,n=c,c,0
  236.         local benefit=home.production*distance(enemy,home)/2000
  237.         local prod=home.production
  238.         for i=1,#p do local v=p[i]
  239.             if home and enemy and distance(enemy,v)>distance(home,v)then
  240.                 local s=max(1,v.ships+1)
  241.                 local ben = (distance(enemy,v)-pathlength(home,v,expand))*v.production/2000-s
  242.                 local liability = s-max(c, 0)
  243.                 if c < s then
  244.                     ben = ben - liability*v.production/prod
  245.                 end
  246.                 if benefit+ben+c>cinit then
  247.                     benefit=benefit+ben
  248.                     c=c-s
  249.                     if c>0 then prod=prod+v.production end
  250.                     expand[#expand+1]=v
  251.                 end
  252.             end
  253.         end
  254.         memory.expand=expand
  255.         return
  256.     end
  257.     local stuff=data.mystuff
  258.     sort(stuff,function(a,b)return a.ships>b.ships end)
  259.     local targets=data.eplanets
  260.     local defend=myprod/tprod>.5 and control<.8
  261.     if memory.expand then
  262.         for _,v in pairs(memory.expand)do
  263.             local v=G[v.n]
  264.             if v and v.neutral then
  265.                 targets[#targets+1]=v
  266.             end
  267.         end
  268.     end
  269.     if control > .5 then
  270.         local excess = total*(control-.5)
  271.         for _, v in pairs(data.neutral) do
  272.             if v.ships < excess then
  273.                 targets[#targets+1] = v
  274.             end
  275.         end
  276.     end
  277.     local function tunnel(f,t)
  278.         if not t then return end
  279.         if behavior.tunnels == "off" then return t end
  280.         local planets = behavior.tunnels == "on" and data.myteam or data.planets
  281.         local ft=distance(f,t)
  282.         for _,p in pairs(planets)do
  283.             local fp=distance(f,p)
  284.             if fp<ft then
  285.                 local pt=distance(p,t)
  286.                 if p~=f and pt<ft and(pt+fp-p.r*2)<ft then
  287.                     t,ft=p,fp
  288.                 end
  289.             end
  290.         end
  291.         return t
  292.     end
  293.     local finish=control>.7
  294.     local GetTarget=function(f)
  295.         local teval,t=HUGE
  296.         for _,v in pairs(targets)do
  297.             local s=v.ships
  298.             if v.neutral then
  299.                 local dist=distance(f,v)
  300.                 for _,x in pairs(data.myfleets)do if x~=f and x.target==v.n then s=s-floor(x.ships)end end
  301.             else
  302.                 local close=distance(f,v)
  303.                 s=s+ceil(v.production*close/2000)
  304.             end
  305.             if floor(s)>=0 then
  306.                 local n=distance(f,v)/10+s-v.production/4
  307.                 if v.neutral then n=distance(f,v)/10 end
  308.                 if teval>n then teval,t=n,v end
  309.             end
  310.         end
  311.         return t
  312.     end
  313.     if finish then
  314.         GetTarget=function(f)
  315.             local teval,t=HUGE
  316.             for _,v in pairs(targets)do
  317.                 local d=distance(f,v)
  318.                 if teval>d then teval,t=d,v end
  319.             end
  320.             return t
  321.         end
  322.     end
  323.     local selected,maintarget,percent={}
  324.     if behavior.percent then
  325.         percent = behavior.percent
  326.     end
  327.     local danger,help={},{}
  328.     for ind,f in pairs(data.myplanets)do
  329.         local available,dist=floor(f.ships),HUGE
  330.         for _,v in pairs(data.efleets)do
  331.             local targ=G[v.target]
  332.             if targ.neutral then
  333.                 if distance(targ,f)<100 then
  334.                     targ=f
  335.                 end
  336.             end
  337.             if targ==f then
  338.                 available=available-ceil(v.ships)
  339.                 dist=min(dist,distance(v,f)-f.r)
  340.             end
  341.         end
  342.         local h=0
  343.         for _,v in pairs(data.myfleets)do
  344.             if v.target==f.n then
  345.                 h=h+v.ships
  346.             end
  347.         end
  348.         help[f]=h
  349.         danger[f]=floor(available+f.production*dist/2000)
  350.     end
  351.     for ind,f in pairs(stuff)do
  352.         if f.is_planet then
  353.             local available=danger[f]or f.ships
  354.             if available>0 then
  355.                 local t0=GetTarget(f)
  356.                 local t=tunnel(f,t0)
  357.                 local low,t2=0
  358.                 for _,v in pairs(data.myplanets)do
  359.                     local d=(danger[v]or v.ships)+(help[v]or 0)
  360.                     if d<low then low,t2=d,v end
  361.                 end
  362.                 if t2 and defend and t2 ~= f then
  363.                     if not t or (not t.neutral and distance(t,f) > distance(t2,f)) then
  364.                         t = t2
  365.                         percent = percent or 15
  366.                     end
  367.                 end
  368.                 if t then
  369.                     if behavior.unlimited then
  370.                         local a = 100*available/f.ships
  371.                         if t.neutral and not behavior.percent then a = min(a, 200*(t.ships+1)/f.ships) end
  372.                         g2_fleet_send(math.min(100, percent or a), f.n, t.n)
  373.                         if not behavior.multiselect then break end
  374.                     else
  375.                         if not maintarget then maintarget=t end
  376.                         if maintarget~=t and t0 and behavior.tunnels ~= "only" then
  377.                             if distance(f,t0)+10>distance(f,maintarget)+distance(maintarget,t0)-maintarget.r*2 then
  378.                                 t=maintarget
  379.                             end
  380.                         end
  381.                         if maintarget==t then
  382.                             local a=floor(available*20/f.ships)*5
  383.                             if t.neutral and not behavior.percent then a=min(a,ceil((t.ships+1)*20/f.ships)*10)end
  384.                             if not percent then percent=a end
  385.                             if percent<=a then
  386.                                 t.ships=t.ships-floor(ceil(percent*20/f.ships)*f.ships/20+.5)
  387.                                 selected[#selected+1]=f.n
  388.                             end
  389.                         end
  390.                     end
  391.                 end
  392.             end
  393.         elseif behavior.redirects then
  394.             local t=tunnel(f,GetTarget(f))
  395.             local low,t2=0
  396.             local targ=G[f.target]
  397.             for _,v in pairs(data.myplanets)do
  398.                 local d=(danger[v]or v.ships)+(help[v]or 0)
  399.                 if v==targ then d=d-f.ships end
  400.                 if d<low then low,t2=d,v end
  401.             end
  402.             if t then
  403.                 if t2 and defend and not t.neutral and distance(t,f)>distance(t2,f) then t=t2 end
  404.             elseif t2 and defend then
  405.                 t=t2
  406.             end
  407.             if t then
  408.                 if f.target~=t.n then
  409.                     if behavior.unlimited then
  410.                         g2_fleet_redirect(G[f.n]._n, t.n)
  411.                         if not behavior.multiselect then break end
  412.                     else
  413.                         if not maintarget then maintarget=t end
  414.                         if maintarget==t then
  415.                             f.target=t.n
  416.                             selected[#selected+1]=f.n
  417.                         end
  418.                     end
  419.                 end
  420.             end
  421.         end
  422.         if selected[1] and not behavior.multiselect then
  423.             break
  424.         end
  425.     end
  426.     if maintarget and #selected>0 then
  427.         --[[if finish then
  428.             return send(50,{selected[1],selected[2]},maintarget)
  429.         end]]
  430.         return send(percent,selected,maintarget)
  431.     end
  432. end
  433.  
  434. function copy(o)
  435.     if type(o) ~= 'table' then return o end
  436.     local r = {}
  437.     for k,v in pairs(o) do r[k] = copy(v) end
  438.     return r
  439. end
  440.  
  441. function _bots_run(_data, uid, memory, behavior)
  442.     local data = copy(_data)
  443.     local res = bot({items=data, user=uid, memory=memory}, behavior)
  444.     if not res then return end
  445.     local data = _data
  446.     local percent = res.percent or 50
  447.     percent = math.max(5,math.min(100,math.floor(percent/5 + .5) * 5))
  448.     local from = res.from ; if type(from) ~= 'table' then from = {from} end
  449.     local to = res.to
  450.     return {data = data, to = to, from = from, uid = uid, percent = percent}
  451. end
  452.  
  453. function loop(t)
  454.     if g2.state ~= "play" then return end
  455.     if freezeplay then
  456.         local moved = false
  457.         for _, f in pairs(g2.search("fleet owner:"..g2.player)) do
  458.             local id = f.sync_id
  459.             local r = fleetrecord[id]
  460.             if not r or f.fleet_target ~= r then
  461.                 fleetrecord[id] = f.fleet_target
  462.                 moved = true
  463.             end
  464.         end
  465.         if moved then
  466.             freezetime = freezetime + .25
  467.             g2.speed = 1
  468.         end
  469.         if freezetime <= 0 then
  470.             g2.speed = 0
  471.             for _, p in pairs(g2.search("planet owner:"..g2.player)) do
  472.                 if p.ships_value > 1 then
  473.                     return
  474.                 end
  475.             end
  476.             g2.speed = 1 -- resume for 1 frame until player has a planet it can send from
  477.             return
  478.         else
  479.             freezetime = freezetime - t
  480.         end
  481.     end
  482.     bot1time = bot1time + t
  483.     bot2time = bot2time + t
  484.     local bot1rate = behavior1.speed
  485.     local bot2rate = behavior2.speed
  486.     local data;
  487.     local moves = {}
  488.     if bot1time >= bot1rate then
  489.         bot1time = bot1time - bot1rate
  490.         data = _bots_data()
  491.         moves[#moves+1] = _bots_run(data, enemy.n, botmemory, behavior1)
  492.     end
  493.     if botvsbot and bot2time >= bot2rate then
  494.         bot2time = bot2time - bot2rate
  495.         data = data or _bots_data()
  496.         moves[#moves+1] = _bots_run(data, player.n, bot2memory, behavior2)
  497.     end
  498.     if #moves == 2 and math.random() > .5 then
  499.         moves[1], moves[2] = moves[2], moves[1]
  500.     end
  501.     for i = 1, #moves do
  502.         local move = moves[i]
  503.         local data, to, from, uid, percent = move.data, move.to, move.from, move.uid, move.percent
  504.         if data[to].is_planet then
  505.             for _, f in pairs(from) do
  506.                 if data[f].is_planet and data[f].owner == uid then
  507.                     g2_fleet_send(percent, f, to)
  508.                 end
  509.                 if data[f].is_fleet and data[f].owner == uid and data[f].target ~= to then
  510.                     g2_fleet_redirect(data[f]._n, to)
  511.                 end
  512.             end
  513.         end
  514.     end
  515.    
  516.     local winner;
  517.     for _, p in pairs(g2.search("planet OR fleet -neutral")) do
  518.         local user = p:owner()
  519.         if not winner then
  520.             winner = user
  521.         elseif winner ~= user then
  522.             return
  523.         end
  524.     end
  525.    
  526.     if winner then
  527.         if winner.has_player then
  528.             init_pause("win")
  529.         else
  530.             init_pause("lose")
  531.         end
  532.     end
  533. end
  534.  
  535. local menustate = "menu"
  536. function readmenu()
  537.     if g2.state ~= "menu" and menustate == "menu" then return end
  538.     if menustate == "menu" then
  539.         seed     = tonumber(g2.form.seed    ) or 0
  540.         neutrals = tonumber(g2.form.neutrals) or 24
  541.         homes    = tonumber(g2.form.homes   ) or 1
  542.         homeprod = tonumber(g2.form.homeprod) or 100
  543.         ships    = tonumber(g2.form.ships   ) or 100
  544.         prodmin  = tonumber(g2.form.prodmin ) or 15
  545.         prodmax  = tonumber(g2.form.prodmax ) or 100
  546.         costmin  = tonumber(g2.form.costmin ) or 0
  547.         costmax  = tonumber(g2.form.costmax ) or 50
  548.         speed    = tonumber(g2.form.speed   ) or 1
  549.         if speed < 1 then speed = 1 end
  550.         neutrals = neutrals + neutrals%2
  551.         if prodmin > prodmax then prodmin, prodmax = prodmax, prodmin end
  552.         if costmin > costmax then costmin, costmax = costmax, costmin end
  553.         g2.ticks = speed
  554.     else
  555.         behavior1.speed  = tonumber(g2.form.moverate1) or 0
  556.         behavior2.speed  = tonumber(g2.form.moverate2) or 0
  557.         bot1time, bot2time = 0, 0
  558.         if behavior1.percent then
  559.             behavior1.percent = math.max(5, math.min(100, tonumber(g2.form.percent1) or 100))
  560.             behavior1.percent = math.floor(behavior1.percent/5+.5)*5
  561.         end
  562.         if behavior2.percent then
  563.             behavior2.percent = math.max(5, math.min(100, tonumber(g2.form.percent2) or 100))
  564.             behavior2.percent = math.floor(behavior2.percent/5+.5)*5
  565.         end
  566.     end
  567. end
  568.  
  569. function refresh()
  570.     if g2.state == "menu" then
  571.         readmenu()
  572.         init_menu()
  573.     elseif menustate == "behavior" then
  574.         readmenu()
  575.         init_botmenu()
  576.     else
  577.         show_toggles()
  578.     end
  579. end
  580.  
  581. function event(e)
  582.     if e.type == "onclick" then
  583.         if e.value:sub(1,4) == "init" then
  584.             readmenu()
  585.             init_game()
  586.             init_getready()
  587.             botvsbot = e.value == "initbot"
  588.         elseif e.value == "newmap" then
  589.             readmenu()
  590.             seed = seed + 1
  591.             init_game()
  592.             init_getready()
  593.         elseif e.value == "restart" then
  594.             readmenu()
  595.             init_game()
  596.             init_getready()
  597.         elseif e.value == "resume" then
  598.             readmenu()
  599.             g2.state = "play"
  600.         elseif e.value == "menu" then
  601.             init_menu()
  602.         elseif e.value == "toggle" then
  603.             show_toggles()
  604.         elseif e.value == "quit" then
  605.             g2.state = "quit"
  606.         elseif e.value == "switch" then
  607.             if spawnside == "right" then
  608.                 spawnside = "left"
  609.             elseif spawnside == "left" then
  610.                 spawnside = "right"
  611.             end
  612.             for _, p in pairs(g2.search("planet -neutral")) do
  613.                 p:planet_chown(p:owner() == player and enemy or player)
  614.             end
  615.             for _, f in pairs(g2.search("fleet")) do
  616.                 for _, p in pairs(g2.search("planet")) do
  617.                     if p.n == f.fleet_target then
  618.                         g2.new_fleet(f:owner() == player and enemy or player, f.fleet_ships, f, p)
  619.                         f:destroy()
  620.                         break
  621.                     end
  622.                 end
  623.             end
  624.         elseif e.value == "crash" then
  625.             crash = not crash
  626.             refresh()
  627.             if running then
  628.                 local value = crash and 100 or 0
  629.                 player.fleet_crash  = value
  630.                 enemy.fleet_crash   = value
  631.                 for _, f in pairs(g2.search("fleet")) do
  632.                     for _, p in pairs(g2.search("planet")) do
  633.                         if p.n == f.fleet_target then
  634.                             g2.new_fleet(f:owner(), f.fleet_ships, f, p)
  635.                             f:destroy()
  636.                             break
  637.                         end
  638.                     end
  639.                 end
  640.             end
  641.         elseif e.value == "show" then
  642.             show = not show
  643.             refresh()
  644.             if running then
  645.                 player.ui_ships_show_mask = show and 0xf or 0x17
  646.             end
  647.         elseif e.value == "solid" then
  648.             solid = not solid
  649.             refresh()
  650.             if running then
  651.                 for _, p in pairs(g2.search("planet")) do
  652.                     p.has_collide = solid
  653.                 end
  654.             end
  655.         elseif e.value == "lockside" then
  656.             spawnside = spawnside == "random" and "right" or "random"
  657.             refresh()
  658.         elseif e.value == "bebot" then
  659.             botvsbot = not botvsbot
  660.             g2.state = "play"
  661.         elseif e.value == "pause" then
  662.             readmenu()
  663.             init_pause()
  664.         elseif e.value == "toggleback" then
  665.             botrate  = tonumber(g2.form.botrate ) or 0
  666.             speed    = tonumber(g2.form.speed   ) or 1
  667.             g2.ticks = speed
  668.             init_pause()
  669.         elseif e.value == "behavior" then
  670.             if g2.state == "menu" then
  671.                 readmenu()
  672.                 menustate = menustate == "menu" and "behavior" or "menu"
  673.                 init_menu()
  674.             elseif menustate == "behavior" then
  675.                 readmenu()
  676.                 menustate = "menu"
  677.                 show_toggles()
  678.             else
  679.                 menustate = "behavior"
  680.                 init_botmenu()
  681.             end
  682.         elseif e.value == "redirect1" then
  683.             behavior1.redirects = not behavior1.redirects
  684.             refresh()
  685.         elseif e.value == "redirect2" then
  686.             behavior2.redirects = not behavior2.redirects
  687.             refresh()
  688.         elseif e.value == "tunnel1" then
  689.             behavior1.tunnels = behavior1.tunnels == "on" and "off" or behavior1.tunnels == "off" and "only" or "on"
  690.             refresh()
  691.         elseif e.value == "tunnel2" then
  692.             behavior2.tunnels = behavior2.tunnels == "on" and "off" or behavior2.tunnels == "off" and "only" or "on"
  693.             refresh()
  694.         elseif e.value == "multisel1" then
  695.             behavior1.multiselect = not behavior1.multiselect
  696.             refresh()
  697.         elseif e.value == "multisel2" then
  698.             behavior2.multiselect = not behavior2.multiselect
  699.             refresh()
  700.         elseif e.value == "percent1" then
  701.             behavior1.percent = (not behavior1.percent) and 100
  702.             refresh()
  703.         elseif e.value == "percent2" then
  704.             behavior2.percent = (not behavior2.percent) and 100
  705.             refresh()
  706.         elseif e.value == "unlimited1" then
  707.             behavior1.unlimited = not behavior1.unlimited
  708.             refresh()
  709.         elseif e.value == "unlimited2" then
  710.             behavior2.unlimited = not behavior2.unlimited
  711.             refresh()
  712.         elseif e.value == "freeze" then
  713.             freezeplay = not freezeplay
  714.             refresh()
  715.         end
  716.     elseif e.type == "pause" then
  717.         init_pause()
  718.     end
  719. end
  720.  
  721. function init_menu()
  722.     g2.state = "menu"
  723.     if menustate == "behavior" then
  724.         init_botmenu()
  725.         return
  726.     end
  727.     g2.html  = [[<table>
  728.     <tr><td colspan=6><h1>Waffle3z's Bot 1v1</h1>
  729.     <tr><td><p>&nbsp;</p>
  730.     <tr><td><p>Map seed:            </p><td colspan=2><input type='text' name='seed'    />
  731.         <td><p>Homes:               </p><td colspan=2><input type='text' name='homes'   />
  732.     <tr><td><p>Neutrals:            </p><td colspan=2><input type='text' name='neutrals'/>
  733.         <td><p>Starting ships:      </p><td colspan=2><input type='text' name='ships'   />
  734.     <tr><td><p>Home production:     </p><td colspan=2><input type='text' name='homeprod'/>
  735.         <td colspan=3><input type='button'      name='crash' value='Ships crash'   onclick='crash'    class='ibutton]]..(crash and 2 or 1)..[[' icon='klass-fighter'/>
  736.     <tr><td><p>Neutral prod range:  </p><td><input type='text' name='prodmin' /><td><input type='text' name='prodmax' />
  737.         <td colspan=3><input type='button'      name='show'  value='Enemy ships'   onclick='show'     class='ibutton]]..(show  and 2 or 1)..[[' icon='icon-search'  />
  738.     <tr><td><p>Neutral cost range:  </p><td><input type='text' name='costmin' /><td><input type='text' name='costmax' />
  739.         <td colspan=3><input type='button'      name='solid' value='Solid planets' onclick='solid'    class='ibutton]]..(solid and 2 or 1)..[[' icon='icon-world'   />
  740.     <tr><td><p>Game speed:          </p><td colspan=2><input type='text' name='speed'/>
  741.         <td colspan=3><input type='button'      name='side'  value='Lock side'     onclick='lockside' class='ibutton]]..(spawnside == "random" and 1 or 2)..[[' icon='icon-forever'/>
  742.     <tr><td colspan=3><p>&nbsp;</p>
  743.         <td colspan=3><input type='button' name='freeze'value='Freeze play'   onclick='freeze'   class='ibutton]]..(freezeplay and 2 or 1)..[[' icon='icon-review'/>
  744.     <tr><td colspan=3><p>&nbsp;</p>
  745.         <td colspan=3><input type='button'      name='bots'  value='Bot behavior'  onclick='behavior' class='ibutton1' icon='icon-custom'/>]]
  746.     if running then
  747.         g2.html = g2.html..[[
  748.         <tr><td colspan=6><table><tr><td><input type='button' value='Resume'  onclick='pause'  class='ibutton1' icon='icon-play'   /></table>
  749.         <tr><td colspan=6><table><tr><td><input type='button' value='Restart' onclick='init'   class='ibutton1' icon='icon-restart'/></table>
  750.         <tr><td colspan=6><table><tr><td><input type='button' value='New Map' onclick='newmap' class='ibutton1' icon='icon-new_map'/></table>]]
  751.     else
  752.         g2.html = g2.html..[[
  753.         <tr><td colspan=6><table><tr><td><input type='button' value='Play'       onclick='init'    class='ibutton1' icon='icon-play'   /></table>
  754.         <tr><td colspan=6><table><tr><td><input type='button' value='Bot VS Bot' onclick='initbot' class='ibutton1' icon='icon-rivalry'/></table>
  755.         </table>]]
  756.     end
  757.     g2.form.seed     = seed
  758.     g2.form.neutrals = neutrals
  759.     g2.form.homes    = homes
  760.     g2.form.homeprod = homeprod
  761.     g2.form.ships    = ships
  762.     g2.form.prodmin  = prodmin
  763.     g2.form.prodmax  = prodmax
  764.     g2.form.costmin  = costmin
  765.     g2.form.costmax  = costmax
  766.     g2.form.botrate  = botrate
  767.     g2.form.speed    = speed
  768. end
  769. init = init_menu
  770.  
  771. function init_botmenu()
  772.     local tunnel1 = behavior1.tunnels
  773.     local tunnel1n = tunnel1 == "off" and 1 or tunnel1 == "on" and 2 or 3
  774.     local tunnel2 = behavior2.tunnels
  775.     local tunnel2n = tunnel2 == "off" and 1 or tunnel2 == "on" and 2 or 3
  776.     g2.html  = [[<table>
  777.     <tr><td colspan=4><h1>Bot Behavior Menu</h1>
  778.     <tr><td><p>&nbsp;</p>
  779.     <tr><td colspan=2><h2>Enemy bot</h2>
  780.         <td colspan=2><h2>Auto bot</h2>
  781.     <tr><td><p>Move rate:</p><td><input type='text' name='moverate1'/>
  782.         <td><p>Move rate:</p><td><input type='text' name='moverate2'/>
  783.     <tr><td colspan=2><input type='button' name='redirect1' value='Redirects'   onclick='redirect1' class='ibutton]]..(behavior1.redirects and 2 or 1)..[[' icon='klass-rocket'/>
  784.         <td colspan=2><input type='button' name='redirect2' value='Redirects'   onclick='redirect2' class='ibutton]]..(behavior2.redirects and 2 or 1)..[[' icon='klass-rocket'/>
  785.     <tr><td colspan=2><input type='button' name='tunnel1'   value='Tunnel ]]..tunnel1..[['  onclick='tunnel1' class='ibutton]]..tunnel1n..[[' icon='icon-more'/>
  786.         <td colspan=2><input type='button' name='tunnel2'   value='Tunnel ]]..tunnel2..[['  onclick='tunnel2' class='ibutton]]..tunnel2n..[[' icon='icon-more'/>
  787.     <tr><td colspan=2><input type='button' name='multisel1' value='Multiselect' onclick='multisel1' class='ibutton]]..(behavior1.multiselect and 2 or 1)..[[' icon='icon-clans'/>
  788.         <td colspan=2><input type='button' name='multisel2' value='Multiselect' onclick='multisel2' class='ibutton]]..(behavior2.multiselect and 2 or 1)..[[' icon='icon-clans'/>
  789.     <tr><td colspan=2><input type='button' name='percentb1' value='Auto percent' onclick='percent1' class='ibutton]]..(behavior1.percent and 1 or 2)..[['  icon='icon-controls'/>
  790.         <td colspan=2><input type='button' name='percentb2' value='Auto percent' onclick='percent2' class='ibutton]]..(behavior2.percent and 1 or 2)..[['  icon='icon-controls'/>
  791.     <tr><td><p>Set percent:</p><td><input type='text' name='percent1' ]]..(behavior1.percent and '' or 'disabled=true')..[[/>
  792.         <td><p>Set percent:</p><td><input type='text' name='percent2' ]]..(behavior2.percent and '' or 'disabled=true')..[[/>
  793.     <tr><td colspan=2><input type='button' name='unlimited1' value='Unlimited' onclick='unlimited1' class='ibutton]]..(behavior1.unlimited and 3 or 1)..[[' icon='icon-forever'/>
  794.         <td colspan=2><input type='button' name='unlimited2' value='Unlimited' onclick='unlimited2' class='ibutton]]..(behavior2.unlimited and 3 or 1)..[[' icon='icon-forever'/>
  795.     <tr><td><p>&nbsp;</p>
  796.     <tr><td colspan=4><input type='button' name='bots' value='Back' onclick='behavior' class='ibutton1' icon='icon-restart'/>]]
  797.     g2.form.moverate1 = behavior1.speed
  798.     g2.form.moverate2 = behavior2.speed
  799.     g2.form.percent1 = behavior1.percent or "Bot decision"
  800.     g2.form.percent2 = behavior2.percent or "Bot decision"
  801. end
  802.  
  803. function show_toggles()
  804.     g2.html = [[<table>
  805.         <tr><td colspan=2><input type='button' name='crash' value='Ships crash'   onclick='crash'    class='ibutton]]..(crash and 2 or 1)..[[' icon='klass-fighter'/>
  806.         <tr><td colspan=2><input type='button' name='show'  value='Enemy ships'   onclick='show'     class='ibutton]]..(show  and 2 or 1)..[[' icon='icon-search'  />
  807.         <tr><td colspan=2><input type='button' name='solid' value='Solid planets' onclick='solid'    class='ibutton]]..(solid and 2 or 1)..[[' icon='icon-world'   />
  808.         <tr><td colspan=3><input type='button' name='freeze'value='Freeze play'   onclick='freeze'   class='ibutton]]..(freezeplay and 2 or 1)..[[' icon='icon-review'/>
  809.         <tr><td colspan=3><input type='button' name='bots'  value='Bot behavior'  onclick='behavior' class='ibutton1' icon='icon-custom'/> 
  810.         <tr><td><p>Game speed:          </p><td><input type='text' name='speed'  />
  811.         <tr><td colspan=2><input type='button' value='Back' onclick='toggleback' class='ibutton1' icon='icon-restart'/>
  812.     ]]
  813.     g2.form.speed    = speed
  814.     g2.form.botrate  = botrate
  815. end
  816.  
  817. function init_getready()
  818.     g2.state = "pause"
  819.     g2.html  = [[<table>
  820.     <tr><td><h1>Get Ready!</h1>
  821.     <tr><td><input type='button' value='Begin'        onclick='resume' class='ibutton1'                             icon='icon-play'    />
  822.     <tr><td><input type='button' value='Become bot'   onclick='bebot'  class='ibutton]]..(botvsbot and 2 or 1)..[[' icon='icon-rivalry' />
  823.     <tr><td><input type='button' value='Switch sides' onclick='switch' class='ibutton1'                             icon='icon-forever' />
  824.     <tr><td><input type='button' value='New Map'      onclick='newmap' class='ibutton1'                             icon='icon-new_map' />
  825.     <tr><td><input type='button' value='Toggles'      onclick='toggle' class='ibutton1'                             icon='icon-custom'  />
  826.     <tr><td><input type='button' value='Menu'         onclick='menu'   class='ibutton1'                             icon='icon-settings'/>]]
  827. end
  828.  
  829. function init_pause(x)
  830.     g2.state = "pause"
  831.     g2.html  = [[<table>
  832.     <tr><td><input type='button' value='Resume'  onclick='resume'  class='ibutton1' icon='icon-resume' />
  833.     <tr><td><input type='button' value='Restart' onclick='restart' class='ibutton1' icon='icon-restart'/>]]
  834.     if x == "win" then
  835.         running = false
  836.         g2.html = [[<table>
  837.         <tr><td><h1>Good Job!</h1>
  838.         <tr><td><input type='button' value='Replay'  onclick='restart' class='ibutton1' icon='icon-restart' />
  839.         <tr><td><input type='button' value='New Map' onclick='newmap'  class='ibutton1' icon='icon-new_map' />
  840.         <tr><td><input type='button' value='Menu'    onclick='menu'    class='ibutton1' icon='icon-settings'/>]]
  841.     elseif x == "lose" then
  842.         running = false
  843.         g2.html = [[<table>
  844.         <tr><td><input type='button' value='Try Again' onclick='restart' class='ibutton1' icon='icon-restart' />
  845.         <tr><td><input type='button' value='New Map'   onclick='newmap'  class='ibutton1' icon='icon-new_map' />
  846.         <tr><td><input type='button' value='Menu'      onclick='menu'    class='ibutton1' icon='icon-settings'/>]]
  847.     else
  848.         g2.html = g2.html..[[
  849.         <tr><td><input type='button' value='Become bot'   onclick='bebot'  class='ibutton]]..(botvsbot and 2 or 1)..[[' icon='icon-rivalry' />
  850.         <tr><td><input type='button' value='Switch sides' onclick='switch' class='ibutton1'                             icon='icon-forever' />
  851.         <tr><td><input type='button' value='New Map'      onclick='newmap' class='ibutton1'                             icon='icon-new_map' />
  852.         <tr><td><input type='button' value='Toggles'      onclick='toggle' class='ibutton1'                             icon='icon-custom'  />
  853.         <tr><td><input type='button' value='Menu'         onclick='menu'   class='ibutton1'                             icon='icon-settings'/>]]
  854.     end
  855.     g2.html = g2.html..[[]]
  856. end
Advertisement
Add Comment
Please, Sign In to add comment