以下は、GTA Vのスクリプトモッド(SHVDN3ベース)で、F9キーを押すとプレイヤー周囲にボディーガードが出現し、フォーメーションを維持しながら移動する機能を実装するC#コードです。
using System;
using System.Collections.Generic;
using System.Linq;
using GTA;
using GTA.Math;
using GTA.Native;
public class BodyguardFormation : Script
{
private List<Ped> bodyguards = new List<Ped>();
private bool isFormationActive = false;
private Vector3 formationOffset = new Vector3(0, 0, 0);
private float formationRadius = 2.5f;
private float updateInterval = 100; // 100ms
private int maxBodyguards = 4;
public BodyguardFormation()
{
Tick += OnTick;
KeyDown += OnKeyDown;
Interval = updateInterval;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F9)
{
ToggleFormation();
}
}
private void ToggleFormation()
{
if (!isFormationActive)
{
ActivateFormation();
}
else
{
DeactivateFormation();
}
isFormationActive = !isFormationActive;
}
private void ActivateFormation()
{
if (bodyguards.Count == 0)
{
CreateBodyguards();
}
else
{
foreach (var guard in bodyguards)
{
Function.Call(Hash.SET_PED_KEEP_TASK, guard, true);
}
}
}
private void DeactivateFormation()
{
foreach (var guard in bodyguards)
{
Function.Call(Hash.TASK_STAND_STILL, guard, -1);
}
}
private void CreateBodyguards()
{
bodyguards.Clear();
Vector3 playerPos = Game.Player.Character.Position;
Vector3 forward = Game.Player.Character.GetOffsetInWorldCoords(Vector3.RelativeFront);
// 4方向の位置計算
Vector3[] positions = new Vector3[]
{
CalculatePosition(forward, 45), // 右前
CalculatePosition(forward, 135), // 左前
CalculatePosition(forward, 225), // 左後
CalculatePosition(forward, 315) // 右後
};
for (int i = 0; i < maxBodyguards; i++)
{
Ped guard = World.CreatePed(PedHash.Bodyguard, positions[i]);
Function.Call(Hash.SET_PED_COMBAT_ATTRIBUTES, guard, 46, true); // 攻撃的
Function.Call(Hash.SET_PED_COMBAT_ABILITY, guard, 100);
Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, guard, Game.Player.Character.Handle);
Function.Call(Hash.SET_PED_NEVER_LEAVES_GROUP, guard, true);
Function.Call(Hash.SET_PED_DIES_IN_WATER, guard, false);
Function.Call(Hash.SET_PED_DIES_IN_SINKING_VEHICLE, guard, false);
Function.Call(Hash.SET_PED_SUFFERS_CRITICAL_HITS, guard, false);
Function.Call(Hash.SET_PED_CONFIG_FLAG, guard, 184, true); // 無敵モード
bodyguards.Add(guard);
}
}
private Vector3 CalculatePosition(Vector3 forward, float angle)
{
Vector3 offset = Vector3.Normalize(forward) * formationRadius;
offset = Vector3.Rotate(offset, angle, Vector3.WorldUp);
return Game.Player.Character.Position + offset;
}
private void UpdateFormation()
{
if (!isFormationActive || bodyguards.Count == 0) return;
Vector3 playerPos = Game.Player.Character.Position;
Vector3 forward = Game.Player.Character.GetOffsetInWorldCoords(Vector3.RelativeFront);
for (int i = 0; i < bodyguards.Count; i++)
{
Vector3 targetPos = CalculatePosition(forward, i * 90);
Ped guard = bodyguards[i];
// スムーズな移動
Function.Call(Hash.TASK_GO_TO_COORD_ANY_MEANS, guard,
targetPos.X, targetPos.Y, targetPos.Z,
2.0f, 0, false, 786603, 0xbf800000);
// プレイヤーの方を向く
Function.Call(Hash.TASK_LOOK_AT_COORD, guard,
playerPos.X, playerPos.Y, playerPos.Z,
-1, 0, 0);
}
}
private void OnTick(object sender, EventArgs e)
{
if (isFormationActive)
{
UpdateFormation();
}
}
}
主な機能と特徴:
-
F9キー操作:
- 押すたびにフォーメーションの有効/無効を切り替え
- 初回起動時にボディーガードを生成
-
フォーメーションシステム:
- プレイヤーを中心に半径2.5mの円状に配置
- 4方向(前左右後)に最大4体のボディーガード
- プレイヤーの進行方向に合わせて自動調整
-
ボディーガード設定:
- 強力な戦闘能力(SET_PED_COMBAT_ABILITY)
- グループ行動(SET_PED_AS_GROUP_MEMBER)
- 特殊能力(水死/車両沈没無効、クリティカルヒット無効)
-
動的追従システム:
- プレイヤー移動時にスムーズにフォーメーションを維持
- TASK_GO_TO_COORD_ANY_MEANSで地形を考慮した移動
- 常にプレイヤー方向を向くよう設定
改善可能なポイント:
- フォーメーション形状のカスタマイズ(円形/方形など)
- ボディーガード数の動的変更
- 武器や服装のカスタマイズ
- 敵対勢力検知時の自動戦闘モード
- 車両搭乗時の対応
SHVDNプロジェクトに追加する際は、以下の点に注意してください:
- 必要なネイティブ関数(Hashクラス)を正しく参照
- ペドのモデル(PedHash.Bodyguard)を好みのものに変更可能
- 戦闘パラメータを調整して難易度を制御
- スクリプトのロード順序を確認(他のMODとの競合防止)
このスクリプトは基本的なフォーメーションシステムを提供しますが、実際のゲームプレイでは以下のような拡張が可能です:
- フォーメーション形状の変更(三角形/六芒星など)
- 敵対勢力検知時の自動防御陣形
- ボディーガードのレベルや装備のカスタマイズ
- 車両搭乗時の自動フォーメーション調整
- プレイヤーが戦闘状態になった際の特別行動パターン