File: reduced.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 (124 lines) | stat: -rw-r--r-- 2,837 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
-- Reduced but very effective bot.
-- Borrows from axji.lua, com_10.lua and com_11.lua as distributed with
-- Blobby Volley.
-- Copyright (C) 2010 Soeren D. Schulze
-- The terms of the GPLv2 or later apply.
-- See the Blobby Volley distribution for the license text.

-- 15.01.14 - ngc92: Use blobby volley api provided constants when possible
-- 22.09.15 - ngc92: Adapted to new API, clean up

modeLock = false
timeto = 0
target = 0
naivetarget = 0
estimbspeedx = 0
servrand = nil

function OnServe(ballready)
   if servrand == nil then
      servrand = math.random()
   end
   
   if moveto(ballx() + servrand * 5) and ballready then
      jump()
      servrand = nil
   end
end	

function OnOpponentServe()
   moveto(100)
end

function OnGame()
   -- try high play
   if(estimImpactHigh()) then
		if naivetarget < CONST_FIELD_MIDDLE and target < CONST_FIELD_MIDDLE
			and (modeLock == true or timeto > math.abs(posx()-highPlayPos())/4.5 + 26)
			and touches() < 3
		then
			modeLock = timeto < 30
			if modeLock == false then
				servrand = nil
			end
			highPlay()
			return
		end
   end

	-- otherwise, low play
	modeLock = false
	servrand = nil
	
	local balldir = estimbspeedx > 0 and 1 or -1
	
	if(estimImpactLow()) then
		if (timeto > (balldir*(target-posx()) - 10)/CONST_BLOBBY_SPEED
			or naivetarget >= CONST_FIELD_MIDDLE) 
		then
			lowPlay()
		-- HEELLPPP...
		elseif naivetarget < CONST_FIELD_MIDDLE then
--			print("PANIC!", ballx(), bally())
			-- This often saves your ass if you're standing inside a
			-- corner and the ball bounces from the wall or the net.
			lowPlay()
			jump()
		end
	end
end

function highPlayPos()
   if estimbspeedx < 0 then
      -- safety againt fast balls
      return target - 50 - estimbspeedx*5
   else
      return target - 50
   end
end

function highPlay()
   if (target > CONST_FIELD_MIDDLE) then
      moveto(100)
   else
      moveto(highPlayPos())
      -- 33 time units for jumping to max height
      -- Regarding naive target here.
      -- If the ball really bounces back, it would be a bad idea to jump...
      if servrand == nil then
		servrand = math.random()
      end
      if naivetarget < CONST_FIELD_MIDDLE and timeto < 28 + servrand then
		jump()
      end
   end
end


function lowPlay()
   if (target > CONST_FIELD_MIDDLE) then
      moveto(100)
   else
      moveto(target)
   end
end

function estimImpactHigh()
	return estimImpact(CONST_BLOBBY_MAX_JUMP - 25)
end

function estimImpactLow()
	return estimImpact(CONST_BALL_BLOBBY_HEAD)
end

function estimImpact(destY) -- erlaubt ein besseres Estimate mit ein paar unbeding nötigen Angaben
   local x, v, t = estimate_x_at_y(destY)
   if (t == math.huge) then
      target = nil
      return false
   end
   
   naivetarget = (bspeedx() * t) + ballx()
   target, estimbspeedx, timeto = x, v, t
   return true
end