File: Union.lua

package info (click to toggle)
blobby 1.1.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,944 kB
  • sloc: cpp: 22,442; xml: 779; python: 56; makefile: 3
file content (284 lines) | stat: -rw-r--r-- 8,445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--UNION
--27.3.2007 - version 2
--Enormator
-- 11.01.12 - insert game-provided physic constants where possible - by ngc92
-- 11.04.15 - updated math helper functions

--Startwerte setzen
--set starting values
OOSinit=false
OSinit=false
OGinit=false
decision=0
funcs=4
oldtouches=0
oldbspeedx=0
serve=false

--Weltkonstanten definieren
--define world constants
blobbymaxjump = 393.625
blobbygroundpos = CONST_GROUND_HEIGHT + CONST_BLOBBY_HEIGHT / 2
netheight = CONST_NET_HEIGHT + CONST_NET_RADIUS -- height is just the rod. for total height, we have to add the net-sphere-radius too

--Hauptfunktionen
--main functions

function OnOpponentServe()
    --initialisiere Funktion
    --initialise function
    OSinit=false
    OGinit=false
    if (OOSinit==false) then --einmal pro Aufruf von OnOpponentServe
        --one time per OnOpponentServe
        decision=decide(1) --Entscheidung fällen
        --make a decision
        OOSinit=true
        serve=false
    end
    dofunc (1, decision, true) --Entscheidung ausführen
    --follow the taken decision
end

function OnServe(ballready)
    --initialisiere Funktion
    --initialise function
    OOSinit=false
    OGinit=false
    if (OSinit==false) then --einmal pro Aufruf von OnServe
        --one time per OnServe
        decision=decide(2) --Entscheidung fällen
        --make a decision
        OSinit=true
        serve=true --Aufschlag auch während OnGame weitermachen
        --continue serve while OnGame
    end
    dofunc (2, decision, true) --Entscheidung ausführen
    --follow the taken decision
end

function OnGame()
    --initialisiere Funktion
    --initialise function
    OOSinit=false
    OSinit=false
    if (OGinit==false) and (serve==false) then --einmal pro Aufruf von OnGame und wenn der Aufschlag schon vorbei ist
        --one time per OnGame and when serve is already over
        decision=decide(3) --Entscheidung faellen
        --make a decision
        OGinit=true
    end
    if (serve==true) then --Falls noch beim Aufschlag
        --if still while serve
        dofunc (2, decision, true) --Der Funktion sagen, dass sie immernoch beim Aufschlag ist
        --tell the function, it's still OnServe
    else
        dofunc (3, decision, true) --Der Funktion sagen, dass sie im Spiel ist
        --tell the function that it's OnGame
    end
    if (ballx()>CONST_BALL_LEFT_NET+CONST_BALL_RADIUS) then --Falls Ball nicht mehr erreichbar
        --If Ball not gettable by Bot
        serve=false --Aufschlagende erzwingen
        --Make an end to the serve
    end
    if (touches()~=oldtouches) or (math.abs(oldbspeedx)~=math.abs(bspeedx())) then --Hat sich die Situation geändert
        --If the situation has changed
        OGinit=false --gleich Entscheidung neu fällen
        --redo decision
        oldtouches=touches()
        oldbspeedx=bspeedx()
    end
end

--Entscheidungsfunktionen
--decision functions

function decide (funcno)
    t1=1
    chosen=1
    chosenret=0
    --Alle Funktionen abfragen und die mit dem größten Return wählen
    --test all functions and take the one with the highest return value
    while (t1 <= funcs) do
        temp=dofunc(funcno,t1,false)
        if (temp > chosenret) then
            chosen=t1
            chosenret=temp
        end
        t1=t1+1
    end
    -- print ("decided:", chosen, chosenret) --Sagt, für wie gut sich die gewählte Funktion hält
    --tells how good the chosen function says to fit
    return chosen
end

function dofunc (funcno, actionno, action) --Weist jeder Aktionsnummer den echten Funktionsnamen zu
    --converts actionnumbers to the real names of the functions
    if (actionno==1) then
        return std45deg (funcno, action)
    end
    if (actionno==2) then
        return takelow (funcno, action)
    end
    if (actionno==3) then
        return wait (funcno, action)
    end
    if (actionno==4) then
        return twohitserve1 (funcno, action)
    end
    return false
