aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_async.h
diff options
context:
space:
mode:
authorSmallJoker <mk939@ymail.com>2019-07-27 20:55:38 +0200
committerSmallJoker <mk939@ymail.com>2019-07-27 21:13:47 +0200
commit8efa1de8356630f2943e3d74e7287b5e976e8706 (patch)
treec3f541a68bd8e65a5257c6d6b32520a0bb54bcc5 /src/script/cpp_api/s_async.h
parentfe6c666da26e08b726bd21f34d25558cf456bd1f (diff)
downloadminetest-8efa1de8356630f2943e3d74e7287b5e976e8706.tar.gz
minetest-8efa1de8356630f2943e3d74e7287b5e976e8706.tar.bz2
minetest-8efa1de8356630f2943e3d74e7287b5e976e8706.zip
Check for 'action' field in ABMs & LBMs
Diffstat (limited to 'src/script/cpp_api/s_async.h')
0 files changed, 0 insertions, 0 deletions
f='#n146'>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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,y=0,z=0}},dpos={
{{x=0.2,y=0.2,z=0},{x=-0.2,y=0.2,z=0},{x=0.2,y=-0.2,z=0},{x=-0.2,y=-0.2,z=0}},
{{x=0,y=0.2,z=0.2},{x=0,y=0.2,z=-0.2},{x=0,y=-0.2,z=0.2},{x=0,y=-0.2,z=-0.2}},
{{x=-0.2,y=0.2,z=0},{x=0.2,y=0.2,z=0},{x=-0.2,y=-0.2,z=0},{x=0.2,y=-0.2,z=0}},
{{x=0,y=0.2,z=-0.2},{x=0,y=0.2,z=0.2},{x=0,y=-0.2,z=-0.2},{x=0,y=-0.2,z=0.2}}}
}

-- table with itemname: number of items being traded
smartshop.itemstats = {}
smartshop.itemprices = {}
smartshop.stuffsold = {}



smartshop.itemsatpos = function(pos, item, count)
   -- set number of items of type 'item' sold at position 'pos'
   if smartshop.itemstats[item] == nil then
      smartshop.itemstats[item] = {}
   end
   smartshop.itemstats[item][pos] = count
   local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "w")
   if file then
      file:write(minetest.serialize(smartshop.itemstats))
      file:close()
   end
end

smartshop.itempriceatpos = function(pos, item, price)
   -- set number of items of type 'item' sold at position 'pos'
   if smartshop.itemprices[item] == nil then
      smartshop.itemprices[item] = {}
   end
   local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "w")
   if file then
      file:write(minetest.serialize(smartshop.itemprices))
      file:close()
   end
   smartshop.itemprices[item][pos] = price
end

smartshop.minegeldtonumber = function(stack)
   -- return number of minegeld in stack, returns nil if stack is not composed of minegeld
   count = stack:get_count()
   if count == 0 then
      return 0
   end
   if stack:get_name() == "currency:minegeld" then
      return count
   elseif stack:get_name() == "currency:minegeld_5" then
      return count * 5
   elseif stack:get_name() == "currency:minegeld_10" then
      return count * 10
   elseif stack:get_name() == "currency:minegeld_50" then
      return count * 50
   else
      return nil
   end
end


