Refactored code so the weapon temperature is correctly updated when checking it

This commit is contained in:
Nicolás Badano 2015-04-01 14:29:22 -03:00 committed by PabstMirror
parent ac67e45098
commit 4264789631
5 changed files with 85 additions and 46 deletions

View File

@ -4,12 +4,12 @@ ADDON = false;
PREP(checkTemperature);
PREP(clearJam);
PREP(cooldown);
PREP(displayTemperature);
PREP(firedEH);
PREP(jamWeapon);
PREP(overheat);
PREP(swapBarrel);
PREP(swapBarrelCallback);
PREP(updateTemperature);
ADDON = true;

View File

@ -30,4 +30,4 @@ if (_action == "") then {
_player playActionNow _action;
// Launch a PFH that waits a sec before displaying the temperature
[FUNC(displayTemperature), [_player, _weapon], 1.0, 0] call EFUNC(common,waitAndExecute);
[FUNC(displayTemperature), [_player, _weapon], 1.0, 0] call EFUNC(common,waitAndExecute);

View File

@ -15,26 +15,10 @@
EXPLODE_2_PVT(_this,_player,_weapon);
private ["_temperature", "_scaledTemperature", "_color", "_count", "_string", "_text", "_picture"];
// Calculate cool down of weapon since last shot
private ["_string", "_overheat", "_temperature", "_time", "_barrelMass", "_a"];
_string = format [QGVAR(%1), _weapon];
_overheat = _player getVariable [_string, [0, 0]];
_temperature = _overheat select 0;
_time = _overheat select 1;
// Get physical parameters
_barrelMass = 0.50 * (getNumber (configFile >> "CfgWeapons" >> _weapon >> "WeaponSlotsInfo" >> "mass") / 22.0) max 1.0;
// Calculate cooling
_temperature = [_temperature, _barrelMass, ACE_time - _time] call FUNC(cooldown);
// Store new temperature
_time = ACE_time;
_player setVariable [_string, [_temperature, _time], false];
private ["_scaledTemperature", "_action", "_color", "_count", "_string", "_text", "_picture"];
_temperature = [_player, _weapon, 0] call FUNC(updateTemperature)
_scaledTemperature = (_temperature / 1000) min 1;
_color = [
@ -43,7 +27,7 @@ _color = [
00
];
_count = round (12 * _scaledTemperature);
_count = 2 + round (10 * _scaledTemperature);
_string = "";
for "_a" from 1 to _count do {
_string = _string + "|";
@ -57,7 +41,7 @@ for "_a" from (_count + 1) to 12 do {
_text = composeText [
_text,
[_string, [0.5, 0.5, 0.5]] call EFUNC(common,stringToColoredText)
[_string, [0.5, 0.5, 0.5]] call EFUNC(common,stringToColoredTex)t
];
_picture = getText (configFile >> "CfgWeapons" >> _weapon >> "picture");

View File

@ -23,17 +23,7 @@ _weapon = _this select 1;
_ammo = _this select 4;
_projectile = _this select 6;
_velocity = velocity _projectile;
private ["_variableName", "_overheat", "_temperature", "_time", "_energyIncrement", "_barrelMass"];
// each weapon has it's own variable. Can't store the temperature in the weapon since they are not objects unfortunately.
_variableName = format [QGVAR(%1), _weapon];
// get old values
_overheat = _unit getVariable [_variableName, [0, 0]];
_temperature = _overheat select 0;
_time = _overheat select 1;
private ["_bulletMass","_energyIncrement"];
// Get physical parameters
_bulletMass = getNumber (configFile >> "CfgAmmo" >> _ammo >> "ACE_BulletMass");
@ -41,16 +31,6 @@ if (_bulletMass == 0) then {
// If the bullet mass is not configured, estimate it
_bulletMass = 3.4334 + 0.5171 * (getNumber (configFile >> "CfgAmmo" >> _ammo >> "hit") + getNumber (configFile >> "CfgAmmo" >> _ammo >> "caliber"));
};
_energyIncrement = 0.75 * 0.0005 * _bulletMass * (vectorMagnitudeSqr _velocity);
_barrelMass = 0.50 * (getNumber (configFile >> "CfgWeapons" >> _weapon >> "WeaponSlotsInfo" >> "mass") / 22.0) max 1.0;
_energyIncrement = 0.75 * 0.0005 * _bulletMass * (vectorMagnitudeSqr velocity _projectile);
// Calculate cooling
_temperature = [_temperature, _barrelMass, time - _time] call FUNC(cooldown);
// Calculate heating
_temperature = _temperature + _energyIncrement / (_barrelMass * 466); // Steel Heat Capacity = 466 J/(Kg.K)
// set updated values
_time = time;
// Publish the variable
[_unit, _variableName, [_temperature, _time], 2.0] call EFUNC(common,setVariablePublic);
[_unit, _weapon, _energyIncrement] call FUNC(updateTemperature)

View File

@ -0,0 +1,75 @@
/*
* Author: esteldunedain
* Update temperature of a weapon.
*
* Argument:
* 0: Unit <OBJECT>
* 1: Weapon <STRING>
* 2: Heat increment (J) <NUMBER>
*
* Return value:
* Current temperature <NUMBER>
*
* Public: No
*/
#include "\z\ace\addons\overheating\script_component.hpp"
EXPLODE_3_PVT(_this,_unit,_weapon,_heatIncrement);
private ["_variableName", "_oldHeat", "_temperature", "_time", "_barrelMass"];
// each weapon has it's own variable. Can't store the temperature in the weapon since they are not objects unfortunately.
_variableName = format [QGVAR(%1), _weapon];
// get old values
_oldHeat = _unit getVariable [_variableName, [0, 0]];
_temperature = _oldHeat select 0;
_time = _oldHeat select 1;
_barrelMass = 0.50 * (getNumber (configFile >> "CfgWeapons" >> _weapon >> "WeaponSlotsInfo" >> "mass") / 22.0) max 1.0;
_fnc_cooling = {
EXPLODE_3_PVT(_this,_temperature,_barrelMass,_totalTime);
// If a long time passed since the last shot, there's no need to calculate anything; the weapon should be cool
if (_totalTime > 1800) exitWith {0};
private ["_barrelSurface", "_time", "_deltaTime"];
_barrelSurface = _barrelMass / 7850 / 0.003;
_time = 0;
while {true} do {
_deltaTime = (_totalTime - _time) min 20;
_temperature = _temperature - (
// Convective cooling
25 * _barrelSurface * _temperature
// Radiative cooling
+ 0.4 * 5.67e-8 * _barrelSurface *
( (_temperature + 273.15)*(_temperature + 273.15)
* (_temperature + 273.15)*(_temperature + 273.15)
- 273.15 * 273.15 * 273.15 *273.15 )
) * _deltaTime / (_barrelMass * 466);
if (_temperature < 1) exitWith {0};
if (isNil "_temperature") exitWith {
diag_log text format ["[ACE] ERROR: _totalTime = %1; _time = %2; _deltaTime = %3;", _totalTime, _time, _deltaTime];
0
};
_time = _time + _deltaTime;
if (_time >= _totalTime) exitWith { _temperature max 0 };
};
};
// Calculate cooling
_temperature = [_temperature, _barrelMass, time - _time] call _fnc_cooldown;
// Calculate heating
// Steel Heat Capacity = 466 J/(Kg.K)
_temperature = _temperature + _heatIncrement / (_barrelMass * 466);
// Publish the variable
[_unit, _variableName, [_temperature, time], 2.0] call EFUNC(common,setVariablePublic);
_temperature