Crossfire Server, Trunk
perf_tests.cpp
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2024 Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
23 /*
24  * This is a performance-related test. You need to specify the number of loops to do on the command line.
25  */
26 
27 #include <stdlib.h>
28 #include <global.h>
29 #include <sproto.h>
30 
31 #include <array>
32 
33 constexpr int TestSize = 20;
34 int testCount = 0;
35 
37  int16_t nx, ny;
38  mapstruct *m;
39 
40  if (!op)
41  return 0;
42  if (op->glow_radius > 0)
43  return 1;
44 
45  if (op->map) {
46  int x, y;
47 
48  /* Check the spaces with the max light radius to see if any of them
49  * have lights, and if any of them light the player enough, then return 1.
50  */
51  for (x = op->x-MAX_LIGHT_RADII; x <= op->x+MAX_LIGHT_RADII; x++) {
52  for (y = op->y-MAX_LIGHT_RADII; y <= op->y+MAX_LIGHT_RADII; y++) {
53  m = op->map;
54  nx = x;
55  ny = y;
56 
57  if (get_map_flags(m, &m, nx, ny, &nx, &ny)&P_OUT_OF_MAP)
58  continue;
59 
60  if (ihypot(x-op->x, y-op->y) < GET_MAP_LIGHT(m, nx, ny))
61  return 1;
62  }
63  }
64  }
65  return 0;
66 }
67 
69  pticks = 1;
70  printf("running %d checks...\n", testCount);
71  while (testCount-- > 0) {
72  object dummy;
73  memset(&dummy, 0, sizeof(dummy));
74  mapstruct *test_maps[4];
75  for (size_t i = 0; i < 4; i++)
76  test_maps[i] = get_empty_map(TestSize, TestSize);
77  test_maps[0]->tile_map[1] = test_maps[1];
78  test_maps[0]->tile_map[2] = test_maps[2];
79  test_maps[1]->tile_map[3] = test_maps[0];
80  test_maps[1]->tile_map[2] = test_maps[3];
81  test_maps[2]->tile_map[0] = test_maps[0];
82  test_maps[2]->tile_map[1] = test_maps[3];
83  test_maps[3]->tile_map[0] = test_maps[1];
84  test_maps[3]->tile_map[3] = test_maps[2];
85 
86  for (int l = 40 + cf_random() % 25; l > 0; l--) {
87  int map = cf_random() % 4, x = cf_random() % TestSize, y = cf_random() % TestSize, light = (1 + cf_random() % (MAX_LIGHT_RADII - 1));
88  SET_MAP_LIGHT(test_maps[map], x, y, light);
89  }
90 
91  for (int m = 0; m < 4; m++) {
92  dummy.map = test_maps[m];
93  for (dummy.x = 0; dummy.x < TestSize; dummy.x++) {
94  for (dummy.y = 0; dummy.y < TestSize; dummy.y++) {
95  int old = legacy_monster_stand_in_light(&dummy);
96  int current = monster_stand_in_light(&dummy);
97  if (old != current) {
98  printf("%d %d?!?\n", old, current);
99  }
100  pticks = 1 - pticks;
101  }
102  }
103  }
104 
105  for (size_t i = 0; i < 4; i++)
106  delete_map(test_maps[i]);
107  }
108  printf("end\n");
109 }
110 
111 int main(int argc, const char **argv) {
112  if (argc > 1 && ((testCount = atoi(argv[1])) > 0))
114  return 0;
115 }
global.h
banquet.l
l
Definition: banquet.py:164
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.cpp:843
monster_stand_in_light
int monster_stand_in_light(object *op)
Definition: monster.cpp:2760
diamondslots.x
x
Definition: diamondslots.py:15
object::x
int16_t x
Definition: object.h:335
object::map
struct mapstruct * map
Definition: object.h:305
pticks
uint32_t pticks
Definition: time.cpp:47
ihypot
int ihypot(int a, int b)
Definition: utils.cpp:570
object::y
int16_t y
Definition: object.h:335
m
static event_registration m
Definition: citylife.cpp:425
disinfect.map
map
Definition: disinfect.py:4
legacy_monster_stand_in_light
int legacy_monster_stand_in_light(object *op)
Definition: perf_tests.cpp:36
test_stand_in_light
void test_stand_in_light()
Definition: perf_tests.cpp:68
sproto.h
GET_MAP_LIGHT
#define GET_MAP_LIGHT(M, X, Y)
Definition: map.h:162
cf_random
uint32_t cf_random(void)
Definition: cf_random.cpp:5
delete_map
void delete_map(mapstruct *m)
Definition: map.cpp:1699
P_OUT_OF_MAP
#define P_OUT_OF_MAP
Definition: map.h:247
TestSize
constexpr int TestSize
Definition: perf_tests.cpp:33
get_map_flags
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.cpp:300
mapstruct
Definition: map.h:313
give.op
op
Definition: give.py:33
testCount
int testCount
Definition: perf_tests.cpp:34
diamondslots.y
y
Definition: diamondslots.py:16
mapstruct::tile_map
mapstruct * tile_map[4]
Definition: map.h:352
replace.current
current
Definition: replace.py:64
SET_MAP_LIGHT
#define SET_MAP_LIGHT(M, X, Y, L)
Definition: map.h:164
main
int main(int argc, const char **argv)
Definition: perf_tests.cpp:111
MAX_LIGHT_RADII
#define MAX_LIGHT_RADII
Definition: define.h:450