forked from EnsageSharp/Ensage.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitDatabase.cs
212 lines (186 loc) · 6.87 KB
/
UnitDatabase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// <copyright file="UnitDatabase.cs" company="EnsageSharp">
// Copyright (c) 2015 EnsageSharp.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
// </copyright>
namespace Ensage.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ensage.Common.Properties;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/// <summary>
/// The unit database.
/// </summary>
public static class UnitDatabase
{
#region Static Fields
/// <summary>
/// The units.
/// </summary>
public static List<AttackAnimationData> Units = new List<AttackAnimationData>();
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes static members of the <see cref="UnitDatabase" /> class.
/// </summary>
static UnitDatabase()
{
JsonConvert.DeserializeObject<AttackAnimationData>(
JObject.Parse(Encoding.Default.GetString(Resources.UnitDatabase)).ToString());
}
#endregion
#region Public Methods and Operators
/// <summary>
/// Gets the attack backswing.
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="double" />.
/// </returns>
public static double GetAttackBackswing(Unit unit)
{
var attackRate = GetAttackRate(unit);
var attackPoint = GetAttackPoint(unit);
return attackRate - attackPoint;
}
/// <summary>
/// Gets the attack point.
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="double" />.
/// </returns>
public static double GetAttackPoint(Unit unit)
{
if (unit == null)
{
return 0;
}
var classId = unit.ClassID;
var name = unit.Name;
var data = GetByClassId(classId) ?? GetByName(name);
if (data == null)
{
return 0;
}
var attackSpeed = GetAttackSpeed(unit);
return (data.AttackPoint / (1 + ((attackSpeed - 100) / 100)))
- (((Game.Ping / 1000) / (1 + (1 - (1 / UnitData.MaxCount)))) * 2)
+ ((1 / UnitData.MaxCount) * 3 * (1 + (1 - (1 / UnitData.MaxCount))));
}
/// <summary>
/// Gets the attack rate.
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="double" />.
/// </returns>
public static double GetAttackRate(Unit unit)
{
var attackSpeed = GetAttackSpeed(unit);
var attackBaseTime = unit.BaseAttackTime;
Ability spell = null;
if (
!unit.Modifiers.Any(
x =>
(x.Name == "modifier_alchemist_chemical_rage" || x.Name == "modifier_terrorblade_metamorphosis"
|| x.Name == "modifier_lone_druid_true_form" || x.Name == "modifier_troll_warlord_berserkers_rage")))
{
return (attackBaseTime / (1 + ((attackSpeed - 100) / 100))) - 0.03;
}
switch (unit.ClassID)
{
case ClassID.CDOTA_Unit_Hero_Alchemist:
spell = unit.Spellbook.Spells.First(x => x.Name == "alchemist_chemical_rage");
break;
case ClassID.CDOTA_Unit_Hero_Terrorblade:
spell = unit.Spellbook.Spells.First(x => x.Name == "terrorblade_metamorphosis");
break;
case ClassID.CDOTA_Unit_Hero_LoneDruid:
spell = unit.Spellbook.Spells.First(x => x.Name == "lone_druid_true_form");
break;
case ClassID.CDOTA_Unit_Hero_TrollWarlord:
spell = unit.Spellbook.Spells.First(x => x.Name == "troll_warlord_berserkers_rage");
break;
}
if (spell != null)
{
var baseAttackTime = spell.AbilityData.FirstOrDefault(x => x.Name == "base_attack_time");
if (baseAttackTime != null)
{
attackBaseTime = baseAttackTime.Value;
}
return (attackBaseTime / (1 + ((attackSpeed - 100) / 100)))
- (((Game.Ping / 1000) / (1 + (1 - (1 / UnitData.MaxCount)))) * 2)
+ ((1 / UnitData.MaxCount) * 3 * (1 + (1 - (1 / UnitData.MaxCount))));
}
return 0d;
}
/// <summary>
/// Gets the attack speed.
/// </summary>
/// <param name="unit">
/// The unit.
/// </param>
/// <returns>
/// The <see cref="float" />.
/// </returns>
public static float GetAttackSpeed(Unit unit)
{
var attackSpeed = Math.Min(unit.Speed, 600);
if (unit.Modifiers.Any(x => (x.Name == "modifier_ursa_overpower")))
{
attackSpeed = 600;
}
return attackSpeed;
}
/// <summary>
/// Gets the attack animation data by class id.
/// </summary>
/// <param name="classId">
/// The class id.
/// </param>
/// <returns>
/// The <see cref="AttackAnimationData" />.
/// </returns>
public static AttackAnimationData GetByClassId(ClassID classId)
{
return Units.FirstOrDefault(unitData => unitData.UnitClassId.Equals(classId));
}
/// <summary>
/// Gets the attack animation data by name.
/// </summary>
/// <param name="unitName">
/// The unit name.
/// </param>
/// <returns>
/// The <see cref="AttackAnimationData" />.
/// </returns>
public static AttackAnimationData GetByName(string unitName)
{
return Units.FirstOrDefault(unitData => unitData.UnitName.ToLower() == unitName);
}
#endregion
}
}