Crossfire Server, Trunk
check_attack.cpp
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_attack_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/attack.c
31  */
32 
33 #include <global.h>
34 #include <stdlib.h>
35 #include <check.h>
36 #include <loader.h>
37 #include <toolkit_common.h>
38 #include <sproto.h>
39 
40 static void setup(void) {
41 }
42 
43 static void teardown(void) {
44  /* put any cleanup steps here, they will be run after each testcase */
45 }
46 
47 START_TEST(test_hit_player) {
48  object *victim = NULL;
49  object *hitter = NULL;
50  object *floor = NULL;
51  mapstruct *map = NULL;
52  archetype *deplete = NULL;
53  int test;
54 
55  map = get_empty_map(5, 5);
56  floor = create_archetype("battleground");
57  FAIL_UNLESS(floor != NULL, "can't find archetype battleground");
58  object_insert_in_map_at(floor, map, NULL, 0, 0, 0);
59  floor = create_archetype("battleground");
60  FAIL_UNLESS(floor != NULL, "can't find archetype battleground");
61  object_insert_in_map_at(floor, map, NULL, 0, 1, 0);
62 
63  deplete = find_archetype(ARCH_DEPLETION);
64  FAIL_UNLESS(deplete != NULL, "can't find archetype %s", ARCH_DEPLETION);
65 
66  victim = create_archetype("kobold");
67  FAIL_UNLESS(victim != NULL, "couldn't create kobold");
68  FAIL_UNLESS(victim->inv == NULL, "kobold shouldn't have an inventory");
69  victim->stats.hp = 5000;
70  victim->stats.maxhp = 5000;
71  victim->resist[ATNR_DEPLETE] = 100;
72  victim->resist[ATNR_FIRE] = 100;
73  object_insert_in_map_at(victim, map, NULL, 0, 0, 0);
74  hitter = create_archetype("sword");
75  FAIL_UNLESS(hitter != NULL, "couldn't create sword");
76  hitter->attacktype = AT_DEPLETE|AT_FIRE;
77  hitter->stats.dam = 100;
78  hitter->map = map;
79  object_insert_in_map_at(hitter, map, NULL, 0, 1, 0);
80 
81  FAIL_UNLESS(arch_present_in_ob(deplete, victim) == NULL, "victim shouldn't be depleted before being attacked");
82 
83  for (test = 0; test < 100; test++) {
84  hit_player(victim, hitter->stats.dam, hitter, hitter->attacktype, 0);
85  FAIL_UNLESS(victim->stats.hp == victim->stats.maxhp, "victim should have %d hp and not %d.", victim->stats.maxhp, victim->stats.hp);
86  }
87  FAIL_UNLESS(victim->inv == NULL, "kobold shouldn't have an inventory after attacked");
88  FAIL_UNLESS(arch_present_in_ob(deplete, victim) == NULL, "victim shouldn't be depleted when slaying not set");
89 
90  hitter->slaying = add_string(victim->race);
91  victim->resist[ATNR_FIRE] = 95;
92  for (test = 0; test < 100 && arch_present_in_ob(deplete, victim) == NULL; test++) {
93  hit_player(victim, hitter->stats.dam, hitter, hitter->attacktype, 0);
94  }
95  FAIL_UNLESS(arch_present_in_ob(deplete, victim) != NULL, "victim should be depleted when slaying is set");
96  FAIL_UNLESS(victim->stats.hp != victim->stats.maxhp, "victim shouldn't have %d hp", victim->stats.hp);
97 }
98 END_TEST
99 
100 START_TEST(test_fix_object_confused) {
101  player *pl = static_cast<player *>(calloc(1, sizeof(player)));
102  object *ob = create_archetype("human_player");
103  FAIL_UNLESS(ob, "wrong archetype human_player");
104  FAIL_UNLESS(ob->type == PLAYER, "archetype isn't a player");
105  ob->contr = pl;
106 
107  confuse_living(ob, NULL, 0);
108  FAIL_UNLESS(QUERY_FLAG(ob, FLAG_CONFUSED), "player should be confused after confuse_living");
109 
110  fix_object(ob);
111  FAIL_UNLESS(QUERY_FLAG(ob, FLAG_CONFUSED), "player should be confused after fix_object");
112 
114  free(pl);
115 }
116 END_TEST
117 
118 static Suite *attack_suite(void) {
119  Suite *s = suite_create("attack");
120  TCase *tc_core = tcase_create("Core");
121 
122  /*setup and teardown will be called before each test in testcase 'tc_core' */
123  tcase_add_checked_fixture(tc_core, setup, teardown);
124 
125  suite_add_tcase(s, tc_core);
126  tcase_add_test(tc_core, test_hit_player);
127  tcase_add_test(tc_core, test_fix_object_confused);
128 
129  return s;
130 }
131 
132 int main(void) {
133  int nf;
134  Suite *s = attack_suite();
135  SRunner *sr = srunner_create(s);
136 
137  /* If you wish to debug the program, uncomment this line. */
138  /*srunner_set_fork_status (sr, CK_NOFORK); */
139 
141  cctk_setdatadir(SOURCE_ROOT "lib");
142  init(0, NULL);
143 
144  srunner_set_xml(sr, LOGDIR "/unit/server/attack.xml");
145  srunner_set_log(sr, LOGDIR "/unit/server/attack.out");
146  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
147  nf = srunner_ntests_failed(sr);
148  srunner_free(sr);
149  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
150 }
PLAYER
@ PLAYER
Definition: object.h:112
global.h
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:545
settings
struct Settings settings
Definition: init.cpp:139
attack_suite
static END_TEST Suite * attack_suite(void)
Definition: check_attack.cpp:118
FLAG_CONFUSED
#define FLAG_CONFUSED
Definition: define.h:311
llevError
@ llevError
Definition: logger.h:11
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
ARCH_DEPLETION
#define ARCH_DEPLETION
Definition: object.h:590
confuse_living
void confuse_living(object *op, object *hitter, int dam)
Definition: attack.cpp:2311
arch_present_in_ob
object * arch_present_in_ob(const archetype *at, const object *op)
Definition: object.cpp:3222
guildjoin.ob
ob
Definition: guildjoin.py:42
fix_object
void fix_object(object *op)
Definition: living.cpp:1132
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
setup
static void setup(void)
Definition: check_attack.cpp:40
disinfect.map
map
Definition: disinfect.py:4
Settings::debug
LogLevel debug
Definition: global.h:243
sword_of_souls.victim
victim
Definition: sword_of_souls.py:12
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
object_free
void object_free(object *ob, int flags)
Definition: object.cpp:1592
archetype
Definition: object.h:483
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:2100
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
ATNR_FIRE
#define ATNR_FIRE
Definition: attack.h:51
ATNR_DEPLETE
#define ATNR_DEPLETE
Definition: attack.h:65
mapstruct
Definition: map.h:313
AT_DEPLETE
#define AT_DEPLETE
Definition: attack.h:92
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:266
init
void init(int argc, char **argv)
Definition: init.cpp:1087
hit_player
int hit_player(object *op, int dam, object *hitter, uint32_t type, int full_hit)
Definition: attack.cpp:1903
loader.h
teardown
static void teardown(void)
Definition: check_attack.cpp:43
death_message.hitter
hitter
Definition: death_message.py:33
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
START_TEST
START_TEST(test_hit_player)
Definition: check_attack.cpp:47
main
int main(void)
Definition: check_attack.cpp:132
AT_FIRE
#define AT_FIRE
Definition: attack.h:78