Language:
None specified     Change language:
Pastebin: 92295
Author: qfox
Subject: fceu basicbot
Created: 2008-07-23 17:15:36
Download and save
Toggle line numbers
1local key;          -- table to hold the future keys pressed 
2local lastkey;      -- keys pressed in previous frame 
3local frames = 0;   -- number of frames (current value is current frame count) 
4local attempts = 1; -- number of attempts (current value is current attempt) 
5local okattempts = 0; 
6local failattempts = 0; 
7 
8local maxframes = 100; 
9local maxattempts = 5; 
10 
11local records = {}; 
12 
13local function getOk() -- is current run ok? heuristic value 0-100! 100 = always yes, 0 = always no 
14 return 100; 
15end 
16 
17local function getScore() -- score of current attempt 
18 return math.random(0,100); 
19end 
20 
21local function getTie1() -- tie breaker of current attempt in case score is equal 
22 return math.random(0,100); 
23end 
24 
25local function getTie2() -- second tie breaker 
26 return math.random(0,100); 
27end 
28 
29local function getTie3() -- third tie breaker 
30 return math.random(0,100); 
31end 
32 
33local function getTie4() -- fourth tie breaker 
34 return math.random(0,100); 
35end 
36 
37local function getSegmentEnd() 
38 if (attempts >= maxattempts) then return 100 end; 
39 return 0; 
40end 
41 
42local function getAttemptEnd() -- determine when a attempt should be restarted 
43 if (frames >= maxframes) then return 100 end; 
44 return 0; 
45end 
46 
47local function getA() 
48 return math.random(0,100); 
49end; 
50 
51local function getB() 
52 return math.random(0,100); 
53end; 
54 
55local function getStart() 
56 return math.random(0,100); 
57end; 
58 
59local function getSelect() 
60 return math.random(0,100); 
61end; 
62 
63local function getUp() 
64 return math.random(0,100); 
65end; 
66 
67local function getDown() 
68 return math.random(0,100); 
69end; 
70 
71local function getLeft() 
72 return math.random(0,100); 
73end; 
74 
75local function getRight() 
76 return math.random(0,100); 
77end; 
78 
79-- This will loops for each frame, at the end of the while 
80-- the frameadvance is called, causing it to advance 
81while (getSegmentEnd() <= math.random(0,99)) do 
82 frames = frames + 1; -- inrease the frame count (++frames?) 
83 if (getAttemptEnd() > math.random(0, 99)) then -- load save state, continue with next attempt 
84 frames = 1; 
85 -- record this attempt 
86 if (not records.prev) then records.prev = {}; end; 
87 records.prev.frames = frames; 
88 records.prev.attempt = attempts; 
89 records.prev.score = getScore(); 
90 records.prev.tie1 = getTie1(); 
91 records.prev.tie2 = getTie2(); 
92 records.prev.tie3 = getTie3(); 
93 records.prev.tie4 = getTie4(); 
94 records.prev.ok = (getOk() > math.random(0,99)); -- this is the check whether this attempt was valid or not. if not, it cannot become the best attempt. 
95 -- update ok/failed attempt counters 
96 if (records.prev.ok) then 
97 okattempts = okattempts + 1; 
98 else 
99 failattempts = failattempts + 1; 
100 end; 
101 -- if this attempt was better then the previous one, replace it 
102 -- its a long IF, but all it checks (lazy eval) is whether the current 
103 -- score is better then the previous one, or if its equal and the tie1 
104 -- is better then the previous tie1 or if the tie1 is equal to the prev 
105 -- etc... for all four ties. Only tie4 actually needs to be better, tie1 
106 -- through tie3 can be equal as well, as long as the next tie breaks the 
107 -- same tie of the previous attempt :) 
108 if (records.prev.ok and (not records.best or (getScore() > records.best.score or (getScore() == records.best.score and (getTie1() > records.best.tie1 or (getTie1() == records.best.tie1 and (getTie1() > records.best.tie1 or (getTie1() == records.best.tie1 and (getTie1() > records.best.tie1 or (getTie1() == records.best.tie1 and getTie1() > records.best.tie1)))))))))) then 
109 -- previous attempt was better then current best (or no current best 
110 -- exists), so we (re)place it. 
111 if (not records.best) then records.best = {}; end; 
112 records.best.frames = records.prev.frames; 
113 records.best.attempt = records.prev.attempt; 
114 records.best.score = records.prev.score; 
115 records.best.tie1 = records.prev.tie1; 
116 records.best.tie2 = records.prev.tie2; 
117 records.best.tie3 = records.prev.tie3; 
118 records.best.tie4 = records.prev.tie4; 
119 end 
120 attempts = attempts + 1; 
121 end -- continues with new attempt 
122 gui.text(10,10,"Attempt: "..attempts.."\nFrame: "..frames); 
123 
124 key = {}; 
125 if (getUp() > math.random(0, 99) )    then key.up = 1; end; 
126 if (getDown() > math.random(0, 99))   then key.down = 1; end; 
127 if (getLeft() > math.random(0, 99))   then key.left = 1; end; 
128 if (getRight() > math.random(0, 99))  then key.right = 1; end; 
129 if (getA() > math.random(0, 99))      then key.A = 1; end; 
130 if (getB() > math.random(0, 99))      then key.B = 1; end; 
131 if (getSelect() > math.random(0, 99)) then key.select = 1; end; 
132 --if (getStart() > math.random(0, 99))  then key.start = 1; end; 
133 lastkey = key; 
134 joypad.set(1, key); 
135 -- next frame 
136 FCEU.frameadvance() 
137end; 
138 
Download and save
Toggle line numbers
Thread:
[92295] fceu basicbot by qfox at 2008-07-23 17:15:36
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.