]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/t_items.qc
Random start weapons: Rename i and j.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / t_items.qc
index 9055a09a1359b87e23e84c0a3922dbef08719039..1a2e92c4ff4b146203198c698cea6ff48bd4b049 100644 (file)
@@ -682,163 +682,21 @@ void Item_ScheduleInitialRespawn(entity e)
        Item_ScheduleRespawnIn(e, max(0, game_starttime - time) + ((e.respawntimestart) ? e.respawntimestart : ITEM_RESPAWNTIME_INITIAL(e)));
 }
 
-int GetResourceType(.float resource_property)
-{
-       switch (resource_property)
-       {
-               case health: { return RESOURCE_HEALTH; }
-               case armorvalue: { return RESOURCE_ARMOR; }
-               case ammo_shells: { return RESOURCE_SHELLS; }
-               case ammo_nails: { return RESOURCE_BULLETS; }
-               case ammo_rockets: { return RESOURCE_ROCKETS; }
-               case ammo_cells: { return RESOURCE_CELLS; }
-               case ammo_plasma: { return RESOURCE_PLASMA; }
-               case ammo_fuel: { return RESOURCE_FUEL; }
-       }
-       error("GetResourceType: Invalid property.");
-       return 0;
-}
-
-.float GetResourceProperty(int resource_type)
-{
-       switch (resource_type)
-       {
-               case RESOURCE_HEALTH: { return health; }
-               case RESOURCE_ARMOR: { return armorvalue; }
-               case RESOURCE_SHELLS: { return ammo_shells; }
-               case RESOURCE_BULLETS: { return ammo_nails; }
-               case RESOURCE_ROCKETS: { return ammo_rockets; }
-               case RESOURCE_CELLS: { return ammo_cells; }
-               case RESOURCE_PLASMA: { return ammo_plasma; }
-               case RESOURCE_FUEL: { return ammo_fuel; }
-       }
-       error("GetResourceProperty: Invalid resource type.");
-       return health;
-}
-
-float GetResourceLimit(entity e, int resource_type)
-{
-       float limit;
-       switch (resource_type)
-       {
-               case RESOURCE_HEALTH:
-               {
-                       limit = autocvar_g_balance_health_limit;
-                       break;
-               }
-               case RESOURCE_ARMOR:
-               {
-                       limit = autocvar_g_balance_armor_limit;
-                       break;
-               }
-               case RESOURCE_SHELLS:
-               {
-                       limit = g_pickup_shells_max;
-                       break;
-               }
-               case RESOURCE_BULLETS:
-               {
-                       limit = g_pickup_nails_max;
-                       break;
-               }
-               case RESOURCE_ROCKETS:
-               {
-                       limit = g_pickup_rockets_max;
-                       break;
-               }
-               case RESOURCE_CELLS:
-               {
-                       limit = g_pickup_cells_max;
-                       break;
-               }
-               case RESOURCE_PLASMA:
-               {
-                       limit = g_pickup_plasma_max;
-                       break;
-               }
-               case RESOURCE_FUEL:
-               {
-                       limit = autocvar_g_balance_fuel_limit;
-                       break;
-               }
-               default:
-               {
-                       error("GetResourceLimit: Invalid resource type.");
-                       return 0;
-               }
-       }
-       MUTATOR_CALLHOOK(GetResourceLimit, e, resource_type, limit);
-       limit = M_ARGV(2, float);
-       if (limit > RESOURCE_AMOUNT_HARD_LIMIT)
-       {
-               limit = RESOURCE_AMOUNT_HARD_LIMIT;
-       }
-       return limit;
-}
-
-void GiveResource(entity receiver, int resource_type, float amount)
-{
-       if (amount == 0)
-       {
-               return;
-       }
-       bool forbid = MUTATOR_CALLHOOK(GiveResource, receiver, resource_type,
-               amount);
-       if (forbid)
-       {
-               return;
-       }
-       resource_type = M_ARGV(1, int);
-       amount = M_ARGV(2, float);
-       .float resource_property = GetResourceProperty(resource_type);
-       float max_amount = GetResourceLimit(receiver, resource_type);
-       receiver.(resource_property) = bound(receiver.(resource_property),
-               receiver.(resource_property) + amount, max_amount);
-       switch (resource_type)
-       {
-               case RESOURCE_HEALTH:
-               {
-                       receiver.pauserothealth_finished =
-                               max(receiver.pauserothealth_finished, time +
-                               autocvar_g_balance_pause_health_rot);
-                       return;
-               }
-               case RESOURCE_ARMOR:
-               {
-                       receiver.pauserotarmor_finished =
-                               max(receiver.pauserotarmor_finished, time +
-                               autocvar_g_balance_pause_armor_rot);
-                       return;
-               }
-               case RESOURCE_FUEL:
-               {
-                       receiver.pauserotfuel_finished = max(receiver.pauserotfuel_finished,
-                               time + autocvar_g_balance_pause_fuel_rot);
-                       return;
-               }
-       }
-}
-
-void GiveResourceViaProperty(entity receiver, .float resource_property,
-       float amount)
-{
-       GiveResource(receiver, GetResourceType(resource_property), amount);
-}
-
 void GiveRandomWeapons(entity receiver, int num_weapons, string weapon_names,
-       float shells, float bullets, float rockets, float cells, float plasma)
+       entity ammo_entity)
 {
        if (num_weapons == 0)
        {
                return;
        }
        int num_potential_weapons = tokenize_console(weapon_names);
-       for (int i = 0; i < num_weapons; ++i)
+       for (int give_attempt = 0; give_attempt < num_weapons; ++give_attempt)
        {
                RandomSelection_Init();
-               for (int j = 0; j < num_potential_weapons; ++j)
+               for (int weapon_index = 0; weapon_index < num_potential_weapons;
+                       ++weapon_index)
                {
-                       string weapon = argv(j);
+                       string weapon = argv(weapon_index);
                        FOREACH(Weapons, it != WEP_Null,
                        {
                                // Finding a weapon which player doesn't have.
@@ -854,85 +712,44 @@ void GiveRandomWeapons(entity receiver, int num_weapons, string weapon_names,
                        return;
                }
                receiver.weapons |= RandomSelection_chosen_ent.m_wepset;
-               switch (RandomSelection_chosen_ent.ammo_field)
+               if (RandomSelection_chosen_ent.ammo_type == RESOURCE_NONE)
                {
-                       case (ammo_shells):
-                       {
-                               if (receiver.ammo_shells != 0)
-                               {
-                                       break;
-                               }
-                               GiveResource(receiver, RESOURCE_SHELLS, shells);
-                               break;
-                       }
-                       case (ammo_nails):
-                       {
-                               if (receiver.ammo_nails != 0)
-                               {
-                                       break;
-                               }
-                               GiveResource(receiver, RESOURCE_BULLETS, bullets);
-                               break;
-                       }
-                       case (ammo_rockets):
-                       {
-                               if (receiver.ammo_rockets != 0)
-                               {
-                                       break;
-                               }
-                               GiveResource(receiver, RESOURCE_ROCKETS, rockets);
-                               break;
-                       }
-                       case (ammo_cells):
-                       {
-                               if (receiver.ammo_cells != 0)
-                               {
-                                       break;
-                               }
-                               GiveResource(receiver, RESOURCE_CELLS, cells);
-                               break;
-                       }
-                       case (ammo_plasma):
-                       {
-                               if (receiver.ammo_plasma != 0)
-                               {
-                                       break;
-                               }
-                               GiveResource(receiver, RESOURCE_PLASMA, plasma);
-                               break;
-                       }
+                       continue;
+               }
+               if (GetResourceAmount(receiver,
+                       RandomSelection_chosen_ent.ammo_type) != 0)
+               {
+                       continue;
                }
+               GiveResource(receiver, RandomSelection_chosen_ent.ammo_type,
+                       GetResourceAmount(ammo_entity,
+                       RandomSelection_chosen_ent.ammo_type));
        }
 }
 
-float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax)
+float Item_GiveAmmoTo(entity item, entity player, int resource_type, float ammomax)
 {
-       if (!item.(ammotype))
+       float amount = GetResourceAmount(item, resource_type);
+       if (amount == 0)
+       {
                return false;
-
+       }
+       float player_amount = GetResourceAmount(player, resource_type);
        if (item.spawnshieldtime)
        {
-               if ((player.(ammotype) < ammomax) || item.pickup_anyway > 0)
+               if ((player_amount >= ammomax) && (item.pickup_anyway <= 0))
                {
-                       float amount = item.(ammotype);
-                       if ((player.(ammotype) + amount) > ammomax)
-                       {
-                               amount = ammomax - player.(ammotype);
-                       }
-                       GiveResourceViaProperty(player, ammotype, amount);
-                       return true;
+                       return false;
                }
+               GiveResourceWithLimit(player, resource_type, amount, ammomax);
+               return true;
        }
-       else if(g_weapon_stay == 2)
+       if (g_weapon_stay != 2)
        {
-               float mi = min(item.(ammotype), ammomax);
-               if (player.(ammotype) < mi)
-               {
-                       GiveResourceViaProperty(player, ammotype, mi - player.(ammotype));
-               }
-               return true;
+               return false;
        }
-       return false;
+       GiveResourceWithLimit(player, resource_type, amount, min(amount, ammomax));
+       return true;
 }
 
 float Item_GiveTo(entity item, entity player)
@@ -961,14 +778,14 @@ float Item_GiveTo(entity item, entity player)
                        }
                }
        }
