Track temperatures of spare barrels

This commit is contained in:
esteldunedain 2016-02-27 00:29:30 -03:00
parent db71aa54b8
commit e950552345
10 changed files with 167 additions and 58 deletions

View File

@ -0,0 +1,11 @@
class CfgMagazines {
class CA_Magazine;
class ACE_SpareBarrel: CA_Magazine {
descriptionshort = CSTRING(SpareBarrelDescription);
picture = QUOTE(PATHTOF(UI\spare_barrel_ca.paa));
displayName = "ACE Spare Barrel"; //!!!CANNOT be localized!!!: because it is used as part of the magazineDetail string
count = 1;
mass = 40;
ACE_isUnique = 1;
};
};

View File

@ -15,7 +15,7 @@ class CfgVehicles {
};
class ACE_SwapBarrel {
displayName = CSTRING(SwapBarrel);
condition = QUOTE( 'ACE_SpareBarrel' in items _player && {getNumber (configFile >> 'CfgWeapons' >> currentWeapon _player >> 'ACE_Overheating_allowSwapBarrel') == 1} );
condition = QUOTE( 'ACE_SpareBarrel' in magazines _player && {getNumber (configFile >> 'CfgWeapons' >> currentWeapon _player >> 'ACE_Overheating_allowSwapBarrel') == 1} );
statement = QUOTE( [ARR_2(_player, currentWeapon _player)] call FUNC(swapBarrel); );
showDisabled = 0;
priority = 3;
@ -99,7 +99,7 @@ class CfgVehicles {
MACRO_ADDITEM(ACE_SpareBarrel,6);
};
};
/*
// Add ACE_SpareBarrel to every machine gunner.
#define MACRO_ADDSPAREBARREL \
items[] = {"FirstAidKit", "ACE_SpareBarrel"}; \
@ -124,4 +124,5 @@ class CfgVehicles {
// Czech
class I_Soldier_02_F; class I_Soldier_AR_F:I_Soldier_02_F {MACRO_ADDSPAREBARREL};
class I_Soldier_support_base_F; class I_Soldier_AAR_F:I_Soldier_support_base_F {MACRO_ADDSPAREBARREL};
*/
};

View File

