indexing description: "Manages pieces that move independently"; deferred class AUTO_MOBILE inherit MOBILE redefine init, react_to_first_movement, restart, clear, win, build_record, customized_init end TM_CLIENT feature{NONE} strategy : STRATEGY is deferred end period : INTEGER is deferred end init( r : like room ; c : like northwest ) is do precursor( r, c ) change_clock( room.clock ) end move_event : TM_EVENT can_move : BOOLEAN is -- Am I able to move? deferred end feature -- save/restore build_record is -- 6 is_moving -- 7 if is_moving is true, then time_remaining do {MOBILE} precursor build_time_remaining ensure then proper_num_rec: record.count > 5 end feature{NONE} -- save/restore -- Used by Anaconda build_time_remaining is do if is_moving then record.extend( 1 ) record.extend( time_remaining ) else record.extend( 0 ) end end -- build_time_remaining -- Used by Anaconda customized_init_time_remaining( pr : like record ) is do change_clock( room.clock ) pr.forth is_moving := (pr.item = 1) if is_moving then pr.forth create move_event.make_delayed_periodic( Current, pr.item, period ) clock.register( move_event ) end end -- customized_init_time_remaining customized_init( r : like room ; pr : like record ) is do precursor( r, pr ) customized_init_time_remaining( pr ) end time_remaining : INTEGER is -- Time remaining before my current event occurs. do Result := move_event.absolute_time - clock.milliseconds end feature start_moving is do is_moving := True create move_event.make_periodic( Current, period ) clock.register( move_event ) ensure is_moving clock.is_present( move_event ) end stop_moving is do if is_moving then is_moving := False clock.cancel( move_event ) end ensure stopped: not is_moving event_canceled: not clock.is_present( move_event ) end wake_up( e : TM_EVENT ) is do try_to_move end is_moving : BOOLEAN try_to_move is -- Try to move according to my current strategy, -- set `success' accordingly. do if can_move then strategy.try_move if success and then not stooge.dead then room.react end end end react_to_first_movement is -- Default is that I start moving as soon as Stoogie does. do if not is_moving then start_moving end react end restart is do stop_moving create move_event.make_periodic( Current, period ) {MOBILE}precursor -- restart strategy? strategy.set_direction( initial_direction ) end clear is -- Cancel my timer and stop moving do {MOBILE}precursor stop_moving end win is do {MOBILE}precursor stop_moving end end