Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

インフェルノMODのパラメータをconfファイルで設定できるようにした #48

Merged
merged 11 commits into from
Jun 19, 2016
3 changes: 1 addition & 2 deletions Inferno/ChaosMode/ChaosMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ protected override void Setup()
//敵対関係のグループを作成
chaosRelationShipId = World.AddRelationshipGroup("Inferno:ChaosPeds");


var chaosSettingLoader = new ChaosModeSettingLoader();
chaosModeSetting = chaosSettingLoader.LoadSettingFile(@"./scripts/chaosmode/default.conf");
chaosModeSetting = chaosSettingLoader.LoadSettingFile(@"ChaosMode_Default.conf");

chaosChecker = new CharacterChaosChecker(chaosModeSetting.DefaultMissionCharacterTreatment,
chaosModeSetting.IsChangeMissionCharacterWeapon);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ChaosModeSettingDTO
public string[] WeaponList { get; set; } = new[] { "" };
public string[] WeaponListForDriveBy { get; set; } = new[] { "" };
public bool IsStupidShooting { get; set; } = true;
public int ShootAccuracy { get; set; } = 20;
public int ShootAccuracy { get; set; } = 30;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

難易度が下がりすぎていたのでついでに命中頻度を上げておく

public int WeaponChangeProbabillity { get; set; } = 30;
}
}
92 changes: 7 additions & 85 deletions Inferno/ChaosMode/ChaosSettingLoader/ChaosModeSettingLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,34 @@
using System.IO;
using System.Linq;
using System.Text;
using Inferno.Utilities;
using Newtonsoft.Json;

namespace Inferno.ChaosMode
{
/// <summary>
/// カオスモード用設定ファイルのローダー
/// </summary>
public class ChaosModeSettingLoader
public class ChaosModeSettingLoader : InfernoConfigLoader<ChaosModeSettingDTO>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChaosModeSettingLoader は内部的にInfernoConfigLoaderを使う

{
private readonly Encoding _encoding = Encoding.GetEncoding("Shift_JIS");
private DebugLogger _debugLogger;

protected virtual DebugLogger ChaosModeDebugLogger
{
get
{
if (_debugLogger != null) return _debugLogger;
_debugLogger = new DebugLogger(@"ChaosMod.log");
return _debugLogger;
}
}

/// <summary>
/// ファイルから読み込んで設定ファイルを生成する
/// </summary>
/// <param name="filePath">設定ファイルパス</param>
/// <param name="fileName">設定ファイルパス</param>
/// <returns>設定ファイル</returns>
public ChaosModeSetting LoadSettingFile(string filePath)
public new ChaosModeSetting LoadSettingFile(string fileName)
{
//ファイルロード
var readJson = ReadFile(filePath);
try
{
var dto = JsonConvert.DeserializeObject<ChaosModeSettingDTO>(readJson);
return new ChaosModeSetting(dto);
}
catch (Exception e)
{
ChaosModeDebugLogger.Log(e.Message);
ChaosModeDebugLogger.Log(e.StackTrace);
//例外発生時はデフォルトの設定ファイルを返す
return new ChaosModeSetting(new ChaosModeSettingDTO());
}
return new ChaosModeSetting(base.LoadSettingFile(fileName));
}

/// <summary>
/// ファイルから中身のstringを読み取る
/// </summary>
/// <param name="filePath">ファイルパス</param>
/// <returns>結果</returns>
protected virtual string ReadFile(string filePath)
protected override ChaosModeSettingDTO CreateDefault()
{
if (!File.Exists(filePath))
{
//存在しないならデフォルト設定ファイルを生成する
CreateDefaultSettingFile(filePath);
return "";
}
var readString = "";
try
{
using (var sr = new StreamReader(filePath, _encoding))
{
readString = sr.ReadToEnd();
}
}
catch (Exception e)
{
ChaosModeDebugLogger.Log(e.Message);
ChaosModeDebugLogger.Log(e.StackTrace);
}
return readString;
}

/// <summary>
/// デフォルトの設定ファイルを生成する
/// </summary>
protected void CreateDefaultSettingFile(string filePath)
{
var directoryPath = Path.GetDirectoryName(filePath);
//存在しないならディレクトリを作る
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

//デフォルト設定を吐き出す
var dto = new ChaosModeSettingDTO();
var chaosModeWeapons = new ChaosModeWeapons();
dto.WeaponList = chaosModeWeapons.ExcludeClosedWeapons.Select(x => x.ToString()).ToArray();
dto.WeaponListForDriveBy = chaosModeWeapons.DriveByWeapons.Select(x => x.ToString()).ToArray();

try
{
using (var w = new StreamWriter(filePath, false, _encoding))
{
var json = JsonConvert.SerializeObject(dto, Formatting.Indented);
w.WriteAsync(json);
w.Flush();
}
}
catch (Exception e)
{
ChaosModeDebugLogger.Log(e.Message);
ChaosModeDebugLogger.Log(e.StackTrace);
}
return dto;
}
}
}
2 changes: 2 additions & 0 deletions Inferno/Inferno.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@
<Compile Include="UniRx\System\Unit.cs" />
<Compile Include="UniRx\TimeInterval.cs" />
<Compile Include="UniRx\Timestamped.cs" />
<Compile Include="Utilities\InfernoConfig.cs" />
<Compile Include="Utilities\InfernoConfigLoader.cs" />
<Compile Include="Utilities\InfernoUtilities.cs" />
<Compile Include="Utilities\RequestDataPackage.cs" />
<Compile Include="Utilities\SingleThreadSynchronizationContext.cs" />
Expand Down
21 changes: 19 additions & 2 deletions Inferno/InfernoScripts/Citizen/CitizenNitro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,38 @@
using System.Collections.Generic;
using System.Linq;
using Inferno.ChaosMode;
using Inferno.Utilities;
using UniRx;

