<?php
define('MY_LIFE', 3);

function SOUL($MY) {return 0xFFFF & $MY;}
function BODY($MY) {return $MY>>0x10;}

class emotion {
    const neutral  = 0; const upbeat = 1; const sleepy = 2; const tired = 3; const pessimistic = 4; 
    const betrayed = 5; const mad    = 6; const afraid = 7; const down  = 8; const left_behind = 9;
};

define('bormand', 'x!( "@"!, #(&!$ $\&!$ %(\'!$ &T&!  \'T& $ !, %0&!$ $X&!$ #$&!$ )$& $ ');

define('p_x', 15);
define('p_y', 10);

$podushka = array_fill(0, p_x*p_y, 0);

function embrace($a, $i) {
    $x = 0;
    for($j = 0; $j<3; ++$j)
        $x += (ord($a[$i+$j])-0x20)<<($j*6);
    return $x;
}

function make_love($x, $y) {
    global $podushka;
    $my = $podushka[$x + p_x*$y];
    $my_soul = SOUL($my);
    if ($my_soul >= strlen(bormand) - 3)
        return;
    $my_happiness = embrace(bormand, $my_soul);
    $dx = 0;
    $dy = 0;
    switch($my_happiness & 0xFF) {
        case emotion::upbeat:
            $podushka[$x + $y*p_x] = $my_soul + MY_LIFE + (($my_happiness & 0xFFFF00)<<8);
            break;
        case emotion::sleepy:
            $dx = -1; goto sleep;
        case emotion::tired:
            $dx = -1; $dy = -1; goto sleep;
        case emotion::left_behind:
            $dx = -1; $dy = 1; goto sleep;
        case emotion::mad:
            $dx = 1; goto sleep;
        case emotion::betrayed:
            $dx = 1; $dy = -1; goto sleep;
        case emotion::afraid:
            $dx = 1; $dy = 1; goto sleep;
        case emotion::pessimistic:
            $dy = -1; goto sleep;
        case emotion::down:
            $dy = 1; goto sleep;
            break;
        default:
            return;
        sleep:
            $podushka[($x+$dx)%p_x + ($y+$dy)%p_y*p_x] = BODY($my) ? $my - 0x10000 : $my_soul + MY_LIFE;
            $podushka[$x + p_x*$y] = $my_happiness>>8;
    }
}

$podushka[8+p_x*9] = 1;
for ($t = 0; $t<20; ++$t) {
    for ($i = 0; $i<p_x*p_y; ++$i) {
        make_love($i%p_x, floor($i/p_x));
    }
}
for ($j = 0; $j<p_y; ++$j) {
    for ($i = 0; $i<p_x; ++$i) {
        $c = $podushka[$i+p_x*$j];
        echo(($c>0x20 && $c<0x7e)?chr($c):' ');
    }
    echo PHP_EOL;
}

return 0;
