Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 7ded942

Browse files
committed
Removed unnecessary comments
Moved human functions into a separate file Minor tweaks
1 parent f5df328 commit 7ded942

File tree

4 files changed

+234
-238
lines changed

4 files changed

+234
-238
lines changed

Base.rte/AI/HumanAI.lua

Lines changed: 11 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,35 @@
11
dofile("Base.rte/Constants.lua")
22
require("AI/NativeHumanAI")
3+
require("AI/HumanFunctions");
34

45
function Create(self)
56
self.AI = NativeHumanAI:Create(self);
6-
77
-- You can turn features on and off here
88
self.armSway = true;
9-
self.autoEquip = false;
9+
self.automaticEquip = true;
1010
self.alternativeGib = true;
1111
self.visibleInventory = true;
12-
13-
self.pushStrength = 10; -- Variable
14-
self.tilt = 0.3; -- Visible inventory item angle
15-
16-
self.lastAngle = self:GetAimAngle(false);
17-
18-
if self.armSway == true then
19-
self.lastHandPos = {self.Pos, self.Pos};
20-
end
21-
22-
if self.alternativeGib == true then
23-
self.detachLimit = self.GibWoundLimit;
24-
self.GibWoundLimit = self.GibWoundLimit * 1.5;
25-
end
2612
end
27-
2813
function Update(self)
2914

30-
self.ctrl = self:GetController();
15+
self.controller = self:GetController();
3116

