summaryrefslogtreecommitdiff
path: root/npc/functions/libquest.txt
blob: cb5996dc7d418cbdad1b7205c6f095ee365138f2 (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
// Evol scripts for simplified quest development.
// Author:
//    Livio

/*
    @brief Prints the list of quest ingredients on the NPC window
    @param Array with items IDs
    @param Array with relative amount required
    @returns nothing
*/
function	script	printIngredients	{
    for (.@i = 0; .@i < getarraysize(getarg(0)); .@i++) {
        mesf("%d/%d %s", countitem(getelementofarray(getarg(0), .@i)), getelementofarray(getarg(1), .@i), getitemlink(getelementofarray(getarg(0), .@i)));
    }
    return;
}

/*
    @brief Checks if player has items required
    @param Array with required items IDs
    @param Array with relative amount required
    @returns false if player doesn't have required items
*/
function	script	checkForItems	{
    for (.@i = 0; .@i < getarraysize(getarg(0)); .@i++) {
        // If even a single thing is missing abort immediately
        if(getelementofarray(getarg(1), .@i) > countitem(getelementofarray(getarg(0), .@i))) {
            return false;
        }
    }
    return true;
}

/*
    @brief Craft an item from some other items. All input items got deleted.
    @param Array with required items IDs
    @param Array with relative amount required
    @param output item IDs
    @param output amount
    @returns 0 if successful, 1 if player lack ingredients, 2 if overburdened, 255 code error
*/
function	script	craftFromPlayer	{
    // Check input parameter amount
    if (getargcount() != 4) return 255;

    // Check item amounts
    if (!checkForItems(getarg(0), getarg(1))) return 1;

    // Check if player is able to carry output from crafting
    if (!checkweight(getarg(2), getarg(3))) return 2;
    else getitem(getarg(2), getarg(3));

    // Delete Items from player inventory
    for (.@i = getarrayindex(getarg(0)); .@i < getarraysize(getarg(0)); .@i++) {
        delitem(getelementofarray(getarg(0), .@i), getelementofarray(getarg(1), .@i));
    }

    return 0;
}

/*
    @brief Craft an item from some other items. All input items will be deleted in case of success.
    @param Array with required items IDs
    @param Array with relative amount required
    @param output item IDs
    @param output amount
    @param NPC question about ingredients
    @param NPC answer if successful
    @param NPC answer if player lacks ingredients
    @param NPC answer if player is overburdened
    @param NPC answer if player doesn't accept
    @returns true if successful
    @note returns false in case of wrong parameter count
*/
function	script	NPCcrafting	{
    // Check input parameter amount
    if (getargcount() != 9) return false;
    speech getarg(4);
    printIngredients(getarg(0), getarg(1));
    if (askyesno() == ASK_NO) {
        mesq getarg(8);
        close;
    } else {
        switch(craftFromPlayer(getarg(0), getarg(1), getarg(2), getarg(3))) {
            case 0:
                mesq getarg(5);
                return true;
                break;

            case 1:
                mesq getarg(6);
                // FIXME - Gather proper variables
                printIngredients(getarg(0), getarg(1));
                break;

            case 2:
                mesq getarg(7);
                break;

            case 255:
                mesq l("[BUG ENCOUNTERED] Dammit...");
                break;

            default:
        }
    }
    return false;
}