Atrinik Server  4.0
light_apply.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <stdarg.h>
30 #include <arch.h>
31 #include <player.h>
32 #include <object.h>
33 
34 /*
35  * Player applies a torch on the ground. Ensure the torch is lit and not
36  * applied.
37  */
38 START_TEST(test_light_apply_apply_1)
39 {
40  mapstruct *map;
41  object *pl, *torch;
42 
43  check_setup_env_pl(&map, &pl);
44 
45  torch = object_insert_map(arch_get("torch"), map, NULL, 0);
46  player_apply(pl, torch, 0, 0);
47  ck_assert(!QUERY_FLAG(torch, FLAG_APPLIED));
48  ck_assert_int_ne(torch->glow_radius, 0);
49  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], NULL);
50 }
51 END_TEST
52 
53 /*
54  * Player applies a lit torch on the ground Ensure the torch is extinguished and
55  * not applied.
56  */
57 START_TEST(test_light_apply_apply_2)
58 {
59  mapstruct *map;
60  object *pl, *torch;
61 
62  check_setup_env_pl(&map, &pl);
63 
64  torch = object_insert_map(arch_get("torch"), map, NULL, 0);
65  manual_apply(torch, torch, 0);
66  player_apply(pl, torch, 0, 0);
67  ck_assert(!QUERY_FLAG(torch, FLAG_APPLIED));
68  ck_assert_int_eq(torch->glow_radius, 0);
69  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], NULL);
70 }
71 END_TEST
72 
73 /*
74  * Player applies a torch in his inventory. Ensure it's lit and applied.
75  */
76 START_TEST(test_light_apply_apply_3)
77 {
78  mapstruct *map;
79  object *pl, *torch;
80 
81  check_setup_env_pl(&map, &pl);
82 
83  torch = object_insert_into(arch_get("torch"), pl, 0);
84  player_apply(pl, torch, 0, 0);
85  ck_assert(QUERY_FLAG(torch, FLAG_APPLIED));
86  ck_assert_int_ne(torch->glow_radius, 0);
87  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], torch);
88 }
89 END_TEST
90 
91 /*
92  * Player applies a lit torch in his inventory. Ensure it was extinguished and
93  * not applied.
94  */
95 START_TEST(test_light_apply_apply_4)
96 {
97  mapstruct *map;
98  object *pl, *torch;
99 
100  check_setup_env_pl(&map, &pl);
101 
102  torch = object_insert_map(arch_get("torch"), map, NULL, 0);
103  manual_apply(torch, torch, 0);
104  ck_assert(!QUERY_FLAG(torch, FLAG_APPLIED));
105  ck_assert_int_ne(torch->glow_radius, 0);
106  object_remove(torch, 0);
107  torch = object_insert_into(torch, pl, 0);
108  ck_assert(!QUERY_FLAG(torch, FLAG_APPLIED));
109  ck_assert_int_ne(torch->glow_radius, 0);
110  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], NULL);
111 
112  player_apply(pl, torch, 0, 0);
113 
114  ck_assert(QUERY_FLAG(torch, FLAG_APPLIED));
115  ck_assert_int_ne(torch->glow_radius, 0);
116  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], torch);
117 }
118 END_TEST
119 
120 /*
121  * Player applies a torch in his inventory, then a torch on the ground. Ensure
122  * both torches are lit, but only the one in inventory is applied.
123  */
124 START_TEST(test_light_apply_apply_5)
125 {
126  mapstruct *map;
127  object *pl, *torch, *torch2;
128 
129  check_setup_env_pl(&map, &pl);
130 
131  torch = object_insert_into(arch_get("torch"), pl, 0);
132  ck_assert_ptr_ne(torch, NULL);
133  torch2 = object_insert_map(arch_get("torch"), map, NULL, 0);
134  ck_assert_ptr_ne(torch2, NULL);
135 
136  player_apply(pl, torch, 0, 0);
137  ck_assert(QUERY_FLAG(torch, FLAG_APPLIED));
138  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], torch);
139  ck_assert_int_ne(torch->glow_radius, 0);
140  ck_assert(!QUERY_FLAG(torch2, FLAG_APPLIED));
141  ck_assert_int_eq(torch2->glow_radius, 0);
142 
143  player_apply(pl, torch2, 0, 0);
144  ck_assert(QUERY_FLAG(torch, FLAG_APPLIED));
145  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], torch);
146  ck_assert_int_ne(torch->glow_radius, 0);
147  ck_assert(!QUERY_FLAG(torch2, FLAG_APPLIED));
148  ck_assert_int_ne(torch2->glow_radius, 0);
149 }
150 END_TEST
151 
152 /*
153  * Player applies stacked torches, ensure a new torch is created and lit and
154  * the original is not lit. Then apply the original and ensure it's lit and
155  * applied, and that the old one gets extinguished and not applied.
156  */
157 START_TEST(test_light_apply_apply_6)
158 {
159  mapstruct *map;
160  object *pl, *torch, *torch2;
161 
162  check_setup_env_pl(&map, &pl);
163 
164  torch = arch_get("torch");
165  torch->nrof = 2;
166  torch = object_insert_into(torch, pl, 0);
167  ck_assert_ptr_ne(torch, NULL);
168 
169  player_apply(pl, torch, 0, 0);
170  torch2 = torch;
171  torch = CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT];
172 
173  ck_assert_ptr_ne(torch, torch2);
174  ck_assert_ptr_ne(torch, NULL);
175  ck_assert(QUERY_FLAG(torch, FLAG_APPLIED));
176  ck_assert_int_ne(torch->glow_radius, 0);
177  ck_assert_uint_eq(torch->nrof, 1);
178  ck_assert(!QUERY_FLAG(torch2, FLAG_APPLIED));
179  ck_assert_int_eq(torch2->glow_radius, 0);
180 
181  player_apply(pl, torch2, 0, 0);
182  ck_assert(!QUERY_FLAG(torch, FLAG_APPLIED));
183  ck_assert_int_eq(torch->glow_radius, 0);
184  ck_assert(QUERY_FLAG(torch2, FLAG_APPLIED));
185  ck_assert_int_ne(torch2->glow_radius, 0);
186  ck_assert_ptr_eq(CONTR(pl)->equipment[PLAYER_EQUIP_LIGHT], torch2);
187 }
188 END_TEST
189 
190 static Suite *suite(void)
191 {
192  Suite *s = suite_create("light_apply");
193  TCase *tc_core = tcase_create("Core");
194 
195  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
196  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
197 
198  suite_add_tcase(s, tc_core);
199  tcase_add_test(tc_core, test_light_apply_apply_1);
200  tcase_add_test(tc_core, test_light_apply_apply_2);
201  tcase_add_test(tc_core, test_light_apply_apply_3);
202  tcase_add_test(tc_core, test_light_apply_apply_4);
203  tcase_add_test(tc_core, test_light_apply_apply_5);
204  tcase_add_test(tc_core, test_light_apply_apply_6);
205 
206  return s;
207 }
208 
209 void check_types_light_apply(void)
210 {
211  check_run_suite(suite(), __FILE__);
212 }
object * arch_get(const char *name)
Definition: arch.c:430
#define QUERY_FLAG(xyz, p)
Definition: define.h:761
void object_remove(object *op, int flags)
Definition: object.c:1623
int manual_apply(object *op, object *tmp, int aflag)
Definition: apply.c:52
object * object_insert_into(object *op, object *where, int flag)
Definition: object.c:2158
uint32_t nrof
Definition: object.h:264
object * object_insert_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:1741
#define FLAG_APPLIED
Definition: define.h:1182
int8_t glow_radius
Definition: object.h:338
int player_apply(object *pl, object *op, int aflag, int quiet)
Definition: apply.c:123
Definition: map.h:536