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(0, loguser, 79)
parse_loguser(loguser, name, 31)
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(bomb, pev_owner) > get_maxplayers())
// Is bomb carried/who is the carrier?
new carrier = 0, ownerent, bomb = fm_find_ent_by_class(-1, "weapon_c4")
if (bomb && (ownerent = pev(bomb, pev_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(id, CSW_C4)) // method #1
if (pev(id, pev_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(carrier, recipient, CSW_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 weapbox, bomb = fm_find_ent_by_class(-1, "weapon_c4")
if (bomb && (weapbox = pev(bomb, pev_owner)) > get_maxplayers()) {
dllfunc(DLLFunc_Think, weapbox) // will remove weaponbox + weapon_c4 entity pair
// remove blinking red bomb mark on the radar
message_begin(MSG_ALL, get_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(id, CSW_C4, 0)
engclient_cmd(id, "lastinv")
// remove bomb hud icon
message_begin(MSG_ONE, get_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(killer, victim, wpnindex, hitplace, TK) {
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(index, target)) {
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 = -1, classname[] = "func_bomb_target"
while ((target = fm_find_ent_by_class(target, classname)))
fm_remove_entity(target)
classname = "info_bomb_target"
while ((target = fm_find_ent_by_class(target, classname)))
fm_remove_entity(target)