32-
-- Detach limbs instead of regular gibbing
33-
if self.alternativeGib == true then
34-
if self.WoundCount > self.detachLimit then
35-
self.detachLimit = self.WoundCount + 1;
36-
local parts = {self.BGArm, self.BGLeg, self.FGArm, self.FGLeg, self.Head}; -- Piority order
37-
local mostWounds = -1;
38-
local detachLimb;
39-
-- Pick the limb with most wounds and detach it
40-
for i = 1, #parts do
41-
local limb = parts[i];
42-
if limb and limb.WoundCount > mostWounds then
43-
detachLimb = limb;
44-
mostWounds = limb.WoundCount;
45-
end
46-
end
47-
if detachLimb then
48-
detachLimb.JointStrength = -1;
49-
end
50-
end
17+
if self.alternativeGib then
18+
HumanFunctions.DoAlternativeGib(self);
5119
end
52-
53-
-- Equip a weapon automatically if the one held is destroyed
54-
if self.autoEquip == true then
55-
if self:IsPlayerControlled() and self.EquippedItem == nil and self.InventorySize > 0 then
56-
self:EquipFirearm(true);
57-
end
20+
if self.automaticEquip then
21+
HumanFunctions.DoAutomaticEquip(self);
5822
end
59-
60-
-- Flail around if moving mouse too fast
61-
if self.ctrl:IsMouseControlled() then
62-
63-
local mouseVec = Vector(self.ctrl.MouseMovement.X, self.ctrl.MouseMovement.Y):SetMagnitude(math.sqrt(self.ctrl.MouseMovement.Magnitude));
64-
local ang = self.lastAngle - self:GetAimAngle(false);
65-
66-
self.AngularVel = self.AngularVel - (2 * ang * self.FlipFactor + mouseVec.Y * self.FlipFactor / 10) / math.sqrt(math.abs(self.AngularVel) + 1);
67-
end
68-
69-
-- Shove when unarmed
70-
if not (self.EquippedItem or self.EquippedBGItem) and self.ctrl:IsState(Controller.WEAPON_FIRE) and self.Status < 1 and (self.FGArm or self.BGArm) then
71-
if self.shoved ~= true then
72-
self.AngularVel = self.AngularVel / 3 + (self:GetAimAngle(false) - 1.57) * 3 * self.FlipFactor / (1 + math.abs(self.RotAngle));
73-
self.Vel = self.Vel + Vector(3 / (1 + self.Vel.Magnitude), 0):RadRotate(self:GetAimAngle(true)) * math.abs(math.cos(self:GetAimAngle(true)));
74-
self.shoved = true;
75-
end
76-
else
77-
self.shoved = false;
23+
if self.armSway then
24+
HumanFunctions.DoArmSway(self, 10); -- Argument: shove strength
7825
end
79-
80-
-- Controlling the arm movements
81-
local arms = {{self.FGArm, self.FGLeg, self.BGLeg}, {self.BGArm, self.BGLeg, self.FGLeg}};
82-
for i = 1, #arms do
83-
local arm = arms[i][1];
84-
if arm then
85-
local arm = ToArm(arms[i][1]);
86-
local armLength = ToMOSprite(arm):GetSpriteWidth();
87-
local rotAng = self.RotAngle - (1.57 * self.FlipFactor);
88-
local legMain = arms[i][2];
89-
local legAlt = arms[i][3];
90-
91-
if legMain and not (self.ctrl:IsState(Controller.MOVE_LEFT) or self.ctrl:IsState(Controller.MOVE_RIGHT)) then
92-
rotAng = legMain.RotAngle;
93-
elseif legAlt then
94-
rotAng = legAlt.RotAngle;
95-
end
96-
97-
-- Flail arms in tandem with leg movement
98-
ToArm(arm).IdleOffset = Vector(0, armLength * 0.7):RadRotate(rotAng * self.FlipFactor + 1.5 + (i / 5));
99-
100-
if self.shoved == true or (self.EquippedItem and IsTDExplosive(self.EquippedItem) and self.ctrl:IsState(Controller.WEAPON_FIRE)) then
101-
arm.IdleOffset = Vector(armLength, 0):RadRotate(self:GetAimAngle(false));
102-
local dist = SceneMan:ShortestDistance(self.lastHandPos[i], arm.HandPos, SceneMan.SceneWrapsX);
103-
104-
local dots = math.sqrt(arm.Radius) * (arm.Frame / arm.FrameCount);
105-
106-
for i = 1, dots do
107-
local part = CreateMOPixel("Smack Particle Light");
108-
part.Pos = arm.HandPos;
109-
part.Vel = (self.Vel + dist):SetMagnitude(math.sqrt(dist.Magnitude * self.pushStrength + math.abs(self.AngularVel) * self.Vel.Magnitude)):RadRotate(0.8 / dots * i);
110-
part.Mass = (arm.Mass + arm.Material.StructuralIntegrity) * self.pushStrength; part.Sharpness = math.random() * 0.1;
111-
part.Team = self.Team; part.IgnoresTeamHits = true;
112-
MovableMan:AddParticle(part);
113-
end
114-
end
115-
self.lastHandPos[i] = arm.HandPos;
116-
end
117-
end
118-
119-
-- Visualize inventory with primitive bitmaps
12026
if self.visibleInventory then
121-
if self.Status < 3 and self:IsInventoryEmpty() == false then
122-
local heldCount = 0;
123-
local thrownCount = 0;
124-
for i = 1, self.InventorySize do
125-
local item = self:Inventory();
126-
if item then
127-
local fixNum = self.HFlipped and -1 or 0; -- Fix offsets slightly when facing left
128-
if item.ClassName == "TDExplosive" then
129-
thrownCount = thrownCount + 1;
130-
elseif item.ClassName == "HDFirearm" or item.ClassName == "HeldDevice" then
131-
heldCount = heldCount + 1;
132-
133-
local isFirearm = item.ClassName == "HeldDevice" and 0 or 1;
134-
135-
local sRad = math.sqrt(self.Radius);
136-
local iRad = math.sqrt(item.Radius);
137-
local iMass = math.sqrt(math.abs(item.Mass));
138-
139-
fixNum = fixNum + item.Radius * 0.2 + math.sqrt(heldCount);
140-
141-
-- Bigger actors carry weapons higher up
142-
-- Smaller weapons are carried lower down
143-
local pos = self.Pos + Vector((-sRad - fixNum) * self.FlipFactor, -sRad - iMass + 1 + isFirearm * 3):RadRotate(self.RotAngle);
144-
145-
local itemCount = math.sqrt(math.abs(self.InventorySize - thrownCount));
146-
147-
-- Display tall objects upright
148-
local tallAng = 1.57;
149-
if ToMOSprite(item):GetSpriteWidth() < ToMOSprite(item):GetSpriteHeight() then
150-
tallAng = 0;
151-
end
152-
153-
local rotAng = self.RotAngle + tallAng + (heldCount * self.tilt - itemCount * self.tilt + isFirearm / iMass) / itemCount * self.FlipFactor;
154-
155-
for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
156-
local screen = ActivityMan:GetActivity():ScreenOfPlayer(self.ctrl.Player);
157-
if not SceneMan:IsUnseen(pos.X, pos.Y, ActivityMan:GetActivity():GetTeamOfPlayer(player)) then
158-
FrameMan:DrawBitmapPrimitive(screen, pos, item, rotAng, 0);
159-
end
160-
end
161-
end
162-
self:SwapNextInventory(item, true);
163-
end
164-
end
165-
end
27+
HumanFunctions.DoVisibleInventory(self, false); -- Argument: whether to show all items
16628
end
167-
self.lastAngle = self:GetAimAngle(false);
16829
end
169-
17030
function UpdateAI(self)
17131
self.AI:Update(self);
17232
end
173-
17433
function Destroy(self)
17534
self.AI:Destroy(self);
176-
end
35+
end

