1 /*
2     C-Dogs SDL
3     A port of the legendary (and fun) action/arcade cdogs.
4     Copyright (C) 1995 Ronny Wester
5     Copyright (C) 2003 Jeremy Chin
6     Copyright (C) 2003-2007 Lucas Martin-King
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22     This file incorporates work covered by the following copyright and
23     permission notice:
24 
25     Copyright (c) 2013-2015, 2018-2019 Cong Xu
26     All rights reserved.
27 
28     Redistribution and use in source and binary forms, with or without
29     modification, are permitted provided that the following conditions are met:
30 
31     Redistributions of source code must retain the above copyright notice, this
32     list of conditions and the following disclaimer.
33     Redistributions in binary form must reproduce the above copyright notice,
34     this list of conditions and the following disclaimer in the documentation
35     and/or other materials provided with the distribution.
36 
37     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47     POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "damage.h"
50 
51 #include "actors.h"
52 #include "campaigns.h"
53 #include "game_events.h"
54 #include "objs.h"
55 
56 // Spread the blood spurt direction
57 #define MELEE_SPREAD_FACTOR 0.5f
58 // make blood spurt further
59 #define MELEE_VEL_SCALE 40.0f
60 
61 
CanHitCharacter(const int flags,const int uid,const TActor * actor)62 bool CanHitCharacter(const int flags, const int uid, const TActor *actor)
63 {
64 	// Don't let players hurt themselves
65 	if (!(flags & FLAGS_HURTALWAYS) && uid == actor->uid)
66 	{
67 		return false;
68 	}
69 	return true;
70 }
71 
CanDamageCharacter(const int flags,const TActor * source,const TActor * target,const special_damage_e special)72 bool CanDamageCharacter(
73 	const int flags, const TActor *source,
74 	const TActor *target, const special_damage_e special)
75 {
76 	if (!CanHitCharacter(flags, source ? source->uid : -1, target))
77 	{
78 		return false;
79 	}
80 	return !ActorIsInvulnerable(
81 		target, flags, source ? source->PlayerUID : -1, gCampaign.Entry.Mode, special);
82 }
83 
84 static void TrackKills(PlayerData *pd, const TActor *victim);
DamageActor(TActor * victim,const int power,const int sourceActorUID)85 void DamageActor(TActor *victim, const int power, const int sourceActorUID)
86 {
87 	const int startingHealth = victim->health;
88 	InjureActor(victim, power);
89 	const TActor *source = ActorGetByUID(sourceActorUID);
90 	if (startingHealth > 0 && victim->health <= 0 &&
91 		source != NULL && source->PlayerUID >= 0)
92 	{
93 		TrackKills(PlayerDataGetByUID(source->PlayerUID), victim);
94 	}
95 }
TrackKills(PlayerData * pd,const TActor * victim)96 static void TrackKills(PlayerData *pd, const TActor *victim)
97 {
98 	if (!IsPVP(gCampaign.Entry.Mode) &&
99 		(victim->PlayerUID >= 0 ||
100 		(victim->flags & (FLAGS_GOOD_GUY | FLAGS_PENALTY))))
101 	{
102 		pd->Stats.Friendlies++;
103 		pd->Totals.Friendlies++;
104 	}
105 	else if (pd->UID == victim->PlayerUID)
106 	{
107 		pd->Stats.Suicides++;
108 		pd->Totals.Suicides++;
109 	}
110 	else
111 	{
112 		pd->Stats.Kills++;
113 		pd->Totals.Kills++;
114 	}
115 }
116 
DamageMelee(const NActorMelee m)117 void DamageMelee(const NActorMelee m)
118 {
119 	const TActor *a = ActorGetByUID(m.UID);
120 	if (!a->isInUse) return;
121 	const BulletClass *b = StrBulletClass(m.BulletClass);
122 	if ((HitType)m.HitType != HIT_NONE &&
123 		HasHitSound((ThingKind)m.TargetKind, m.TargetUID,
124 		SPECIAL_NONE, false))
125 	{
126 		PlayHitSound(b, (HitType)m.HitType, a->Pos);
127 	}
128 	if (!gCampaign.IsClient)
129 	{
130 		const Thing *target = ThingGetByUID(
131 			(ThingKind)m.TargetKind, m.TargetUID);
132 		const struct vec2 vel = svec2_scale(
133 			svec2_add(
134 				svec2_normalize(svec2_subtract(target->Pos, a->Pos)),
135 				svec2(
136 					RAND_FLOAT(-MELEE_SPREAD_FACTOR, MELEE_SPREAD_FACTOR),
137 					RAND_FLOAT(-MELEE_SPREAD_FACTOR, MELEE_SPREAD_FACTOR))),
138 			MELEE_VEL_SCALE);
139 		Damage(
140 			vel, b, a->flags, a, (ThingKind)m.TargetKind, m.TargetUID);
141 	}
142 }
143