Example #1
0
static int caldera_handle(trigger * t, void *data)
{
    /* call an event handler on caldera.
     * data.v -> ( variant event, int timer )
     */
    building *b = (building *)t->data.v;
    if (b != NULL) {
        unit **up = &b->region->units;
        while (*up) {
            unit *u = *up;
            if (u->building == b) {
                message *msg;
                if (u->items) {
                    item **ip = &u->items;
                    msg = msg_message("caldera_handle_1", "unit items", u, u->items);
                    while (*ip) {
                        item *i = *ip;
                        i_remove(ip, i);
                        if (*ip == i)
                            ip = &i->next;
                    }
                }
                else {
                    msg = msg_message("caldera_handle_0", "unit", u);
                }
                add_message(&u->region->msgs, msg);
                set_number(u, 0);
            }
            if (*up == u)
                up = &u->next;
        }
    }
    else {
        log_error("could not perform caldera::handle()\n");
    }
    unused_arg(data);
    return 0;
}
Example #2
0
/** give all items to friends or peasants.
 * this function returns 0 on success, or 1 if there are items that
 * could not be destroyed.
 */
int gift_items(unit * u, int flags)
{
  region *r = u->region;
  item **itm_p = &u->items;
  int retval = 0;
  int rule = rule_give();

  assert(u->region);
  assert(u->faction);

  if ((u->faction->flags & FFL_QUIT) == 0 || (rule & GIVE_ONDEATH) == 0) {
    if ((rule & GIVE_ALLITEMS) == 0 && (flags & GIFT_FRIENDS))
      flags -= GIFT_FRIENDS;
    if ((rule & GIVE_PEASANTS) == 0 && (flags & GIFT_PEASANTS))
      flags -= GIFT_PEASANTS;
    if ((rule & GIVE_SELF) == 0 && (flags & GIFT_SELF))
      flags -= GIFT_SELF;
  }

  if (u->items == NULL || fval(u_race(u), RCF_ILLUSIONARY))
    return 0;
  if ((u_race(u)->ec_flags & GIVEITEM) == 0)
    return 0;

  /* at first, I should try giving my crap to my own units in this region */
  if (u->faction && (u->faction->flags & FFL_QUIT) == 0 && (flags & GIFT_SELF)) {
    unit *u2, *u3 = NULL;
    for (u2 = r->units; u2; u2 = u2->next) {
      if (u2 != u && u2->faction == u->faction && u2->number > 0) {
        /* some units won't take stuff: */
        if (u_race(u2)->ec_flags & GETITEM) {
          /* we don't like to gift it to units that won't give it back */
          if (u_race(u2)->ec_flags & GIVEITEM) {
            i_merge(&u2->items, &u->items);
            u->items = NULL;
            break;
          } else {
            u3 = u2;
          }
        }
      }
    }
    if (u->items && u3) {
      /* if nobody else takes it, we give it to a unit that has issues */
      i_merge(&u3->items, &u->items);
      u->items = NULL;
    }
    if (u->items == NULL)
      return 0;
  }

  /* if I have friends, I'll try to give my stuff to them */
  if (u->faction && (flags & GIFT_FRIENDS)) {
    int number = 0;
    buddy *friends = get_friends(u, &number);

    while (friends) {
      struct buddy *nf = friends;
      unit *u2 = nf->unit;
      item *itm = u->items;
      while (itm != NULL) {
        const item_type *itype = itm->type;
        item *itn = itm->next;
        int n = itm->number;
        n = n * nf->number / number;
        if (n > 0) {
          i_change(&u->items, itype, -n);
          i_change(&u2->items, itype, n);
        }
        itm = itn;
      }
      number -= nf->number;
      friends = nf->next;
      free(nf);
    }
    if (u->items == NULL)
      return 0;
  }

  /* last, but not least, give money and horses to peasants */
  while (*itm_p) {
    item *itm = *itm_p;

    if (flags & GIFT_PEASANTS) {
      if (!fval(u->region->terrain, SEA_REGION)) {
        if (itm->type == olditemtype[I_HORSE]) {
          rsethorses(r, rhorses(r) + itm->number);
          itm->number = 0;
        } else if (itm->type == i_silver) {
          rsetmoney(r, rmoney(r) + itm->number);
          itm->number = 0;
        }
      }
    }
    if (itm->number > 0 && (itm->type->flags & ITF_NOTLOST)) {
      itm_p = &itm->next;
      retval = -1;
    } else {
      i_remove(itm_p, itm);
      i_free(itm);
    }
  }
  return retval;
}