/
get.c
159 lines (141 loc) · 4.6 KB
/
get.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//*****************************************************************************
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
inherit "/lib/commands/baseCommand.c";
private string TargetString;
/////////////////////////////////////////////////////////////////////////////
public nomask void reset(int arg)
{
if (!arg)
{
addCommandTemplate("get [-a] [##Item##] [from ##Target##]");
addCommandTemplate("pick up [-a] ##Item##");
}
}
/////////////////////////////////////////////////////////////////////////////
private nomask object *getAll(object initiator, object source)
{
object *targets = filter(all_inventory(source),
(: $1->get() :));
if (!sizeof(targets))
{
notify_fail("There is nothing to get.\n");
}
return targets;
}
/////////////////////////////////////////////////////////////////////////////
private nomask object *getAllOfSpecificId(object initiator, object source)
{
object *targets = filter(all_inventory(source),
(: $1->get() && $1->id(TargetString) :));
if (!sizeof(targets))
{
notify_fail(sprintf("Could not find any '%s' to get.\n",
TargetString));
}
return targets;
}
/////////////////////////////////////////////////////////////////////////////
private nomask object *getSpecificItem(object initiator, object source, string targetString)
{
object *targets = ({});
object target = present(targetString, source) ||
present(targetString, initiator);
if (target)
{
if (target->get())
{
targets += ({ target });
}
else
{
notify_fail("You cannot get that.\n");
}
}
else
{
notify_fail(sprintf("Could not find '%s'.\n", TargetString));
}
return targets;
}
/////////////////////////////////////////////////////////////////////////////
private nomask int getObjects(object initiator, object source, object *targets)
{
int ret = 0;
if (sizeof(targets))
{
ret = 1;
foreach(object target in targets)
{
if (function_exists("canGet", source) &&
!source->canGet(target))
{
tell_object(initiator, "You cannot get that.\n");
}
else if (initiator->canCarry(target))
{
move_object(target, initiator);
displayMessage("##InitiatorName## ##Infinitive::pick## up " +
target->query("name") + ".\n", initiator);
}
else
{
tell_object(initiator, "You cannot carry any more weight.\n");
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private object getSource(object initiator, string command)
{
object source = environment(initiator);
if (sizeof(regexp(({ command }), "from .+")))
{
string from = regreplace(command, ".*from (.+)", "\\1", 1);
TargetString = regreplace(TargetString, "(.+) from .*", "\\1", 1);
source = present(from, environment(initiator)) ||
present(from, initiator);
if (!source && environment(initiator)->isEnvironmentalElement(from))
{
source = environment(initiator)->getEnvironmentalElement(from);
}
if (source && ((function_exists("isContainer", source) &&
!source->isContainer()) || source->isRealizationOfLiving()))
{
source = 0;
notify_fail("The 'from' item is not a valid container.\n");
}
}
return source;
}
/////////////////////////////////////////////////////////////////////////////
public nomask int execute(string command, object initiator)
{
int ret = 0;
if (canExecuteCommand(command))
{
object *targets = ({});
TargetString = getTargetString(initiator, command);
object source = getSource(initiator, command);
if (source)
{
if (TargetString == "all")
{
targets += getAll(initiator, source);
}
else if (sizeof(regexp(({ command }), "-a")))
{
targets += getAllOfSpecificId(initiator, source);
}
else
{
targets += getSpecificItem(initiator, source, TargetString);
}
}
targets -= ({ 0 });
ret = getObjects(initiator, source, targets);
}
return ret;
}