Atrinik Server 2.5
types/converter.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00032 #define CONV_FROM(xyz)  (xyz->slaying)
00033 #define CONV_TO(xyz)    (xyz->other_arch)
00034 #define CONV_NR(xyz)    ((unsigned long) xyz->stats.sp)
00035 #define CONV_NEED(xyz)  ((unsigned long) xyz->stats.food)
00036 
00043 int convert_item(object *item, object *converter)
00044 {
00045     int nr = 0;
00046     object *tmp;
00047 
00048     /* We make some assumptions - we assume if it takes money as it type,
00049      * it wants some amount.  We don't make change (ie, if something costs
00050      * 3 gp and player drops a platinum, tough luck) */
00051     if (!strcmp(CONV_FROM(converter), "money"))
00052     {
00053         int cost;
00054         nr = (item->nrof * item->value) / CONV_NEED(converter);
00055 
00056         if (!nr)
00057         {
00058             return 0;
00059         }
00060 
00061         cost = nr * CONV_NEED(converter) / item->value;
00062 
00063         /* take into account rounding errors */
00064         if (nr * CONV_NEED(converter) % item->value)
00065         {
00066             cost++;
00067         }
00068 
00069         decrease_ob_nr(item, cost);
00070     }
00071     else
00072     {
00073         if (item->type == PLAYER || CONV_FROM(converter) != item->arch->name || (CONV_NEED(converter) && CONV_NEED(converter) > item->nrof))
00074         {
00075             return 0;
00076         }
00077 
00078         if (CONV_NEED(converter))
00079         {
00080             nr = item->nrof / CONV_NEED(converter);
00081             decrease_ob_nr(item, nr * CONV_NEED(converter));
00082         }
00083         else
00084         {
00085             remove_ob(item);
00086             check_walk_off(item, NULL, MOVE_APPLY_VANISHED);
00087         }
00088     }
00089 
00090     item = arch_to_object(converter->other_arch);
00091 
00092     if (CONV_NR(converter))
00093     {
00094         item->nrof = CONV_NR(converter);
00095     }
00096 
00097     if (nr)
00098     {
00099         item->nrof *= nr;
00100     }
00101 
00102     for (tmp = get_map_ob(converter->map, converter->x, converter->y); tmp != NULL; tmp = tmp->above)
00103     {
00104         if (tmp->type == SHOP_FLOOR)
00105         {
00106             break;
00107         }
00108     }
00109 
00110     if (tmp != NULL)
00111     {
00112         SET_FLAG(item, FLAG_UNPAID);
00113     }
00114 
00115     item->x = converter->x;
00116     item->y = converter->y;
00117     insert_ob_in_map(item, converter->map, converter, 0);
00118 
00119     return 1;
00120 }