@ -2,17 +2,6 @@ class CfgWeapons {
class ACE_ItemCore;
class InventoryItem_Base_F;
class ACE_SpareBarrel: ACE_ItemCore {
displayname = CSTRING(SpareBarrelName);
descriptionshort = CSTRING(SpareBarrelDescription);
//model = "";
picture = QUOTE(PATHTOF(UI\spare_barrel_ca.paa));
scope = 2;
class ItemInfo: InventoryItem_Base_F {
mass = 30;
};
};
class RifleCore;
class Rifle: RifleCore {
//Mean Rounds Between Stoppages (this will be scaled based on the barrel temp)

View File

@ -1,4 +1,5 @@
PREP(calculateCooling);
PREP(canUnjam);
PREP(checkTemperature);
PREP(clearJam);
@ -10,5 +11,6 @@ PREP(jamWeapon);
PREP(overheat);
PREP(swapBarrel);
PREP(swapBarrelCallback);
PREP(updateSpareBarrelsTemperaturesThread);
PREP(updateTemperature);
PREP(updateTemperatureThread);

View File

@ -8,6 +8,46 @@ if (isServer) then {
GVAR(pseudoRandomList) pushBack [-1 + random 2, -1 + random 2];
};
publicVariable QGVAR(pseudoRandomList);
// Keep track of the temperature of stored spare barrels
GVAR(storedSpareBarrels) = [] call CBA_fnc_hashCreate;
["spareBarrelLoadedCoolest", {
params ["_unit", "_weapon", "_weaponTemp", "_barrelMass"];
TRACE_4("spareBarrelLoadedCoolest1",_unit,_weapon,_weaponTemp,_barrelMass);
// Find all spare barrel the player has
private _allMags = magazinesDetail _unit;
TRACE_1("spareBarrelLoadedCoolest2",_allMags);
_allMags = _allMags select {_x find "ACE Spare Barrel" == 0};
TRACE_1("spareBarrelLoadedCoolest3",_allMags);
if ((count _allMags) < 1) exitWith {};
// Determine which on is coolest
private _coolestTemp = 10000;
private _coolestMag = _allMags select 0;
{
private _temp = 0;
if ([GVAR(storedSpareBarrels), _x] call CBA_fnc_hashHasKey) then {
_temp = ([GVAR(storedSpareBarrels), _x] call CBA_fnc_hashGet) select 0;
};
TRACE_2("spareBarrelLoadedCoolest4",_x,_temp);
if (_temp < _coolestTemp) then {
_coolestTemp = _temp;
_coolestMag = _x;
};
} forEach _allMags;
TRACE_3("spareBarrelLoadedCoolest5",_coolestTemp,_coolestMag,_weaponTemp);
// The new weapon temperature is similar to the coolest barrel
// Publish the new temperature value
_unit setVariable [format [QGVAR(%1_temp), _weapon], _coolestTemp, true];
// Heat up the coolest barrel to the former weapon temperature
[GVAR(storedSpareBarrels), _coolestMag, [_weaponTemp, ACE_Time, _barrelMass]] call CBA_fnc_hashSet;
}] call EFUNC(common,addEventHandler);
// Schedule cool down calculation of stored spare barrels
[] call FUNC(updateSpareBarrelsTemperaturesThread);
};

View File

@ -18,6 +18,8 @@ class CfgPatches {
#include "CfgVehicles.hpp"
#include "CfgMagazines.hpp"
#include "CfgWeapons.hpp"
#include "ACE_Settings.hpp"

View File

@ -0,0 +1,58 @@
/*
* Author: esteldunedain
* Calculate the cooling down of a weapon over a time interval.
*
* Argument:
* 0: Initial temperature <NUMBER>
* 1: Barrel mass <NUMBER>
* 2: Time interval <NUMBER>
*
* Return value:
* Final temperature <NUMBER>
*
* Example:
* [_temperature, _barrelMass, _totalTime] call ace_overheating_fnc_calculateCooling
*
* Public: No
*/
#include "script_component.hpp"
params ["_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};
//AR-15 (0.00570m bullet diameter) (barrel diameter usually 0.75" or 0.008255m radius)
//Steel Denisty = 7850 m^3 / kg
//Area of a cylinder (2/r)*(Pi * r^3 + V) - for a 0.008255m radius barrel -> Area = 210(1/meters) * Volume
//Adjusted volume for being hollowed out is ~1.1x
//So Area = 210 * 1.1 * (mass / 7850) = mass * 0.029427 (for steel near that diameter)
private _barrelSurface = _barrelMass * 0.029427;
TRACE_4("cooling",_temperature,_totalTime,_barrelMass,_barrelSurface);
private _time = 0;
while {true} do {
private _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 };
};

View File

@ -14,6 +14,7 @@
*
* Public: No
*/
#define DEBUG_MODE_FULL
#include "script_component.hpp"
params ["_player", "_weapon"];
@ -26,7 +27,14 @@ playSound "ACE_BarrelSwap";
// don't consume the barrel, but rotate through them.
[localize LSTRING(SwappedBarrel), QUOTE(PATHTOF(UI\spare_barrel_ca.paa))] call EFUNC(common,displayTextPicture);
// Publish the temperature variable
_player setVariable [format [QGVAR(%1_temp), _weapon], 0, true];
private _temp = _player getVariable [format [QGVAR(%1_temp), _weapon], 0];
private _barrelMass = 0.50 * (getNumber (configFile >> "CfgWeapons" >> _weapon >> "WeaponSlotsInfo" >> "mass") / 22.0) max 1.0;
// Instruct the server to load the coolest spare barrel into the weapon and
// store the removed barrel with the former weapon temperature. The server
// also updates the current weapon temperature to match that of the new
// loaded barrel.
["spareBarrelLoadedCoolest", [_player, _weapon, _temp, _barrelMass]] call EFUNC(common,serverEvent);
// Store the update time
_player setVariable [format [QGVAR(%1_time), _weapon], ACE_time];

View File

@ -0,0 +1,40 @@
/*
* Author: esteldunedain
* Calculate cooldown of all the stored spare barrels.
*
* Argument:
* None
*
* Return value:
* None
*
* Example:
* [] call ace_overheating_fnc_updateSpareBarrelsTemperaturesThread
*
* Public: No
*/
#define DEBUG_MODE_FULL
#include "script_component.hpp"
private _pairs = [];
TRACE_1("updateSpareBarrelsTemperaturesThread1",GVAR(storedSpareBarrels));
[GVAR(storedSpareBarrels), {_pairs pushBack [_key, _value];}] call CBA_fnc_hashEachPair;
TRACE_1("updateSpareBarrelsTemperaturesThread2",_pairs);
{
_x params ["_barrelMagazineID","_value"];
_value params ["_initialTemp","_initialTime", "_barrelMass"];
// Calculate cooling
private _finalTemp = [_initialTemp, _barrelMass, ACE_time - _initialTime] call FUNC(calculateCooling);
TRACE_4("updateSpareBarrelsTemperaturesThread3",_barrelMagazineID,_initialTemp,_finalTemp,_barrelMass);
if (_finalTemp < 5) then {
// The barrel is cool enough to keep calculating. Remove it from the hash
[GVAR(storedSpareBarrels), _barrelMagazineID] call CBA_fnc_hashRem;
} else {
// Store the new temp
[GVAR(storedSpareBarrels), _barrelMagazineID, [_finalTemp, ACE_time, _barrelMass]] call CBA_fnc_hashSet;
};
} forEach _pairs;
// Schedule for execution again after 10 seconds
[DFUNC(updateSpareBarrelsTemperaturesThread), [], 10] call EFUNC(common,waitAndExecute);

View File

@ -29,50 +29,8 @@ private _lastTime = _unit getVariable [_timeVarName, 0];
private _barrelMass = 0.50 * (getNumber (configFile >> "CfgWeapons" >> _weapon >> "WeaponSlotsInfo" >> "mass") / 22.0) max 1.0;
_fnc_cooling = {
params ["_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};
//AR-15 (0.00570m bullet diameter) (barrel diameter usually 0.75" or 0.008255m radius)
//Steel Denisty = 7850 m^3 / kg
//Area of a cylinder (2/r)*(Pi * r^3 + V) - for a 0.008255m radius barrel -> Area = 210(1/meters) * Volume
//Adjusted volume for being hollowed out is ~1.1x
//So Area = 210 * 1.1 * (mass / 7850) = mass * 0.029427 (for steel near that diameter)
private _barrelSurface = _barrelMass * 0.029427;
TRACE_4("cooling",_temperature,_totalTime,_barrelMass,_barrelSurface);
private _time = 0;
while {true} do {
private _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, ACE_time - _lastTime] call _fnc_cooling;
_temperature = [_temperature, _barrelMass, ACE_time - _lastTime] call FUNC(calculateCooling);
TRACE_1("cooledTo",_temperature);
// Calculate heating
// Steel Heat Capacity = 466 J/(Kg.K)