/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.examples.pacman

/**
 * By increasing the tick we slow down the time to the next move.
 * I use the CE 'or' here rathre than an infix "in" to maximise node sharing
 * with both the EatFood and EatPowerPill rules.
 */
rule SlowWhenEating dialect "mvel" no-loop salience 10when
    $char : Character( name == "Pacman" )
    $l : Location( character == $char )
    $target : Cell( row == $l.row, col == $l.col)
    (or $contents : CellContents( cell == $target, cellType == CellType.FOOD )
        $contents : CellContents( cell == $target, cellType == CellType.POWER_PILL ) )    
    $update : ScheduledLocationUpdate( character == $char )
then  
    modify ( $update ) { tock += 2 };
end

/**
 * When we move onto a FOOD cell, increase the score by 1
 */
rule EatFood dialect "mvel" when
    $char : Character( name == "Pacman" )
    $l : Location( character == $char )
    $target : Cell( row == $l.row, col == $l.col)
    $contents : CellContents( cell == $target, cellType == CellType.FOOD )
    $s : Score()   
then
    modify( $contents ) { cellType = CellType.EMPTY };
    modify( $s ) { score += 1 };    
end

/**
 * When we move onto a POWER_PILL cell, increase the score by 5
 */
rule EatPowerPill dialect "mvel" when
    $char : Character( name == "Pacman" )
    $l : Location( character == $char )
    $target : Cell( row == $l.row, col == $l.col)
    $contents : CellContents( cell == $target, cellType == CellType.POWER_PILL )
    $s : Score()   
then
    modify( $contents ) { cellType = CellType.EMPTY };
    modify( $s ) { score += 5 };    
end



rule MonsterCollision dialect "mvel" no-loop when
    $pac    : Character( name == "Pacman" )
    $pacLoc : Location( character == $pac )
    $mon    : Character( name == "Monster" )
    $monLoc : Location( character == $mon, col == $pacLoc.col, row == $pacLoc.row )
    $t : Tick()
then
    delete( $t );
end
