From: Lyberta Date: Sat, 26 Aug 2017 23:01:13 +0000 (+0300) Subject: Added GivePlayerResource. X-Git-Tag: xonotic-v0.8.5~2459^2~21^2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=6a592b9766b01b812c267aeebff6595b9fec27c7;p=xonotic%2Fxonotic-data.pk3dir.git Added GivePlayerResource. --- diff --git a/qcsrc/common/t_items.qc b/qcsrc/common/t_items.qc index d1d2cb1d9..6ee7c1d8b 100644 --- a/qcsrc/common/t_items.qc +++ b/qcsrc/common/t_items.qc @@ -648,40 +648,71 @@ void Item_ScheduleInitialRespawn(entity e) Item_ScheduleRespawnIn(e, max(0, game_starttime - time) + ((e.respawntimestart) ? e.respawntimestart : ITEM_RESPAWNTIME_INITIAL(e))); } -void GivePlayerHealth(entity player, float amount) +void GivePlayerResource(entity player, .float resource_type, float amount) { if (amount == 0) { return; } - // Ugly hack. We do not check if health goes beyond hard limit since - // currently it is done in player_regen. We need to bring back this check - // when other code is ported to this function. - player.health = bound(player.health, player.health + amount, - autocvar_g_balance_health_limit); - // Correct code: - //player.health = bound(player.health, player.health + amount, - // min(autocvar_g_balance_health_limit, ITEM_COUNT_HARD_LIMIT)); - player.pauserothealth_finished = max(player.pauserothealth_finished, time + - autocvar_g_balance_pause_health_rot); + switch (resource_type) + { + case health: + { + // Ugly hack. We do not check if health goes beyond hard limit since + // currently it is done in player_regen. We need to bring back this + // check when other code is ported to this function. + player.health = bound(player.health, player.health + amount, + autocvar_g_balance_health_limit); + // Correct code: + //player.health = bound(player.health, player.health + amount, + // min(autocvar_g_balance_health_limit, ITEM_COUNT_HARD_LIMIT)); + player.pauserothealth_finished = max(player.pauserothealth_finished, + time + autocvar_g_balance_pause_health_rot); + return; + } + case armorvalue: + { + // Ugly hack. We do not check if armor goes beyond hard limit since + // currently it is done in player_regen. We need to bring back this + // check when other code is ported to this function. + player.armorvalue = bound(player.armorvalue, player.armorvalue + + amount, autocvar_g_balance_armor_limit); + // Correct code: + //player.armorvalue = bound(player.armorvalue, player.armorvalue + + // amount, min(autocvar_g_balance_armor_limit, + // ITEM_COUNT_HARD_LIMIT)); + player.pauserotarmor_finished = max(player.pauserotarmor_finished, + time + autocvar_g_balance_pause_armor_rot); + return; + } + case ammo_shells: + case ammo_nails: + case ammo_rockets: + case ammo_cells: + case ammo_plasma: + { + GivePlayerAmmo(player, resource_type, amount); + return; + } + case ammo_fuel: + { + player.ammo_fuel = bound(player.ammo_fuel, player.ammo_fuel + + amount, min(g_pickup_fuel_max, ITEM_COUNT_HARD_LIMIT)); + player.pauserotfuel_finished = max(player.pauserotfuel_finished, + time + autocvar_g_balance_pause_fuel_rot); + return; + } + } +} + +void GivePlayerHealth(entity player, float amount) +{ + GivePlayerResource(player, health, amount); } void GivePlayerArmor(entity player, float amount) { - if (amount == 0) - { - return; - } - // Ugly hack. We do not check if armor goes beyond hard limit since - // currently it is done in player_regen. We need to bring back this check - // when other code is ported to this function. - player.armorvalue = bound(player.armorvalue, player.armorvalue + amount, - autocvar_g_balance_armor_limit); - // Correct code: - //player.armorvalue = bound(player.armorvalue, player.armorvalue + amount, - // min(autocvar_g_balance_armor_limit, ITEM_COUNT_HARD_LIMIT)); - player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + - autocvar_g_balance_pause_armor_rot); + GivePlayerResource(player, armorvalue, amount); } void GivePlayerAmmo(entity player, .float ammotype, float amount) @@ -725,17 +756,10 @@ void GivePlayerAmmo(entity player, .float ammotype, float amount) void GivePlayerFuel(entity player, float amount) { - if (amount == 0) - { - return; - } - player.ammo_fuel = bound(player.ammo_fuel, player.ammo_fuel + amount, - min(g_pickup_fuel_max, ITEM_COUNT_HARD_LIMIT)); - player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + - autocvar_g_balance_pause_fuel_rot); + GivePlayerResource(player, ammo_fuel, amount); } -float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax, float mode) +float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax) { if (!item.(ammotype)) return false; @@ -744,8 +768,13 @@ float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax { if ((player.(ammotype) < ammomax) || item.pickup_anyway > 0) { - player.(ammotype) = bound(player.(ammotype), ammomax, player.(ammotype) + item.(ammotype)); - goto YEAH; + float amount = item.(ammotype); + if ((player.(ammotype) + amount) > ammomax) + { + amount = ammomax - player.(ammotype); + } + GivePlayerResource(player, ammotype, amount); + return true; } } else if(g_weapon_stay == 2) @@ -753,29 +782,11 @@ float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax float mi = min(item.(ammotype), ammomax); if (player.(ammotype) < mi) { - player.(ammotype) = mi; - goto YEAH; + GivePlayerResource(player, ammotype, mi - player.(ammotype)); } + return true; } - return false; - -LABEL(YEAH) - switch(mode) - { - case ITEM_MODE_FUEL: - player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + autocvar_g_balance_pause_fuel_rot); - break; - case ITEM_MODE_HEALTH: - player.pauserothealth_finished = max(player.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot); - break; - case ITEM_MODE_ARMOR: - player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + autocvar_g_balance_pause_armor_rot); - break; - default: - break; - } - return true; } float Item_GiveTo(entity item, entity player) @@ -804,16 +815,14 @@ float Item_GiveTo(entity item, entity player) } } } - - pickedup |= Item_GiveAmmoTo(item, player, ammo_fuel, g_pickup_fuel_max, ITEM_MODE_FUEL); - pickedup |= Item_GiveAmmoTo(item, player, ammo_shells, g_pickup_shells_max, ITEM_MODE_NONE); - pickedup |= Item_GiveAmmoTo(item, player, ammo_nails, g_pickup_nails_max, ITEM_MODE_NONE); - pickedup |= Item_GiveAmmoTo(item, player, ammo_rockets, g_pickup_rockets_max, ITEM_MODE_NONE); - pickedup |= Item_GiveAmmoTo(item, player, ammo_cells, g_pickup_cells_max, ITEM_MODE_NONE); - pickedup |= Item_GiveAmmoTo(item, player, ammo_plasma, g_pickup_plasma_max, ITEM_MODE_NONE); - pickedup |= Item_GiveAmmoTo(item, player, health, item.max_health, ITEM_MODE_HEALTH); - pickedup |= Item_GiveAmmoTo(item, player, armorvalue, item.max_armorvalue, ITEM_MODE_ARMOR); - + pickedup |= Item_GiveAmmoTo(item, player, health, item.max_health); + pickedup |= Item_GiveAmmoTo(item, player, armorvalue, item.max_armorvalue); + pickedup |= Item_GiveAmmoTo(item, player, ammo_shells, g_pickup_shells_max); + pickedup |= Item_GiveAmmoTo(item, player, ammo_nails, g_pickup_nails_max); + pickedup |= Item_GiveAmmoTo(item, player, ammo_rockets, g_pickup_rockets_max); + pickedup |= Item_GiveAmmoTo(item, player, ammo_cells, g_pickup_cells_max); + pickedup |= Item_GiveAmmoTo(item, player, ammo_plasma, g_pickup_plasma_max); + pickedup |= Item_GiveAmmoTo(item, player, ammo_fuel, g_pickup_fuel_max); if (item.itemdef.instanceOfWeaponPickup) { WepSet w; diff --git a/qcsrc/common/t_items.qh b/qcsrc/common/t_items.qh index 41843306f..14cbd8e3f 100644 --- a/qcsrc/common/t_items.qh +++ b/qcsrc/common/t_items.qh @@ -89,6 +89,13 @@ void Item_ScheduleRespawn(entity e); void Item_ScheduleInitialRespawn(entity e); +/// \brief Gives player a resource such as health, armor or ammo. +/// \param[in,out] player Player to give resource to. +/// \param[in] resource_type Type of the resource. +/// \param[in] amount Amount of resource to give. +/// \return No return. +void GivePlayerResource(entity player, .float resource_type, float amount); + /// \brief Gives health to the player. /// \param[in,out] player Player to give health to. /// \param[in] amount Amount of health to give. @@ -114,11 +121,7 @@ void GivePlayerAmmo(entity player, .float ammotype, float amount); /// \return No return. void GivePlayerFuel(entity player, float amount); -float ITEM_MODE_NONE = 0; -float ITEM_MODE_HEALTH = 1; -float ITEM_MODE_ARMOR = 2; -float ITEM_MODE_FUEL = 3; -float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax, float mode); +float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax); float Item_GiveTo(entity item, entity player);