-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDecoration.cpp
226 lines (196 loc) · 11.4 KB
/
Decoration.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "stdafx.h"
#include "Decoration.h"
#include "Skill.h"
#include "Solution.h"
using namespace System;
void Decoration::Load( System::String^ filename )
{
static_decoration_map.Clear();
static_decoration_ability_map.Clear();
static_decorations.Clear();
static_decorations.Capacity = 100;
IO::StreamReader fin( filename );
while( !fin.EndOfStream )
{
String^ line = fin.ReadLine();
if( line == L"" || line[ 0 ] == L'#' )
continue;
List_t< String^ > split;
Utility::SplitString( %split, line, L',' );
Decoration^ decoration = gcnew Decoration;
decoration->is_event = false;
decoration->num_owned = 0;
//Name,Slot Level,Rarity,Acquire,Skill
decoration->name = split[ 0 ];
decoration->rarity = Convert::ToUInt32( split[ 2 ] );
decoration->slot_level = Convert::ToUInt32( split[ 1 ] );
decoration->hr = Convert::ToUInt32( split[ 3 ] );
decoration->difficulty = 0;
decoration->num_owned = 0;
decoration->can_use = false;
Ability^ ability = Ability::FindAbility( split[ 4 ] );
Assert( ability, L"Cannot find decoration ability: " + split[4] );
decoration->abilities.Add( gcnew AbilityPair( ability, 1 ) );
Assert( !ability->decoration, L"Two decorations with the same ability" );
ability->decoration = decoration;
decoration->index = static_decorations.Count;
static_decorations.Add( decoration );
if( !static_decoration_ability_map.ContainsKey( decoration->abilities[ 0 ]->ability ) )
static_decoration_ability_map.Add( decoration->abilities[ 0 ]->ability, gcnew List_t< Decoration^ > );
static_decoration_ability_map[ decoration->abilities[ 0 ]->ability ]->Add( decoration );
static_decoration_map.Add( decoration->name, decoration );
}
static_decorations.TrimExcess();
}
int Decoration::GetSkillAt( Ability^ ability )
{
for( int i = 0; i < abilities.Count; ++i )
if( abilities[ i ]->ability == ability )
return abilities[ i ]->amount;
return 0;
}
bool Decoration::IsBetterThan( Decoration^ other, List_t< Ability^ >^ rel_abilities )
{
if( slot_level < other->slot_level || abilities[ 0 ]->ability != other->abilities[ 0 ]->ability )
return true;
return abilities[ 0 ]->amount > other->abilities[ 0 ]->amount;
}
Decoration^ Decoration::GetBestDecoration( Ability^ ability, const unsigned max_slot_level, const unsigned hr )
{
if( !static_decoration_ability_map.ContainsKey( ability ) )
return nullptr; //no decorations exist for this skill
Decoration^ best = nullptr;
List_t< Ability^ > rel;
rel.Add( ability );
for each( Decoration^ dec in static_decoration_ability_map[ ability ] )
{
if( dec->hr > hr || dec->slot_level > max_slot_level )
continue;
for each( AbilityPair^ ap in dec->abilities )
{
if( ap->amount > 0 && ap->ability == ability )
{
if( !best || dec->IsBetterThan( best, %rel ) )
best = dec;
}
}
}
return best;
}
bool Decoration::MatchesQuery( Query^ query )
{
//check requirements
if( query->my_decos && this->num_owned > 0 )
{
can_use = true;
return true;
}
if( this->hr > query->hr )
{
can_use = false;
return false;
}
can_use = true;
//check for relevant skills
for each( Skill^ skill in query->skills )
{
if( skill->ability == abilities[ 0 ]->ability )
return true;
}
return false;
}
Decoration^ Decoration::FindDecoration( System::String^ name )
{
if( static_decoration_map.ContainsKey( name ) )
return static_decoration_map[ name ];
return nullptr;
}
Decoration^ Decoration::FindDecorationFromString( System::String^ line )
{
//Expert Jewel
Ability^ best_ability = nullptr;
for each( Ability^ ability in Ability::static_abilities )
{
if( line->IndexOf( ability->name ) > -1 )
{
if( !best_ability || best_ability->name->Length < ability->name->Length )
best_ability = ability;
}
}
if( !best_ability )
return nullptr;
for each( Decoration^ dec in static_decorations )
{
if( dec->abilities[ 0 ]->ability == best_ability )
return dec;
}
return nullptr;
}
Decoration^ Decoration::GetBestDecoration( Ability^ ability, const unsigned max_slots, List_t< List_t< Decoration^ >^ >% rel_deco_map )
{
for( int i = max_slots + 1; i --> 0; )
{
for each( Decoration^ deco in rel_deco_map[ i ] )
{
if( deco->abilities[ 0 ]->ability == ability )
return deco;
}
}
return nullptr;
}
void Decoration::LoadLanguage( System::String^ filename )
{
static_decoration_map.Clear();
IO::StreamReader fin( filename );
for( int i = 0; i < static_decorations.Count; )
{
String^ line = fin.ReadLine();
if( line == L"" || line[ 0 ] == L'#' )
continue;
static_decorations[ i ]->name = line;
static_decoration_map.Add( line, static_decorations[ i ] );
i++;
}
}
#define CUSTOM_LIST L"Data/mydecorations.txt"
void Decoration::LoadCustom()
{
if( IO::File::Exists( CUSTOM_LIST ) )
LoadCustom( CUSTOM_LIST );
}
void Decoration::LoadCustom( String^ filename )
{
IO::StreamReader fin( filename );
do
{
String^ line = fin.ReadLine();
if( line == "" || line->StartsWith( L"#" ) )
continue;
int comma = line->IndexOf( L',' );
if( comma < 0 )
{
Decoration^ d = FindDecoration( line->Trim() );
if( d )
d->num_owned++;
}
else
{
Decoration^ d = FindDecoration( line->Substring( 0, comma )->Trim() );
if( d )
d->num_owned += Convert::ToInt32( line->Substring( comma + 1 ) );
}
}
while( !fin.EndOfStream );
}
void Decoration::SaveCustom()
{
IO::StreamWriter fout( CUSTOM_LIST );
fout.WriteLine( L"#Name,Number" );
for each( Decoration^ d in static_decorations )
{
if( d->num_owned > 0 )
{
fout.WriteLine( d->name + L"," + d->num_owned );
}
}
}