minetest.register_craft({
	output = "smartshop:shop",
	recipe = {
		{"default:chest_locked", "default:chest_locked", "default:chest_locked"},
		{"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"},
		{"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},
	}
})
smartshop.get_human_name = function(item)
   if core.registered_items[item] then
      return core.registered_items[item].description
   else
      return "Unknown Item"
   end
end

smartshop.use_offer=function(pos,player,n)
	local pressed={}
	pressed["buy" .. n]=true
	smartshop.user[player:get_player_name()]=pos
	smartshop.receive_fields(player,pressed)
	smartshop.user[player:get_player_name()]=nil
	smartshop.update(pos)
end

smartshop.get_offer=function(pos)
	if not pos or not minetest.get_node(pos) then return end
	if minetest.get_node(pos).name~="smartshop:shop" then return end
	local meta=minetest.get_meta(pos)
	local inv=meta:get_inventory()
	local offer={}
	for i=1,4,1 do
		local pay_stack = inv:get_stack("pay" .. i,1);
		local give_stock = 0;
		offer[i]={
			give=inv:get_stack("give" .. i,1):get_name(),
			give_count=inv:get_stack("give" .. i,1):get_count(),
			pay=pay_stack:get_name(),
			pay_count=pay_stack:get_count(),
			stock = 0,		-- times customer can purchase before sold out
			pay_price = 0,	-- average minegeld price of sold item, nonzero for minegeld-taking selling shop
		}
		local mg_price = smartshop.minegeldtonumber(pay_stack)
		if mg_price ~= nil then
			offer[i].pay_price = mg_price/offer[i].give_count
		end
		for ii=1,32,1 do
			local name=inv:get_stack("main",ii):get_name()
			local count=inv:get_stack("main",ii):get_count()
			if name==offer[i].give then
				give_stock = give_stock + count
			end
		end
		offer[i].stock = math.floor(give_stock/offer[i].give_count)
	end
	return offer
end

smartshop.send_mail=function(owner, pos, item, pname)
   if not minetest.get_modpath( "mail" ) then
      return
   end
   local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
   mail.send("DO NOT REPLY", owner, "Out of "..smartshop.get_human_name(item).." at "..spos, "Your smartshop at "..spos.." is out of "..smartshop.get_human_name(item)..". Please restock! Thanks, " .. pname)
end

local function is_creative(pname)
	return minetest.check_player_privs(pname, {creative=true}) or minetest.check_player_privs(pname, {give=true})
end

smartshop.receive_fields=function(player,pressed)
	local pname = player:get_player_name()
	local pos = smartshop.user[pname]
	if not pos then
		return
	end
		if pressed.customer then
			return smartshop.showform(pos, player, true)
		elseif pressed.tooglelime then
			local meta=minetest.get_meta(pos)
			if not is_creative(pname) then
				meta:set_int("type", 1)
				meta:set_int("creative", 0)
				minetest.chat_send_player(pname, "You are not allowed to make a creative shop!")
				return
			end
			if meta:get_int("type")==0 then
				meta:set_int("type",1)
				minetest.chat_send_player(pname, "Your stock is limited")
			else
				meta:set_int("type",0)
				minetest.chat_send_player(pname, "Your stock is unlimited")
			end
		elseif pressed.channel then
			local meta=minetest.get_meta(pos)
			meta:set_string("channel",pressed.channel)
		elseif not pressed.quit then
			local n=1
			for i=1,4,1 do
				n=i
				if pressed["buy" .. i] then break end
			end
			local meta=minetest.get_meta(pos)
			local type=meta:get_int("type")
			local inv=meta:get_inventory()
			local pinv=player:get_inventory()
			if pressed["buy" .. n] then
				local stack=inv:get_stack("give" .. n,1)
				local name=stack:get_name()
				local pay=inv:get_stack("pay" .. n,1)
				if name~="" then
					if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owner's stock is full, can't receive, exchange aborted.") return end
					if meta:get_int("ghost") ~=1 then
					   -- transition shops to ghost inventory.
					   for i=1,4 do
					      if inv:room_for_item("main", "pay"..i) and inv:room_for_item("main", "give"..i) then
								meta:set_int("ghost", 1)
								inv:add_item("main", inv:get_stack("pay"..i,1))
								inv:add_item("main", inv:get_stack("give"..i,1))
					      end
					   end
					end
					if type==1 and inv:contains_item("main", stack)==false then
					   minetest.chat_send_player(pname, "Error: "..smartshop.get_human_name(name).." is sold out.")
					   if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
					      meta:set_int("alerted",1) -- Do not alert twice
					      smartshop.send_mail(meta:get_string("owner"), pos, name, pname)
					   end
					   return
					end
					if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You don't have enough in your inventory to buy this, exchange aborted.") return end
					if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
					if type == 0 then
						pinv:remove_item("main", pay)
						pinv:add_item("main", stack)
					else
						local item = inv:remove_item("main", stack)
						pinv:add_item("main", item)
						item = pinv:remove_item("main",pay)
						inv:add_item("main", item)
						if not inv:contains_item("main", stack)  and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
						   meta:set_int("alerted",1) -- Do not alert twice
						   smartshop.send_mail(meta:get_string("owner"), pos, name, pname)
						end
					end
				end
			end
		else
			smartshop.update_info(pos)
			local meta = minetest.get_meta(pos)
			if meta:get_string("owner") == pname then
				smartshop.update(pos, "update")
			end
			smartshop.user[pname] = nil
		end
end

minetest.register_on_player_receive_fields(function(player, form, pressed)
	if form=="smartshop.showform" then
		smartshop.receive_fields(player,pressed)
	end
end)




smartshop.update_info=function(pos)
        if not pos then
	   return
        end
	local meta=minetest.get_meta(pos)
	local spos=minetest.pos_to_string(pos)
	local inv = meta:get_inventory()
	local owner=meta:get_string("owner")
	if meta:get_int("type")==0 then
		meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
		return false
	end
	local name=""
	local count=0
	local stuff={}
	for i=1,4,1 do
		stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
		stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
		stuff["stock" ..i]=0 -- stuff["count" ..i]
		local mg_price = smartshop.minegeldtonumber(inv:get_stack("pay" .. i,1))
		if mg_price ~= nil then
		   stuff["pay"..i] = mg_price/stuff["count" ..i]
		end
		stuff["buy" ..i]=0
		for ii=1,32,1 do
			name=inv:get_stack("main",ii):get_name()
			count=inv:get_stack("main",ii):get_count()
			if name==stuff["name" ..i] then
				stuff["stock" ..i]=stuff["stock" ..i]+count
			end
		end
		local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
		nstr=nstr.split(nstr, ".")
		stuff["buy" ..i]=tonumber(nstr[1])
		if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
			stuff["buy" ..i]=""
			stuff["name" ..i]=""
			if smartshop.stuffsold[spos..i] then
			   smartshop.itemsatpos(spos, smartshop.stuffsold[spos..i], 0)
			   smartshop.itempriceatpos(spos, smartshop.stuffsold[spos..i], nil)
			   smartshop.stuffsold[spos..i] = nil
			end						   
		else
		   smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])
		   smartshop.itempriceatpos(spos, stuff["name"..i], stuff["pay"..i])
		   smartshop.stuffsold[spos..i] = stuff["name"..i]
		   stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])
		   stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
		   stuff["name" ..i]=stuff["name" ..i] .."\n"
		end
	end
		meta:set_string("infotext",
		"(Smartshop by " .. owner ..") Purchases left:\n"
		.. stuff.buy1 ..  stuff.name1