-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshop.lua
executable file
·447 lines (405 loc) · 12.4 KB
/
shop.lua
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
----------------------------------------------------------------------
-- Eggwars by wilkgr --
-- with additional code by shivajiva101@hotmail.com --
-- Licensed under the AGPL v3 --
-- You MUST make any changes you make open source --
-- even if you just run it on your server without publishing it --
-- Supports a maximum of 8 players per instance and 8 concurrent --
-- instances for a max of 64 players --
----------------------------------------------------------------------
local TF = 'eggwars:trade'
local BF = 'eggwars:bot'
eggwars.shop_items = {}
eggwars.register_shop_item = function(def)
def.entry = #eggwars.shop_items + 1
eggwars.shop_items[def.entry] = def
end
local function build_formspec_items(name, items)
local fsi = {}
local y = 0.5
for i, item in ipairs(items) do
local fs = {
'image[0,',
y,
';1,1;',
item.image,
']',
'tooltip[0,',
y,
';1,1;',
item.name.count,
' ',
item.description,
']',
'label[1.1,',
y + 0.7,
';',
item.name.count,
']',
'item_image_button[2.5,',
y,
';1,1;',
item.cost.name,
';btn_',
i,
';]',
'label[3.6,',
y + 0.7,
';',
item.cost.count,
']'
}
table.insert(fsi, table.concat(fs))
y = y + 1.1
end
return table.concat(fsi)
end
local function build_form(name, items)
local frm = {
"formspec_version[3]",
"size[6,5.5]",
"image_button[5,0.5;0.5,0.5;eggwars_up.png;up;]",
"image_button[5,4.4;0.5,0.5;eggwars_down.png;down;]",
"container[0.5,",
context[name].y,
"]",
}
-- nasty hack to window the container!
local fse = {
"container_end[]",
'box[0.05,0.0;5.9,0.5;#343434FF]',
'box[0.05,4.9;5.9,0.6;#343434FF]',
}
local fstbl = build_formspec_items(name, items)
return table.concat(frm) .. fstbl .. table.concat(fse)
end
local function trader_form_handler(clicker)
local name = clicker:get_player_name()
local key = eggwars.player[name]
local def = eggwars.match[key].player[name]
context[name] = {y=0, val=0}
minetest.show_formspec(name, TF, build_form(name, def.shop_items))
end
local function bot_form_handler(clicker)
local name = clicker:get_player_name()
local key = eggwars.player[name]
local def = eggwars.match[key]
if def.player[name].rate == 1 then return end
local level = 5 - def.player[name].rate
local cost = (level + 1)
local fs = {}
fs[#fs+1] = 'size[2,2]'
fs[#fs+1] = 'label[0.3,0;Level ' .. level + 1 .. ']'
fs[#fs+1] = 'image_button_exit[0.5,0.6;1.0,1.0;eggwars_ruby.png;btn;]'
fs[#fs+1] = 'label[0.5,1.6;x '.. cost .. ']'
minetest.show_formspec(name, BF, table.concat(fs))
end
context = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
-- Only handle this mods forms!
if formname ~= TF and formname ~= BF then return end
local name = player:get_player_name()
-- Only match players have access to these forms!
if not eggwars.player[name] then return end
local key = eggwars.player[name]
local def = eggwars.match[key].player[name]
local pinv = player:get_inventory()
-- Trader form
if formname == TF then
-- buttons
for item, _ in pairs(fields) do
if string.find(item, 'btn_') then
local i = tonumber(string.sub(item, 5))
local row, goods, cost
row = def.shop_items[i]
goods = row.name
cost = row.cost
if pinv:contains_item('main', cost) then
if pinv:room_for_item('main', goods) then
pinv:remove_item('main', cost)
pinv:add_item('main', goods)
else
minetest.chat_send_player(name, 'insufficient room in your inventory!')
end
else
minetest.chat_send_player(name, 'insufficient funds in your inventory!')
end
return
end
end
-- scroll
if formname == TF then
local y = context[name].y
local limit = -4.4 * 5 -- height * number of pages
if fields.up and y ~= 0 then
y = y + 4.4
context[name].y = y
minetest.show_formspec(name, TF, build_form(name, def.shop_items))
elseif fields.down and y > limit then
y = y - 4.4
context[name].y = y
minetest.show_formspec(name, TF, build_form(name, def.shop_items))
end
end
elseif formname == BF and fields.btn then
if def.rate <= 1 then return end -- limit reached
local qty = (5 - def.rate + 1)
local upd = def.rate - 1
local cost = ItemStack('eggwars:ruby ' .. qty)
if pinv:contains_item('main', cost) then
pinv:remove_item('main', cost)
eggwars.match[key].player[name].rate = upd
minetest.get_node_timer(def.spawner):start(upd)
else
minetest.chat_send_player(name, 'insufficient funds in your inventory!')
end
end
end)
minetest.register_entity("eggwars:trader", {
initial_properties = {
physical = false,
collisionbox = {-0.35,-1.0,-0.35, 0.35,0.8,0.35},
visual = "mesh",
mesh = "eggwars_character.b3d",
textures = {"eggwars_trader7.png"},
is_visible = true,
},
on_activate = function(self, staticdata)
local data = minetest.deserialize(staticdata)
self.owner = data.owner
self.nametag = data.nametag
self.tmr = data.tmr or 0
self.uid = data.uid
self.object:set_armor_groups({immortal = 1})
self.object:set_properties({
nametag = self.nametag,
nametag_color = "#FFFF1F",
})
end,
on_rightclick = function(self, clicker)
trader_form_handler(clicker)
end,
on_step = function(self, dtime, ...)
self.tmr = self.tmr + dtime
if self.tmr < 0.25 then return end
self.tmr = 0
local key = eggwars.player[self.owner]
if not key then
self.object:remove()
else
local match = eggwars.match[key]
if match.uid ~= self.uid then
self.object:remove()
end
end
end,
get_staticdata = function(self)
return minetest.serialize({
owner = self.owner,
tmr = self.tmr,
uid = self.uid,
nametag = self.nametag
})
end
})
minetest.register_entity("eggwars:bot", {
initial_properties = {
physical = false,
collisionbox = {-0.25,-0.4,-0.25, 0.25,0.5,0.25},
visual = "mesh",
mesh = "eggwars_character.b3d",
textures = {"eggwars_trader1.png"},
is_visible = true,
visual_size = {x=0.5, y=0.5}
},
on_activate = function(self, staticdata)
local data = minetest.deserialize(staticdata)
self.tmr = data.tmr or 0
self.uid = data.uid
self.owner = data.owner
self.object:set_armor_groups({immortal = 1})
end,
on_rightclick = function(self, clicker)
bot_form_handler(clicker)
end,
on_step = function(self, dtime, ...)
self.tmr = self.tmr + dtime
if self.tmr < 0.25 then return end
self.tmr = 0
local key = eggwars.player[self.owner]
if not key then
self.object:remove()
else
local match = eggwars.match[key]
if match.uid ~= self.uid then
self.object:remove()
end
end
end,
get_staticdata = function(self)
return minetest.serialize({
owner = self.owner,
tmr = self.tmr,
uid = self.uid
})
end
})
-- wooden log
eggwars.register_shop_item({
name = {name="eggwars:log", count=20, wear=0, metadata=""},
description = 'Wood Log',
image = 'default_tree.png',
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
-- obsidian block
eggwars.register_shop_item({
name = {name="default:obsidian_block", count=20, wear=0, metadata=""},
description = 'Obsidian Block',
image = 'default_obsidian_block.png',
cost = {name="eggwars:ruby", count=5, wear=0, metadata=""},
})
-- binoculars
eggwars.register_shop_item({
name = {name="binoculars:binoculars", count=1, wear=0, metadata=""},
description = 'Binoculars',
image = 'binoculars_binoculars.png',
cost = {name="default:gold_ingot", count=5, wear=0, metadata=""},
})
-- Steel sword
eggwars.register_shop_item({
name = {name="default:sword_steel", count=1, wear=0, metadata=""},
description = 'Stone Sword',
image = 'default_tool_steelsword.png',
cost = {name="default:gold_ingot", count=15, wear=0, metadata=""},
})
-- Steel axe
eggwars.register_shop_item({
name = {name="default:axe_steel", count=1, wear=0, metadata=""},
description = 'Steel Axe',
image = 'default_tool_steelaxe.png',
cost = {name="default:gold_ingot", count=15, wear=0, metadata=""},
})
-- Diamond sword
eggwars.register_shop_item({
name = {name="default:sword_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Sword',
image = 'default_tool_diamondsword.png',
cost = {name="default:diamond", count=5, wear=0, metadata=""},
})
-- Diamond pick
eggwars.register_shop_item({
name = {name="default:pick_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Pick',
image = 'default_tool_diamondpick.png',
cost = {name="default:diamond", count=10, wear=0, metadata=""},
})
if eggwars.armor then
-- wood
eggwars.register_shop_item({
name = {name="3d_armor:helmet_wood", count=1, wear=0, metadata=""},
description = 'Wooden Helmet',
image = "3d_armor_inv_helmet_wood.png",
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:chestplate_wood", count=1, wear=0, metadata=""},
description = 'Wooden Chestplate',
image = "3d_armor_inv_chestplate_wood.png",
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:leggings_wood", count=1, wear=0, metadata=""},
description = 'Wooden Leggings',
image = "3d_armor_inv_leggings_wood.png",
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:boots_wood", count=1, wear=0, metadata=""},
description = 'Wooden Boots',
image = "3d_armor_inv_boots_wood.png",
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
--diamond
eggwars.register_shop_item({
name = {name="3d_armor:helmet_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Helmet',
image = "3d_armor_inv_helmet_diamond.png",
cost = {name="default:diamond", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:chestplate_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Chestplate',
image = "3d_armor_inv_chestplate_diamond.png",
cost = {name="default:diamond", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:leggings_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Leggings',
image = "3d_armor_inv_leggings_diamond.png",
cost = {name="default:diamond", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="3d_armor:boots_diamond", count=1, wear=0, metadata=""},
description = 'Diamond Boots',
image = "3d_armor_inv_boots_diamond.png",
cost = {name="default:diamond", count=10, wear=0, metadata=""},
})
end
if eggwars.bows then
-- Bows
eggwars.register_shop_item({
name = {name="bows:bow_wood", count=1, wear=0, metadata=""},
description = 'Wooden Bow',
image = 'bows_bow.png',
cost = {name="default:gold_ingot", count=50, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="bows:bow_steel", count=1, wear=0, metadata=""},
description = 'Steel Bow',
image = 'bows_bow_steel.png',
cost = {name="eggwars:ruby", count=25, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="bows:bow_bowie", count=1, wear=0, metadata=""},
description = 'Bowie Bow',
image = 'bows_bow_bowie.png',
cost = {name="default:diamond", count=35, wear=0, metadata=""},
})
bows.register_arrow("arrow_tp",{
description = "Teleport arrow",
texture = "bows_arrow_wood.png^[colorize:#0000FFcc",
damage = 0,
craft_count = 0,
drop_chance = 99,
on_hit_node = function(self, pos, user, arrow_pos)
if arrow_pos.y > 0 then
user:set_pos(arrow_pos)
end
end,
})
-- Arrows
eggwars.register_shop_item({
name = {name="bows:arrow", count=5, wear=0, metadata=""},
description = 'Wooden Arrow',
image = 'bows_arrow_wood.png',
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="bows:arrow_steel", count=10, wear=0, metadata=""},
description = 'Steel Arrow',
image = 'eggwars_arrow_steel.png',
cost = {name="eggwars:ruby", count=20, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="bows:arrow_diamond", count=10, wear=0, metadata=""},
description = 'Diamond Arrow',
image = 'eggwars_arrow_diamond.png',
cost = {name="default:diamond", count=20, wear=0, metadata=""},
})
eggwars.register_shop_item({
name = {name="bows:arrow_tp", count=5, wear=0, metadata=""},
description = 'Teleport Arrow',
image = 'bows_arrow_wood.png',
cost = {name="default:gold_ingot", count=10, wear=0, metadata=""},
})
end