14#include <unordered_map>
22std::unordered_map<std::string, IWeaponLogic*>& logicRegistry() {
23 static std::unordered_map<std::string, IWeaponLogic*>
registry;
32void pushEvent(
const WeaponEvent& e) {
33 if (eventSink()) eventSink()(e);
37float angleDelta(
float from,
float to) {
38 float d = std::fmod(
to -
from + 180.f, 360.f);
39 if (
d < 0.f)
d += 360.f;
44class HitscanLogic :
public IWeaponLogic {
46 const char*
name()
const override {
return "hitscan"; }
47 bool canFire(
const WeaponEntity&)
const override {
return true; }
48 void fire(WeaponEntity&,
const AttackRequest&)
override {}
49 void update(WeaponEntity&,
float)
override {}
53class ProjectileLogic :
public IWeaponLogic {
55 const char*
name()
const override {
return "projectile"; }
56 bool canFire(
const WeaponEntity&)
const override {
return true; }
57 void fire(WeaponEntity&
w,
const AttackRequest& req)
override {
58 if (IProjectileService* svc = eve::cap::query<IProjectileService>()) svc->spawnProjectile(
w, req);
60 void update(WeaponEntity&,
float)
override {}
63const WeaponDefinition* defOf(WeaponEntity&
w);
64void pushFireEvent(WeaponEntity&
w,
const WeaponDefinition* def,
const AttackRequest& req,
int ammo);
67class MeleeLogic :
public IWeaponLogic {
69 const char*
name()
const override {
return "melee"; }
70 bool canFire(
const WeaponEntity&)
const override {
return true; }
71 void fire(WeaponEntity&,
const AttackRequest&)
override {}
72 void update(WeaponEntity&,
float)
override {}
76ProjectileLogic gProjectile;
79const WeaponDefinition* defOf(WeaponEntity&
w) {
return w.definition()->def; }
82bool usesStageMachine(
const WeaponDefinition* def) {
86void pushFireEvent(WeaponEntity&
w,
const WeaponDefinition* def,
const AttackRequest& req,
int ammo) {
89 e.weaponId =
w.identity()->id;
96 e.aoe = req.aoeRadius;
97 e.spread = req.spread;
98 e.pellets = std::max(1, req.pelletCount);
99 e.recoilPitch = def->recoilPitch;
100 e.recoilYaw = def->recoilYaw;
101 e.damageType = def->damageType;
102 e.element = def->element;
107void updateFeel(WeaponEntity&
w,
const WeaponDefinition* def,
float dt) {
110 if (def->spreadRecover > 0.f &&
state->currentSpread > def->spreadMin) {
111 state->currentSpread = std::max(def->spreadMin,
state->currentSpread - def->spreadRecover *
dt);
114 if (def->recoilRecover > 0.f) {
115 const float p = std::min(std::fabs(
state->recoilPitch), def->recoilRecover *
dt);
116 state->recoilPitch +=
state->recoilPitch > 0.f ? -
p :
p;
117 const float y = std::min(std::fabs(
state->recoilYaw), def->recoilRecover *
dt);
123float applyBloom(WeaponEntity&
w,
const WeaponDefinition* def) {
125 if (def->spreadPerShot > 0.f && def->spreadMax > def->spreadMin) {
126 state->currentSpread = std::min(def->spreadMax,
state->currentSpread + def->spreadPerShot);
128 return state->currentSpread;
132int spawnPellets(WeaponEntity&
w,
const WeaponDefinition* def, AttackRequest&
r) {
133 const int n = std::max(1, def->projectile.pelletCount);
136 for (
int i = 0; i <
n; ++i) {
144void pushEmptyEvent(WeaponEntity&
w,
const WeaponDefinition* def) {
147 e.weaponId =
w.identity()->id;
153void pushReloadEnd(WeaponEntity&
w,
const WeaponDefinition* def,
int ammo) {
156 e.weaponId =
w.identity()->id;
166bool canFireRanged(WeaponEntity&
w,
const WeaponDefinition* def) {
167 const auto state =
w.state();
168 if (
state->resource.reloading ||
state->jammed)
return false;
169 if (
state->cooldown > 0.f)
return false;
170 if (
state->resource.value <= 0.f)
return false;
172 return logic ==
nullptr || logic->canFire(
w);
175bool tryFireRanged(WeaponEntity&
w,
const AttackRequest& req,
const WeaponDefinition* def) {
176 if (!canFireRanged(
w, def))
return false;
179 state->resource.value -= 1.f;
181 const float effSpread = applyBloom(
w, def);
182 state->recoilPitch += def->recoilPitch;
183 state->recoilYaw += def->recoilYaw;
185 AttackRequest
r = req;
186 r.yaw =
w.aim()->yaw;
187 r.pitch =
w.aim()->pitch;
188 r.spread = effSpread;
189 const int pellets = spawnPellets(
w, def,
r);
191 state->cooldown = def->cooldown;
193 state->burstRemaining = def->burstSize - 1;
194 state->burstTimer = def->burstInterval;
197 r.pelletCount = pellets;
198 pushFireEvent(
w, def,
r,
static_cast<int>(
state->resource.value));
199 if (
state->resource.value <= 0.f) pushEmptyEvent(
w, def);
203void updateRanged(WeaponEntity&
w,
const WeaponDefinition* def,
float dt) {
207 updateFeel(
w, def,
dt);
210 if (!
r.reloading &&
r.value <= 0.f &&
state->cooldown <= 0.f && !
state->jammed) {
216 r.reloadProgress +=
dt;
217 if (
r.reloadProgress >= def->reloadTime) {
218 const int missing =
static_cast<int>(
r.max) -
static_cast<int>(
r.value);
219 if (def->reserveSize < 0) {
221 }
else if (AmmoPoolEntity* pool =
state->ammoPool) {
222 const int take = std::min(missing, pool->state()->count);
223 r.value +=
static_cast<float>(take);
224 pool->state()->count -= take;
226 const int take = std::min(missing,
r.reserve);
227 r.value +=
static_cast<float>(take);
231 r.reloadProgress = 0.f;
232 pushReloadEnd(
w, def,
static_cast<int>(
r.value));
237 if (!
r.reloading &&
state->burstRemaining > 0) {
239 if (
state->burstTimer <= 0.f) {
243 req.yaw =
w.aim()->yaw;
244 req.pitch =
w.aim()->pitch;
245 req.spread = applyBloom(
w, def);
246 state->recoilPitch += def->recoilPitch;
247 state->recoilYaw += def->recoilYaw;
248 const int pellets = spawnPellets(
w, def, req);
249 state->cooldown = def->cooldown;
250 state->burstRemaining--;
251 state->burstTimer = def->burstInterval;
253 req.pelletCount = pellets;
254 pushFireEvent(
w, def, req,
static_cast<int>(
r.value));
256 if (
r.value <= 0.f) {
257 state->burstRemaining = 0;
258 pushEmptyEvent(
w, def);
261 state->burstRemaining = 0;
274bool canAttackStage(WeaponEntity&
w,
const WeaponDefinition* def) {
275 const auto state =
w.state();
277 if (
state->jammed)
return false;
278 if (
state->cooldown > 0.f)
return false;
285 current.ignore(
"legacy bool canFire cannot expose weapon attribute diagnostics");
288 if (!
r.infinite &&
current.value() <
static_cast<double>(
r.cost))
return false;
289 }
else if (!
r.infinite &&
r.value <
r.cost) {
293 return logic ==
nullptr || logic->canFire(
w);
296bool tryAttackStage(WeaponEntity&
w,
const AttackRequest& req,
const WeaponDefinition* def) {
297 if (!canAttackStage(
w, def))
return false;
304 consumed.ignore(
"legacy bool tryFire cannot expose weapon attribute diagnostics");
307 }
else if (!
r.infinite) {
308 r.value = std::max(0.f,
r.value -
r.cost);
312 state->stageTimer = 0.f;
313 state->lastRequest = req;
318 e.weaponId =
w.identity()->id;
320 e.ammoLeft =
static_cast<int>(
r.value);
325void updateStage(WeaponEntity&
w,
const WeaponDefinition* def,
float dt) {
328 updateFeel(
w, def,
dt);
332 if (
r.regen > 0.f &&
r.value <
r.max)
r.value = std::min(
r.max,
r.value +
r.regen *
dt);
334 const AttackStageSpec* stages =
state->stages !=
nullptr ?
state->stages : &def->stages;
340 state->stageTimer = 0.f;
341 AttackRequest req =
state->lastRequest;
342 req.yaw =
w.aim()->yaw;
343 req.pitch =
w.aim()->pitch;
344 req.spread = applyBloom(
w, def);
345 const int pellets = spawnPellets(
w, def, req);
346 req.pelletCount = pellets;
347 pushFireEvent(
w, def, req,
static_cast<int>(
r.value));
348 if (!
r.infinite &&
r.value <= 0.f) pushEmptyEvent(
w, def);
351 if (
state->stageTimer >= stages->activeTime) {
353 state->stageTimer = 0.f;
357 e.weaponId =
w.identity()->id;
359 e.ammoLeft =
static_cast<int>(
r.value);
364 state->stageTimer = 0.f;
374 if (logic ==
nullptr)
return;
375 logicRegistry()[logic->
name()] = logic;
379 auto it = logicRegistry().find(
name);
380 return it == logicRegistry().
end() ? nullptr : it->second;
391 if (def ==
nullptr)
return;
392 if (usesStageMachine(def)) {
393 updateStage(
w, def,
dt);
395 updateRanged(
w, def,
dt);
401 if (def ==
nullptr)
return false;
402 if (usesStageMachine(def))
return tryAttackStage(
w, req, def);
403 return tryFireRanged(
w, req, def);
408 if (def ==
nullptr)
return false;
409 if (usesStageMachine(def))
return canAttackStage(
w, def);
410 return canFireRanged(
w, def);
415 if (def ==
nullptr)
return;
418 if (
r.reloading ||
r.value >=
r.max)
return;
420 if (pool->state()->count <= 0)
return;
421 }
else if (!
r.infinite && (def->
reserveSize == 0 ||
r.reserve <= 0)) {
426 r.reloadProgress = 0.f;
427 state->burstRemaining = 0;
438 w.state()->resource.reloading =
false;
439 w.state()->resource.reloadProgress = 0.f;
444 if (aim->turnSpeed <= 0.f) {
445 aim->yaw = aim->desiredYaw;
446 aim->pitch = aim->desiredPitch;
449 const float step = aim->turnSpeed *
dt;
450 const float dyaw = angleDelta(aim->yaw, aim->desiredYaw);
451 const float dpitch = aim->desiredPitch - aim->pitch;
452 aim->yaw += (std::fabs(dyaw) <=
step) ? dyaw : (dyaw > 0.f ?
step : -
step);
453 aim->pitch += (std::fabs(dpitch) <=
step) ? dpitch : (dpitch > 0.f ?
step : -
step);
454 return std::fabs(angleDelta(aim->yaw, aim->desiredYaw)) < 0.01f &&
455 std::fabs(aim->pitch - aim->desiredPitch) < 0.01f;
464struct BuiltinLogicRegistrar {
465 BuiltinLogicRegistrar() {
472BuiltinLogicRegistrar gBuiltinRegistrar;
499class ScriptProjectileService final :
public IProjectileService {
501 explicit ScriptProjectileService(ScriptProviderHandle
handle) : handle_(
std::move(
handle)) {}
504 [[nodiscard]]
const ScriptProviderHandle&
handle() const noexcept {
return handle_; }
506 void spawnProjectile(
const WeaponEntity&
w,
const AttackRequest& req)
override {
510 WeaponEntity& mutableWeapon =
const_cast<WeaponEntity&
>(
w);
511 const auto identity = mutableWeapon.identity();
513 const ScriptArg args[] = {
529 if (reportedGeneration_ != handle_.generation) {
530 reportedGeneration_ = handle_.generation;
532 "' failed and no projectile was spawned: " +
spawned.status().describe());
538 ScriptProviderHandle handle_;
540 std::uint64_t reportedGeneration_ = 0;
552struct ScriptProjectileProviders {
553 std::vector<std::unique_ptr<ScriptProjectileService>>
live;
554 std::vector<std::unique_ptr<ScriptProjectileService>>
retired;
557ScriptProjectileProviders& scriptProjectileProviders() {
558 static ScriptProjectileProviders
state;
565 ScriptProjectileProviders&
owned = scriptProjectileProviders();
571 if (eve::cap::query<IProjectileService>() !=
nullptr) {
573 "' replaced the provider that owned IProjectileService");
575 auto adapter = std::make_unique<ScriptProjectileService>(
handle);
578 eve::cap::provide<IProjectileService>(
raw);
583 const auto match = std::find_if(
owned.live.begin(),
owned.live.end(),
584 [&
handle](
const std::unique_ptr<ScriptProjectileService>&
adapter) {
585 return adapter->handle() == handle;
587 if (match ==
owned.live.end()) {
592 eve::cap::revoke<IProjectileService>(match->get());
593 owned.retired.push_back(std::move(*match));
594 owned.live.erase(match);
600 ScriptProjectileProviders&
owned = scriptProjectileProviders();
602 eve::cap::revoke<IProjectileService>(
adapter.get());
610 .
ignore(
"module teardown may already have dropped this seam");
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
Persistent, thread-safe record of engine crash and problem events.
const eve::script::BindingContractRegistry & registry
Selective canonical mana/stamina adapter for weapons.
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
Result< void > removeCapability(std::string_view capability)
Withdraw a declared seam when its owning module goes away.
static ScriptProviderRegistry & instance()
Process-lifetime registry; clearAll is the only reset.
void declareCapability(std::string capability, ScriptCapabilityBinder binder, ScriptCapabilityUnbinder unbinder)
Declare that one native capability may be served by a script provider.
Result< void > call(const ScriptProviderHandle &handle, std::string_view method) const
Call method() on a published provider.
共享弹药池实体:多把武器共用的备用弹药,装填时从池中取。
static constexpr const char * capabilityName
武器逻辑接口:注册到 WeaponSystem 的可插拔行为。
virtual void end(WeaponEntity &w, const AttackRequest &req)
阶段机进入 Recover 时调用(默认空实现)。
virtual const char * name() const =0
稳定名字("hitscan" / "projectile" / "melee" / 自定义名)。
static eve::Result< double > read(WeaponEntity &weapon, std::string_view attribute)
Read a selected final mana/stamina attribute.
static constexpr std::string_view manaAttribute
static constexpr std::string_view staminaAttribute
static eve::Result< void > consumeTriggerResource(WeaponEntity &weapon)
Consume the current weapon's canonical Mana/Stamina trigger cost.
武器实体:数据全部在组件里,行为在 WeaponSystem。
static void update(WeaponEntity &w, float dt)
每帧推进:冷却 / 连发 / 装填 / 炮口旋转 / 逻辑自身。
static IWeaponLogic * findLogic(const std::string &name)
按名字取逻辑;未注册返回 nullptr。
static bool canFire(WeaponEntity &w)
是否可开火(未装填/未卡壳/冷却完毕/有弹药/逻辑放行)。
static void registerLogic(IWeaponLogic *logic)
注册武器逻辑实现;同名替换。
static void setEventSink(WeaponEventSink sink)
注册事件汇(替换旧的;传 nullptr 清空)。
static void cancelReload(WeaponEntity &w)
打断装填。
static bool tryFire(WeaponEntity &w, const FireRequest &req)
尝试开火;满足条件时扣弹药、调逻辑、推事件并返回 true。
static void startReload(WeaponEntity &w)
开始装填(有备用弹药且弹匣不满时生效)。
static void emitEvent(const WeaponEvent &e)
向事件汇推一条事件(模块/脚本便捷入口)。
static bool updateAim(WeaponEntity &w, float dt)
炮口朝目标旋转;返回本帧是否到位。
static int logicCount()
已注册逻辑数量。
载具模块入口(eve.Vehicle):定义注册 / 实体工厂 / RTS 命令 / 帧调度。 设计文档:docs/dev/通用载具系统设计.md
std::function< void(const WeaponEvent &)> WeaponEventSink
事件汇:由 Weapon 模块(或测试)注册,把事件写入自己的队列。
void declareScriptProjectileSeam()
声明 IProjectileService 的脚本绑定接缝。
void withdrawScriptProjectileSeam()
撤回本模块声明的脚本接缝,并卸载全部脚本提供方。
void recordLogEvent(const std::string &level, const std::string &message)
Appends a timestamped event line to the crash/error log.
static ScriptArg fromInteger(std::int64_t value)
static ScriptArg fromText(std::string value)
static ScriptArg fromBoolean(bool value)
static ScriptArg fromNumber(double value)
Generation-qualified identity of one published script provider.
一次攻击请求(C++ 系统 / 脚本发起)。角度均为度。 旧名 FireRequest 保留为别名,向后兼容。
武器模板(registerWeaponsFromJson 注册,进程级注册表)。
武器事件(帧缓存,脚本轮询,与 building/rpg 风格一致)。