Debian Codegraph @

modules/contrib/src/wumpus.c

raw
1 /*
2 * Wumpus - 0.2.0
3 * Copyright (c) 2006, 2011 William Pitcock <nenolod -at- nenolod.net>
4 * Portions copyright (c) 2006 Kiyoshi Aman <kiyoshi.aman -at- gmail.com>
5 *
6 * Rights to this code are as documented in doc/LICENSE.
7 *
8 * Hunt the Wumpus game implementation.
9 */
10
11 #include "atheme-compat.h"
12
13 /* contents */
14 typedef enum {
15 E_NOTHING = 0,
16 E_WUMPUS,
17 E_PIT,
18 E_BATS,
19 E_ARROWS,
20 E_CRYSTALBALL
21 } contents_t;
22
23 /* room_t: Describes a room that the wumpus or players could be in. */
24 struct room_ {
25 int id; /* room 3 or whatever */
26 mowgli_list_t exits; /* old int count == exits.count */
27 contents_t contents;
28 mowgli_list_t players; /* player_t players */
29 };
30
31 typedef struct room_ room_t;
32
33 /* player_t: A player object. */
34 struct player_ {
35 user_t *u;
36 room_t *location;
37 int arrows;
38 int hp;
39 bool has_moved;
40 };
41
42 typedef struct player_ player_t;
43
44 struct game_ {
45 int wumpus;
46 int mazesize;
47 mowgli_list_t players;
48 bool running;
49 bool starting;
50
51 room_t *rmemctx; /* memory page context */
52 service_t *svs;
53 int wump_hp;
54 int speed;
55
56 unsigned int wantsize;
57
58 mowgli_eventloop_timer_t *move_timer;
59 mowgli_eventloop_timer_t *start_game_timer;
60 };
61
62 typedef struct game_ game_t;
63
64 game_t wumpus;
65
66 struct __wumpusconfig
67 {
68 char *chan;
69 char *nick;
70 char *user;
71 char *host;
72 char *real;
73 } wumpus_cfg = {
74 "#wumpus",
75 "Wumpus",
76 "wumpus",
77 "services.int",
78 "Hunt the Wumpus"
79 };
80
81 /* ------------------------------ utility functions */
82
83 /* returns 1 or 2 depending on if the wumpus is 1 or 2 rooms away */
84 static int
85 distance_to_wumpus(player_t *player)
86 {
87 mowgli_node_t *n, *tn;
88
89 MOWGLI_ITER_FOREACH(n, player->location->exits.head)
90 {
91 room_t *r = (room_t *) n->data;
92
93 if (r->contents == E_WUMPUS)
94 return 1;
95
96 MOWGLI_ITER_FOREACH(tn, r->exits.head)
97 {
98 room_t *r2 = (room_t *) tn->data;
99
100 if (r2->contents == E_WUMPUS)
101 return 2;
102
103 /* we don't evaluate exitpoints at this depth */
104 }
105 }
106
107 return 0;
108 }
109
110 /* can we move or perform an action on this room? */
111 static bool
112 adjacent_room(player_t *p, int id)
113 {
114 mowgli_node_t *n;
115
116 MOWGLI_ITER_FOREACH(n, p->location->exits.head)
117 {
118 room_t *r = (room_t *) n->data;
119
120 if (r->id == id)
121 return true;
122 }
123
124 return false;
125 }
126
127 /* finds a player in the list */
128 static player_t *
129 find_player(user_t *u)
130 {
131 mowgli_node_t *n;
132
133 MOWGLI_ITER_FOREACH(n, wumpus.players.head)
134 {
135 player_t *p = n->data;
136
137 if (p->u == u)
138 return p;
139 }
140
141 return NULL;
142 }
143
144 /* adds a player to the game */
145 static player_t *
146 create_player(user_t *u)
147 {
148 player_t *p;
149
150 if (find_player(u))
151 {
152 notice(wumpus_cfg.nick, u->nick, "You are already playing the game!");
153 return NULL;
154 }
155
156 if (wumpus.running)
157 {
158 notice(wumpus_cfg.nick, u->nick, "The game is already in progress. Sorry!");
159 return NULL;
160 }
161
162 p = smalloc(sizeof(player_t));
163 memset(p, '\0', sizeof(player_t));
164
165 p->u = u;
166 p->arrows = 10;
167 p->hp = 30;
168
169 mowgli_node_add(p, mowgli_node_create(), &wumpus.players);
170
171 return p;
172 }
173
174 /* destroys a player object and removes them from the game */
175 static void
176 resign_player(player_t *player)
177 {
178 mowgli_node_t *n;
179
180 if (player == NULL)
181 return;
182
183 if (player->location)
184 {
185 n = mowgli_node_find(player, &player->location->players);
186 mowgli_node_delete(n, &player->location->players);
187 mowgli_node_free(n);
188 }
189
190 n = mowgli_node_find(player, &wumpus.players);
191 mowgli_node_delete(n, &wumpus.players);
192 mowgli_node_free(n);
193
194 sfree(player);
195 }
196
197 /* ------------------------------ game functions */
198
199 /* builds the maze, and returns false if the maze is too small */
200 static bool
201 build_maze(unsigned int size)
202 {
203 unsigned int i, j;
204 room_t *w;
205
206 if (size < 10)
207 return false;
208
209 slog(LG_DEBUG, "wumpus: building maze of %u chambers", size);
210
211 /* allocate rooms */
212 wumpus.mazesize = size;
213 wumpus.rmemctx = scalloc(size, sizeof(room_t));
214
215 for (i = 0; i < size; i++)
216 {
217 room_t *r = &wumpus.rmemctx[i];
218 memset(r, '\0', sizeof(room_t));
219
220 r->id = i;
221
222 /* rooms have 3 exit points, exits are one-way */
223 for (j = 0; j < 3 && r->exits.count < 3; j++)
224 {
225 int t = rand() % size;
226
227 /* make sure this isn't a tunnel to itself */
228 while (t == r->id)
229 {
230 mowgli_node_t *rn;
231 t = rand() % size;
232
233 /* also check that this path doesn't already exist. */
234 MOWGLI_ITER_FOREACH(rn, r->exits.head)
235 {
236 room_t *rm = (room_t *) rn->data;
237
238 if (rm->id == t)
239 t = r->id;
240 }
241 }
242
243 slog(LG_DEBUG, "wumpus: creating link for route %u -> %d", i, t);
244 mowgli_node_add(&wumpus.rmemctx[t], mowgli_node_create(), &r->exits);
245 }
246
247 slog(LG_DEBUG, "wumpus: finished creating exit paths for chamber %u", i);
248 }
249
250 /* place the wumpus in the maze */
251 wumpus.wumpus = rand() % size;
252 w = &wumpus.rmemctx[wumpus.wumpus];
253 w->contents = E_WUMPUS;
254
255 /* pits */
256 for (j = 0; j < size; j++)
257 {
258 /* 42 will do very nicely */
259 if (rand() % (42 * 2) == 0)
260 {
261 room_t *r = &wumpus.rmemctx[j];
262
263 r->contents = E_PIT;
264
265 slog(LG_DEBUG, "wumpus: added pit to chamber %u", j);
266 }
267 }
268
269 /* bats */
270 for (i = 0; i < 2; i++)
271 {
272 for (j = 0; j < size; j++)
273 {
274 /* 42 will do very nicely */
275 if (rand() % 42 == 0)
276 {
277 room_t *r = &wumpus.rmemctx[j];
278
279 r->contents = E_BATS;
280
281 slog(LG_DEBUG, "wumpus: added bats to chamber %u", j);
282 }
283 }
284 }
285
286 /* arrows */
287 for (i = 0; i < 3; i++)
288 {
289 for (j = 0; j < size; j++)
290 {
291 /* 42 will do very nicely */
292 if (rand() % 42 == 0)
293 {
294 room_t *r = &wumpus.rmemctx[j];
295
296 r->contents = E_ARROWS;
297
298 slog(LG_DEBUG, "wumpus: added arrows to chamber %u", j);
299 }
300 }
301 }
302
303 /* find a place to put the crystal ball */
304 w = &wumpus.rmemctx[rand() % size];
305 w->contents = E_CRYSTALBALL;
306 slog(LG_DEBUG, "wumpus: added crystal ball to chamber %d", w->id);
307
308 /* ok, do some sanity checking */
309 for (j = 0; j < size; j++)
310 if (wumpus.rmemctx[j].exits.count < 3)
311 {
312 slog(LG_DEBUG, "wumpus: sanity checking failed");
313 return false;
314 }
315
316 slog(LG_DEBUG, "wumpus: built maze");
317
318 return true;
319 }
320
321 /* init_game depends on these */
322 static void move_wumpus(void *unused);
323 static void look_player(player_t *p);
324 static void end_game(void);
325
326 /* sets the game up */
327 static void
328 init_game(unsigned int size)
329 {
330 mowgli_node_t *n;
331
332 if (!build_maze(size))
333 {
334 msg(wumpus_cfg.nick, wumpus_cfg.chan, "Maze generation failed, please try again.");
335 end_game();
336 return;
337 }
338
339 /* place players in random positions */
340 MOWGLI_ITER_FOREACH(n, wumpus.players.head)
341 {
342 player_t *p = (player_t *) n->data;
343
344 p->location = &wumpus.rmemctx[rand() % wumpus.mazesize];
345 mowgli_node_add(p, mowgli_node_create(), &p->location->players);
346
347 look_player(p);
348 }
349
350 /* timer initialization */
351 wumpus.move_timer = mowgli_timer_add(base_eventloop, "move_wumpus", move_wumpus, NULL, 60);
352
353 msg(wumpus_cfg.nick, wumpus_cfg.chan, "The game has started!");
354
355 wumpus.running = true;
356 wumpus.speed = 60;
357 wumpus.wump_hp = 70;
358
359 wumpus.start_game_timer = NULL;
360 }
361
362 /* starts the game */
363 static void
364 start_game(void *unused)
365 {
366 wumpus.starting = false;
367
368 if (wumpus.players.count < 2)
369 {
370 msg(wumpus_cfg.nick, wumpus_cfg.chan, "Not enough players to play. :(");
371 return;
372 }
373
374 if (wumpus.wantsize >= 300)
375 wumpus.wantsize = 300;
376
377 init_game(wumpus.wantsize);
378 }
379
380 /* destroys game objects */
381 static void
382 end_game(void)
383 {
384 mowgli_node_t *n, *tn;
385 int i;
386
387 /* destroy players */
388 MOWGLI_ITER_FOREACH_SAFE(n, tn, wumpus.players.head)
389 resign_player((player_t *) n->data);
390
391 /* free memory vector */
392 if (wumpus.rmemctx)
393 {
394 /* destroy links between rooms */
395 for (i = 0; i < wumpus.mazesize; i++)
396 {
397 room_t *r = &wumpus.rmemctx[i];
398
399 MOWGLI_ITER_FOREACH_SAFE(n, tn, r->exits.head)
400 mowgli_node_delete(n, &r->exits);
401 }
402 sfree(wumpus.rmemctx);
403 wumpus.rmemctx = NULL;
404 }
405
406 wumpus.wumpus = -1;
407 wumpus.running = false;
408
409 mowgli_timer_destroy(base_eventloop, wumpus.move_timer);
410 wumpus.move_timer = NULL;
411
412 /* game is now ended */
413 }
414
415 /* gives the player information about their surroundings */
416 static void
417 look_player(player_t *p)
418 {
419 mowgli_node_t *n;
420
421 return_if_fail(p != NULL);
422 return_if_fail(p->location != NULL);
423
424 notice(wumpus_cfg.nick, p->u->nick, "You are in room %d.", p->location->id);
425
426 MOWGLI_ITER_FOREACH(n, p->location->exits.head)
427 {
428 room_t *r = (room_t *) n->data;
429
430 notice(wumpus_cfg.nick, p->u->nick, "You can move to room %d.", r->id);
431 }
432
433 if (distance_to_wumpus(p))
434 notice(wumpus_cfg.nick, p->u->nick, "You smell a wumpus!");
435
436 /* provide warnings */
437 MOWGLI_ITER_FOREACH(n, p->location->exits.head)
438 {
439 room_t *r = (room_t *) n->data;
440
441 if (r->contents == E_WUMPUS)
442 notice(wumpus_cfg.nick, p->u->nick, "You smell a wumpus!");
443 if (r->contents == E_PIT)
444 notice(wumpus_cfg.nick, p->u->nick, "You feel a draft!");
445 if (r->contents == E_BATS)
446 notice(wumpus_cfg.nick, p->u->nick, "You hear bats!");
447 if (r->players.count > 0)
448 notice(wumpus_cfg.nick, p->u->nick, "You smell humans!");
449 }
450 }
451
452 /* shoot and kill other players */
453 static void
454 shoot_player(player_t *p, int target_id)
455 {
456 room_t *r;
457 player_t *tp;
458 /* chance to hit; moved up here for convenience. */
459 int hit = rand() % 3;
460
461 if (!p->arrows)
462 {
463 notice(wumpus_cfg.nick, p->u->nick, "You have no arrows!");
464 return;
465 }
466
467 if (adjacent_room(p, target_id) == false)
468 {
469 notice(wumpus_cfg.nick, p->u->nick, "You can't shoot into room %d from here.", target_id);
470 return;
471 }
472
473 if (p->location->id == target_id)
474 {
475 notice(wumpus_cfg.nick, p->u->nick, "You can only shoot into adjacent rooms!");
476 return;
477 }
478
479 r = &wumpus.rmemctx[target_id];
480 tp = r->players.head ? r->players.head->data : NULL;
481
482 p->arrows--;
483
484 if ((!tp) && (r->contents != E_WUMPUS))
485 {
486 notice(wumpus_cfg.nick, p->u->nick, "You shoot at nothing.");
487 return;
488 }
489
490 if (tp)
491 {
492 if ((hit < 2) && (tp->hp <= 10))
493 {
494 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has been killed by \2%s\2!",
495 tp->u->nick, p->u->nick);
496 resign_player(tp);
497 }
498 else if ((tp->hp > 0) && (hit < 2)) {
499 notice(wumpus_cfg.nick, tp->u->nick,
500 "You were hit by an arrow from room %d.",p->location->id);
501 notice(wumpus_cfg.nick, p->u->nick, "You hit something.");
502 tp->hp -= 10;
503 }
504 else
505 {
506 notice(wumpus_cfg.nick, tp->u->nick, "You have been shot at from room %d.",
507 p->location->id);
508 notice(wumpus_cfg.nick, p->u->nick, "You miss what you were shooting at.");
509 }
510 }
511 else if (r->contents == E_WUMPUS) /* Shootin' at the wumpus, we are... */
512 {
513 if (((wumpus.wump_hp > 0) && wumpus.wump_hp <= 5) && (hit < 2))
514 /* we killed the wumpus */
515 {
516 notice(wumpus_cfg.nick, p->u->nick, "You have killed the wumpus!");
517 msg(wumpus_cfg.nick, wumpus_cfg.chan, "The wumpus was killed by \2%s\2.",
518 p->u->nick);
519 msg(wumpus_cfg.nick, wumpus_cfg.chan,
520 "%s has won the game! Congratulations!", p->u->nick);
521 end_game();
522 }
523 else if ((wumpus.wump_hp > 5) && (hit < 2))
524 {
525 notice(wumpus_cfg.nick, p->u->nick,
526 "You shoot the Wumpus, but he shrugs it off and seems angrier!");
527
528 wumpus.wump_hp -= 5;
529 wumpus.speed -= 3;
530
531 move_wumpus(NULL);
532
533 mowgli_timer_destroy(base_eventloop, wumpus.move_timer);
534 wumpus.move_timer = mowgli_timer_add(base_eventloop, "move_wumpus", move_wumpus, NULL, wumpus.speed);
535 }
536 else
537 {
538 notice(wumpus_cfg.nick, p->u->nick, "You miss what you were shooting at.");
539 move_wumpus(NULL);
540 }
541 }
542 }
543
544 /* move_wumpus depends on this */
545 static void regen_obj(contents_t);
546
547 /* check for last-man-standing win condition. */
548 static void
549 check_last_person_alive(void)
550 {
551 if (wumpus.players.count == 1)
552 {
553 player_t *p = (player_t *) wumpus.players.head->data;
554
555 msg(wumpus_cfg.nick, wumpus_cfg.chan, "%s won the game! Congratulations!", p->u->nick);
556
557 end_game();
558 }
559 else if (wumpus.players.count == 0)
560 {
561 msg(wumpus_cfg.nick, wumpus_cfg.chan, "Everyone lost. Sucks. :(");
562 end_game();
563 }
564 }
565
566 /* move the wumpus, the wumpus moves every 60 seconds */
567 static void
568 move_wumpus(void *unused)
569 {
570 mowgli_node_t *n, *tn;
571 room_t *r, *tr;
572 int w_kills = 0;
573
574 /* can we do any of this? if this is null, we really shouldn't be here */
575 if (wumpus.rmemctx == NULL)
576 {
577 slog(LG_DEBUG, "wumpus: move_wumpus() called while game not running!");
578 mowgli_timer_destroy(base_eventloop, wumpus.move_timer);
579 return;
580 }
581
582 msg(wumpus_cfg.nick, wumpus_cfg.chan, "You hear footsteps...");
583
584 /* start moving */
585 r = &wumpus.rmemctx[wumpus.wumpus]; /* memslice describing the wumpus's current location */
586
587 regen_obj(r->contents);
588 r->contents = E_NOTHING;
589
590 tr = mowgli_node_nth_data(&r->exits, rand() % MOWGLI_LIST_LENGTH(&r->exits));
591
592 #ifdef DEBUG_AI
593 msg(wumpus_cfg.nick, wumpus_cfg.chan, "I moved to chamber %d", tr->id);
594 #endif
595
596 slog(LG_DEBUG, "wumpus: the wumpus is now in room %d! (was in %d)",
597 tr->id, wumpus.wumpus);
598 wumpus.wumpus = tr->id;
599 tr->contents = E_WUMPUS;
600
601 #ifdef DEBUG_AI
602 msg(wumpus_cfg.nick, wumpus_cfg.chan, "On my next turn, I can move to:");
603 r = &wumpus.rmemctx[wumpus.wumpus];
604
605 MOWGLI_ITER_FOREACH(n, r->exits.head)
606 {
607 room_t *tr = (room_t *) n->data;
608
609 msg(wumpus_cfg.nick, wumpus_cfg.chan, "- %d", tr->id);
610 }
611 #endif
612
613 MOWGLI_ITER_FOREACH_SAFE(n, tn, wumpus.players.head)
614 {
615 player_t *p = (player_t *) n->data;
616
617 if (wumpus.wumpus == p->location->id)
618 {
619 notice(wumpus_cfg.nick, p->u->nick, "The wumpus has joined your room and eaten you. Sorry.");
620 w_kills++;
621
622 /* player_t *p has been eaten and is no longer in the game */
623 resign_player(p);
624 }
625 else
626 {
627 /* prepare for the next turn */
628 p->has_moved = false;
629 }
630 }
631
632 /* report any wumpus kills */
633 if (w_kills)
634 msg(wumpus_cfg.nick, wumpus_cfg.chan, "You hear the screams of %d surprised adventurer%s.", w_kills,
635 w_kills != 1 ? "s" : "");
636
637 check_last_person_alive();
638 }
639
640 /* regenerates objects */
641 static void
642 regen_obj(contents_t obj)
643 {
644 wumpus.rmemctx[rand() % wumpus.mazesize].contents = obj;
645 }
646
647 /* handles movement requests from players */
648 static void
649 move_player(player_t *p, int id)
650 {
651 mowgli_node_t *n;
652
653 if (adjacent_room(p, id) == false)
654 {
655 notice(wumpus_cfg.nick, p->u->nick, "Sorry, you cannot get to room %d from here.", id);
656 return;
657 }
658
659 /* What about bats? We check for this first because yeah... */
660 if (wumpus.rmemctx[id].contents == E_BATS)
661 {
662 int target_id = rand() % wumpus.mazesize;
663
664 notice(wumpus_cfg.nick, p->u->nick, "Bats have picked you up and taken you to room %d.",
665 target_id);
666 msg(wumpus_cfg.nick, wumpus_cfg.chan, "You hear a surprised yell.");
667
668 /* move the bats */
669 wumpus.rmemctx[id].contents = E_NOTHING;
670 wumpus.rmemctx[target_id].contents = E_BATS;
671
672 id = target_id;
673
674 /* and fall through, sucks if you hit the two conditions below :-P */
675 }
676
677 /* Is the wumpus in here? */
678 if (wumpus.wumpus == id)
679 {
680 notice(wumpus_cfg.nick, p->u->nick, "You see the wumpus approaching you. You scream for help, but it is too late.");
681 msg(wumpus_cfg.nick, wumpus_cfg.chan, "You hear a blood-curdling scream.");
682
683 /* player_t *p has been killed by the wumpus, remove him from the game */
684 resign_player(p);
685 check_last_person_alive();
686 return;
687 }
688
689 /* What about a pit? */
690 if (wumpus.rmemctx[id].contents == E_PIT)
691 {
692 notice(wumpus_cfg.nick, p->u->nick, "You have fallen into a bottomless pit. Sorry.");
693 msg(wumpus_cfg.nick, wumpus_cfg.chan, "You hear a faint wail, which gets fainter and fainter.");
694
695 /* player_t *p has fallen down a hole, remove him from the game */
696 resign_player(p);
697 check_last_person_alive();
698 return;
699 }
700
701 /* and arrows? */
702 if (wumpus.rmemctx[id].contents == E_ARROWS)
703 {
704 if (p->arrows == 0)
705 {
706 notice(wumpus_cfg.nick, p->u->nick, "You found some arrows. You pick them up and continue on your way.");
707 p->arrows += 5;
708 }
709 else
710 notice(wumpus_cfg.nick, p->u->nick, "You found some arrows. You don't have any room to take them however, "
711 "so you break them in half and continue on your way.");
712
713 wumpus.rmemctx[id].contents = E_NOTHING;
714
715 regen_obj(E_ARROWS);
716 }
717
718 /* crystal ball */
719 if (wumpus.rmemctx[id].contents == E_CRYSTALBALL)
720 {
721 notice(wumpus_cfg.nick, p->u->nick, "You find a strange pulsating crystal ball. You examine it, and it shows room %d with the wumpus in it.",
722 wumpus.wumpus);
723 notice(wumpus_cfg.nick, p->u->nick, "The crystal ball then vanishes into the miasma.");
724
725 wumpus.rmemctx[id].contents = E_NOTHING;
726 wumpus.rmemctx[rand() % wumpus.mazesize].contents = E_CRYSTALBALL;
727 }
728
729 /* we recycle the mowgli_node_t here for speed */
730 n = mowgli_node_find(p, &p->location->players);
731 mowgli_node_delete(n, &p->location->players);
732 mowgli_node_free(n);
733
734 p->location = &wumpus.rmemctx[id];
735 mowgli_node_add(p, mowgli_node_create(), &p->location->players);
736
737 /* provide player with information, including their new location */
738 look_player(p);
739
740 /* tell players about joins. */
741 if (p->location->players.count > 1)
742 {
743 MOWGLI_ITER_FOREACH(n, p->location->players.head)
744 {
745 if (n->data != p)
746 {
747 player_t *tp = (player_t *) n->data;
748
749 notice(wumpus_cfg.nick, tp->u->nick, "%s has joined room %d with you.",
750 p->u->nick, id);
751 notice(wumpus_cfg.nick, p->u->nick, "You see %s!",
752 tp->u->nick);
753 }
754 }
755 }
756 }
757
758 /* ------------------------------ -*-atheme-*- code */
759
760 static void
761 cmd_start(sourceinfo_t *si, int parc, char *parv[])
762 {
763 if (wumpus.running || wumpus.starting)
764 {
765 notice(wumpus_cfg.nick, si->su->nick, "A game is already in progress. Sorry.");
766 return;
767 }
768
769 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has started the game! Use \2/msg Wumpus JOIN\2 to play! You have\2 60 seconds\2.",
770 si->su->nick);
771
772 wumpus.starting = true;
773 wumpus.wantsize = 100;
774
775 if (parv[0])
776 wumpus.wantsize = atoi(parv[0]);
777
778 wumpus.start_game_timer = mowgli_timer_add_once(base_eventloop, "start_game", start_game, NULL, 60);
779 }
780
781 static void
782 cmd_join(sourceinfo_t *si, int parc, char *parv[])
783 {
784 player_t *p;
785
786 if (!wumpus.starting || wumpus.running)
787 {
788 notice(wumpus_cfg.nick, si->su->nick, "You cannot use this command right now. Sorry.");
789 return;
790 }
791
792 p = create_player(si->su);
793
794 if (p)
795 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has joined the game!", si->su->nick);
796 }
797
798 static void
799 cmd_look(sourceinfo_t *si, int parc, char *parv[])
800 {
801 player_t *p = find_player(si->su);
802
803 if (p == NULL)
804 {
805 notice(wumpus_cfg.nick, si->su->nick, "You must be playing the game in order to use this command.");
806 return;
807 }
808
809 if (!wumpus.running)
810 {
811 notice(wumpus_cfg.nick, si->su->nick, "You cannot use this command right now. Sorry.");
812 return;
813 }
814
815 look_player(p);
816 }
817
818 static void
819 cmd_move(sourceinfo_t *si, int parc, char *parv[])
820 {
821 player_t *p = find_player(si->su);
822 char *id = parv[0];
823
824 if (!p)
825 {
826 notice(wumpus_cfg.nick, si->su->nick, "You must be playing the game in order to use this command.");
827 return;
828 }
829
830 if (!id)
831 {
832 notice(wumpus_cfg.nick, si->su->nick, "You must provide a room to move to.");
833 return;
834 }
835
836 if (!wumpus.running)
837 {
838 notice(wumpus_cfg.nick, si->su->nick, "The game must be running in order to use this command.");
839 return;
840 }
841
842 move_player(p, atoi(id));
843 }
844
845 static void
846 cmd_shoot(sourceinfo_t *si, int parc, char *parv[])
847 {
848 player_t *p = find_player(si->su);
849 char *id = parv[0];
850
851 if (!p)
852 {
853 notice(wumpus_cfg.nick, si->su->nick, "You must be playing the game in order to use this command.");
854 return;
855 }
856
857 if (!id)
858 {
859 notice(wumpus_cfg.nick, si->su->nick, "You must provide a room to shoot at.");
860 return;
861 }
862
863 if (!wumpus.running)
864 {
865 notice(wumpus_cfg.nick, si->su->nick, "The game must be running in order to use this command.");
866 return;
867 }
868
869 shoot_player(p, atoi(id));
870 }
871
872 static void
873 cmd_resign(sourceinfo_t *si, int parc, char *parv[])
874 {
875 player_t *p = find_player(si->su);
876
877 if (!p)
878 {
879 notice(wumpus_cfg.nick, si->su->nick, "You must be playing the game in order to use this command.");
880 return;
881 }
882
883 if (!wumpus.running)
884 {
885 notice(wumpus_cfg.nick, si->su->nick, "The game must be running in order to use this command.");
886 return;
887 }
888
889 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has quit the game!", p->u->nick);
890
891 resign_player(p);
892 }
893
894 static void
895 cmd_reset(sourceinfo_t *si, int parc, char *parv[])
896 {
897 if (wumpus.running)
898 {
899 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has ended the game.", si->su->nick);
900
901 end_game();
902
903 wumpus.running = false;
904 wumpus.starting = false;
905 }
906 }
907
908 static void
909 cmd_help(sourceinfo_t *si, int parc, char *parv[])
910 {
911 command_help(si, si->service->commands);
912 }
913
914 static void
915 cmd_who(sourceinfo_t *si, int parc, char *parv[])
916 {
917 mowgli_node_t *n;
918
919 notice(wumpus_cfg.nick, si->su->nick, "The following people are playing:");
920
921 MOWGLI_ITER_FOREACH(n, wumpus.players.head)
922 {
923 player_t *p = (player_t *) n->data;
924
925 notice(wumpus_cfg.nick, si->su->nick, "- %s", p->u->nick);
926 }
927 }
928
929 /* removes quitting players */
930 static void
931 user_deleted(user_t *u)
932 {
933 player_t *p;
934
935 if ((p = find_player(u)) != NULL)
936 {
937 msg(wumpus_cfg.nick, wumpus_cfg.chan, "\2%s\2 has quit the game!", p->u->nick);
938 resign_player(p);
939 }
940 }
941
942 static void
943 join_wumpus_channel(server_t *s)
944 {
945 join(wumpus_cfg.chan, wumpus.svs->me->nick);
946
947 hook_del_server_eob(join_wumpus_channel);
948 }
949
950 static command_t wumpus_start = {
951 .name = "START",
952 .desc = N_("Starts the game."),
953 .access = AC_NONE,
954 .maxparc = 1,
955 .cmd = &cmd_start,
956 .help = { .path = "" },
957 };
958
959 static command_t wumpus_join = {
960 .name = "JOIN",
961 .desc = N_("Joins the game."),
962 .access = AC_NONE,
963 .maxparc = 0,
964 .cmd = &cmd_join,
965 .help = { .path = "" },
966 };
967
968 static command_t wumpus_look = {
969 .name = "LOOK",
970 .desc = N_("View surroundings."),
971 .access = AC_NONE,
972 .maxparc = 0,
973 .cmd = &cmd_look,
974 .help = { .path = "" },
975 };
976
977 static command_t wumpus_move = {
978 .name = "MOVE",
979 .desc = N_("Move to another room."),
980 .access = AC_NONE,
981 .maxparc = 1,
982 .cmd = &cmd_move,
983 .help = { .path = "" },
984 };
985
986 static command_t wumpus_shoot = {
987 .name = "SHOOT",
988 .desc = N_("Shoot at another room."),
989 .access = AC_NONE,
990 .maxparc = 1,
991 .cmd = &cmd_shoot,
992 .help = { .path = "" },
993 };
994
995 static command_t wumpus_resign = {
996 .name = "RESIGN",
997 .desc = N_("Resign from the game."),
998 .access = AC_NONE,
999 .maxparc = 0,
1000 .cmd = &cmd_resign,
1001 .help = { .path = "" },
1002 };
1003
1004 static command_t wumpus_reset = {
1005 .name = "RESET",
1006 .desc = N_("Resets the game."),
1007 .access = AC_IRCOP,
1008 .maxparc = 0,
1009 .cmd = &cmd_reset,
1010 .help = { .path = "" },
1011 };
1012
1013 static command_t wumpus_help = {
1014 .name = "HELP",
1015 .desc = N_("Displays this command listing."),
1016 .access = AC_NONE,
1017 .maxparc = 0,
1018 .cmd = &cmd_help,
1019 .help = { .path = "help" },
1020 };
1021
1022 static command_t wumpus_who = {
1023 .name = "WHO",
1024 .desc = N_("Displays who is playing the game."),
1025 .access = AC_NONE,
1026 .maxparc = 0,
1027 .cmd = &cmd_who,
1028 .help = { .path = "" },
1029 };
1030
1031 /* start handler */
1032 static void
1033 mod_init(module_t *m)
1034 {
1035 wumpus.svs = service_add("Wumpus", NULL);
1036 service_set_chanmsg(wumpus.svs, false);
1037
1038 if (cold_start)
1039 {
1040 hook_add_event("server_eob");
1041 hook_add_server_eob(join_wumpus_channel);
1042 }
1043 else if (me.connected)
1044 join(wumpus_cfg.chan, wumpus.svs->me->nick);
1045
1046 hook_add_event("user_delete");
1047 hook_add_user_delete(user_deleted);
1048
1049 service_bind_command(wumpus.svs, &wumpus_help);
1050 service_bind_command(wumpus.svs, &wumpus_start);
1051 service_bind_command(wumpus.svs, &wumpus_join);
1052 service_bind_command(wumpus.svs, &wumpus_move);
1053 service_bind_command(wumpus.svs, &wumpus_shoot);
1054 service_bind_command(wumpus.svs, &wumpus_resign);
1055 service_bind_command(wumpus.svs, &wumpus_reset);
1056 service_bind_command(wumpus.svs, &wumpus_who);
1057 service_bind_command(wumpus.svs, &wumpus_look);
1058 }
1059
1060 static void
1061 mod_deinit(module_unload_intent_t intent)
1062 {
1063 /* cleanup after ourselves if necessary */
1064 if (wumpus.running)
1065 end_game();
1066
1067 service_delete(wumpus.svs);
1068
1069 hook_del_user_delete(user_deleted);
1070
1071 service_unbind_command(wumpus.svs, &wumpus_help);
1072 service_unbind_command(wumpus.svs, &wumpus_start);
1073 service_unbind_command(wumpus.svs, &wumpus_join);
1074 service_unbind_command(wumpus.svs, &wumpus_move);
1075 service_unbind_command(wumpus.svs, &wumpus_shoot);
1076 service_unbind_command(wumpus.svs, &wumpus_resign);
1077 service_unbind_command(wumpus.svs, &wumpus_reset);
1078 service_unbind_command(wumpus.svs, &wumpus_who);
1079 service_unbind_command(wumpus.svs, &wumpus_look);
1080
1081 if (wumpus.move_timer)
1082 mowgli_timer_destroy(base_eventloop, wumpus.move_timer);
1083
1084 if (wumpus.start_game_timer)
1085 mowgli_timer_destroy(base_eventloop, wumpus.start_game_timer);
1086 }
1087
1088 VENDOR_DECLARE_MODULE_V1("contrib/wumpus", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_NENOLOD)