Crossfire Server, Trunk
check_monster.cpp
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  socket_struct sock;
50 
51  // The function randomizes directions, so check 200 times to be safe.
52 
53  memset(&pl, 0, sizeof(pl));
54  memset(&sock, 0, sizeof(sock));
55  pl.socket = &sock;
56 
57  mapstruct *map = get_empty_map(3, 3);
58 
59  first = create_archetype("kobold");
60  fail_unless(QUERY_FLAG(first, FLAG_MONSTER));
61  object_insert_in_map_at(first, map, NULL, 0, 1, 1);
62 
63  for (uint8_t i = 0; i < 200; i++) {
64  fail_unless(monster_find_nearest_enemy(first, NULL) == NULL, "Found something when nothing?");
65  }
66 
67  second = create_archetype("kobold");
68  object_insert_in_map_at(second, map, NULL, 0, 0, 1);
69  for (uint8_t i = 0; i < 200; i++) {
70  fail_unless(monster_find_nearest_enemy(first, NULL) == second, "Didn't find second monster?");
71  }
72 
73  owner = create_archetype("dwarf_player");
74  owner->contr = &pl;
75  object_insert_in_map_at(owner, map, NULL, 0, 0, 0);
76  first->owner = owner;
77  first->ownercount = owner->count;
78  for (uint8_t i = 0; i < 200; i++) {
80  fail_if(found == owner, "Found owner?");
81  fail_unless(found == second, "Should find second!");
82  }
83 
84  second->owner = owner;
85  second->ownercount = owner->count;
86  for (uint8_t i = 0; i < 200; i++) {
88  fail_if(found == owner, "Found owner?");
89  fail_unless(found == NULL, "Shouldn't find anything since both are pets");
90  }
91 
92  pl.petmode = pet_sad;
93  for (uint8_t i = 0; i < 200; i++) {
95  fail_unless(found == NULL, "Pets shouldn't attack other pets");
96  }
97 
98  third = create_archetype("kobold");
99  object_insert_in_map_at(third, map, NULL, 0, 1, 0);
100  for (uint8_t i = 0; i < 200; i++) {
102  fail_unless(found == third, "Should find third monster");
103  }
104 
105  uint8_t co = 0, cs = 0, ct = 0;
106  for (uint16_t i = 0; i < 2000; i++) {
108  fail_unless(found, "Should find a target!");
109  if (found == owner) {
110  co++;
111  } else if (found == second) {
112  cs++;
113  } else {
114  ct++;
115  }
116  }
117  fail_unless(co != 0, "Should have found the owner");
118  fail_unless(cs != 0, "Should have found second");
119  fail_unless(ct != 0, "Should have found third");
120 }
121 END_TEST
122 
123 Suite *monster_suite(void) {
124  Suite *s = suite_create("monster");
125  TCase *tc_core = tcase_create("Core");
126 
127  /*setup and teardown will be called before each test in testcase 'tc_core' */
128  tcase_add_checked_fixture(tc_core, setup, teardown);
129 
130  suite_add_tcase(s, tc_core);
131  tcase_add_test(tc_core, test_monster_find_nearest_enemy);
132 
133  return s;
134 }
135 
136 int main(void) {
137  int nf;
138 
139  cctk_setdatadir(SOURCE_ROOT "lib");
140  init(0, NULL);
141 
142  Suite *s = monster_suite();
143  SRunner *sr = srunner_create(s);
144 
145  // Uncomment to debug
146  // srunner_set_fork_status(sr, CK_NOFORK);
147 
148  srunner_set_xml(sr, LOGDIR "/unit/server/monster.xml");
149  srunner_set_log(sr, LOGDIR "/unit/server/monster.out");
150  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
151  nf = srunner_ntests_failed(sr);
152  srunner_free(sr);
153  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
154 }
teardown
void teardown(void)
Definition: check_monster.cpp:40
global.h
object::owner
object * owner
Definition: object.h:387
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.cpp:842
player
Definition: player.h:105
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.cpp:69
START_TEST
START_TEST(test_empty)
Definition: check_monster.cpp:44
socket_struct
Definition: newserver.h:89
object::ownercount
tag_t ownercount
Definition: object.h:390
object::count
tag_t count
Definition: object.h:307
setup
void setup(void)
Definition: check_monster.cpp:36
monster_find_nearest_enemy
object * monster_find_nearest_enemy(object *npc, object *owner)
Definition: monster.cpp:175
object::contr
struct player * contr
Definition: object.h:284
toolkit_common.h
disinfect.map
map
Definition: disinfect.py:4
main
int main(void)
Definition: check_monster.cpp:62
init
pluglist shows those as well as a short text describing each the list will simply appear empty The keyword for the Python plugin is Python plugout< keyword > Unloads a given identified by its _keyword_ So if you want to unload the Python you need to do plugout Python plugin< libname > Loads a given whose _filename_ is libname So in the case of you d have to do a plugin cfpython so Note that all filenames are relative to the default plugin it tries to load all available files in the SHARE plugins directory as plugin libraries It first displays the Initializing the plugin has the opportunity to signal itself by a message on the console Then the server displays an informative message containing both the plugin content and its keyword For the Python the standard load process thus GreenGoblin When a plugin has been it can request to be warned whenever a global event and are named freely by the developer If the directory doesn t nothing will happen< event name > can be init
Definition: plugins.txt:54
monster_suite
END_TEST Suite * monster_suite(void)
Definition: check_monster.cpp:49
sproto.h
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.cpp:2095
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
pet_sad
@ pet_sad
Definition: player.h:59
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
mapstruct
Definition: map.h:314
first
Crossfire Protocol most of the time after the actual code was already omit certain important and possibly make life miserable any new developer or curious player should be able to find most of the relevant information here If inconsistencies are found or this documentation proves to be consider the latest server side protocol code in the public source code repository as the authoritative reference Introduction If you were ever curious enough to telnet or netcat to a Crossfire chances are you were sorely disappointed While the protocol may seem to use plain text at first
Definition: protocol.txt:20
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28