-       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);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_HEALTH, item.max_health);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_ARMOR, item.max_armorvalue);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_SHELLS, g_pickup_shells_max);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_BULLETS, g_pickup_nails_max);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_ROCKETS, g_pickup_rockets_max);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_CELLS, g_pickup_cells_max);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_PLASMA, g_pickup_plasma_max);
+       pickedup |= Item_GiveAmmoTo(item, player, RESOURCE_FUEL, g_pickup_fuel_max);
        if (item.itemdef.instanceOfWeaponPickup)
        {
                WepSet w;
@@ -1129,6 +946,7 @@ LABEL(pickup)
                                if(it.itemdef) // is a registered item
                                {
                                        Item_Show(it, -1);
+                                       it.scheduledrespawntime = 0;
                                        RandomSelection_AddEnt(it, it.cnt, 0);
                                }
                        });
@@ -1260,14 +1078,14 @@ float ammo_pickupevalfunc(entity player, entity item)
                        if(!(player.weapons & (it.m_wepset)))
                                continue;
 
-                       switch(it.ammo_field)
+                       switch(it.ammo_type)
                        {
-                               case ammo_shells:  need_shells  = true; break;
-                               case ammo_nails:   need_nails   = true; break;
-                               case ammo_rockets: need_rockets = true; break;
-                               case ammo_cells:   need_cells   = true; break;
-                               case ammo_plasma:  need_plasma  = true; break;
-                               case ammo_fuel:    need_fuel    = true; break;
+                               case RESOURCE_SHELLS:  need_shells  = true; break;
+                               case RESOURCE_BULLETS: need_nails   = true; break;
+                               case RESOURCE_ROCKETS: need_rockets = true; break;
+                               case RESOURCE_CELLS:   need_cells   = true; break;
+                               case RESOURCE_PLASMA:  need_plasma  = true; break;
+                               case RESOURCE_FUEL:    need_fuel    = true; break;
                        }
                });
                rating = item.bot_pickupbasevalue;