Base.rte/AI/HumanBehaviors.lua

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ function HumanBehaviors.GetShootingSkill()
3535
end
3636

3737
-- spot targets by casting a ray in a random direction
38-
function HumanBehaviors.LookForTargets(AI, Owner, Skill) -- Skill is obsolete here
39-
local viewAngDeg = RangeRand(35, 85)
38+
function HumanBehaviors.LookForTargets(AI, Owner, Skill)
39+
local viewAngDeg = RangeRand(50, 120) * (0.5 + Skill / 200)
4040
if AI.deviceState == AHuman.AIMING then
41-
viewAngDeg = 20
41+
AI.Ctrl:SetState(Controller.AIM_SHARP, true) -- reinforce sharp aim controller state to enable SharpLength in LookForMOs
42+
viewAngDeg = 15 + (Skill / 10)
4243
end
4344

4445
local FoundMO = Owner:LookForMOs(viewAngDeg, rte.grassID, false)
@@ -52,7 +53,7 @@ function HumanBehaviors.LookForTargets(AI, Owner, Skill) -- Skill is obsolete he
5253
end
5354
end
5455

55-
-- brains spot targets by casting rays at all nearby enemy actors
56+
-- brains and snipers spot targets by casting rays at all nearby enemy actors
5657
function HumanBehaviors.CheckEnemyLOS(AI, Owner, Skill)
5758
if not AI.Enemies then -- add all enemy actors on our screen to a table and check LOS to them, one per frame
5859
AI.Enemies = {}
@@ -273,15 +274,14 @@ end
273274

274275
-- deprecated since B30. make sure we equip our preferred device if we have one. return true if we must run this function again to be sure
275276
function HumanBehaviors.EquipPreferredWeapon(AI, Owner)
276-
if AI.squadshoot == false then
277-
if AI.PlayerPreferredHD then
278-
Owner:EquipNamedDevice(AI.PlayerPreferredHD, true)
279-
elseif not Owner:EquipDeviceInGroup("Weapons - Primary", true) then
280-
Owner:EquipDeviceInGroup("Weapons - Secondary", true)
281-
end
282-
return false
277+
if AI.squadShoot == false then
278+
if AI.PlayerPreferredHD then
279+
Owner:EquipNamedDevice(AI.PlayerPreferredHD, true)
280+
elseif not Owner:EquipDeviceInGroup("Weapons - Primary", true) then
281+
Owner:EquipDeviceInGroup("Weapons - Secondary", true)
282+
end
283+
return false
283284
end
284-
285285
end
286286

287287
-- deprecated since B30. make sure we equip a primary weapon if we have one. return true if we must run this function again to be sure
@@ -1666,7 +1666,7 @@ function HumanBehaviors.GoToWpt(AI, Owner, Abort)
16661666

16671667
if Waypoint then -- move towards the waypoint
16681668
-- control horizontal movement
1669-
--if Owner.FGLeg or Owner.BGLeg then -- test
1669+
if Owner.FGLeg or Owner.BGLeg then
16701670
if not AI.flying then
16711671
if CurrDist.X < -3 then
16721672
nextLatMove = Actor.LAT_LEFT
@@ -1675,14 +1675,14 @@ function HumanBehaviors.GoToWpt(AI, Owner, Abort)
16751675
else
16761676
nextLatMove = Actor.LAT_STILL
16771677
end
1678-
if not Owner.FGLeg and not Owner.BGLeg then
1678+
if not (Owner.FGLeg and Owner.BGLeg) and not AI.jump then
16791679
Owner:GetController():SetState(Controller.BODY_CROUCH,true) -- crawl if no legs
16801680
end
16811681
end
1682-
--elseif ((CurrDist.X < -5 and Owner.HFlipped) or (CurrDist.X > 5 and not Owner.HFlipped)) and math.abs(Owner.Vel.X) < 1 then
1683-
-- -- no legs, jump forward
1684-
-- AI.jump = true
1685-
--end
1682+
elseif ((CurrDist.X < -5 and Owner.HFlipped) or (CurrDist.X > 5 and not Owner.HFlipped)) and math.abs(Owner.Vel.X) < 1 then
1683+
-- no legs, jump forward
1684+
AI.jump = true
1685+
end
16861686

