summaryrefslogtreecommitdiff
path: root/src/emap/utils/formatutils.c
blob: 57f27cc2fe9c70375769885391c5439943e4cf3c (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
// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// Copyright (c) 2014 - 2015 Evol developers

#include "common/hercules.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "map/script.h"

#include "map/chrif.h"
#include "map/clif.h"
#include "map/npc.h"
#include "map/pc.h"
#include "map/script.h"
#include "map/quest.h"

#include "emap/utils/formatutils.h"
#include "emap/lang.h"

int format_sub(struct script_state* st, int translate)
{
    if (!st)
        return 0;

    TBL_PC *sd = NULL;
    if (translate)
        sd = script->rid2sd(st);

    if (!script_hasdata(st, 3))
    {
        if (sd)
        {
            if (translate == 2)
            {
                char *buf = aCalloc (strlen(script_getstr(st, 2)) + 3, sizeof(char));
                strcpy (buf, script_getstr(st, 2));
                if (sd->status.sex)
                    strcat (buf, "#1");
                else
                    strcat (buf, "#0");
                script_pushstr(st, aStrdup(lang_pctrans(buf, sd)));
                aFree (buf);
            }
            else
            {
                script_pushstr(st, aStrdup(lang_pctrans(script_getstr(st, 2), sd)));
            }
        }
        else
        {
            script_pushstr(st, aStrdup(script_getstr(st, 2)));
        }
        return 1;
    }

    char *line = (char *) aCalloc (550, sizeof (char));
    int idx = 3;
    if (sd)
    {
        if (translate == 2)
        {
            const char *str = NULL;
            char *buf = NULL;
            if (sd->status.sex)
            {
                str = script_getstr(st, 3);
                buf = aCalloc (strlen(str) + 3, sizeof(char));
                strcpy (buf, str);
                strcat (buf, "#1");
            }
            else
            {
                str = script_getstr(st, 2);
                buf = aCalloc (strlen(str) + 3, sizeof(char));
                strcpy (buf, str);
                strcat (buf, "#0");
            }
            strcpy(line, lang_pctrans(buf, sd));
            aFree (buf);
            idx = 4;
        }
        else
        {
            strcpy(line, lang_pctrans(script_getstr(st, 2), sd));
        }
    }
    else
    {
        strcpy(line, script_getstr(st, 2));
    }

    char *ptr = line;
    int sz = strlen(line);
    while (script_hasdata(st, idx))
    {
        char *tmp = strstr(ptr, "@@");
        if (!tmp)
            break;
        const char *item = script_getstr(st, idx);
        int len = strlen(item);
        if (len > 50)
            break;
        sz += len - 2;
        if (sz > 490)
            break;
        memmove(tmp + len, tmp + 2, strlen(tmp + 2) + 1);
        memcpy(tmp, item, len);
        ptr = tmp + len;
        idx ++;
    }

    script_pushstr(st, line);
    return 0;
}