Atrinik Server  4.0
swarm_spell.c
Go to the documentation of this file.
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 
30 #include <global.h>
31 #include <object.h>
32 #include <object_methods.h>
33 
37 static const int cardinal_adjust[9] = {
38  -3, -2, -1, 0, 0, 0, 1, 2, 3
39 };
40 
44 static const int diagonal_adjust[10] = {
45  -3, -2, -2, -1, 0, 0, 1, 2, 2, 3
46 };
47 
49 static void
50 process_func (object *op)
51 {
52  HARD_ASSERT(op != NULL);
53 
54  if (op->stats.hp == 0 || object_owner(op) == NULL) {
55  object_remove(op, 0);
56  object_destroy(op);
57  return;
58  }
59 
60  op->stats.hp--;
61  int basedir = op->direction;
62 
63  if (basedir == 0) {
64  basedir = get_random_dir();
65  }
66 
67  /* New offset calculation to make swarm element distribution
68  * more uniform */
69  int adjustdir;
70  if (op->stats.hp != 0) {
71  if (basedir & 1) {
72  adjustdir = cardinal_adjust[rndm(0, 8)];
73  } else {
74  adjustdir = diagonal_adjust[rndm(0, 9)];
75  }
76  } else {
77  /* Fire the last one from forward. */
78  adjustdir = 0;
79  }
80 
81  int target_x = op->x + freearr_x[absdir(basedir + adjustdir)];
82  int target_y = op->y + freearr_y[absdir(basedir + adjustdir)];
83 
84  /* Back up one space so we can hit point-blank targets, but this
85  * necessitates extra get_map_from_coord check below */
86  int x = target_x - freearr_x[basedir];
87  int y = target_y - freearr_y[basedir];
88 
89  if (get_map_from_coord(op->map, &x, &y) == NULL) {
90  return;
91  }
92 
93  if (wall(op->map, x, y)) {
94  return;
95  }
96 
97  object *caster = object_owner(op);
98  if (caster == NULL) {
99  caster = op;
100  }
101 
102  /* Bombs away! */
104  caster,
105  x,
106  y,
107  basedir,
108  op->other_arch,
109  op->stats.sp,
110  NULL);
111 }
112 
117 {
118  OBJECT_METHODS(SWARM_SPELL)->process_func = process_func;
119 }
static const int cardinal_adjust[9]
Definition: swarm_spell.c:37
void object_destroy(object *ob)
Definition: object.c:1441
mapstruct * get_map_from_coord(mapstruct *m, int *x, int *y)
Definition: map.c:1869
int wall(mapstruct *m, int x, int y)
Definition: map.c:486
static int absdir(int d)
Definition: define.h:1838
int16_t sp
Definition: living.h:78
struct archetype * other_arch
Definition: object.h:228
void object_remove(object *op, int flags)
Definition: object.c:1623
int32_t hp
Definition: living.h:72
int16_t y
Definition: object.h:276
#define SWARM_SPELL
Definition: define.h:518
int8_t direction
Definition: object.h:350
struct mapdef * map
Definition: object.h:139
OBJECT_TYPE_INIT_DEFINE(swarm_spell)
Definition: swarm_spell.c:116
int16_t x
Definition: object.h:273
int fire_arch_from_position(object *op, object *caster, int16_t x, int16_t y, int dir, struct archetype *at, int type, object *target)
Definition: spell_util.c:615
static const int diagonal_adjust[10]
Definition: swarm_spell.c:44
living stats
Definition: object.h:481
int get_random_dir(void)
Definition: move.c:41
int freearr_x[SIZEOFFREE]
Definition: object.c:84
#define OBJECT_METHODS(type)
object * object_owner(object *op)
Definition: object.c:857
static void process_func(object *op)
Definition: swarm_spell.c:50
int freearr_y[SIZEOFFREE]
Definition: object.c:99