summaryrefslogtreecommitdiff
path: root/npc/functions/marriage.txt
blob: d6d76aafcee278ef0eb7834df5dc44fd6c48db6a (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// marriage npc
// allows to marry other players
// * must be duplicated

-	script	marriage	NPC_NO_SPRITE,{
    end;

    function toPlayer {
        // send a message to the player
        .previous = playerattached();
        // attachrid is required because we want to use l() on another player
        attachrid(getarg(1, getcharid(CHAR_ID_ACCOUNT)));
        dispbottom(b(l(getarg(0))));
        attachrid(.previous);
        return;
    }

    function _officiator {
        // make the wedding officiator say a message
        if (getarg(1, 0)) {
            // only send to the target player
            unittalk(.officiator, l(getarg(0)), true, SELF, getarg(1, getcharid(CHAR_ID_ACCOUNT)));
        } else {
            // regular npctalk
            unittalk(.officiator, getarg(0), true);
        }
        return;
    }

    function officiator {
        if (.officiator) {
            return _officiator(getarg(0));
        } else {
            toPlayer(getarg(0));

            if (getarg(1, 0)) {
                toPlayer(getarg(0), getarg(1));
            }
            return;
        }
    }

    function explain {
        // send an explanation message
        if (.officiator) {
            return _officiator(getarg(0), playerattached());
        } else {
            return toPlayer(getarg(0));
        }
    }

    function do_marriage {
        if (.officiator) {
            // let the officiator handle it
            @marriage$ = getarg(0);
            doevent(.officiator$ + "::OnDoMarriage");
        } else {
            // no officiator
            marriage(getarg(0));
            announce(sprintf("%s and %s are now married!", strcharinfo(PC_NAME), getarg(0)), bc_all);
        }
        return;
    }

OnCheckWord:
    .@verb$ = strtolower($@p1$);
    if (.@verb$ != "marry" && .@verb$ != l("marry")) {
        end; // ignore
    }

    // get the location of the invoking character
    getmapxy(.@map$, .@x, .@y, UNITTYPE_PC);


    if (.@map$ != .map$ || .@x < .x1 || .@x > .x2 || .@y < .y1 || .@y > .y2) {
        explain("You must stand in the designated area");
        end;
    }

    // check if the target character exists and is online
    .@target$ = $@p2$;
    .@target = getcharid(CHAR_ID_ACCOUNT, .@target$);

    if (.@target == 0) {
        explain(sprintf("Player %s doesn't exist or is offline", .@target$));
        end;
    }

    if (.@target == playerattached()) {
        explain("You cannot marry yourself");
        end;
    }

    // get the location of the target character
    getmapxy(.@map$, .@x, .@y, UNITTYPE_PC, .@target);

    if (.@map$ != .map$ || .@x < .x1 || .@x > .x2 || .@y < .y1 || .@y > .y2) {
        explain("Your partner must stand in the designated area");
        end;
    }

    if (BaseLevel < WEDDING_MIN_LEVEL || readparam(BaseLevel, .@target) < WEDDING_MIN_LEVEL) {
        explain(sprintf("You and your partner must be at least level %i to marry", WEDDING_MIN_LEVEL));
        end;
    }

    // XXX: do we want to check for wedding rings?
    if (!isequipped(WeddingRing)) {
        explain("You must be wearing a %s to marry.", getitemname(WeddingRing));
        end;
    }

    if (getpartnerid()) {
        if (getpartnerid() == getcharid(CHAR_ID_CHAR, .@target$)) {
            explain("You are already married to this person.");
        } else {
            explain("You are already married to someone else.");
        }
        end;
    }

    if (getvariableofpc(@marriage, .@target, 0) == getcharid(CHAR_ID_CHAR)) {
        // the target already requested to marry us

        if (getvariableofpc(@marriage[1], .@target, 0) > now()) {
            // You must be wearing a wedding ring at all times
            if (!isequipped(WeddingRing)) {
                explain("You must be wearing a %s to marry.", getitemname(WeddingRing));
                end;
            }
            // marriage request has not expired: procceed to marry
            do_marriage(.@target$);
            end;
        } else {
            // marriage request expired
            explain("This marriage proposal has expired. Please try again");

            // refresh our own expiration
            @marriage = getcharid(CHAR_ID_CHAR, .@target$);
            @marriage[1] = time_from_minutes(+15); // expiration
        }
    } else if (@marriage && @marriage[1] > now()) {
        // we already initiated a marriage request

        if (@marriage == getcharid(CHAR_ID_CHAR, .@target$)) {
            explain("You are already marrying this person.");
            @marriage[1] = time_from_minutes(+15); // refresh expiration
        } else {
            // already marrying someone else?
            explain("You are already in the process of marrying someone else.");
            explain(sprintf("Are you sure you wish to marry %s?", .@target$));
            explain(sprintf("To confirm, type the same message again: %s %s", $@p1$, $@p2$));
            @marriage = 0; // clear the current target
        }
    } else {
        // initiate a new marriage request
        @marriage = getcharid(CHAR_ID_CHAR, .@target$);
        @marriage[1] = time_from_minutes(+15); // expiration

        announce(sprintf("%s is asking %s for marriage!", strcharinfo(PC_NAME), .@target$), bc_all);
        officiator(sprintf("%s, to accept this marriage proposal, say: marry %s", .@target$, strcharinfo(PC_NAME)), .@target);
    }
    end;

OnInit:
    if (strnpcinfo(NPC_MAP) == "") {
        // this is not a duplicate
        end;
    }

    explode(.@hidden$, strnpcinfo(NPC_NAME_HIDDEN), "#");
    explode(.@area$, .@hidden$[getarraysize(.@hidden$) - 1], ",");

    switch (getarraysize(.@area$)) {
    case 1:
        if (.@area$[0] == "") {
            .@d = 3;
        } else {
            .@d = atoi(.@area$[0]);
        }
        .x1 = .x - .@d; .x2 = .x + .@d;
        .y1 = .y - .@d; .y2 = .y + .@d;
        break;
    case 2:
        .@dx = atoi(.@area$[0]); .@dy = atoi(.@area$[1]);
        .x1 = .x - .@dx; .x2 = .x + .@dx;
        .y1 = .y - .@dy; .y2 = .y + .@dy;
        break;
    case 4:
        .x1 = atoi(.@area$[0]); .x2 = atoi(.@area$[2]);
        .y1 = atoi(.@area$[1]); .y2 = atoi(.@area$[3]);
        break;
    default:
        consolemes(CONSOLEMES_ERROR, "unexpected number of arguments for the marriage duplicate");
        break;
    }

    // now find the wedding officiator (if any)
    .@count = getunits(BL_NPC, .@units, false, strnpcinfo(NPC_MAP));

    freeloop(true);
    for (.@i = 0; .@i < .@count; ++.@i) {
        .@name$ = strtolower(strnpcinfo(NPC_NAME_HIDDEN, "", .@units[.@i]));
        .@full$ = strnpcinfo(NPC_NAME, "", .@units[.@i]);

        if (getnpcsubtype(.@units[.@i]) != NPCSUBTYPE_SCRIPT) {
            // getunits() doesn't filter npc subtypes
            continue;
        }

        if (endswith(.@name$, "officiator") ||
            endswith(.@name$, "marriage") ||
            endswith(.@name$, "wedding") ||
            getvariableofnpc(.officiator, .@full$) ||
            getvariableofnpc(.marriage, .@full$) ||
            getvariableofnpc(.wedding, .@full$)) {
            .officiator = .@units[.@i];
            .officiator$ = .@full$;
            break;
        }
    }
    freeloop(false);

    defpattern(.id, "^.+ : (?:##[0-9aBb])?([A-Za-z]+) (.{4,23})$", "OnCheckWord");
    activatepset(.id);
    .distance = 14;
    end;
}

// to use, create a duplicate:
//001-1,30,31,0	duplicate(marriage)	name#x1,y1,x2,y2	NPCID
//001-1,30,31,0	duplicate(marriage)	name#range	NPCID