16871687
if Waypoint.Type == "right" then
16881688
if CurrDist.X > -3 then
@@ -2007,7 +2007,7 @@ function HumanBehaviors.GoToWpt(AI, Owner, Abort)
20072007
if (AI.Target and AI.BehaviorName ~= "AttackTarget") or
20082008
(Owner.AIMode ~= Actor.AIMODE_SQUAD and (AI.BehaviorName == "ShootArea" or AI.BehaviorName == "FaceAlarm"))
20092009
then
2010-
if AI.behaviorType == 1 then -- aggressive (4zK)
2010+
if Owner.aggressive then -- the aggressive behavior setting makes the AI pursue waypoint at all times
20112011
AI.lateralMoveState = nextLatMove
20122012
else
20132013
AI.lateralMoveState = Actor.LAT_STILL
@@ -2431,7 +2431,7 @@ function HumanBehaviors.ShootTarget(AI, Owner, Abort)
24312431
end
24322432

24332433
if AI.canHitTarget then
2434-
if AI.behaviorType ~= 1 then -- non-aggressive
2434+
if not Owner.aggressive then
24352435
AI.lateralMoveState = Actor.LAT_STILL
24362436
end
24372437
if not AI.flying then
@@ -2818,36 +2818,33 @@ function HumanBehaviors.AttackTarget(AI, Owner, Abort)
28182818
if not AI.Target or not MovableMan:ValidMO(AI.Target) then
28192819
break
28202820
end
2821-
-- Use following sequence to attack either with a suited melee weapon or arms
2822-
local meleeDist = 0;
2823-
local startPos = Vector(Owner.EyePos.X, Owner.EyePos.Y);
2821+
-- use following sequence to attack either with a suited melee weapon or arms
2822+
local meleeDist = 0
2823+
local startPos = Vector(Owner.EyePos.X, Owner.EyePos.Y)
28242824

28252825
if Owner.EquippedItem then
2826-
if Owner.EquippedItem:HasObjectInGroup("Tools - Diggers")
2827-
or Owner.EquippedItem:HasObjectInGroup("Weapons - Melee") then
2828-
2829-
meleeDist = Owner.Radius + 25;
2830-
startPos = Vector(Owner.EquippedItem.Pos.X, Owner.EquippedItem.Pos.Y);
2831-
end
2832-
--[[ disabled
2833-
elseif Owner.FGArm then
2834-
meleeDist = Owner.Radius + Owner.FGArm.Radius;
2835-
startPos = Owner.FGArm.Pos;
2836-
elseif Owner.BGArm then
2837-
meleeDist = Owner.Radius + Owner.BGArm.Radius;
2838-
startPos = Owner.BGArm.Pos;
2839-
]]--
2826+
if Owner.EquippedItem:HasObjectInGroup("Tools - Diggers") or Owner.EquippedItem:HasObjectInGroup("Weapons - Melee") then
2827+
meleeDist = Owner.Radius + 25
2828+
startPos = Vector(Owner.EquippedItem.Pos.X, Owner.EquippedItem.Pos.Y)
2829+
end
2830+
elseif Owner.armSway then
2831+
if Owner.FGArm then
2832+
meleeDist = Owner.Radius + Owner.FGArm.Radius
2833+
startPos = Owner.FGArm.Pos
2834+
elseif Owner.BGArm then
2835+
meleeDist = Owner.Radius + Owner.BGArm.Radius
2836+
startPos = Owner.BGArm.Pos
2837+
end
28402838
end
28412839
if meleeDist > 0 then
2842-
2843-
local Dist = SceneMan:ShortestDistance(startPos, AI.Target.Pos, false)
2844-
if Dist.Magnitude < meleeDist then
2845-
AI.Ctrl.AnalogAim = SceneMan:ShortestDistance(Owner.EyePos, AI.Target.Pos, false).Normalized;
2840+
local dist = SceneMan:ShortestDistance(startPos, AI.Target.Pos, false)
2841+
if dist.Magnitude < meleeDist then
2842+
AI.Ctrl.AnalogAim = SceneMan:ShortestDistance(Owner.EyePos, AI.Target.Pos, false).Normalized
28462843
AI.fire = true
28472844
else
28482845
AI.fire = false
28492846
end
2850-
else--if not Owner:EquipDiggingTool(true) then
2847+
else
28512848
break
28522849
-- else TODO: periodically look for weapons?
28532850
end
@@ -3100,7 +3097,7 @@ function HumanBehaviors.FaceAlarm(AI, Owner, Abort)
31003097
AI.AlarmPos = nil
31013098
for _ = 1, math.ceil(200/TimerMan.DeltaTimeMS) do
31023099
AI.deviceState = AHuman.AIMING
3103-
if AI.behaviorType ~= 1 then -- non-aggressive
3100+
if not Owner.aggressive then
31043101
AI.lateralMoveState = Actor.LAT_STILL
31053102
end
31063103
AI.Ctrl.AnalogAim = AlarmDist.Normalized

0 commit comments

Comments
 (0)