namespace Inferno
{

/// <summary>
/// 市ニトロ
/// </summary>
public class CitizenNitro : InfernoScript
{
class CitizenNitroConfig : InfernoConfig
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conf(Jsonファイル)に書き出したいパラメータを定義したConfigクラスを作る場合、InfernoConfigを継承させる

{
public int Probability { get; set; } = 7;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで指定するデフォルト値は、Jsonが存在しなかった時に書き込まれるデフォルトの値となる


public override bool Validate()
{
return Probability > 0 && Probability <= 100;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

値が正しいかどうかのチェック(バリデーション)をここに定義する
falseを返した場合は読み込まれたConfファイルを無視してConfのデフォルト設定値を利用する

}
}

protected override string ConfigFileName { get; } = "CitizenNitro.conf";
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confファイル名はここで指定

private CitizenNitroConfig config;
private int Probability => config?.Probability ?? 7;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで指定しているデフォルト値はConfigのLoadを忘れていた時にnullで落ちるのを防止するため。
逆にnullで落ちてくれたほうが安全かどうかは議論したい。


private readonly string Keyword = "cnitro";
private readonly int probability = 7;
private readonly int[] _velocities = { -100, -70, -50, 50, 70, 100 };

protected override void Setup()
{
config = LoadConfig<CitizenNitroConfig>();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setupの最初で指定したConfigをロードする


//キーワードが入力されたらON/OFFを切り替える
CreateInputKeywordAsObservable(Keyword)
.Subscribe(_ =>
Expand Down Expand Up @@ -50,7 +67,7 @@ private void CitizenNitroAction()

foreach (var veh in nitroAvailableVeles)
{
if (Random.Next(0, 100) <= probability)
if (Random.Next(0, 100) <= Probability)
{
StartCoroutine(DelayCoroutine(veh));
}
Expand Down
19 changes: 17 additions & 2 deletions Inferno/InfernoScripts/Citizen/CitizenVehicleBomb.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
using System;
using System.Linq;
using Inferno.Utilities;
using UniRx;

namespace Inferno
{

/// <summary>
/// 爆雷
/// </summary>
internal class CitizenVehicleBomb : InfernoScript
{
private float probability = 10;
class CitizenVehicleBombConfig : InfernoConfig
{
public int Probability { get; set; } = 10;

public override bool Validate()
{
return Probability > 0 && Probability <= 100;
}
}

protected override string ConfigFileName { get; } = "CitizenVehicleBomb.conf";
private CitizenVehicleBombConfig config;
private int Probability => config?.Probability ?? 10;

protected override void Setup()
{
config = LoadConfig<CitizenVehicleBombConfig>();
CreateInputKeywordAsObservable("vbomb")
.Subscribe(_ =>
{
Expand All @@ -37,7 +52,7 @@ private void VehicleBombAction()

foreach (var vehicle in targetVehicles)
{
if (Random.Next(0, 100) <= probability)
if (Random.Next(0, 100) <= Probability)
{
vehicle.PetrolTankHealth = -1;
}
Expand Down
22 changes: 21 additions & 1 deletion Inferno/InfernoScripts/Citizen/SpawnParachuteCitizenArmy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,37 @@
using Inferno.ChaosMode;
using System;
using System.Collections.Generic;
using Inferno.Utilities;
using UniRx;

namespace Inferno
{

/// <summary>
/// 市民を生成してパラシュート降下させる
/// </summary>
internal class SpawnParachuteCitizenArmy : InfernoScript
{
class SpawnParachuteCitizenArmyConfig : InfernoConfig
{
/// <summary>
/// 生成間隔
/// </summary>
public int SpawnDurationSeconds { get; set; } = 5;

public override bool Validate()
{
return SpawnDurationSeconds > 0;
}
}

protected override string ConfigFileName { get; } = "SpawnParachuteCitizenArmy.conf";
private SpawnParachuteCitizenArmyConfig config;
private int SpawnDurationSeconds => config?.SpawnDurationSeconds ?? 5;

protected override void Setup()
{
config = LoadConfig<SpawnParachuteCitizenArmyConfig>();
CreateInputKeywordAsObservable("carmy")
.Subscribe(_ =>
{
Expand All @@ -23,7 +43,7 @@ protected override void Setup()

OnAllOnCommandObservable.Subscribe(_ => IsActive = true);

CreateTickAsObservable(5000)
CreateTickAsObservable(SpawnDurationSeconds * 1000)
.Where(_ => IsActive)
.Subscribe(_ => CreateParachutePed());
}
Expand Down
21 changes: 19 additions & 2 deletions Inferno/InfernoScripts/InfernoCore/InfernoScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ public abstract class InfernoScript : Script

private readonly ReactiveProperty<bool> _isActiveReactiveProperty = new ReactiveProperty<bool>(false);

protected virtual string ConfigFileName => null;

/// <summary>
/// 設定ファイルをロードする
/// </summary>
protected T LoadConfig<T>() where T : InfernoConfig, new()
{
if (string.IsNullOrEmpty(ConfigFileName))
{
throw new Exception("設定ファイル名が設定されていません");
}
var loader = new InfernoConfigLoader<T>();
var dto = loader.LoadSettingFile(ConfigFileName);
//バリデーションに引っかかったらデフォルト値を返す
return dto.Validate() ? dto : new T();
}

/// <summary>
/// スクリプトが動作中であるか
/// </summary>
Expand Down Expand Up @@ -149,7 +166,7 @@ protected UniRx.IObservable<Unit> CreateTickAsObservable(int millsecond)
/// <summary>
/// ゲーム中断時に自動開放する対象リスト
/// </summary>
private List<Entity> _autoReleaseEntities = new List<Entity>();
private List<Entity> _autoReleaseEntities = new List<Entity>();

/// <summary>
/// ゲーム中断時に自動開放する
Expand All @@ -170,7 +187,7 @@ protected void AutoReleaseOnGameEnd(Entity entity)
protected override void Dispose(bool A_0)
{
IsActive = false;
foreach (var e in _autoReleaseEntities.Where(x=>x.IsSafeExist()))
foreach (var e in _autoReleaseEntities.Where(x => x.IsSafeExist()))
{
e.MarkAsNoLongerNeeded();
}
Expand Down
23 changes: 22 additions & 1 deletion Inferno/InfernoScripts/Player/BondCar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@
using GTA;
using GTA.Math;
using GTA.Native;
using Inferno.Utilities;
using UniRx;

namespace Inferno.InfernoScripts.Player
{
class BondCar : InfernoScript
{
#region config
class BondCarConfig : InfernoConfig
{
/// <summary>
/// ミサイルの発射間隔[ms]
/// </summary>
public int CoolDownMillSeconds { get; set; } = 500;

public override bool Validate()
{
return CoolDownMillSeconds >= 0;
}
}

protected override string ConfigFileName { get; } = "BondCar.conf";
private BondCarConfig config;
private int CoolDownMillSeconds => config?.CoolDownMillSeconds ?? 500;

#endregion

protected override void Setup()
{
config = LoadConfig<BondCarConfig>();

OnTickAsObservable
.Where(_ =>
Expand All @@ -23,7 +44,7 @@ protected override void Setup()
&& this.IsGamePadPressed(GameKey.VehicleAttack)
&& PlayerPed.Weapons.Current.Hash == WeaponHash.Unarmed
)
.ThrottleFirst(TimeSpan.FromMilliseconds(500))
.ThrottleFirst(TimeSpan.FromMilliseconds(CoolDownMillSeconds))
.Subscribe(_ =>
{
var v = PlayerVehicle.Value;
Expand Down
Loading