sfcnr/gamemodes/irresistible/cnr/features/fires.pwn

370 lines
10 KiB
Plaintext
Raw Permalink Normal View History

2018-10-10 08:20:25 +00:00
/*
* Irresistible Gaming (c) 2018
* Developed by Lorenc
2018-10-10 08:20:25 +00:00
* Module: cnr\features\fires.pwn
* Purpose: extinguishable fire system
*/
/* ** Includes ** */
#include < YSI\y_hooks >
/* ** Definitions ** */
#define MAX_FIRES ( 10 )
2019-06-17 13:19:41 +00:00
#define FIRE_EXTINGUISH_PAYOUT ( 5000 )
2018-10-10 08:20:25 +00:00
/* ** Variables ** */
enum E_FIRE_DATA
{
E_OBJECT, Float: E_HEALTH, E_HOUSE,
Text3D: E_LABEL, E_MAP_ICON
2018-10-10 08:20:25 +00:00
};
static stock
g_fireData [ MAX_FIRES ] [ E_FIRE_DATA ],
Iterator: fires < MAX_FIRES >,
p_FireTracker [ MAX_PLAYERS char ]
2018-10-10 08:20:25 +00:00
;
/* ** Forwards ** */
forward OnPlayerTakeOutFire ( playerid, fireid );
/* ** Hooks ** */
hook OnPlayerDisconnect( playerid, reason )
{
StopPlayerFireTracker( playerid );
return 1;
}
2019-02-16 23:28:58 +00:00
hook OnPlayerSpawn( playerid )
2018-10-10 08:20:25 +00:00
{
StopPlayerFireTracker( playerid );
return 1;
}
hook OnPlayerUpdateEx( playerid )
{
new
iKeys;
GetPlayerKeys( playerid, iKeys, tmpVariable, tmpVariable );
// Taking Out Fires
if ( ( iKeys & KEY_FIRE ) || ( iKeys & KEY_WALK ) )
2018-10-10 08:20:25 +00:00
{
new
using_firetruck = GetVehicleModel( GetPlayerVehicleID( playerid ) ) == 407;
2018-10-10 08:20:25 +00:00
if ( GetPlayerWeapon( playerid ) == 42 || using_firetruck )
2018-10-10 08:20:25 +00:00
{
foreach ( new i : fires )
2018-10-10 08:20:25 +00:00
{
static
Float: fX, Float: fY, Float: fZ;
if ( GetDynamicObjectPos( g_fireData[ i ] [ E_OBJECT ], fX, fY, fZ ) )
{
// add a bit of height cause of the fire
2018-10-10 08:20:25 +00:00
fZ += 2.3;
// check if range of point
if ( IsPlayerInRangeOfPoint( playerid, ( using_firetruck ? 30.0 : 10.0 ), fX, fY, fZ ) )
2018-10-10 08:20:25 +00:00
{
if ( IsPlayerAimingAt( playerid, fX, fY, fZ, ( using_firetruck ? 3.5 : 1.0 ) ) )
2018-10-10 08:20:25 +00:00
{
new
Float: removed_health = ( 2.5 + fRandomEx( 1.0, 5.0 ) ) * ( using_firetruck ? 2.5 : 1.0 );
if ( ( g_fireData[ i ] [ E_HEALTH ] -= removed_health ) < 0.0 ) {
g_fireData[ i ] [ E_HEALTH ] = 0.0;
2018-10-10 08:20:25 +00:00
}
UpdateDynamic3DTextLabelText( g_fireData[ i ] [ E_LABEL ], 0xA83434FF, sprintf( "House Fire %0.1f%", g_fireData[ i ] [ E_HEALTH ] ) );
if ( g_fireData[ i ] [ E_HEALTH ] <= 0.0 )
2018-10-10 08:20:25 +00:00
{
2019-06-17 13:19:41 +00:00
new min_money_earned = floatround( float( FIRE_EXTINGUISH_PAYOUT ) * 0.75 );
new money_earned = RandomEx( min_money_earned, FIRE_EXTINGUISH_PAYOUT );
2018-12-08 13:05:09 +00:00
2018-10-10 08:20:25 +00:00
ach_HandleExtinguishedFires( playerid );
2018-12-08 13:05:09 +00:00
SendClientMessageToAllFormatted( -1, "{A83434}[FIREMAN]"COL_WHITE" %s(%d) has earned "COL_GOLD"%s"COL_WHITE" for extinguishing a house fire.", ReturnPlayerName( playerid ), playerid, cash_format( money_earned ) );
2018-10-10 08:20:25 +00:00
GivePlayerScore( playerid, 2 );
2019-06-03 11:51:25 +00:00
GivePlayerExperience( playerid, E_ROLEPLAY, 0.5 );
2018-12-08 13:05:09 +00:00
GivePlayerCash( playerid, money_earned );
StockMarket_UpdateEarnings( E_STOCK_GOVERNMENT, money_earned, 0.15 );
HouseFire_Remove( i );
2018-10-10 08:20:25 +00:00
}
return 1;
2018-10-10 08:20:25 +00:00
}
}
}
}
}
}
return 1;
}
hook OnServerUpdate( )
2018-10-10 08:20:25 +00:00
{
// create fires if there are no fires existing and houses available
if ( ! Iter_Count( fires ) && Iter_Count( houses ) ) {
HouseFire_Create( );
}
return 1;
}
2018-10-10 08:20:25 +00:00
hook OnServerGameDayEnd( )
{
2019-06-12 19:11:11 +00:00
for ( new fireid = 0; fireid < MAX_FIRES; fireid ++ )
{
HouseFire_Remove( fireid );
}
HouseFire_Create( );
2018-10-10 08:20:25 +00:00
return 1;
}
hook OnPlayerDriveVehicle( playerid, vehicleid )
2018-10-10 08:20:25 +00:00
{
new
2018-12-25 06:59:13 +00:00
modelid = GetVehicleModel( vehicleid ),
Float: total_experience;
GetPlayerTotalExperience( playerid, total_experience );
2018-10-10 08:20:25 +00:00
if ( modelid == 407 ) {
2018-12-25 06:59:13 +00:00
if ( total_experience < 2500 )
{
RemovePlayerFromVehicle( playerid );
return SendServerMessage( playerid, "You must have 2,500XP to use a Firetruck." );
}
ShowPlayerHelpDialog( playerid, 2500, "You can see where fires are using ~g~/fires" );
}
2018-10-10 08:20:25 +00:00
return 1;
}
/* ** Commands ** */
CMD:firetracker( playerid, params[ ] ) cmd_fires( playerid, params );
CMD:fires( playerid, params[ ] )
2018-10-10 08:20:25 +00:00
{
p_FireTracker{ playerid } = ! p_FireTracker{ playerid };
if ( p_FireTracker{ playerid } )
2018-10-10 08:20:25 +00:00
{
UpdatePlayerFireTracker( playerid );
return SendServerMessage( playerid, "All house fires are now in your minimap as red markers." );
2018-10-10 08:20:25 +00:00
}
else
{
StopPlayerFireTracker( playerid );
return SendServerMessage( playerid, "You have hidden all the fires from your radar." );
}
}
/* ** Functions ** */
stock HouseFire_Create( )
{
// create house house fires for random homes
for ( new fireid = 0; fireid < MAX_FIRES; fireid ++ ) if ( ! Iter_Contains( fires, fireid ) )
{
new
houseid = HouseFire_GetRandomHome( );
if ( Iter_Contains( houses, houseid ) )
{
static
Float: X, Float: Y, Float: Z;
GetHousePos( houseid, X, Y, Z );
g_fireData[ fireid ] [ E_HEALTH ] = 100.0 + fRandomEx( 1, 25 );
g_fireData[ fireid ] [ E_HOUSE ] = houseid;
g_fireData[ fireid ] [ E_LABEL ] = CreateDynamic3DTextLabel( sprintf( "House Fire %0.1f%", g_fireData[ fireid ] [ E_HEALTH ] ), 0xA83434FF, X, Y, Z + 0.5, 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1 );
g_fireData[ fireid ] [ E_OBJECT ] = CreateDynamicObject( 18691, X, Y, Z - 2.3, 0.0, 0.0, 0.0 );
// fire map icons
g_fireData[ fireid ] [ E_MAP_ICON ] = CreateDynamicMapIcon( X, Y, Z, 0, 0xA83434FF, -1, -1, 0, 6000.0, MAPICON_GLOBAL );
Streamer_RemoveArrayData( STREAMER_TYPE_MAP_ICON, g_fireData[ fireid ] [ E_MAP_ICON ], E_STREAMER_PLAYER_ID, 0 );
Iter_Add( fires, fireid );
2018-10-10 08:20:25 +00:00
}
}
// show on radar when it is created
foreach ( new playerid : Player ) if ( p_FireTracker{ playerid } ) {
UpdatePlayerFireTracker( playerid );
2018-10-10 08:20:25 +00:00
}
return 1;
}
stock HouseFire_Remove( fireid )
{
DestroyDynamicObject( g_fireData[ fireid ] [ E_OBJECT ] );
g_fireData[ fireid ] [ E_OBJECT ] = INVALID_OBJECT_ID;
DestroyDynamic3DTextLabel( g_fireData[ fireid ] [ E_LABEL ] );
g_fireData[ fireid ] [ E_LABEL ] = Text3D: INVALID_3DTEXT_ID;
DestroyDynamicMapIcon( g_fireData[ fireid ] [ E_MAP_ICON ] );
g_fireData[ fireid ] [ E_MAP_ICON ] = -1;
Iter_Remove( fires, fireid );
return 1;
}
stock HouseFire_GetRandomHome( )
{
if ( ! Iter_Count( houses ) ) {
return -1;
}
static szCity[ MAX_ZONE_NAME ];
new ignoredHomes[ MAX_HOUSES ] = { -1, ... };
// first find homes to ignore
for ( new i = 0; i < MAX_HOUSES; i ++ )
{
// Avoid Hills / Avoid V.I.P or Clan Homes
if ( ! Iter_Contains( houses, i ) || g_houseData[ i ] [ E_EZ ] > 300.0 || g_houseData[ i ] [ E_COST ] < 500000 ) {
ignoredHomes[ i ] = i;
continue;
}
// check for house fire
if ( IsHouseOnFire( i ) ) {
ignoredHomes[ i ] = i;
continue;
}
// San Fierro only
Get2DCity( szCity, g_houseData[ i ] [ E_EX ], g_houseData[ i ] [ E_EY ], g_houseData[ i ] [ E_EZ ] );
if ( ! strmatch( szCity, "San Fierro" ) ) {
ignoredHomes[ i ] = i;
continue;
}
}
new
random_home = randomExcept( ignoredHomes, sizeof( ignoredHomes ) );
// apparently 'safer' to return value from variable
return random_home;
}
2018-10-10 08:20:25 +00:00
stock IsHouseOnFire( houseid )
{
if ( houseid < 0 || houseid > MAX_HOUSES )
return 0;
if ( ! Iter_Contains( houses, houseid ) )
return 0;
static
Float: X, Float: Y, Float: Z, Float: HX, Float: HY;
foreach ( new fireid : fires )
2018-10-10 08:20:25 +00:00
{
GetDynamicObjectPos( g_fireData[ fireid ] [ E_OBJECT ], X, Y, Z ); // Z is unused due to the object.
GetHousePos( houseid, HX, HY, Z );
if ( HX == X && HY == Y )
{
return 1;
2018-10-10 08:20:25 +00:00
}
}
return 0;
}
stock UpdatePlayerFireTracker( playerid )
2018-10-10 08:20:25 +00:00
{
// add player to map icon list
foreach ( new fireid : fires ) {
Streamer_AppendArrayData( STREAMER_TYPE_MAP_ICON, g_fireData[ fireid ] [ E_MAP_ICON ], E_STREAMER_PLAYER_ID, playerid );
}
return 1;
}
2018-10-10 08:20:25 +00:00
stock StopPlayerFireTracker( playerid )
{
// remove player from map icon list
foreach ( new fireid : fires ) if ( Streamer_IsInArrayData( STREAMER_TYPE_MAP_ICON, g_fireData[ fireid ] [ E_MAP_ICON ], E_STREAMER_PLAYER_ID, playerid ) ) {
Streamer_RemoveArrayData( STREAMER_TYPE_MAP_ICON, g_fireData[ fireid ] [ E_MAP_ICON ], E_STREAMER_PLAYER_ID, playerid );
2018-10-10 08:20:25 +00:00
}
// reset firetracker
p_FireTracker{ playerid } = false;
2018-10-10 08:20:25 +00:00
return 1;
}
stock Float: GetPointAngleToPoint( Float: x2, Float: y2, Float: X, Float: Y )
{
new Float: DX, Float: DY;
new Float: angle;
DX = floatabs( floatsub( x2, X ) );
DY = floatabs( floatsub( y2, Y ) );
if ( DY == 0.0 || DX == 0.0 )
{
if ( DY == 0 && DX > 0 ) angle = 0.0;
else if ( DY == 0 && DX < 0 ) angle = 180.0;
else if ( DY > 0 && DX == 0 ) angle = 90.0;
else if ( DY < 0 && DX == 0 ) angle = 270.0;
else if ( DY == 0 && DX == 0 ) angle = 0.0;
}
else
{
angle = atan( DX / DY );
if ( X > x2 && Y <= y2 ) angle += 90.0;
else if ( X <= x2 && Y < y2 ) angle = floatsub( 90.0, angle );
else if ( X < x2 && Y >= y2 ) angle -= 90.0;
else if ( X >= x2 && Y > y2 ) angle = floatsub( 270.0, angle );
}
return floatadd( angle, 90.0 );
}
stock Float: DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ) {
new Float: TGTDistance;
TGTDistance = floatsqroot( ( CamX - ObjX) * ( CamX - ObjX ) + ( CamY - ObjY ) * ( CamY - ObjY ) + ( CamZ - ObjZ ) * ( CamZ - ObjZ ) );
new Float: tmpX, Float: tmpY, Float: tmpZ;
tmpX = FrX * TGTDistance + CamX;
tmpY = FrY * TGTDistance + CamY;
tmpZ = FrZ * TGTDistance + CamZ;
return floatsqroot( ( tmpX - ObjX ) * ( tmpX - ObjX ) + ( tmpY - ObjY ) * ( tmpY - ObjY ) + ( tmpZ - ObjZ ) * ( tmpZ - ObjZ ) );
}
stock IsPlayerAimingAt( playerid, Float: x, Float: y, Float: z, Float: radius ) // forgot who made this
{
new Float: camera_x, Float: camera_y, Float: camera_z;
new Float: vector_x, Float: vector_y, Float: vector_z;
GetPlayerCameraPos( playerid, camera_x, camera_y, camera_z );
GetPlayerCameraFrontVector( playerid, vector_x, vector_y, vector_z );
new Float: vertical, Float: horizontal;
switch ( GetPlayerWeapon( playerid ) )
{
case 34, 35, 36: return DistanceCameraTargetToLocation( camera_x, camera_y, camera_z, x, y, z, vector_x, vector_y, vector_z ) < radius;
case 30, 31: vertical = 4.0, horizontal = -1.6;
case 33: vertical = 2.7, horizontal = -1.0;
default: vertical = 6.0, horizontal = -2.2;
}
new Float: angle = GetPointAngleToPoint( 0, 0, floatsqroot( vector_x * vector_x + vector_y * vector_y ), vector_z ) - 270.0;
new Float: resize_x, Float: resize_y, Float: resize_z = floatsin( angle + vertical, degrees );
GetXYInFrontOfPoint( resize_x, resize_y, GetPointAngleToPoint( 0, 0, vector_x, vector_y ) + horizontal, floatcos( angle + vertical, degrees ) );
return DistanceCameraTargetToLocation(camera_x, camera_y, camera_z, x, y, z, resize_x, resize_y, resize_z) < radius;
}
stock GetXYInFrontOfPoint( &Float: x, &Float: y, Float: angle, Float: distance ) {
x += ( distance * floatsin( -angle, degrees ) );
y += ( distance * floatcos( -angle, degrees ) );
}