Home | History | Annotate | Line # | Download | only in dab
human.cc revision 1.2.18.1
      1  1.2.18.1      yamt /*	$NetBSD: human.cc,v 1.2.18.1 2008/05/18 12:29:48 yamt Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  christos  * by Christos Zoulas.
      9       1.1  christos  *
     10       1.1  christos  * Redistribution and use in source and binary forms, with or without
     11       1.1  christos  * modification, are permitted provided that the following conditions
     12       1.1  christos  * are met:
     13       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  christos  *    documentation and/or other materials provided with the distribution.
     18       1.1  christos  *
     19       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  christos  */
     31       1.1  christos 
     32       1.1  christos /*
     33       1.1  christos  * human.C: Human interface for dots, using rogue-like keys.
     34       1.1  christos  */
     35       1.1  christos #include "defs.h"
     36  1.2.18.1      yamt RCSID("$NetBSD: human.cc,v 1.2.18.1 2008/05/18 12:29:48 yamt Exp $")
     37       1.1  christos 
     38       1.1  christos #include "human.h"
     39       1.1  christos #include "board.h"
     40       1.1  christos #include "box.h"
     41       1.2       jdc #include "ttyscrn.h"
     42       1.1  christos 
     43       1.1  christos #define CONTROL(a) ((a) & 037)
     44       1.1  christos 
     45       1.2       jdc extern GAMESCREEN *sc;
     46       1.2       jdc 
     47       1.1  christos HUMAN::HUMAN(const char c) :
     48       1.1  christos     PLAYER(c),
     49       1.1  christos     _curx(0),
     50       1.1  christos     _cury(1)
     51       1.1  christos {
     52       1.1  christos }
     53       1.1  christos 
     54       1.1  christos void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir)
     55       1.1  christos {
     56       1.1  christos     int mv;
     57       1.1  christos     b.setpos(_cury, _curx);
     58       1.1  christos 
     59       1.1  christos     for (;;) {
     60       1.1  christos 	switch (mv = b.getmove()) {
     61       1.1  christos 	case 'h': case 'H':
     62       1.1  christos 	    _curx -= 2;
     63       1.1  christos 	    break;
     64       1.1  christos 
     65       1.1  christos 	case 'l': case 'L':
     66       1.1  christos 	    _curx += 2;
     67       1.1  christos 	    break;
     68       1.1  christos 
     69       1.1  christos 	case 'k': case 'K':
     70       1.1  christos 	    _cury -= 2;
     71       1.1  christos 	    break;
     72       1.1  christos 
     73       1.1  christos 	case 'j': case 'J':
     74       1.1  christos 	    _cury += 2;
     75       1.1  christos 	    break;
     76       1.1  christos 
     77       1.1  christos 	case 'u': case 'U':
     78       1.1  christos 	    _curx += 1;
     79       1.1  christos 	    _cury -= 1;
     80       1.1  christos 	    break;
     81       1.1  christos 
     82       1.1  christos 	case 'y': case 'Y':
     83       1.1  christos 	    _curx -= 1;
     84       1.1  christos 	    _cury -= 1;
     85       1.1  christos 	    break;
     86       1.1  christos 
     87       1.1  christos 	case 'b': case 'B':
     88       1.1  christos 	    _curx -= 1;
     89       1.1  christos 	    _cury += 1;
     90       1.1  christos 	    break;
     91       1.1  christos 
     92       1.1  christos 	case 'n': case 'N':
     93       1.1  christos 	    _curx += 1;
     94       1.1  christos 	    _cury += 1;
     95       1.1  christos 	    break;
     96       1.1  christos 
     97       1.1  christos 	case 'q': case 'Q':
     98       1.2       jdc 	    // Cleanup
     99       1.2       jdc 	    delete sc;
    100       1.1  christos 	    exit(0);
    101       1.1  christos 
    102       1.1  christos 	case CONTROL('L'): case CONTROL('R'):
    103       1.1  christos 	    b.clean();
    104       1.1  christos 	    b.paint();
    105       1.1  christos 	    break;
    106       1.1  christos 
    107       1.1  christos 	case ' ':
    108       1.1  christos 	    {
    109       1.1  christos 		x = _curx / 2;
    110       1.1  christos 		y = _cury / 2;
    111       1.1  christos 
    112       1.1  christos 		if (_cury & 1) {
    113       1.1  christos 		    if (_curx == 0)
    114       1.1  christos 			dir = BOX::left;
    115       1.1  christos 		    else {
    116       1.1  christos 			x--;
    117       1.1  christos 			dir = BOX::right;
    118       1.1  christos 		    }
    119       1.1  christos 		}
    120       1.1  christos 
    121       1.1  christos 		if (_curx & 1) {
    122       1.1  christos 		    if (_cury == 0)
    123       1.1  christos 			dir = BOX::top;
    124       1.1  christos 		    else {
    125       1.1  christos 			y--;
    126       1.1  christos 			dir = BOX::bottom;
    127       1.1  christos 		    }
    128       1.1  christos 		}
    129       1.1  christos 	    }
    130       1.1  christos 	    return;
    131       1.1  christos 
    132       1.1  christos 	default:
    133       1.1  christos 	    break;
    134       1.1  christos 	}
    135       1.1  christos 
    136       1.1  christos         // We add 2 before the comparison to avoid underflow
    137       1.1  christos 	if ((2 + _curx) - (_curx & 1) < 2)
    138       1.1  christos 	    _curx = (b.nx() * 2) + (_curx & 1);
    139       1.1  christos 	if (_curx >= (b.nx() * 2) + 1)
    140       1.1  christos 	    _curx = (_curx & 1);
    141       1.1  christos 
    142       1.1  christos 	if ((2 + _cury) - (_cury & 1) < 2)
    143       1.1  christos 	    _cury = (b.ny() * 2) + (_cury & 1);
    144       1.1  christos 	if (_cury >= (b.ny() * 2) + 1)
    145       1.1  christos 	    _cury = (_cury & 1);
    146       1.1  christos 
    147       1.1  christos 	b.setpos(_cury, _curx);
    148       1.1  christos     }
    149       1.1  christos }
    150