summaryrefslogtreecommitdiff
path: root/tools/petdbconverter.py
blob: 220d545310a5fa38dbdce03246af48fb26a9a27d (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
#! /usr/bin/env python
# -*- coding: utf8 -*-
#
# This file is part of Hercules.
# http://herc.ws - http://github.com/HerculesWS/Hercules
#
# Copyright (C) 2018-2020 Hercules Dev Team
# Copyright (C) 2018 Asheraf
# Copyright (C) 2015 Andrei Karas (4144)
#
# Hercules is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import sys

def isValidEntry(line):
    if re.match('^[0-9]+,.*', line):
        return True
    return False

def curlSplit(line):
    return re.split('[{}]|},', line)

def commaSplit(line):
    return line.split(',')

def printIntField(name, value):
    if int(value) != 0:
        print('\t{0}: {1}'.format(name, value))

def printIntField2(name, value):
    if int(value) != 0:
        print('\t\t{0}: {1}'.format(name, value))

def printStrField(name, value):
    if value != '':
        print('\t{0}: \"{1}\"'.format(name, value))

def printBool(name, value):
    if int(value) != 0:
        print('\t{0}: true'.format(name))

def printIntimacy(arr):
    if int(arr[9]) == 0 or int(arr[10]) == 0 or int(arr[11]) == 0 or int(arr[12]) == 0:
        return
    print('\tIntimacy: {')
    printIntField2('Initial', arr[11])
    printIntField2('FeedIncrement', arr[9])
    printIntField2('OverFeedDecrement', arr[10])
    printIntField2('OwnerDeathDecrement', arr[12])
    print('\t}')

def printScript(name, value):
    if re.match('.*[a-zA-Z0-9,]+.*', value):
        print('\t{0}: <\"{1}\">'.format(name, value))

def printItemName(fieldname, itemid, itemDb):
    value = int(itemid)
    if value != 0:
        if value not in itemDb:
            print("// Error: pet item with id {0} not found in item_db.conf".format(value))
        else:
           printStrField(fieldname, itemDb[value])


def printHeader():
    print("""
pet_db:(
/**************************************************************************
 ************* Entry structure ********************************************
 **************************************************************************
{
    // ================ Mandatory fields ==============================
    Id: ID                               (int)
    SpriteName: "Sprite_Name"            (string)
    Name: "Pet Name"                     (string)
    // ================ Optional fields ===============================
    TamingItem: Taming Item              (string, defaults to 0)
    EggItem: Egg Id                      (string, defaults to 0)
    AccessoryItem: Equipment Id          (string, defaults to 0)
    FoodItem: Food Id                    (string, defaults to 0)
    FoodEffectiveness: hunger points     (int, defaults to 0)
    HungerDelay: hunger time             (int, defaults to 0)
    Intimacy: {
        Initial: start intimacy                   (int, defaults to 0)
        FeedIncrement: feeding intimacy           (int, defaults to 0)
        OverFeedDecrement: overfeeding intimacy   (int, defaults to 0)
        OwnerDeathDecrement: owner die intimacy   (int, defaults to 0)
    }
    CaptureRate: capture rate            (int, defaults to 0)
    Speed: speed                         (int, defaults to 0)
    SpecialPerformance: true/false       (boolean, defaults to false)
    TalkWithEmotes: convert talk         (boolean, defaults to false)
    AttackRate: attack rate              (int, defaults to 0)
    DefendRate: Defence attack           (int, defaults to 0)
    ChangeTargetRate: change target      (int, defaults to 0)
    PetScript: <" Pet Script (can also be multi-line) ">
    EquipScript: <" Equip Script (can also be multi-line) ">
},
**************************************************************************/
    """)

def printFooter():
    print(')\n')

def convertFile(inFile, itemDb):
    if inFile != "" and not os.path.exists(inFile):
        return

    if inFile == "":
        r = sys.stdin
    else:
        r = open(inFile, "r")

    printHeader()
    for line in r:
        if isValidEntry(line) == True:
            print('{')
            firstsplit = curlSplit(line)
            secondsplit = commaSplit(firstsplit[0])
            printIntField('Id', secondsplit[0])
            printStrField('SpriteName', secondsplit[1])
            printStrField('Name', secondsplit[2])
            printItemName('TamingItem', secondsplit[3], itemDb)
            printItemName('EggItem', secondsplit[4], itemDb)
            printItemName('AccessoryItem', secondsplit[5], itemDb)
            printItemName('FoodItem', secondsplit[6], itemDb)
            printIntField('FoodEffectiveness', secondsplit[7])
            printIntField('HungerDelay', secondsplit[8])
            printIntimacy(secondsplit)
            printIntField('CaptureRate', secondsplit[13])
            printIntField('Speed', secondsplit[14])
            printBool('SpecialPerformance', secondsplit[15])
            printBool('TalkWithEmotes', secondsplit[16])
            printIntField('AttackRate', secondsplit[17])
            printIntField('DefendRate', secondsplit[18])
            printIntField('ChangeTargetRate', secondsplit[19])
            printScript('PetScript', firstsplit[1])
            printScript('EquipScript', firstsplit[3])
            print('},')
    printFooter()

def printHelp():
    print("PetDB converter from txt to conf format")
    print("Usage:")
    print("    petdbconverter.py re serverpath dbfilepath")
    print("    petdbconverter.py pre-re serverpath dbfilepath")
    print("Usage for read from stdin:")
    print("    petdbconverter.py re dbfilepath")

def readItemDB(inFile, itemDb):
    itemId = 0
    itemName = ""
    started = False
    with open(inFile, "r") as r:
        for line in r:
            line = line.strip()
            if started == True:
                if line == "},":
                    started = False
                elif line[:10] == "AegisName:":
                    itemName = line[12:-1]
                elif line[:3] == "Id:":
                    try:
                        itemId = int(line[4:])
                    except ValueError:
                        started = False
                if itemId != 0 and itemName != "":
# was need for remove wrong characters
#                    itemName = itemName.replace(".", "")
#                    if itemName[0] >= "0" and itemName[0] <= "9":
#                        itemName = "Num" + itemName
                    itemDb[itemId] = itemName
                    started = False
            else:
                if line == "{":
                    started = True
                    itemId = 0
                    itemName = ""
    return itemDb

if len(sys.argv) != 4 and len(sys.argv) != 3:
    printHelp();
    exit(1)
startPath = sys.argv[2]
if len(sys.argv) == 4:
    sourceFile = sys.argv[3]
else:
    sourceFile = "";

itemDb = dict()
if sys.argv[1] == "re":
    itemDb = readItemDB(startPath + "/db/re/item_db.conf", itemDb)
    itemDb = readItemDB(startPath + "/db/item_db2.conf", itemDb)
elif sys.argv[1] == "pre-re":
    itemDb = readItemDB(startPath + "/db/pre-re/item_db.conf", itemDb)
    itemDb = readItemDB(startPath + "/db/item_db2.conf", itemDb)
else:
    printHelp();
    exit(1)

convertFile(sourceFile, itemDb)