-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMAPracticeComponentExamples.cpp
More file actions
322 lines (288 loc) · 11.6 KB
/
Copy pathMAPracticeComponentExamples.cpp
File metadata and controls
322 lines (288 loc) · 11.6 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
A small selection of functionality from code handling practice mode.
This mode allows players to record movement paths, play them back, spawn bots,
set up and play practice drills with a variety of victory conditions, and more.
*/
//Called from practice menu blueprint widget, stores all practice mode data to a json file to persist it.
void UMAPracticeComponent::SaveAllPracticeDataToFile()
{
if (!IsPracticeModeCommandEnabled())
{
return;
}
FMAMapPracticeData MapPracticeDataToSave;
//Currently we just allow a single map per file, could improve this later to allow multiple maps.
MapPracticeDataToSave.MapName = GetWorld()->GetMapName();
MapPracticeDataToSave.RouteTrails = RouteTrails;
MapPracticeDataToSave.Drills = Drills;
MapPracticeDataToSave.Bots = MapPracticeData.Bots;
MapPracticeDataToSave.Locations = MapPracticeData.Locations;
MapPracticeDataToSave.Author = ParentController->PlayerState->GetPlayerName();
MapPracticeDataToSave.Tutorials = MapPracticeData.Tutorials;
FString JSONPracticeData = "";
FJsonObjectConverter::UStructToJsonObjectString(MapPracticeDataToSave, JSONPracticeData);
/**
* Upload practice data to the API
*/
//UMAGameGlobals::Get().ServicesAPI.CreatePracticeMap(MapPracticeDataToSave, [this](bool bSuccess, FMAMapPracticeData NewPracticeMap)
//{
// return bSuccess;
//});
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
if (DesktopPlatform)
{
void* ParentWindowHandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle();
FString SaveDirectory = FPaths::Combine(FPaths::GameContentDir(), TEXT("/Practice"));
FString FileTypes;
FString DefaultFileName = FString("MidairPracticeData-").Append(MapPracticeDataToSave.MapName).Append(FString(".txt"));
TArray<FString> OutFileNames;
DesktopPlatform->SaveFileDialog(ParentWindowHandle, FString("Save Practice Data File"), SaveDirectory, DefaultFileName, FileTypes, 0, OutFileNames);
if (OutFileNames.Num() > 0)
{
FString FileName = OutFileNames[0];
FString TextToSave = JSONPracticeData;
bool AllowOverwriting = false;
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
// CreateDirectoryTree returns true if the destination
// directory existed prior to call or has been created
// during the call.
if (PlatformFile.CreateDirectoryTree(*SaveDirectory))
{
// Get absolute file path
FString AbsoluteFilePath = SaveDirectory + "/" + FileName;
FFileHelper::SaveStringToFile(TextToSave, *AbsoluteFilePath);
}
}
}
}
void UMAPracticeComponent::StartSelectedDrillOrTutorial(bool bIsTutorial)
{
if (!IsPracticeModeCommandEnabled() || SelectedDrill.Name.Equals("") || SelectedDrill.Name.IsEmpty())
{
return;
}
DrillResultMessage = "";
DrillKillCounter = 0;
DrillMidairCounter = 0;
bIsActiveSpeedDrill = SelectedDrill.VictoryType == EDrillVictoryType::MovementSpeed;
if (!SelectedDrill.LeaveOldBots)
{
KillAllBots();
}
//delete any previously spawned victory locations.
if (IsValid(SpawnedDrillVictoryLocation) && SpawnedDrillVictoryLocation->IsPendingKillPending() == false && SpawnedDrillVictoryLocation->IsPendingKill() == false) {
SpawnedDrillVictoryLocation->Destroy();
SpawnedDrillVictoryLocation = nullptr;
}
//teleport player to drill/tutorial start location, if one is configured
FPlayerLocationAndState PlayerSpawnLocation = SelectedDrill.InitialPlayerNamedLocation.LocationAndState;
if (FMath::Abs(PlayerSpawnLocation.Location.X) > KINDA_SMALL_NUMBER || FMath::Abs(PlayerSpawnLocation.Location.Z) > KINDA_SMALL_NUMBER)
{
if (AMAPlayerState* PS = Cast<AMAPlayerState>(ParentController->PlayerState))
{
if (SelectedDrill.InitialPlayerNamedLocation.LocationTeam != PS->GetTeamId())
{
bool bRotationallyMirrored = IsCurrentMapRotationallyMirrored();
FPlayerLocationAndState MirroredLocation = SwapPlayerLocationAndStateTeam(PlayerSpawnLocation, bRotationallyMirrored);
LoadPosition(MirroredLocation, true);
}
else {
LoadPosition(PlayerSpawnLocation, true);
}
}
}
if (SelectedDrill.DrillLength > 0.0f)
{
ParentController->GetWorldTimerManager().SetTimer(TimerHandle_DrillLength, this, &UMAPracticeComponent::EndCurrentDrillByTimeout, SelectedDrill.DrillLength, true, SelectedDrill.DrillLength);
}
else {
ParentController->GetWorldTimerManager().SetTimer(TimerHandle_DrillLength, this, &UMAPracticeComponent::EndCurrentDrillByTimeout, 9999.0f, true, 9999.0f);
}
if (SelectedDrill.ResetFlagsOnStart)
{
ResetFlags();
}
//First, choose which routes of the loaded routes we are going to run bots on
TArray<int> RoutesToRun;
int BotsToSpawn = SelectedDrill.NumberOfBots;
//drill struct contains just bot names, so pull down the full bot object and fetch the routes they know
TArray<FMABotConfig> BotsForDrill;
TSet<FString> RoutesBotsKnow;
for (FMABotConfig BotConfig : MapPracticeData.Bots)
{
if (SelectedDrill.BotNames.Contains(BotConfig.Name))
{
BotsForDrill.Add(BotConfig);
for (FString RouteTrailName : BotConfig.RouteTrailNames)
{
RoutesBotsKnow.Add(RouteTrailName);
}
}
}
if (SelectedDrill.BotsSpawnOnDifferentRoutes && BotsToSpawn > RoutesBotsKnow.Num())
{
BotsToSpawn = RoutesBotsKnow.Num();
}
if (!SelectedDrill.CanRepeatBots && SelectedDrill.NumberOfBots > SelectedDrill.BotNames.Num())
{
BotsToSpawn = FMath::Min(BotsToSpawn, SelectedDrill.BotNames.Num());
}
//add random bots to the drill from those allowable, preventing duplicates if that flag is set in the drill setup
TArray<FMABotConfig> BotsToSpawnForDrill;
for (int i = 0; i < BotsToSpawn; i++) {
int BotIndex = FMath::RandRange(0, BotsForDrill.Num() - 1);
BotsToSpawnForDrill.Add(BotsForDrill[BotIndex]);
if (!SelectedDrill.CanRepeatBots)
{
BotsForDrill.RemoveAt(BotIndex);
}
}
//if there is an end location marked, spawn it
if (!SelectedDrill.VictoryLocation.Name.IsEmpty())
{
FMANamedLocation VictoryLoc = SelectedDrill.VictoryLocation;
if (AMAPlayerState* PS = Cast<AMAPlayerState>(ParentController->PlayerState))
{
//If we are on the opposite team of the location, mirror the position around the origin to get the corresponding location for the other team
if (PS->GetTeamId() != VictoryLoc.LocationTeam) {
VictoryLoc.LocationAndState = SwapPlayerLocationAndStateTeam(VictoryLoc.LocationAndState, IsCurrentMapRotationallyMirrored());
}
}
ADrillVictoryLocation* VictoryLocation = GetControlledCharacter()->GetWorld()->SpawnActor<ADrillVictoryLocation>(DrillVictoryLocationBluePrintClass,
VictoryLoc.LocationAndState.Location, VictoryLoc.LocationAndState.Rotation);
VictoryLocation->LocationAndState = VictoryLoc.LocationAndState;
VictoryLocation->SetSize(SelectedDrill.VictoryLocationRadius, SelectedDrill.VictoryLocationHalfHeight);
SpawnedDrillVictoryLocation = VictoryLocation;
}
//Then, start any routes we can start immediately:
for (FMABotConfig Bot : BotsToSpawnForDrill)
{
ServerSpawnBot(Bot);
}
}
void UMAPracticeComponent::EndCurrentDrillByTimeout()
{
bool bDrillWon = false;
//on drill end time being hit, we could still win a NoFlagCarrier type drill, since that is the point of the drill -- no carrier by timeout
if (SelectedDrill.VictoryType == EDrillVictoryType::NoFlagCarrier)
{
bDrillWon = true;
//For NoFlagCarrier, we just loop through the flag actors and see if they are being held by a bot. If they are, we lose. Otherwise, win.
for (TActorIterator<AMACTFFlag> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
// Same as with the Object Iterator, access the subclass instance with the * or -> operators.
AMACTFFlag *Flag = *ActorItr;
if (Flag->Holder != nullptr)
{
if (AMAPlayerState* AIPS = Cast<AMAPlayerState>(Flag->Holder->PlayerState))
{
AMAPlayerState* PS = Cast<AMAPlayerState>(ParentController->PlayerState);
if (AIPS->GetTeamId() != PS->GetTeamId())
{
EndCurrentDrill(false);
return;
}
}
}
}
}
EndCurrentDrill(bDrillWon);
}
void UMAPracticeComponent::EndCurrentDrill(bool bDrillWon)
{
ParentController->GetWorldTimerManager().ClearTimer(TimerHandle_DrillLength);
if (!SelectedDrill.LeaveOldBots)
{
KillAllBots();
}
bIsActiveSpeedDrill = false;
if (IsValid(SpawnedDrillVictoryLocation) && SpawnedDrillVictoryLocation->IsPendingKillPending() == false && SpawnedDrillVictoryLocation->IsPendingKill() == false) {
SpawnedDrillVictoryLocation->Destroy();
SpawnedDrillVictoryLocation = nullptr;
}
if (bDrillWon)
{
DrillResultMessage = TEXT("Drill Completed!");
if (!bIsDrillRunningAsTutorial && !bIsDrillRunningAsWatcher)
{
DrillVictories++;
}
}
else
{
if (!bIsDrillRunningAsTutorial && !bIsDrillRunningAsWatcher)
{
DrillLosses++;
}
if (DrillResultMessage.IsEmpty())
{
switch (SelectedDrill.VictoryType)
{
case EDrillVictoryType::HitShot:
DrillResultMessage = TEXT("Drill Failed! You need to damage a bot.");
break;
case EDrillVictoryType::Location:
DrillResultMessage = TEXT("Drill Failed! You need to reach the end location.");
break;
case EDrillVictoryType::MovementSpeed:
DrillResultMessage = TEXT("Drill Failed! You need to reach at least ");
DrillResultMessage.Append(FString::FromInt(SelectedDrill.DrillVictoryAmount)).Append("kph.");
break;
case EDrillVictoryType::FlagCaught:
DrillResultMessage = TEXT("Drill Failed! You need to catch the flag in the air.");
break;
case EDrillVictoryType::NoFlagCarrier:
DrillResultMessage = TEXT("Drill Failed! Enemy team has the flag.");
break;
case EDrillVictoryType::TotalKills:
DrillResultMessage = TEXT("Drill Failed! You needed to kill ");
DrillResultMessage.Append(FString::FromInt(SelectedDrill.DrillVictoryAmount)).Append(TEXT(" bots, but "));
if (DrillKillCounter == 0)
{
DrillResultMessage.Append(TEXT("you didn't kill any!"));
}
else {
DrillResultMessage.Append(TEXT("only killed ")).Append(FString::FromInt(DrillKillCounter)).Append(".");
}
break;
case EDrillVictoryType::TotalMidairs:
DrillResultMessage = TEXT("Drill Failed! You needed to hit ");
DrillResultMessage.Append(FString::FromInt(SelectedDrill.DrillVictoryAmount)).Append(TEXT(" midair shots, but "));
if (DrillMidairCounter == 0)
{
DrillResultMessage.Append(TEXT("you didn't hit any!"));
}
else {
DrillResultMessage.Append(TEXT("only hit ")).Append(FString::FromInt(DrillMidairCounter)).Append(".");
}
break;
}
}
}
if (AMAPlayerController* PC = Cast<AMAPlayerController>(ParentController))
{
if (bIsDrillRunningAsTutorial)
{
if (bDrillWon)
{
DrillResultMessage = "Tutorial Step Completed!";
}
else {
DrillResultMessage = "Tutorial Step Failed. Try Again?";
}
PC->ClientSay_Implementation(nullptr, DrillResultMessage, false);
}
else if (bIsDrillRunningAsWatcher)
{
DrillResultMessage = "Now you try!";
PC->ClientSay_Implementation(nullptr, DrillResultMessage, false);
}
else {
PC->ClientSay_Implementation(nullptr, DrillResultMessage, false);
FString DrillResults = TEXT("Overall results: ");
DrillResults.Append(FString::FromInt(DrillVictories)).Append(TEXT("/")).Append(FString::FromInt(DrillLosses + DrillVictories));
PC->ClientSay_Implementation(nullptr, DrillResults, false);
}
}
ParentController->GetWorldTimerManager().SetTimer(TimerHandle_DrillMessageClear, this, &UMAPracticeComponent::ClearDrillResultMessage, 5.0f, true, 5.0f);
}