Crossfire Server, Trunk
check_monster.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_monster_c =
3  * "$Id$";
4  */
5 
6 /*
7  * CrossFire, A Multiplayer game for X-windows
8  *
9  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  * Copyright (C) 1992 Frank Tore Johansen
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * The authors can be reached via e-mail at crossfire-devel@real-time.com
27  */
28 
29 /*
30  * This is the unit tests file for server/monster.c
31  */
32 
33 #include <stdlib.h>
34 #include <check.h>
35 
36 #include <global.h>
37 #include <sproto.h>
38 #include <toolkit_common.h>
39 
40 void setup(void) {
41 }
42 
43 void teardown(void) {
44 }
45 
46 START_TEST(test_monster_find_nearest_enemy) {
47  object *first, *second, *third, *owner, *found;
48  player pl;
49 
50  // The function randomizes directions, so check 200 times to be safe.
51 
52  memset(&pl, 0, sizeof(pl));
53 
54  mapstruct *map = get_empty_map(3, 3);
55 
56  first = create_archetype("kobold");
57  fail_unless(QUERY_FLAG(first, FLAG_MONSTER));
58  object_insert_in_map_at(first, map, NULL, 0, 1, 1);
59 
60  for (uint8_t i = 0; i < 200; i++) {
61  fail_unless(monster_find_nearest_enemy(first, NULL) == NULL, "Found something when nothing?");
62  }
63 
64  second = create_archetype("kobold");
65  object_insert_in_map_at(second, map, NULL, 0, 0, 1);
66  for (uint8_t i = 0; i < 200; i++) {
67  fail_unless(monster_find_nearest_enemy(first, NULL) == second, "Didn't find second monster?");
68  }
69 
70  owner = create_archetype("dwarf_player");
71  owner->contr = &pl;
72  object_insert_in_map_at(owner, map, NULL, 0, 0, 0);
73  first->owner = owner;
74  first->ownercount = owner->count;
75  for (uint8_t i = 0; i < 200; i++) {
76  found = monster_find_nearest_enemy(first, owner);
77  fail_if(found == owner, "Found owner?");
78  fail_unless(found == second, "Should find second!");
79  }
80 
81  second->owner = owner;
82  second->ownercount = owner->count;
83  for (uint8_t i = 0; i < 200; i++) {
84  found = monster_find_nearest_enemy(first, owner);
85  fail_if(found == owner, "Found owner?");
86  fail_unless(found == NULL, "Shouldn't find anything since both are pets");
87  }
88 
89  pl.petmode = pet_sad;
90  for (uint8_t i = 0; i < 200; i++) {
91  found = monster_find_nearest_enemy(first, owner);
92  fail_unless(found == NULL, "Pets shouldn't attack other pets");
93  }
94 
95  third = create_archetype("kobold");
96  object_insert_in_map_at(third, map, NULL, 0, 1, 0);
97  for (uint8_t i = 0; i < 200; i++) {
98  found = monster_find_nearest_enemy(first, owner);
99  fail_unless(found == third, "Should find third monster");
100  }
101 
102  uint8_t co = 0, cs = 0, ct = 0;
103  for (uint16_t i = 0; i < 2000; i++) {
104  found = monster_find_nearest_enemy(first, NULL);
105  fail_unless(found, "Should find a target!");
106  if (found == owner) {
107  co++;
108  } else if (found == second) {
109  cs++;
110  } else {
111  ct++;
112  }
113  }
114  fail_unless(co != 0, "Should have found the owner");
115  fail_unless(cs != 0, "Should have found second");
116  fail_unless(ct != 0, "Should have found third");
117 }
118 END_TEST
119 
120 Suite *monster_suite(void) {
121  Suite *s = suite_create("monster");
122  TCase *tc_core = tcase_create("Core");
123 
124  /*setup and teardown will be called before each test in testcase 'tc_core' */
125  tcase_add_checked_fixture(tc_core, setup, teardown);
126 
127  suite_add_tcase(s, tc_core);
128  tcase_add_test(tc_core, test_monster_find_nearest_enemy);
129 
130  return s;
131 }
132 
133 int main(void) {
134  int nf;
135 
136  cctk_setdatadir(SOURCE_ROOT "lib");
137  init(0, NULL);
138 
139  Suite *s = monster_suite();
140  SRunner *sr = srunner_create(s);
141 
142  // Uncomment to debug
143  // srunner_set_fork_status(sr, CK_NOFORK);
144 
145  srunner_set_xml(sr, LOGDIR "/unit/server/monster.xml");
146  srunner_set_log(sr, LOGDIR "/unit/server/monster.out");
147  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
148  nf = srunner_ntests_failed(sr);
149  srunner_free(sr);
150  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
151 }
global.h
monster_suite
END_TEST Suite * monster_suite(void)
Definition: check_monster.c:49
obj::count
tag_t count
Definition: object.h:302
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.c:69
pl
Definition: player.h:105
START_TEST
START_TEST(test_empty)
Definition: check_monster.c:44
monster_find_nearest_enemy
object * monster_find_nearest_enemy(object *npc, object *owner)
Definition: monster.c:175
toolkit_common.h
disinfect.map
map
Definition: disinfect.py:4
pl::petmode
petmode_t petmode
Definition: player.h:115
sproto.h
mapdef
Definition: map.h:317
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:281
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
obj::contr
struct pl * contr
Definition: object.h:279
setup
void setup(void)
Definition: check_monster.c:36
pet_sad
@ pet_sad
Definition: player.h:59
init
void init(int argc, char **argv)
Definition: init.c:1108
obj::ownercount
tag_t ownercount
Definition: object.h:385
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:2080
main
int main(void)
Definition: check_monster.c:62
obj::owner
struct obj * owner
Definition: object.h:382
teardown
void teardown(void)
Definition: check_monster.c:40
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:869