Atrinik Server  4.0
apply.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <global.h>
33 #include <toolkit/string.h>
34 #include <player.h>
35 #include <object.h>
36 
46 static object *find_best_object_match(object *pl, const char *params)
47 {
48  object *tmp, *best;
49  int match_val, tmpmatch;
50 
51  if (!params || *params == '\0') {
52  return NULL;
53  }
54 
55  best = NULL;
56  match_val = 0;
57 
58  for (tmp = pl->inv; tmp; tmp = tmp->below) {
59  if (IS_INVISIBLE(tmp, pl)) {
60  continue;
61  }
62 
63  tmpmatch = object_matches_string(tmp, pl, params);
64 
65  if (tmpmatch > match_val) {
66  match_val = tmpmatch;
67  best = tmp;
68  }
69  }
70 
71  return best;
72 }
73 
75 void command_apply(object *op, const char *command, char *params)
76 {
77  enum apply_flag aflag;
78  size_t pos;
79  char word[MAX_BUF];
80  object *ob;
81 
82  if (!params) {
84  return;
85  }
86 
87  aflag = 0;
88  pos = 0;
89 
90  if (string_get_word(params, &pos, ' ', word, sizeof(word), 0)) {
91  if (strcmp(word, "-a") == 0) {
92  aflag = APPLY_ALWAYS;
93  } else {
94  pos = 0;
95  }
96  }
97 
98  if (string_get_word(params, &pos, ' ', word, sizeof(word), 0)) {
99  if (strcmp(word, "-u") == 0) {
100  aflag = APPLY_ALWAYS_UNAPPLY;
101  } else {
102  pos = 0;
103  }
104  }
105 
106  ob = find_best_object_match(op, params + pos);
107 
108  if (ob) {
109  player_apply(op, ob, aflag, 0);
110  } else {
111  draw_info(COLOR_WHITE, op, "Could not find any matching item.");
112  }
113 }
int object_matches_string(object *op, object *caller, const char *str)
Definition: object.c:2676
Always apply, never unapply.
Definition: define.h:1490
Always unapply, never apply.
Definition: define.h:1491
void player_apply_below(object *pl)
Definition: apply.c:161
struct obj * below
Definition: object.h:114
#define IS_INVISIBLE(__ob_, __player_)
Definition: define.h:1356
apply_flag
Definition: define.h:1487
void command_apply(object *op, const char *command, char *params)
Definition: apply.c:75
static object * find_best_object_match(object *pl, const char *params)
Definition: apply.c:46
struct obj * inv
Definition: object.h:123
int player_apply(object *pl, object *op, int aflag, int quiet)
Definition: apply.c:123