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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/*
** - - - iMaze - - -
**
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the authors may not be used to endorse or promote
** products derived from this software without specific prior written
** permission.
** 4. The name ``iMaze'' may not be used for products derived from this
** software unless a prefix or a suffix is added to the name.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
**
** Datei: ninja.c
**
** Kommentar:
** Der Ninja
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "argv.h"
#include "global.h"
#include "speicher.h"
#include "labyrinth.h"
#include "farben.h"
#include "spieler.h"
#include "signale.h"
#include "ereignisse.h"
#include "netzwerk.h"
#include "spiel.h"
#include "system.h"
static char sccsid[] = "@(#)ninja.c 3.22 12/3/01";
static int ziel_farbe_parsen(char *str);
static int schussdichte_parsen(char *str);
static char *spruch = NULL;
static int inverse_strategie = 0;
static int use_quickturn = 0;
struct arg_option argv_opts[] =
{
{ Arg_Include, NULL, net_opts },
{ Arg_Simple, "i", &inverse_strategie, "inverse strategy: flee" },
{ Arg_String, "m", &spruch, "set kill message", "message" },
{ Arg_Callback, "s", (void *)schussdichte_parsen,
"set shot probability", "percentage" },
{ Arg_Callback, "t", (void *)ziel_farbe_parsen,
"hunt player", "color-index" },
{ Arg_Simple, "Q", &use_quickturn, "use quickturn" },
{ Arg_End }
};
u_int feldbreite, feldlaenge;
block **spielfeld;
static int lebenszeit = 2 * 60 * 60 * 1000; /* in ms */
static int schussdichte = 100;
static int ziel_farbe = -1;
static int spieler_aktiv, gegneranz, schuss_aktiv;
static struct spieler spieler, gegner[SPIELERANZ];
static char ereignisse[ERG_ANZ];
static int ziel_feld = -1;
static int ziel_farbe_parsen(char *str)
{
long farbe;
char *end;
farbe = strtol(str, &end, 10);
if (*end != 0 || farbe < 0 || farbe > SPIELERANZ)
return 1; /* parse error */
ziel_farbe = farbe;
return 0; /* ok */
}
static int schussdichte_parsen(char *str)
{
long wert;
char *end;
wert = strtol(str, &end, 10);
if (*end != 0 || wert < 0 || wert > 100)
return 1; /* parse error */
schussdichte = wert;
return 0; /* ok */
}
static void laufen(char signale[SIGNALANZ])
{
int *dist, i, j;
speicher_belegen((void **)&dist, feldlaenge * feldbreite * sizeof(int));
for (i = feldlaenge * feldbreite; i--; dist[i] = 0);
dist[spieler.pos.ygrob * feldbreite + spieler.pos.xgrob] = 1;
for (j = 0, i = 0; i < gegneranz; i++)
{
int k;
if (gegner[i].farbe != ziel_farbe)
continue;
k = gegner[i].pos.ygrob * feldbreite + gegner[i].pos.xgrob;
if (!dist[k])
j++, dist[k] = -1;
}
if (!j)
for (j = 0, i = 0; i < gegneranz; i++)
{
int k;
k = gegner[i].pos.ygrob * feldbreite +
gegner[i].pos.xgrob;
if (!dist[k])
j++, dist[k] = -1;
}
if (!j)
{
if (ziel_feld < 0 || zufall() % 50 == 0)
ziel_feld = zufall() % (feldlaenge * feldbreite);
dist[ziel_feld] = -1;
}
else
ziel_feld = -1;
for (;;)
{
int weiter;
weiter = 0;
for (i = feldlaenge * feldbreite; i--;)
{
if (dist[i] > 0)
{
int x, y;
x = i % feldbreite;
y = i / feldbreite;
if (!spielfeld[x][y][NORD].unbegehbar &&
dist[j = i - feldbreite] <= 0)
{
if (dist[j])
break;
dist[j] = i + 2;
weiter = 1;
}
if (!spielfeld[x][y][WEST].unbegehbar &&
dist[j = i - 1] <= 0)
{
if (dist[j])
break;
dist[j] = i + 2;
weiter = 1;
}
if (!spielfeld[x][y][SUED].unbegehbar &&
dist[j = i + feldbreite] <= 0)
{
if (dist[j])
break;
dist[j] = i + 2;
weiter = 1;
}
if (!spielfeld[x][y][OST].unbegehbar &&
dist[j = i + 1] <= 0)
{
if (dist[j])
break;
dist[j] = i + 2;
weiter = 1;
}
}
j = -1;
}
if (j >= 0)
break;
if (!weiter)
{
speicher_freigeben((void **)&dist);
return;
}
}
while (dist[i] > 1)
j = i, i = dist[i] - 2;
speicher_freigeben((void **)&dist);
switch (j -= i)
{
case -1:
i = WINKANZ / 4;
break;
case 1:
i = WINKANZ * 3 / 4;
break;
default:
if (j < 0)
i = 0;
else
i = WINKANZ / 2;
}
if (spieler.pos.ygrob > 0 && (int)spieler.pos.yfein < KUGELRAD &&
spielfeld[spieler.pos.xgrob][spieler.pos.ygrob - 1][SUED].unbegehbar)
i = 0;
else if (spieler.pos.xgrob > 0 && (int)spieler.pos.xfein < KUGELRAD &&
spielfeld[spieler.pos.xgrob - 1][spieler.pos.ygrob][OST].unbegehbar)
i = WINKANZ / 4;
else if ((int)spieler.pos.ygrob < feldlaenge - 1 &&
(int)spieler.pos.yfein > RASPT - KUGELRAD &&
spielfeld[spieler.pos.xgrob][spieler.pos.ygrob + 1][NORD].unbegehbar)
i = WINKANZ / 2;
else if ((int)spieler.pos.xgrob < feldbreite - 1 &&
(int)spieler.pos.xfein > RASPT - KUGELRAD &&
spielfeld[spieler.pos.xgrob + 1][spieler.pos.ygrob][WEST].unbegehbar)
i = WINKANZ * 3 / 4;
i = (i - spieler.blick + WINKANZ) % WINKANZ;
if (i)
if (i < WINKANZ / 2)
signale[LINKSSIGNAL] = 1;
else
signale[RECHTSSIGNAL] = 1;
if (use_quickturn && i > WINKANZ / 4 && i < WINKANZ * 3 / 4 &&
zufall() % 10 == 0)
signale[QUICKTURNSIGNAL] = 1;
if (i < WINKANZ / 4 || i > WINKANZ * 3 / 4)
if (inverse_strategie)
signale[ZURUECKSIGNAL] = 1;
else
signale[VORSIGNAL] = 1;
}
static void schiessen(char signale[SIGNALANZ])
{
if (!schuss_aktiv && zufall() % 100 < schussdichte)
signale[SCHUSSSIGNAL] = 1;
if (schuss_aktiv && ereignisse[SCHUSS_PRALL_ERG] && zufall() % 4 == 0)
signale[SCHUSSSIGNAL] = 1;
}
static void abbruch_signal_handler(int signum)
{
spiel_verlassen();
exit(0);
}
void uebler_fehler(char **meldung, char *knopf)
{
int i;
for (i = 0; meldung[i] != NULL; i++)
fprintf(stderr, "ninja: %s\n", meldung[i]);
exit(1);
}
void milder_fehler(char **meldung, char *knopf)
{
uebler_fehler(meldung, knopf);
}
void spiel_puffer_anlegen(int **spieler_aktiv_var,
struct spieler **spieler_var, int **gegneranz_var,
struct spieler **gegner_feld, int **schuss_aktiv_var,
struct schuss **schuss_var, int **schussanz_var,
struct schuss **schuesse_feld, int **abgeschossen_durch_var,
char ***abschuss_spruch_var, char **ereignisse_feld, int **punkte_feld)
{
*spieler_aktiv_var = &spieler_aktiv;
*spieler_var = &spieler;
*gegneranz_var = &gegneranz;
*gegner_feld = gegner;
*schuss_aktiv_var = &schuss_aktiv;
*schuss_var = NULL;
*schussanz_var = NULL;
*schuesse_feld = NULL;
*abgeschossen_durch_var = NULL;
*abschuss_spruch_var = NULL;
*ereignisse_feld = ereignisse;
*punkte_feld = NULL;
lebenszeit -= 300 * 1000 - timer_restzeit();
timer_starten(300 * 1000);
if (lebenszeit < 0)
{
spiel_verlassen();
exit(0);
}
}
void netzwerk_spielende(void)
{
exit(0);
}
void signale_abfragen(char signale[SIGNALANZ])
{
int i;
for (i = 0; i < SIGNALANZ; i++)
signale[i] = 0;
if (spieler_aktiv)
{
laufen(signale);
schiessen(signale);
}
}
/*
** main
** die Hauptroutine
**
** Parameter:
** argc: Anzahl der Argumente inklusive Programmname
** argv: Argumentliste
*/
int main(int argc, char **argv)
{
char tmp[100];
process_args(&argc, argv);
/* zur Sicherheit ein Timeout */
signal(SIGALRM, SIG_DFL);
timer_starten(300 * 1000);
/* Netzwerkroutinen initialisieren */
netzwerk_init();
if (spruch == NULL)
{
void *name;
strcpy(tmp, "Ninja of ");
name = benutzer_name();
if (name == NULL)
strcpy(tmp + 9, "nobody");
else
{
strncpy(tmp + 9, name, sizeof tmp - 9);
tmp[sizeof tmp - 1] = 0;
speicher_freigeben(&name);
}
spruch = tmp;
}
timer_starten(300 * 1000);
verbindung_aufbauen(NULL);
timer_starten(300 * 1000);
if (spielparameter_empfangen("iMaze ninja JC/HUK 1.4", "Ninja", spruch,
0 /* keine kamera */, &feldbreite, &feldlaenge, &spielfeld,
NULL, 1 /* starten */))
exit(1);
{
int x, y, i;
for (x = 0; x < feldbreite; x++)
for (y = 0; y < feldlaenge; y++)
for (i = 0; i < 4; i++)
{
struct wand *wand;
wand = &spielfeld[x][y][i];
wand->unbegehbar = !IST_TUER(wand->farbe) &&
wand->farbe != TRANSPARENT;
}
}
timer_starten(300 * 1000);
if (verbindung_auf_spiel())
exit(1);
handle_signal(SIGHUP, abbruch_signal_handler);
handle_signal(SIGINT, abbruch_signal_handler);
handle_signal(SIGTERM, abbruch_signal_handler);
timer_starten(300 * 1000);
/* Hauptschleife, ein Ninja stirbt nicht */
for (;;)
if (spiel_paket_erwarten(-1))
break; /* ausser bei Fehlern */
return 1;
}
|