end

--Ausf�hrbare Funktionen
--executable functions

function std45deg (funcno, action) --spielt Ball aus der Luft bei maxjump im 45° Winkel an
    --plays ball in the air at height maxjump with 45° angle
    --funcno(s)=2,3
    local maxjump  = blobbymaxjump
    local distance = 32.25
    local targetx  = estimatex(maxjump) - distance

    if funcno==1 then
        return -1
    end

    if action==false then
        if funcno==2 and action==false then
            return math.random(10, 100)
        end
        if funcno==3 and action==false then
            if targetx ~= math.huge and bspeedx() <= 3 and ball_time_to_y(maxjump) >= math.max(blob_time_to_y(maxjump), blob_time_to_x(targetx)) then
                if estimhitnet()==true and blob_time_to_x(CONST_BALL_LEFT_NET)<=ball_time_to_y(netheight) then
                    return 190
                end
                if bspeedx()==0 then
                    return 85
                else
                    return math.min((10^(-math.abs(bspeedx()))+1),1)*85
                end
            else
                return 0
            end
        end
    end
    if action==true then
        if funcno==2 then
            if posy()==144.5 then
                moveto (targetx)
            end
            if math.abs(posx()-targetx)<4.5 and ballx()==200 and bally()==299.5 then
                jump()
            end
        end
        if funcno==3 then
            moveto (targetx)
            if (bally()<580) and (bspeedy()<0) then
                jump()
            end
        end
        if jumpcase then
            jump()
        end
    end
end

function takelow (funcno, action) --Ballannahme ohne Sprung zum Stellen (bspeedx()=0) selten exakt
    --take ball without jump to create a ball with bspeedx()=0 (sellen exactly)
    --funcno(s)=3
    if funcno==1 or funcno==2 then
        return -1
    end
    if action==false then
        if touches()<=1 then
            return 1
        else
            return -1
        end
    end
    if action==true then
        local target = estimatex( CONST_BALL_BLOBBY_HEAD )
        if target ~= math.huge then
            moveto ( target )
        else
            moveto( 200 )
        end
    end
end

function wait (funcno, action) --Auf guter Position auf eine gegnerische Aktion warten
    --wait for an opponent's move at a good position
    --funcno(s)=1,3
    if funcno==2 then
        return -1
    end
    if funcno==1 and action==false then
        return 200
    end
    if funcno==3 and action==false then
        local estimpos = estimatex( CONST_BALL_BLOBBY_HEAD )
        if ( estimpos > 424.5 and estimpos ~= math.huge) then
            return 200
        else
            return -1
        end
    end
    if action==true then
        moveto (180)
    end
end

function twohitserve1 (funcno, action) --Aufschlag: Stellen (bspeedx()=0), rüber
    --serve: up (bspeedx()=0), play
    --funcno(s)=2
    if funcno==1 or funcno==3 then
        return -1
    end
    if action==false then
        return math.random(10,100)
    else
        if touches()==0 then
            moveto (200)
            if math.abs(posx()-200)<5 then
                jump()
            end
        end
        if touches()==1 then
            moveto (estimatex(blobbymaxjump)-45)
            if bally()<580 and bspeedy()<0 then
                jump()
            end
        end
        if touches()==3 then
            serve=false
        end
    end
end

--mathematische Hilfsfunktionen
--mathematical helpers

function estimatex(destY) --gibt möglichst genaue Angabe der X-Koordinate zurück, bei der sich der Ball befindet, wenn er sich bei der angegebenen Y Koordinate befindet
    --returns exact ballx when bally will be given destY
    if bspeedy()==0 and bspeedx()==0 then
        return ballx()
    end
    local time1 = ball_time_to_y(destY, balldata())
    -- return #inf if time is #inf, i.e. destY will never be reached
    if time1 == math.huge then
        return time1
    end
    local resultX = estimx(math.ceil(time1))
    return resultX
end

function estimhitnet() --Wird der Ball das Netz treffen (bool)
    --Will the ball hit the net (bool)
    safety=5
    return (361.5-safety < estimatex(323)) and (estimatex(323) < 438.5+safety)
end

function estimatey (x) --Y Position des Balls, wenn er sich an der angegebenen X Koordinate befindet
    --y position of the ball, when it is at the given x coordinate
    return estimy(ball_time_to_x(x))
end