Debian Codegraph @

prboom2/src/dsda/destructible.c

raw
1 //
2 // Copyright(C) 2023 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Destructible
16 //
17
18 #include "lprintf.h"
19 #include "p_map.h"
20 #include "p_maputl.h"
21 #include "p_spec.h"
22 #include "r_main.h"
23
24 #include "dsda/utility.h"
25
26 extern bomb_t bomb;
27
28 typedef struct {
29 int health;
30 int size;
31 int* line_ids;
32 } health_group_t;
33
34 typedef struct {
35 int id;
36 health_group_t group;
37 } health_group_entry_t;
38
39 #define HEALTH_GROUP_HASH_MAX 128
40
41 static health_group_entry_t* health_group_hash[HEALTH_GROUP_HASH_MAX];
42
43 static health_group_t* dsda_HealthGroup(int id) {
44 int i;
45 int hash_id;
46 health_group_entry_t* list;
47
48 hash_id = (id % HEALTH_GROUP_HASH_MAX);
49 list = health_group_hash[hash_id];
50
51 if (!list) {
52 list = Z_CallocLevel(2, sizeof(*list));
53 health_group_hash[hash_id] = list;
54 list[0].id = id;
55 return &list[0].group;
56 }
57
58 for (i = 0; list[i].id; ++i)
59 if (list[i].id == id)
60 return &list[i].group;
61
62 list = Z_ReallocLevel(list, sizeof(*list) * (i + 2));
63 health_group_hash[hash_id] = list;
64 memset(&list[i + 1], 0, sizeof(*list));
65 list[i].id = id;
66 return &list[i].group;
67 }
68
69 void dsda_AddLineToHealthGroup(line_t* line) {
70 health_group_t* group;
71
72 group = dsda_HealthGroup(line->healthgroup);
73 if (group->health && group->health != line->health)
74 lprintf(LO_WARN, "Line %d health does not match group %d!\n",
75 line->iLineID, line->healthgroup);
76
77 group->health = line->health;
78 group->line_ids =
79 Z_ReallocLevel(group->line_ids, sizeof(*group->line_ids) * (group->size + 1));
80 group->line_ids[group->size++] = line->iLineID;
81 }
82
83 void dsda_ResetHealthGroups(void) {
84 memset(health_group_hash, 0, HEALTH_GROUP_HASH_MAX * sizeof(*health_group_hash));
85 }
86
87 static void dsda_DamageHealthGroup(int id, mobj_t* source, int damage) {
88 int i;
89 health_group_t* group;
90
91 group = dsda_HealthGroup(id);
92 group->health -= damage;
93 if (group->health < 0)
94 group->health = 0;
95
96 for (i = 0; i < group->size; ++i) {
97 line_t* line;
98
99 line = &lines[group->line_ids[i]];
100 line->health = group->health;
101 P_ActivateLine(line, source, 0, SPAC_DAMAGE | (line->health ? 0 : SPAC_DEATH));
102 }
103 }
104
105 void dsda_DamageLinedef(line_t* line, mobj_t* source, int damage) {
106 if (damage <= 0)
107 return;
108
109 if (line->healthgroup) {
110 dsda_DamageHealthGroup(line->healthgroup, source, damage);
111 }
112 else {
113 line->health -= damage;
114 if (line->health < 0)
115 line->health = 0;
116
117 P_ActivateLine(line, source, 0, SPAC_DAMAGE | (line->health ? 0 : SPAC_DEATH));
118 }
119 }
120
121 static dboolean dsda_RadiusAttackLine(line_t *line) {
122 fixed_t dist;
123 mobj_t target;
124 sector_t* frontsector;
125 sector_t* backsector;
126 int bombside;
127 dboolean sighted;
128 const fixed_t fudge = (FRACUNIT >> 4);
129
130 if (!line->health)
131 return true;
132
133 bombside = P_PointOnLineSide(bomb.spot->x, bomb.spot->y, line);
134 if (line->sidenum[bombside] == NO_INDEX)
135 return true;
136
137 dist = dsda_FixedDistancePointToLine(line->v1->x, line->v1->y,
138 line->v2->x, line->v2->y,
139 bomb.spot->x, bomb.spot->y,
140 &target.x, &target.y);
141 dist = (dist >> FRACBITS);
142 if (dist >= bomb.distance)
143 return true;
144
145 // The target is currently "on the line"
146 // Move it towards the bomb slightly to make sure it's on the right side
147 if (bomb.spot->x - target.x > fudge)
148 target.x += fudge;
149 if (target.x - bomb.spot->x > fudge)
150 target.x -= fudge;
151 if (bomb.spot->y - target.y > fudge)
152 target.y += fudge;
153 if (target.y - bomb.spot->y > fudge)
154 target.y -= fudge;
155
156 // P_CheckSight needs subsector
157 target.subsector = R_PointInSubsector(target.x, target.y);
158
159 frontsector = sides[line->sidenum[bombside]].sector;
160 if (line->sidenum[!bombside] != NO_INDEX)
161 backsector = sides[line->sidenum[!bombside]].sector;
162 else
163 backsector = NULL;
164
165 sighted = false;
166
167 if (!backsector || line->flags & ML_BLOCKEVERYTHING) {
168 if (frontsector->ceilingheight > frontsector->floorheight) {
169 target.z = frontsector->floorheight;
170 target.height = frontsector->ceilingheight - frontsector->floorheight;
171
172 sighted = P_CheckSight(&target, bomb.spot);
173 }
174 }
175 else {
176 fixed_t front_top, back_top, front_bottom, back_bottom;
177
178 front_top = frontsector->ceilingheight;
179 back_top = backsector->ceilingheight;
180 front_bottom = frontsector->floorheight;
181 back_bottom = backsector->floorheight;
182
183 if (front_top > back_top) {
184 target.z = back_top;
185 target.height = front_top - back_top;
186
187 sighted = P_CheckSight(&target, bomb.spot);
188 }
189
190 if (!sighted && front_bottom < back_bottom) {
191 target.z = front_bottom;
192 target.height = back_bottom - front_bottom;
193
194 sighted = P_CheckSight(&target, bomb.spot);
195 }
196 }
197
198 if (sighted) {
199 int damage;
200
201 damage = P_SplashDamage(dist);
202
203 dsda_DamageLinedef(line, bomb.source, damage);
204 }
205
206 return true;
207 }
208
209 void dsda_RadiusAttackDestructibles(int xl, int xh, int yl, int yh) {
210 int x, y;
211
212 // avoid collision with nested P_BlockLinesIterator
213 validcount2++;
214
215 for (y = yl; y <= yh; ++y)
216 for (x = xl; x <= xh; ++x)
217 P_BlockLinesIterator2(x, y, dsda_RadiusAttackLine);
218 }