source: trunk/luarules/gadgets/game_initial_spawn.lua @ 5877

Revision 5776, 24.1 KB checked in by Floris, 3 years ago (diff)

ready / subbutton made button border smaller

Line 
1
2function gadget:GetInfo()
3        return {
4                name    = 'Initial Spawn',
5                desc    = 'Handles initial spawning of units',
6                author  = 'Niobium',
7                version = 'v1.0',
8                date    = 'April 2011',
9                license = 'GNU GPL, v2 or later',
10                layer   = 0,
11                enabled = true
12        }
13end
14
15-- Note: (31/03/13) mo_coop_II deals with the extra startpoints etc needed for teamsIDs with more than one playerID.
16
17----------------------------------------------------------------
18-- Synced
19----------------------------------------------------------------
20if gadgetHandler:IsSyncedCode() then
21
22----------------------------------------------------------------
23-- Speedups
24----------------------------------------------------------------
25local spGetPlayerInfo = Spring.GetPlayerInfo
26local spGetTeamInfo = Spring.GetTeamInfo
27local spGetTeamRulesParam = Spring.GetTeamRulesParam
28local spSetTeamRulesParam = Spring.SetTeamRulesParam
29local spGetTeamStartPosition = Spring.GetTeamStartPosition
30local spGetAllyTeamStartBox = Spring.GetAllyTeamStartBox
31local spCreateUnit = Spring.CreateUnit
32local spGetGroundHeight = Spring.GetGroundHeight
33
34----------------------------------------------------------------
35-- Config
36----------------------------------------------------------------
37local changeStartUnitRegex = '^\138(%d+)$'
38local startUnitParamName = 'startUnit'
39
40----------------------------------------------------------------
41-- Vars
42----------------------------------------------------------------
43local armcomDefID = UnitDefNames.armcom.id
44local corcomDefID = UnitDefNames.corcom.id
45
46local validStartUnits = {
47        [armcomDefID] = true,
48        [corcomDefID] = true,
49}
50local spawnTeams = {} -- spawnTeams[teamID] = allyID
51local nSpawnTeams
52
53--each player gets to choose a faction
54local playerStartingUnits = {} -- playerStartingUnits[unitID] = unitDefID
55GG.playerStartingUnits = playerStartingUnits
56
57--each team gets one startpos. if coop mode is on, extra startpoints are placed in GG.coopStartPoints by mo_coop
58local teamStartPoints = {} -- teamStartPoints[teamID] = {x,y,z}
59GG.teamStartPoints = teamStartPoints
60local startPointTable = {} --temporary, only for use within this gadget & its libs
61
62local nAllyTeams
63local allyTeams = {} --allyTeams[allyTeamID] is non-nil if this allyTeam will spawn at least one starting unit
64
65----------------------------------------------------------------
66-- Start Point Guesser
67----------------------------------------------------------------
68
69include("luarules/gadgets/lib_startpoint_guesser.lua") --start point guessing routines
70
71----------------------------------------------------------------
72-- FFA Startpoints (modoption)
73----------------------------------------------------------------
74
75-- ffaStartPoints is "global"
76local useFFAStartPoints = false
77if (tonumber(Spring.GetModOptions().mo_ffa) or 0) == 1 then
78    useFFAStartPoints = true
79end
80
81
82function GetFFAStartPoints()
83    include("luarules/configs/ffa_startpoints/ffa_startpoints.lua") -- if we have a ffa start points config for this map, use it
84    if not ffaStartPoints and VFS.FileExists("luarules/configs/ffa_startpoints.lua") then
85        include("luarules/configs/ffa_startpoints.lua") -- if we don't have one, see if the map has one
86    end
87end
88
89----------------------------------------------------------------
90-- NewbiePlacer (modoption)
91----------------------------------------------------------------
92
93--Newbie Placer (prevents newbies from choosing their own a startpoint and faction)
94local NewbiePlacer
95local processedNewbies = false
96if (tonumber((Spring.GetModOptions() or {}).mo_newbie_placer) == 1) and (Game.startPosType == 2) then
97        NewbiePlacer = true
98else
99        NewbiePlacer = false
100end
101
102--check if a player is to be considered as a 'newbie', in terms of startpoint placements
103function isPlayerNewbie(pID)
104        local customtable
105        local name,_,isSpec,tID,_,_,_,_,pRank = Spring.GetPlayerInfo(pID)
106        playerRank = tonumber(pRank) or 0
107        customtable = select(10,Spring.GetPlayerInfo(pID)) or {}
108        local tsMu = tostring(customtable.skill) or ""
109        local tsSigma = tonumber(customtable.skilluncertainty) or 3
110        local isNewbie
111        if pRank == 0 and (string.find(tsMu, ")") or tsSigma >= 3) then --rank 0 and not enough ts data
112                isNewbie = true
113        else
114                isNewbie = false
115        end
116        return isNewbie
117end
118
119--a team is a newbie team if it contains at least one newbie player
120function isNewbie(teamID)
121        if not NewbiePlacer then return false end
122        local playerList = Spring.GetPlayerList(teamID) or {}
123        local isNewbie = false
124        for _,playerID in pairs(playerList) do
125                if playerID then
126                local _,_,isSpec,_ = Spring.GetPlayerInfo(playerID)
127                        if not isSpec then
128                                isNewbie = isNewbie or isPlayerNewbie(playerID)
129                        end
130                end
131        end
132        return isNewbie
133end
134
135----------------------------------------------------------------
136-- NoCloseSpawns (modoption)
137----------------------------------------------------------------
138
139local NoCloseSpawns
140local closeSpawnDist = 350
141local mapx = Game.mapX
142local mapz = Game.mapY -- misnomer in API
143local smallmap = (mapx^2 + mapz^2 < 6^2) --TODO: improve this
144if (tonumber(Spring.GetModOptions().mo_no_close_spawns) or 1) and (Game.startPosType ~= 2) and smallmap then --don't load if modoptions says not too or if start pos placement is not 'choose in game' or if map is small
145        NoCloseSpawns = true
146else
147        NoCloseSpawns = false
148end
149
150
151----------------------------------------------------------------
152-- Initialize
153----------------------------------------------------------------
154function gadget:Initialize()
155        local gaiaTeamID = Spring.GetGaiaTeamID()
156        local teamList = Spring.GetTeamList()
157        for i = 1, #teamList do
158                local teamID = teamList[i]
159                if teamID ~= gaiaTeamID then
160                        --set & broadcast (current) start unit
161                        local _, _, _, _, teamSide, teamAllyID = spGetTeamInfo(teamID)
162                        if teamSide == 'core' then
163                                spSetTeamRulesParam(teamID, startUnitParamName, corcomDefID)
164                        else
165                                spSetTeamRulesParam(teamID, startUnitParamName, armcomDefID)
166                        end
167                        spawnTeams[teamID] = teamAllyID
168                       
169                        --broadcast if newbie
170                        local newbieParam
171                        if isNewbie(teamID) then
172                                newbieParam = 1
173                        else
174                                newbieParam = 0
175                        end
176                        spSetTeamRulesParam(teamID, 'isNewbie', newbieParam, {public=true}) --visible to all; some widgets (faction choose, initial queue) need to know if its a newbie -> they unload
177               
178                        --record that this allyteam will spawn something
179                        local _,_,_,_,_,allyTeamID = Spring.GetTeamInfo(teamID)
180                        allyTeams[allyTeamID] = allyTeamID
181                end
182        end
183        processedNewbies = true
184       
185        -- count allyteams
186        nAllyTeams = 0
187        for k,v in pairs(allyTeams) do
188                nAllyTeams = nAllyTeams + 1
189        end
190       
191    -- count teams
192    nSpawnTeams = 0
193    for k,v in pairs(spawnTeams) do
194        nSpawnTeams = nSpawnTeams + 1
195    end
196   
197        -- mark all players as 'not yet placed'
198        local initState
199        if Game.startPosType ~= 2 or ffaStartPoints then
200                initState = -1 -- if players won't be allowed to place startpoints
201        else
202                initState = 0 -- players will be allowed to place startpoints
203        end
204        local playerList = Spring.GetPlayerList()
205        for _,playerID in pairs(playerList) do
206                Spring.SetGameRulesParam("player_" .. playerID .. "_readyState" , initState)
207        end
208       
209end
210
211
212----------------------------------------------------------------
213-- Factions
214----------------------------------------------------------------
215
216-- keep track of choosing faction ingame
217function gadget:RecvLuaMsg(msg, playerID)
218        local startUnit = tonumber(msg:match(changeStartUnitRegex))
219        if startUnit and validStartUnits[startUnit] then
220                local _, _, playerIsSpec, playerTeam = spGetPlayerInfo(playerID)
221                if not playerIsSpec then
222                        playerStartingUnits[playerID] = startUnit
223                        spSetTeamRulesParam(playerTeam, startUnitParamName, startUnit, {allied=true, public=false}) -- visible to allies only, set visible to all on GameStart
224                        return true
225                end
226        end
227end
228
229
230----------------------------------------------------------------
231-- Startpoints
232----------------------------------------------------------------
233
234function gadget:AllowStartPosition(x,y,z,playerID,readyState)
235    -- readyState:
236        -- 0: player did not place startpoint, is unready
237    -- 1: game starting, player is ready
238    -- 2: player pressed ready OR game is starting and player is forcibly readied (note: if the player chose a startpoint, reconnected and pressed ready without re-placing, this case will have the wrong x,z)
239    -- 3: game forcestarted & player absent
240
241        -- we also add the following
242        -- -1: players will not be allowed to place startpoints; automatically readied once ingame
243        --  4: player has placed a startpoint but is not yet ready
244       
245        -- communicate readyState to all
246    Spring.SetGameRulesParam("player_" .. playerID .. "_readyState" , readyState)
247   
248    --[[
249    -- for debugging
250    local name,_,_,tID = Spring.GetPlayerInfo(playerID)
251    Spring.Echo(name,tID,x,z,readyState, (startPointTable[tID]~=nil))
252    Spring.MarkerAddPoint(x,y,z,name .. " " .. readyState)
253        ]]
254   
255        if Game.startPosType ~= 2 then return true end -- accept blindly unless we are in choose-in-game mode
256        if useFFAStartPoints then return true end
257       
258        local _,_,_,teamID,allyTeamID,_,_,_,_,_ = Spring.GetPlayerInfo(playerID)
259        if not teamID or not allyTeamID then return false end --fail
260       
261        -- NewbiePlacer
262        if NewbiePlacer then
263                if not processedNewbies then return false end
264                if readyState == 0 and Spring.GetTeamRulesParam(teamID, 'isNewbie') == 1 then
265                        return false
266                end
267        end
268       
269        -- don't allow player to place startpoint unless its inside the startbox, if we have a startbox
270        if allyTeamID == nil then return false end
271        local xmin, zmin, xmax, zmax = spGetAllyTeamStartBox(allyTeamID)
272        if xmin>=xmax or zmin>=zmax then
273                return true
274        else
275                local isOutsideStartbox = (xmin+1 >= x) or (x >= xmax-1) or (zmin+1 >= z) or (z >= zmax-1) -- the engine rounds startpoints to integers but does not round the startbox (wtf)
276                if isOutsideStartbox then
277                        return false
278                end
279        end
280       
281        -- NoCloseSpawns
282        for otherTeamID,startpoint in pairs(startPointTable) do
283                local sx,sz = startpoint[1],startpoint[2]
284                local tooClose = ((x-sx)^2+(z-sz)^2 <= closeSpawnDist^2)
285                local sameTeam = (teamID == otherTeamID)
286                local _,_,_,_,_,otherAllyTeamID = Spring.GetTeamInfo(otherTeamID)
287                local sameAllyTeam = (allyTeamID == otherAllyTeamID)
288                if (sx>0) and tooClose and sameAllyTeam and not sameTeam then
289                        Spring.SendMessageToPlayer(playerID,"You cannot place your start position too close to another player")
290                        return false
291                end
292        end
293               
294        -- record table of starting points for startpoint assist to use
295        if readyState == 2 then
296        -- player pressed ready (we have already recorded their startpoint when they placed it) OR game was force started and player is forcibly readied
297                if not startPointTable[teamID] then
298            startPointTable[teamID]={-5000,-5000} -- if the player was forcibly readied without having placed a startpoint, place an invalid one far away (thats what the StartPointGuesser wants)
299        end
300    else               
301        -- player placed startpoint OR game is starting and player is ready
302        startPointTable[teamID]={x,z}
303                if readyState ~= 1 then
304            -- game is not starting (therefore, player cannot yet have pressed ready)
305            Spring.SetGameRulesParam("player_" .. playerID .. "_readyState" , 4)
306            SendToUnsynced("StartPointChosen", playerID)
307                end
308        end     
309       
310        return true
311end
312
313
314----------------------------------------------------------------
315-- Spawning
316----------------------------------------------------------------
317
318function gadget:GameStart()
319        GetFFAStartPoints()             
320
321    -- ffa mode spawning
322    if useFFAStartPoints and ffaStartPoints and ffaStartPoints[nAllyTeams] and #(ffaStartPoints[nAllyTeams])==nAllyTeams then
323                -- cycle over ally teams and spawn starting units
324        local allyTeamSpawn = SetPermutedSpawns(nAllyTeams, allyTeams)           
325                        for teamID, allyTeamID in pairs(spawnTeams) do
326            SpawnFFAStartUnit(nAllyTeams, allyTeamSpawn[allyTeamID], teamID)
327                        end
328                        return
329                end
330
331    -- use ffa mode startpoints for random spawning, if possible, but per team instead of per allyTeam
332    if Game.startPosType==1 and ffaStartPoints and ffaStartPoints[nSpawnTeams] and #(ffaStartPoints[nSpawnTeams])==nSpawnTeams then
333        local teamSpawn = SetPermutedSpawns(nSpawnTeams, spawnTeams)
334        for teamID, allyTeamID in pairs(spawnTeams) do       
335            SpawnFFAStartUnit(nSpawnTeams, teamSpawn[teamID], teamID)
336        end
337        return
338        end
339       
340        -- normal spawning (also used as fallback if ffaStartPoints fails)
341        -- cycle through teams and call spawn team starting unit
342        for teamID, allyTeamID in pairs(spawnTeams) do
343                SpawnTeamStartUnit(teamID, allyTeamID)
344        end     
345end
346
347function SetPermutedSpawns(nSpawns, idsToSpawn)
348    -- this function assumes that idsToSpawn is a hash table with nSpawns elements   
349    -- returns a bijective random map from key values of idsToSpawn to [1,...,nSpawns]
350
351    -- first, construct a random permutation of [1,...,nSpawns] using a Knuth shuffle
352        local perm = {}
353    for i=1,nSpawns do
354                perm[i] = i
355        end
356    for i=1,nSpawns-1 do
357        local j = math.random(i,nSpawns)
358                local temp = perm[i]
359                perm[i] = perm[j]
360                perm[j] = temp
361        end
362
363    local permutedSpawns = {}
364        local slot = 1
365    for id,_ in pairs(idsToSpawn) do
366        permutedSpawns[id] = perm[slot]
367                slot = slot + 1
368        end
369    return permutedSpawns
370end
371
372function SpawnFFAStartUnit(nSpawns, spawnID, teamID)
373        -- get allyTeam start pos
374    local startPos = ffaStartPoints[nSpawns][spawnID]
375        local x = startPos.x
376        local z = startPos.z
377       
378        -- get team start pos; randomly move slightly to make it look nicer and (w.h.p.) avoid coms in same place in team ffa
379        local r = math.random(50,120)
380        local theta = math.random(100) / 100 * 2 * math.pi
381        local cx = x + r*math.cos(theta)
382        local cz = z + r*math.sin(theta)
383        if not IsSteep(cx,cz) then --IsSteep comes from lib_startpoint_guesser, returns true if pos is too steep for com to walk on
384                x = cx
385                z = cz
386        end
387       
388        -- spawn
389        SpawnStartUnit(teamID, x, z)
390end
391
392
393function SpawnTeamStartUnit(teamID, allyTeamID)
394        local x,_,z = Spring.GetTeamStartPosition(teamID)
395        local xmin, zmin, xmax, zmax = spGetAllyTeamStartBox(allyTeamID)
396
397        -- if its choose-in-game mode, see if we need to autoplace anyone
398        if Game.startPosType==2 then
399                if ((not startPointTable[teamID]) or (startPointTable[teamID][1] < 0)) then
400                        -- guess points for the ones classified in startPointTable as not genuine (newbies will not have a genuine startpoint)
401                        x,z=GuessStartSpot(teamID, allyID, xmin, zmin, xmax, zmax, startPointTable)
402                else
403                        --fallback
404                        if x<=0 or z<=0 then
405                                x = (xmin + xmax) / 2
406                                z = (zmin + zmax) / 2
407                        end
408                end
409        end
410       
411        --spawn
412        SpawnStartUnit(teamID, x, z)
413end
414
415
416function SpawnStartUnit(teamID, x, z)
417        --get starting unit
418        local startUnit = spGetTeamRulesParam(teamID, startUnitParamName)
419
420        --overwrite startUnit with random faction for newbies
421        if Spring.GetTeamRulesParam(teamID, 'isNewbie') == 1 then
422                if math.random() > 0.5 then
423                        startUnit = corcomDefID
424                else
425                        startUnit = armcomDefID
426                end
427        end
428       
429        --spawn starting unit
430        local y = spGetGroundHeight(x,z)
431        local unitID = spCreateUnit(startUnit, x, y, z, 0, teamID)
432
433        --share info
434        teamStartPoints[teamID] = {x,y,z}
435        spSetTeamRulesParam(teamID, startUnitParamName, startUnit, {public=true}) -- visible to all (and picked up by advpllist)
436
437        --team storage is set up by game_team_resources
438end
439
440function gadget:GameFrame()
441    gadgetHandler:RemoveGadget(self)
442end
443
444
445----------------------------------------------------------------
446-- Unsynced
447else
448----------------------------------------------------------------
449
450local bgcorner = ":n:LuaRules/Images/bgcorner.png"
451local customScale = 1.15
452local uiScale = customScale
453local myPlayerID = Spring.GetMyPlayerID()
454local _,_,spec,myTeamID = Spring.GetPlayerInfo(myPlayerID)
455local amNewbie
456local ffaMode = (tonumber(Spring.GetModOptions().mo_ffa) or 0) == 1
457local isReplay = Spring.IsReplay()
458
459local readied = false --make sure we return true,true for newbies at least once
460local startPointChosen = false
461
462local NETMSG_STARTPLAYING = 4 -- see BaseNetProtocol.h, packetID sent during the 3.2.1 countdown
463local SYSTEM_ID = -1 -- see LuaUnsyncedRead::GetPlayerTraffic, playerID to get hosts traffic from
464local gameStarting
465local timer = 0
466local timer2 = 0
467
468local vsx, vsy = Spring.GetViewGeometry()
469function gadget:ViewResize()
470  vsx,vsy = Spring.GetViewGeometry()
471end
472
473local readyX = vsx * 0.8
474local readyY = vsy * 0.8
475local readyH = 35
476local readyW = 100
477local bgMargin = 2.5
478
479local pStates = {} --local copy of playerStates table
480
481
482local function DrawRectRound(px,py,sx,sy,cs, tl,tr,br,bl)
483        gl.TexCoord(0.8,0.8)
484        gl.Vertex(px+cs, py, 0)
485        gl.Vertex(sx-cs, py, 0)
486        gl.Vertex(sx-cs, sy, 0)
487        gl.Vertex(px+cs, sy, 0)
488       
489        gl.Vertex(px, py+cs, 0)
490        gl.Vertex(px+cs, py+cs, 0)
491        gl.Vertex(px+cs, sy-cs, 0)
492        gl.Vertex(px, sy-cs, 0)
493       
494        gl.Vertex(sx, py+cs, 0)
495        gl.Vertex(sx-cs, py+cs, 0)
496        gl.Vertex(sx-cs, sy-cs, 0)
497        gl.Vertex(sx, sy-cs, 0)
498       
499        local offset = 0.07             -- texture offset, because else gaps could show
500        local o = offset
501        -- bottom left
502        --if ((py <= 0 or px <= 0)  or (bl ~= nil and bl == 0)) and bl ~= 2   then o = 0.5 else o = offset end
503        gl.TexCoord(o,o)
504        gl.Vertex(px, py, 0)
505        gl.TexCoord(o,1-o)
506        gl.Vertex(px+cs, py, 0)
507        gl.TexCoord(1-o,1-o)
508        gl.Vertex(px+cs, py+cs, 0)
509        gl.TexCoord(1-o,o)
510        gl.Vertex(px, py+cs, 0)
511        -- bottom right
512        --if ((py <= 0 or sx >= vsx) or (br ~= nil and br == 0)) and br ~= 2   then o = 0.5 else o = offset end
513        gl.TexCoord(o,o)
514        gl.Vertex(sx, py, 0)
515        gl.TexCoord(o,1-o)
516        gl.Vertex(sx-cs, py, 0)
517        gl.TexCoord(1-o,1-o)
518        gl.Vertex(sx-cs, py+cs, 0)
519        gl.TexCoord(1-o,o)
520        gl.Vertex(sx, py+cs, 0)
521        -- top left
522        --if ((sy >= vsy or px <= 0) or (tl ~= nil and tl == 0)) and tl ~= 2   then o = 0.5 else o = offset end
523        gl.TexCoord(o,o)
524        gl.Vertex(px, sy, 0)
525        gl.TexCoord(o,1-o)
526        gl.Vertex(px+cs, sy, 0)
527        gl.TexCoord(1-o,1-o)
528        gl.Vertex(px+cs, sy-cs, 0)
529        gl.TexCoord(1-o,o)
530        gl.Vertex(px, sy-cs, 0)
531        -- top right
532        --if ((sy >= vsy or sx >= vsx)  or (tr ~= nil and tr == 0)) and tr ~= 2   then o = 0.5 else o = offset end
533        gl.TexCoord(o,o)
534        gl.Vertex(sx, sy, 0)
535        gl.TexCoord(o,1-o)
536        gl.Vertex(sx-cs, sy, 0)
537        gl.TexCoord(1-o,1-o)
538        gl.Vertex(sx-cs, sy-cs, 0)
539        gl.TexCoord(1-o,o)
540        gl.Vertex(sx, sy-cs, 0)
541end
542function RectRound(px,py,sx,sy,cs, tl,tr,br,bl)         -- (coordinates work differently than the RectRound func in other widgets)
543        gl.Texture(bgcorner)
544        gl.BeginEnd(GL.QUADS, DrawRectRound, px,py,sx,sy,cs, tl,tr,br,bl)
545        gl.Texture(false)
546end
547
548function StartPointChosen(_,playerID)
549        if playerID == myPlayerID then
550                startPointChosen = true
551                if not readied and Script.LuaUI("PlayerReadyStateChanged") then
552                        Script.LuaUI.PlayerReadyStateChanged(playerID, 4)
553                end
554        end
555end
556
557function gadget:GameSetup(state,ready,playerStates)
558        -- check when the 3.2.1 countdown starts
559        if gameStarting==nil and ((Spring.GetPlayerTraffic(SYSTEM_ID, NETMSG_STARTPLAYING) or 0) > 0) then --ugly but effective (can also detect by parsing state string)
560                gameStarting = true
561        end
562       
563        -- if we can't choose startpositions, no need for ready button etc
564        if Game.startPosType ~= 2 or ffaMode then
565                return true,true
566        end
567       
568        -- notify LuaUI if readyStates have changed
569        for playerID,readyState in pairs(playerStates) do
570                if pStates[playerID] ~= readyState then
571            if Script.LuaUI("PlayerReadyStateChanged") then
572                if readyState == "ready" then
573                    Script.LuaUI.PlayerReadyStateChanged(playerID, 1)
574                elseif readyState == "missing" then
575                    Script.LuaUI.PlayerReadyStateChanged(playerID, 3)
576                else
577                    Script.LuaUI.PlayerReadyStateChanged(playerID, 0) --unready
578                end
579            end
580                        pStates[playerID] = readyState
581                end
582        end
583       
584        -- set my readyState to true if i am a newbie, or if ffa
585        if not readied or not ready then
586                amNewbie = (Spring.GetTeamRulesParam(myTeamID, 'isNewbie') == 1)
587                if amNewbie or ffaMode then
588                        readied = true
589                        return true, true
590                end
591        end
592       
593        if not ready and readied then -- check if we just readied
594                ready = true
595        elseif ready and not readied then       -- check if we just reconnected/dropped
596                ready = false
597        end
598       
599        return true, ready
600end
601
602
603function correctMouseForScaling(x,y)
604        local buttonScreenCenterPosX = (readyX+(readyW/2))/vsx
605        local buttonScreenCenterPosY = (readyY+(readyH/2))/vsy
606        x = x - (((x/vsx)-buttonScreenCenterPosX) * vsx)*((uiScale-1)/uiScale)
607        y = y - (((y/vsy)-buttonScreenCenterPosY) * vsy)*((uiScale-1)/uiScale)
608        return x,y
609end
610
611function gadget:MousePress(sx,sy)
612        -- pressing ready
613        sx,sy = correctMouseForScaling(sx,sy)
614        if sx > readyX-bgMargin and sx < readyX+readyW+bgMargin and sy > readyY-bgMargin and sy < readyY+readyH+bgMargin and Spring.GetGameFrame() <= 0 and Game.startPosType == 2 and gameStarting==nil and not spec then
615                if startPointChosen then
616                        readied = true
617                        return true
618                else
619                        Spring.Echo("Please choose a start point!")
620                end
621        end
622       
623        -- message when trying to place startpoint but can't
624        if amNewbie then
625                local target,_ = Spring.TraceScreenRay(sx,sy)
626                if target == "ground" then
627                        Spring.Echo("In this match, newbies (rank 0) will have a faction and startpoint chosen for them!")
628                end
629        end
630end
631
632function gadget:MouseRelease(x,y)
633        return false
634end
635
636function gadget:Initialize()
637        -- add function to receive when startpoints were chosen
638        gadgetHandler:AddSyncAction("StartPointChosen", StartPointChosen)
639       
640        -- create ready button
641        readyButton = gl.CreateList(function()
642                -- draws background rectangle
643                gl.Color(0,0,0,0.8)
644                RectRound(-((readyW/2)+bgMargin), -((readyH/2)+bgMargin), ((readyW/2)+bgMargin), ((readyH/2)+bgMargin), 6)
645                gl.Color(1,1,1,0.13)
646                RectRound(-readyW/2, -readyH/2, readyW/2, readyH/2, 4)
647                gl.Color(1,1,1,1)
648        end)
649        -- create ready button
650        readyButtonHover = gl.CreateList(function()
651                -- draws background rectangle
652                gl.Color(0.15,0.12,0,0.8)
653                RectRound(-((readyW/2)+bgMargin), -((readyH/2)+bgMargin), ((readyW/2)+bgMargin), ((readyH/2)+bgMargin), 6)
654                RectRound(readyX-bgMargin, readyY-bgMargin, readyX+readyW+bgMargin, readyY+readyH+bgMargin, 5)
655                gl.Color(1,0.8,0.3,0.33)
656                RectRound(-readyW/2, -readyH/2, readyW/2, readyH/2, 4)
657                gl.Color(1,1,1,1)
658        end)
659end
660
661function gadget:DrawScreen()
662  uiScale = (0.75 + (vsx*vsy / 7500000)) * customScale
663        gl.PushMatrix()
664                gl.Translate(readyX+(readyW/2),readyY+(readyH/2),0)
665                gl.Scale(uiScale, uiScale, 1)
666                       
667                if not readied and readyButton and Game.startPosType == 2 and gameStarting==nil and not spec and not isReplay then
668                --if not readied and readyButton and not spec and not isReplay then
669                       
670                        -- draw ready button and text
671                        local x,y = Spring.GetMouseState()
672                        x,y = correctMouseForScaling(x,y)
673                        if x > readyX-bgMargin and x < readyX+readyW+bgMargin and y > readyY-bgMargin and y < readyY+readyH+bgMargin then
674                                gl.CallList(readyButtonHover)
675                                colorString = "\255\255\222\0"
676                        else
677                                gl.CallList(readyButton)
678              timer2 = timer2 + Spring.GetLastUpdateSeconds()
679              if timer2 % 0.75 <= 0.375 then
680                colorString = "\255\233\215\20"
681              else
682                colorString = "\255\255\255\255"
683              end
684                        end
685                        gl.Text(colorString .. "Ready", -((readyW/2)-12.5), -((readyH/2)-9.5), 25, "o")
686                        gl.Color(1,1,1,1)
687                end
688                       
689                if gameStarting and not isReplay then
690                        timer = timer + Spring.GetLastUpdateSeconds()
691                        if timer % 0.75 <= 0.375 then
692                                colorString = "\255\233\233\20"
693                        else
694                                colorString = "\255\255\255\255"
695                        end
696                        local text = colorString .. "Game starting in " .. math.max(1,3-math.floor(timer)) .. " seconds..."
697                        gl.Text(text, vsx*0.5 - gl.GetTextWidth(text)/2*20, vsy*0.71, 20, "o")
698                end
699        gl.PopMatrix()
700       
701        --remove if after gamestart
702        if Spring.GetGameFrame() > 0 then
703                gadgetHandler:RemoveGadget(self)
704                return
705        end
706end
707
708function gadget:Shutdown()
709                gl.DeleteList(readyButton)
710                gl.DeleteList(readyButtonHover)
711end
712
713----------------------------------------------------------------
714end
715----------------------------------------------------------------
716
Note: See TracBrowser for help on using the repository browser.