View Single Post
Author Message
VEN
Veteran Member
Join Date: Jan 2005
Old 06-24-2006 , 11:35   [FAQ/Tutorial] CS Bomb Scripting
Reply With Quote #1

Intro
Bomb is the one of the most interesting aspects of the CS scripting. I saw many bomb scripting questions/mistakes. So i decided to create the "CS Bomb Scripting FAQ/Tutorial".

Requirements
You should be familiar with the basics of the AMXX scripting. Fakemeta Utilities functions is used here. To get the player's index (only inside "logevent_function_p") you have to use:
PHP Code:
stock get_loguser_index() {
    new 
loguser[80], name[32]
    
read_logargv(0loguser79)
    
parse_loguser(logusername31)
 
    return 
get_user_index(name)

Example:
PHP Code:
public logevent_function_p() {
    new 
id get_loguser_index()
    
// ...

1. Player/Bomb
PHP Code:
// Is bomb dropped/carried?
    
if (fm_find_ent_by_class(-1"weapon_c4"))
 
// Is bomb dropped?
    
new bomb fm_find_ent_by_class(-1"weapon_c4")
    if (
bomb && pev(bombpev_owner) > get_maxplayers())
 
// Is bomb carried/who is the carrier?
    
new carrier 0ownerentbomb fm_find_ent_by_class(-1"weapon_c4")
    if (
bomb && (ownerent pev(bombpev_owner)) <= get_maxplayers())
        
carrier ownerent
    
if (carrier// we have the carrier
    
else // we do not have a carrier
 
// Is given player has the bomb?
    
if (user_has_weapon(idCSW_C4)) // method #1
    
if (pev(idpev_weapons) & (1<<CSW_C4)) // method #2
    
if (fm_find_ent_by_owner(-1"weapon_c4"id)) // method #3
 
// How to transfer the bomb from one player to another?
    
fm_transfer_user_gun(carrierrecipientCSW_C4// returns true on success
 
// How to force a player to drop the bomb?
    
engclient_cmd(id"drop""weapon_c4")
 
// How to remove the dropped bomb?
    
new weapboxbomb fm_find_ent_by_class(-1"weapon_c4")
    if (
bomb && (weapbox pev(bombpev_owner)) > get_maxplayers()) {
        
dllfunc(DLLFunc_Thinkweapbox// will remove weaponbox + weapon_c4 entity pair
        // remove blinking red bomb mark on the radar
        
message_begin(MSG_ALLget_user_msgid("BombPickup"))
        
message_end()
    }
 
// How to give the bomb to a player?
    
fm_give_item(id"weapon_c4")
    
// use cs_set_user_plant(id) to allow planting
 
// How to strip the bomb from a player?
    
engclient_cmd(id"weapon_c4")
    
cs_set_user_bpammo(idCSW_C40)
    
engclient_cmd(id"lastinv")
    
// remove bomb hud icon
    
message_begin(MSG_ONEget_user_msgid("StatusIcon"), _id)
    
write_byte(0)
    
write_string("c4")
    
message_end()
 
// Is bomb planted/how to remove the planted bomb?
    
new bomb
    
if ((bomb fm_find_ent_by_model(-1"grenade""models/w_c4.mdl"))) {
        
// bomb is planted
        
fm_remove_entity(bomb// remove the planted bomb
    

2. Drop/Collect
PHP Code:
// Player spawned with the bomb event
    
register_logevent("logevent_function_p"3"2=Spawned_With_The_Bomb")
 
// Bomb dropped (including disconnect/death) event
    
register_logevent("logevent_function_p"3"2=Dropped_The_Bomb")
    
// use is_user_alive/is_user_connected to check for disconnect/death
 
// Bomb collected (except spawn) event
    
register_logevent("logevent_function_p"3"2=Got_The_Bomb")
 
// Bomb gained (including spawn/give_item) event
    
register_event("WeapPickup""event_function""be""1=6"
3. Plant/Defuse
PHP Code:
// Bomb planting started event
    
register_event("BarTime""event_function""be""1=3")
 
// Bomb planted event
    
register_logevent("logevent_function_p"3"2=Planted_The_Bomb")
 
// Bomb defusion started event
    
register_event("BarTime""event_function""be""1=5""1=10")
 
// Bomb defusion (without kit) started event
    
register_logevent("logevent_function_p"3"2=Begin_Bomb_Defuse_Without_Kit")
 
// Bomb defusion (with kit) started event
    
register_logevent("logevent_function_p"3"2=Begin_Bomb_Defuse_With_Kit")
 
// Bomb defused event
    
register_logevent("logevent_function_p"3"2=Defused_The_Bomb")
 
// Bomb planting/defusion canceled event
    
register_event("BarTime""event_function""b""1=0")
    
register_event("CurWeapon""event_function""be""2!6")
    
// you must be sure that bomb planting/defusion is in progress!
 
// Target saved event
    
register_logevent("logevent_function"6"3=Target_Saved"
4. Explosion
PHP Code:
// Target bombed (right before round end) event
    
register_logevent("logevent_function"6"3=Target_Bombed")
 
// Planted bomb exploded (before/after round end) event (discovered by Ryan)
    
register_event("23""event_function""a""1=17""6=-105""7=17")
 
// Player killed by bomb explosion event(forward) (discovered by Brad/VEN)
    // will not work if victim is killed by env_explosion entity that is triggered by explosion
    
public client_death(killervictimwpnindexhitplaceTK) {
        if (
wpnindex == CSW_C4)
    } 
5. Targets
PHP Code:
// Is player at the bomb target (func_bomb_target)?
    // Note: there are no good way to detect if player is at the info_bomb_target
    
new target = -1, class[] = "func_bomb_target"bool:is_inside false
    
while ((target fm_find_ent_by_class(target, class))) {
        if (!
fm_boxents_distance(indextarget)) {
            
is_inside true
            
break
        }
    }
    if (
is_inside)
 
// Is map contain bomb targets?
    
if (fm_find_ent_by_class(-1"func_bomb_target") || fm_find_ent_by_class(-1"info_bomb_target"))
 
// How to remove bomb targets?
    
new target = -1classname[] = "func_bomb_target"
    
while ((target fm_find_ent_by_class(targetclassname)))
        
fm_remove_entity(target)
    
classname "info_bomb_target"
    
while ((target fm_find_ent_by_class(targetclassname)))
        
fm_remove_entity(target

Last edited by Exolent[jNr]; 11-10-2010 at 02:00. Reason: Added CurWeapon for bomb plant/defuse cancel event.
VEN is offline