1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*
misc.c - misc functions
*/
#include "typespeed.h"
int r(int range)
{ /* better random (?) */
return (int) ((float) rand() / (float) RAND_MAX * (float) range);
}
/* returns the current time in hundreths of seconds. */
clock_t timenow(void)
{
struct timeval tval;
gettimeofday(&tval, NULL);
return ((clock_t) ((tval.tv_sec * 100) + (tval.tv_usec / 10000)));
}
/* yet another purkka by jaakko ..
(no mvgetnstr support in jaakko's ncurses) */
void liima_mvgetnstr(int y, int x, char *buf, int maxlen)
{
int curlen = 0;
int merkki;
memset(buf, 0, maxlen + 1);
noecho();
for (;;) {
refresh();
move(y, x + curlen);
addstr(" ");
move(y, x);
if (curlen != 0) {
addstr(buf);
}
refresh();
merkki = getch();
if (merkki == '\n' || merkki == KEY_ENTER) {
break;
}
switch (merkki) {
case 27:{
flushinp();
}
break;
case 8:
case 127:
case KEY_BACKSPACE:
case 4:
if (curlen != 0) {
echochar(merkki);
curlen--;
buf[curlen] = '\0';
}
break;
default:
if (curlen != maxlen) {
buf[curlen] = merkki;
curlen++;
}
break;
}
}
echo();
}
/* using this 'coz debian and others(?) didn't had it in ncurses
thanks ras (timo sirainen) */
void dcolor_set(unsigned char col, void *ptr)
{
register unsigned long bcol = 0;
if (opt.usecolors == 1) {
if ((col & 0x0f) == 8) {
attroff(A_BOLD);
bcol = A_DIM;
} else {
attroff(A_DIM);
if (col & 8)
bcol |= A_BOLD;
else
attroff(A_BOLD);
}
if ((col & 0x77) == 0)
col |= 7;
if (col & 128)
bcol |= A_BLINK;
else
attroff(A_BLINK);
attron(COLOR_PAIR((col & 7) + (col & 0x70) / 2) | bcol);
}
}
int clower(int lchar)
{
switch (lchar) {
case -60:
lchar = -28;
break;
case -59:
lchar = -27;
break;
case -42:
lchar = -10;
break;
default:
lchar = tolower(lchar);
break;
}
return lchar;
}
int typorankkaus(float typorate)
{
int typorankki = 0;
/* if you get a negative typorate here, you must
be as good as my frend xmunkki */
/* and btw. thanks to xmunkki for his
"OverHumanly debugging without compiler" */
if (typorate < 0)
typorankki = 0;
if (typorate == 0)
typorankki = 1;
if ((typorate > 0) && (typorate < 2))
typorankki = 2;
if ((typorate >= 2) && (typorate < 4))
typorankki = 3;
if ((typorate >= 4) && (typorate < 6))
typorankki = 4;
if ((typorate >= 6) && (typorate < 8))
typorankki = 5;
if ((typorate >= 8) && (typorate < 11))
typorankki = 6;
if ((typorate >= 11) && (typorate < 15))
typorankki = 7;
if ((typorate >= 15) && (typorate < 20))
typorankki = 8;
if ((typorate >= 20) && (typorate < 30))
typorankki = 9;
if ((typorate >= 30) && (typorate < 50))
typorankki = 10;
if (typorate >= 50)
typorankki = 11;
if (typorate < 6)
dcolor_set(6, NULL);
if ((typorate >= 6) && (typorate < 11))
dcolor_set(1, NULL);
if ((typorate >= 11) && (typorate < 20))
dcolor_set(7, NULL);
if (typorate >= 20)
dcolor_set(3, NULL);
return (typorankki);
}
void endcursestuff()
{
clear();
refresh();
endwin();
}
void initcursestuff()
{
initscr();
start_color();
flushinp();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_WHITE, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK);
init_pair(4, COLOR_MAGENTA, COLOR_BLACK);
init_pair(5, COLOR_CYAN, COLOR_BLACK);
init_pair(6, COLOR_BLUE, COLOR_BLACK);
init_pair(7, COLOR_YELLOW, COLOR_BLACK);
}
int level(int pointsit)
{
int leveli = 0;
if (pointsit <= 0)
leveli = 0;
if ((pointsit > 0) && (pointsit < 100))
leveli = 1;
if ((pointsit > 99) && (pointsit < 200))
leveli = 2;
if ((pointsit > 199) && (pointsit < 300))
leveli = 3;
if ((pointsit > 299) && (pointsit < 400))
leveli = 4;
if ((pointsit > 399) && (pointsit < 500))
leveli = 5;
if ((pointsit > 499) && (pointsit < 600))
leveli = 6;
if ((pointsit > 599) && (pointsit < 700))
leveli = 7;
if ((pointsit > 699) && (pointsit < 800))
leveli = 8;
if ((pointsit > 799) && (pointsit < 900))
leveli = 9;
if (pointsit > 899)
leveli = 10;
if (pointsit < 400)
dcolor_set(6, NULL);
if ((pointsit > 399) && (pointsit < 600))
dcolor_set(1, NULL);
if ((pointsit > 599) && (pointsit < 700))
dcolor_set(7, NULL);
if (pointsit > 699)
dcolor_set(3, NULL);
return (leveli);
}
|