Home | History | Annotate | Line # | Download | only in dab
      1 /*	$NetBSD: box.cc,v 1.4 2021/12/05 09:22:45 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Box computations
     34  */
     35 
     36 #include "defs.h"
     37 RCSID("$NetBSD: box.cc,v 1.4 2021/12/05 09:22:45 rillig Exp $")
     38 
     39 #include "box.h"
     40 #include "board.h"
     41 #include "gamescreen.h"
     42 #include <curses.h>
     43 
     44 const POINT BOX::edges[BOX::last] =
     45     { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
     46 const POINT BOX::corners[BOX::last] =
     47     { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } };
     48 const int  BOX::syms[BOX::last] =
     49     { GAMESCREEN::GS_HLINE, GAMESCREEN::GS_HLINE,
     50       GAMESCREEN::GS_VLINE, GAMESCREEN::GS_VLINE };
     51 
     52 BOX::BOX(size_t py, size_t px, BOARD& b) :
     53     _b(b)
     54 {
     55     _centery = py * 2 + 1;
     56     _centerx = px * 2 + 1;
     57 }
     58 
     59 void BOX::addcorner(size_t y, size_t x)
     60 {
     61     char sym;
     62     _b.getScrn()->moveto(y, x);
     63     if (x == 0) {
     64 	if (y == 0)
     65 	    sym = GAMESCREEN::GS_ULCORNER;
     66 	else if (y == _b.ty() - 1)
     67 	    sym = GAMESCREEN::GS_LLCORNER;
     68 	else
     69 	    sym = GAMESCREEN::GS_LTEE;
     70     } else if (x == _b.tx() - 1) {
     71 	if (y == 0)
     72 	    sym = GAMESCREEN::GS_URCORNER;
     73 	else if (y == _b.ty() - 1)
     74 	    sym = GAMESCREEN::GS_LRCORNER;
     75 	else
     76 	    sym = GAMESCREEN::GS_RTEE;
     77     } else if (y == 0)
     78 	sym = GAMESCREEN::GS_TTEE;
     79     else if (y == _b.ty() - 1)
     80 	sym = GAMESCREEN::GS_BTEE;
     81     else
     82 	sym = GAMESCREEN::GS_PLUS;
     83 
     84     _b.getScrn()->addedge(sym);
     85 }
     86 
     87 // Paint a box
     88 void BOX::paint(void)
     89 {
     90     int e;
     91     if (_b.getScrn() == NULL)
     92 	return;
     93 
     94     _b.getScrn()->moveto(_centery, _centerx);
     95     _b.getScrn()->addsym(name());
     96 
     97     for (e = BOX::first; e < BOX::last; e++) {
     98 	addcorner(_centery + corners[e].y, _centerx + corners[e].x);
     99 	_b.getScrn()->moveto(_centery + edges[e].y, _centerx + edges[e].x);
    100 	_b.getScrn()->addedge(edge(static_cast<EDGE>(e)));
    101     }
    102     _b.getScrn()->redraw();
    103 }
    104 
    105 // Return the name
    106 int& BOX::name(void)
    107 {
    108     return _b.data(_centery, _centerx);
    109 }
    110 
    111 // Set an edge
    112 void BOX::set(int e)
    113 {
    114     _b.data(_centery + edges[e].y, _centerx + edges[e].x) = syms[e];
    115 }
    116 
    117 // Clear an edge
    118 void BOX::clr(int e)
    119 {
    120     _b.data(_centery + edges[e].y, _centerx + edges[e].x) = ' ';
    121 }
    122 
    123 // Test an edge
    124 int BOX::isset(int e) const
    125 {
    126     return _b.data(_centery + edges[e].y, _centerx + edges[e].x) != ' ';
    127 }
    128 
    129 // Return the edge
    130 int& BOX::edge(int e)
    131 {
    132     return _b.data(_centery + edges[e].y, _centerx + edges[e].x);
    133 }
    134 
    135 // Count the number of edges set in the box
    136 int BOX::count(void) const
    137 {
    138     int cnt = 0;
    139 
    140     for (int e = BOX::first; e < BOX::last; e++)
    141 	cnt += isset(static_cast<EDGE>(e));
    142     return cnt;
    143 }
    144 
    145 // Clear the box
    146 void BOX::reset(void)
    147 {
    148     for (int e = BOX::first; e < BOX::last; e++)
    149 	clr(static_cast<EDGE>(e));
    150     name() = ' ';
    151 }
    152