human.cc revision 1.2 1 1.2 jdc /* $NetBSD: human.cc,v 1.2 2005/07/02 15:48:03 jdc 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * human.C: Human interface for dots, using rogue-like keys.
41 1.1 christos */
42 1.1 christos #include "defs.h"
43 1.2 jdc RCSID("$NetBSD: human.cc,v 1.2 2005/07/02 15:48:03 jdc Exp $")
44 1.1 christos
45 1.1 christos #include "human.h"
46 1.1 christos #include "board.h"
47 1.1 christos #include "box.h"
48 1.2 jdc #include "ttyscrn.h"
49 1.1 christos
50 1.1 christos #define CONTROL(a) ((a) & 037)
51 1.1 christos
52 1.2 jdc extern GAMESCREEN *sc;
53 1.2 jdc
54 1.1 christos HUMAN::HUMAN(const char c) :
55 1.1 christos PLAYER(c),
56 1.1 christos _curx(0),
57 1.1 christos _cury(1)
58 1.1 christos {
59 1.1 christos }
60 1.1 christos
61 1.1 christos void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir)
62 1.1 christos {
63 1.1 christos int mv;
64 1.1 christos b.setpos(_cury, _curx);
65 1.1 christos
66 1.1 christos for (;;) {
67 1.1 christos switch (mv = b.getmove()) {
68 1.1 christos case 'h': case 'H':
69 1.1 christos _curx -= 2;
70 1.1 christos break;
71 1.1 christos
72 1.1 christos case 'l': case 'L':
73 1.1 christos _curx += 2;
74 1.1 christos break;
75 1.1 christos
76 1.1 christos case 'k': case 'K':
77 1.1 christos _cury -= 2;
78 1.1 christos break;
79 1.1 christos
80 1.1 christos case 'j': case 'J':
81 1.1 christos _cury += 2;
82 1.1 christos break;
83 1.1 christos
84 1.1 christos case 'u': case 'U':
85 1.1 christos _curx += 1;
86 1.1 christos _cury -= 1;
87 1.1 christos break;
88 1.1 christos
89 1.1 christos case 'y': case 'Y':
90 1.1 christos _curx -= 1;
91 1.1 christos _cury -= 1;
92 1.1 christos break;
93 1.1 christos
94 1.1 christos case 'b': case 'B':
95 1.1 christos _curx -= 1;
96 1.1 christos _cury += 1;
97 1.1 christos break;
98 1.1 christos
99 1.1 christos case 'n': case 'N':
100 1.1 christos _curx += 1;
101 1.1 christos _cury += 1;
102 1.1 christos break;
103 1.1 christos
104 1.1 christos case 'q': case 'Q':
105 1.2 jdc // Cleanup
106 1.2 jdc delete sc;
107 1.1 christos exit(0);
108 1.1 christos
109 1.1 christos case CONTROL('L'): case CONTROL('R'):
110 1.1 christos b.clean();
111 1.1 christos b.paint();
112 1.1 christos break;
113 1.1 christos
114 1.1 christos case ' ':
115 1.1 christos {
116 1.1 christos x = _curx / 2;
117 1.1 christos y = _cury / 2;
118 1.1 christos
119 1.1 christos if (_cury & 1) {
120 1.1 christos if (_curx == 0)
121 1.1 christos dir = BOX::left;
122 1.1 christos else {
123 1.1 christos x--;
124 1.1 christos dir = BOX::right;
125 1.1 christos }
126 1.1 christos }
127 1.1 christos
128 1.1 christos if (_curx & 1) {
129 1.1 christos if (_cury == 0)
130 1.1 christos dir = BOX::top;
131 1.1 christos else {
132 1.1 christos y--;
133 1.1 christos dir = BOX::bottom;
134 1.1 christos }
135 1.1 christos }
136 1.1 christos }
137 1.1 christos return;
138 1.1 christos
139 1.1 christos default:
140 1.1 christos break;
141 1.1 christos }
142 1.1 christos
143 1.1 christos // We add 2 before the comparison to avoid underflow
144 1.1 christos if ((2 + _curx) - (_curx & 1) < 2)
145 1.1 christos _curx = (b.nx() * 2) + (_curx & 1);
146 1.1 christos if (_curx >= (b.nx() * 2) + 1)
147 1.1 christos _curx = (_curx & 1);
148 1.1 christos
149 1.1 christos if ((2 + _cury) - (_cury & 1) < 2)
150 1.1 christos _cury = (b.ny() * 2) + (_cury & 1);
151 1.1 christos if (_cury >= (b.ny() * 2) + 1)
152 1.1 christos _cury = (_cury & 1);
153 1.1 christos
154 1.1 christos b.setpos(_cury, _curx);
155 1.1 christos }
156 1.1 christos }
157