Home | History | Annotate | Line # | Download | only in dab
      1  1.4    rillig /*	$NetBSD: human.cc,v 1.4 2021/12/05 09:22:45 rillig 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.4    rillig  * Human interface for dots, using rogue-like keys.
     34  1.1  christos  */
     35  1.4    rillig 
     36  1.1  christos #include "defs.h"
     37  1.4    rillig RCSID("$NetBSD: human.cc,v 1.4 2021/12/05 09:22:45 rillig Exp $")
     38  1.1  christos 
     39  1.1  christos #include "human.h"
     40  1.1  christos #include "board.h"
     41  1.1  christos #include "box.h"
     42  1.2       jdc #include "ttyscrn.h"
     43  1.1  christos 
     44  1.1  christos #define CONTROL(a) ((a) & 037)
     45  1.1  christos 
     46  1.2       jdc extern GAMESCREEN *sc;
     47  1.2       jdc 
     48  1.1  christos HUMAN::HUMAN(const char c) :
     49  1.1  christos     PLAYER(c),
     50  1.1  christos     _curx(0),
     51  1.1  christos     _cury(1)
     52  1.1  christos {
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir)
     56  1.1  christos {
     57  1.1  christos     int mv;
     58  1.1  christos     b.setpos(_cury, _curx);
     59  1.1  christos 
     60  1.1  christos     for (;;) {
     61  1.1  christos 	switch (mv = b.getmove()) {
     62  1.1  christos 	case 'h': case 'H':
     63  1.1  christos 	    _curx -= 2;
     64  1.1  christos 	    break;
     65  1.1  christos 
     66  1.1  christos 	case 'l': case 'L':
     67  1.1  christos 	    _curx += 2;
     68  1.1  christos 	    break;
     69  1.1  christos 
     70  1.1  christos 	case 'k': case 'K':
     71  1.1  christos 	    _cury -= 2;
     72  1.1  christos 	    break;
     73  1.1  christos 
     74  1.1  christos 	case 'j': case 'J':
     75  1.1  christos 	    _cury += 2;
     76  1.1  christos 	    break;
     77  1.1  christos 
     78  1.1  christos 	case 'u': case 'U':
     79  1.1  christos 	    _curx += 1;
     80  1.1  christos 	    _cury -= 1;
     81  1.1  christos 	    break;
     82  1.1  christos 
     83  1.1  christos 	case 'y': case 'Y':
     84  1.1  christos 	    _curx -= 1;
     85  1.1  christos 	    _cury -= 1;
     86  1.1  christos 	    break;
     87  1.1  christos 
     88  1.1  christos 	case 'b': case 'B':
     89  1.1  christos 	    _curx -= 1;
     90  1.1  christos 	    _cury += 1;
     91  1.1  christos 	    break;
     92  1.1  christos 
     93  1.1  christos 	case 'n': case 'N':
     94  1.1  christos 	    _curx += 1;
     95  1.1  christos 	    _cury += 1;
     96  1.1  christos 	    break;
     97  1.1  christos 
     98  1.1  christos 	case 'q': case 'Q':
     99  1.2       jdc 	    // Cleanup
    100  1.2       jdc 	    delete sc;
    101  1.1  christos 	    exit(0);
    102  1.1  christos 
    103  1.1  christos 	case CONTROL('L'): case CONTROL('R'):
    104  1.1  christos 	    b.clean();
    105  1.1  christos 	    b.paint();
    106  1.1  christos 	    break;
    107  1.1  christos 
    108  1.1  christos 	case ' ':
    109  1.1  christos 	    {
    110  1.1  christos 		x = _curx / 2;
    111  1.1  christos 		y = _cury / 2;
    112  1.1  christos 
    113  1.1  christos 		if (_cury & 1) {
    114  1.1  christos 		    if (_curx == 0)
    115  1.1  christos 			dir = BOX::left;
    116  1.1  christos 		    else {
    117  1.1  christos 			x--;
    118  1.1  christos 			dir = BOX::right;
    119  1.1  christos 		    }
    120  1.1  christos 		}
    121  1.1  christos 
    122  1.1  christos 		if (_curx & 1) {
    123  1.1  christos 		    if (_cury == 0)
    124  1.1  christos 			dir = BOX::top;
    125  1.1  christos 		    else {
    126  1.1  christos 			y--;
    127  1.1  christos 			dir = BOX::bottom;
    128  1.1  christos 		    }
    129  1.1  christos 		}
    130  1.1  christos 	    }
    131  1.1  christos 	    return;
    132  1.1  christos 
    133  1.1  christos 	default:
    134  1.1  christos 	    break;
    135  1.1  christos 	}
    136  1.1  christos 
    137  1.1  christos         // We add 2 before the comparison to avoid underflow
    138  1.1  christos 	if ((2 + _curx) - (_curx & 1) < 2)
    139  1.1  christos 	    _curx = (b.nx() * 2) + (_curx & 1);
    140  1.1  christos 	if (_curx >= (b.nx() * 2) + 1)
    141  1.1  christos 	    _curx = (_curx & 1);
    142  1.1  christos 
    143  1.1  christos 	if ((2 + _cury) - (_cury & 1) < 2)
    144  1.1  christos 	    _cury = (b.ny() * 2) + (_cury & 1);
    145  1.1  christos 	if (_cury >= (b.ny() * 2) + 1)
    146  1.1  christos 	    _cury = (_cury & 1);
    147  1.1  christos 
    148  1.1  christos 	b.setpos(_cury, _curx);
    149  1.1  christos     }
    150  1.1  christos }
    151