bugfix: safety against breaking the AI if you press undo while it's thinking
[kaya.git] / lib / plugins / engines / gpsshogi.rb
bloba0b5b861178a1cb68dd6c3b99fb81ce3d7ce9bea
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'plugins/plugin'
9 require 'interaction/match'
10 require_bundle 'engines', 'engine'
12 class GPSShogiEngine < Engine
13   include Plugin
14   
15   plugin :name => 'GPSShogi Engine Protocol',
16          :protocol => 'GPSShogi',
17          :interface => :engine
18   
19   def on_move(data)
20     text = @serializer.serialize(data[:move], data[:old_state])
21     send_command text
22   end
23   
24   def on_engine_start
25     send_command "new"
26     if @color == :black
27       send_command "black"
28       send_command "go"
29     else
30       send_command "white"
31     end
32   end
33   
34   def extra_command(text)
35     if text =~ /^[0-9]*\. (\.\.\.) (\S+)/
36       move = @serializer.deserialize($2, @match.state)
37       if move
38         @match.move(self, move)
39       end
40     end
41   end
42   
43   def on_close(data)
44     send_command "quit"
45     @engine.kill
46   end
47   
48   def allow_undo?(player, manager)
49     # gpsshogi does not wait when you tell it to undo
50     # so we stop it from playing before calling undo
51     if @match.current_player == self
52       warn "Please wait until GPSShogi moves before undoing."
53     else
54       send_command "force"
55       send_command "undo"
56       send_command "undo"
57       if @color == :black
58         send_command "black"
59       else
60         send_command "white"
61       end
62       manager.undo(self, 2)
63     end
64   end
65 end