aboutsummaryrefslogtreecommitdiff
path: root/src/nameidmapping.cpp
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2014-04-20 02:40:25 +0200
committersapier <Sapier at GMX dot net>2014-04-22 20:59:01 +0200
commit127f354e7a5e85cfc46304499e7c8619fc9538c8 (patch)
tree61f77a04d87f8b81a072a7e1f3b68003142f331f /src/nameidmapping.cpp
parente7ef4f07cb19aeed768c9010ac49cf5801541ded (diff)
downloadminetest-127f354e7a5e85cfc46304499e7c8619fc9538c8.tar.gz
minetest-127f354e7a5e85cfc46304499e7c8619fc9538c8.tar.bz2
minetest-127f354e7a5e85cfc46304499e7c8619fc9538c8.zip
Fix formspec replacement handling for in game formspecs
Diffstat (limited to 'src/nameidmapping.cpp')
0 files changed, 0 insertions, 0 deletions
37'>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 448
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}}}
}

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.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
		offer[i]={
		give=inv:get_stack("give" .. i,1):get_name(),
		give_count=inv:get_stack("give" .. i,1):get_count(),
		pay=inv:get_stack("pay" .. i,1):get_name(),
		pay_count=inv:get_stack("pay" .. i,1):get_count(),
		}
	end
	return offer
end

smartshop.send_mail=function(owner, pos, item)
   if not minetest.get_modpath( "mail" ) then
      return
   end
   item = ItemStack(item)
   local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
   mail.send("DO NOT REPLY", owner, "Out of stock at "..spos, "Your smartshop at "..spos.." is out of "..core.registered_items[item:get_name()].description..". Please restock")
end


smartshop.receive_fields=function(player,pressed)
		if pressed.customer then
			return smartshop.showform(smartshop.user[player:get_player_name()],player,true)
		elseif pressed.sellall then
			local pos=smartshop.user[player:get_player_name()]
			local meta=minetest.get_meta(pos)
			local pname=player:get_player_name()
			if meta:get_int("sellall")==0 then
				meta:set_int("sellall",1)
				minetest.chat_send_player(pname, "Sell your stock and give line")
			else
				meta:set_int("sellall",0)
				minetest.chat_send_player(pname, "Sell your stock only")
			end
		elseif pressed.tooglelime then
			local pos=smartshop.user[player:get_player_name()]
			local meta=minetest.get_meta(pos)
			local pname=player:get_player_name()
			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 not pressed.quit then
			local n=1
			for i=1,4,1 do
				n=i
				if pressed["buy" .. i] then break end
			end
			local pos=smartshop.user[player:get_player_name()]
			local meta=minetest.get_meta(pos)
			local type=meta:get_int("type")
			local sellall=meta:get_int("sellall")
			local inv=meta:get_inventory()
			local pinv=player:get_inventory()
			local pname=player:get_player_name()
			if pressed["buy" .. n] then
				local name=inv:get_stack("give" .. n,1):get_name()
				local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
				local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
				if name~="" then
					if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owners stock is full, cant receive, exchange aborted.") return end
					-- if type==1 and sellall==1 and inv:contains_item("main", stack)==false and inv:contains_item("give" .. n, stack)==true then
					-- 	inv:add_item("main", stack)
					-- 	inv:remove_item("give" .. n, stack)
					-- end
					if meta:get_int("ghost") ~=1 then
					   -- transition shops to ghost inventory.
					   if inv:room_for_item("main", pay) and inv:room_for_item("main", stack) then
					      meta:set_int("ghost", 1)
					      inv:add_item("main", pay)
					      inv:add_item("main", stack)
					   end
					end
					if type==1 and inv:contains_item("main", stack)==false then
					   minetest.chat_send_player(pname, "Error: The owners stock is end.")
					   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, stack)
					   end
					   return
					end
					if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You dont 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
					pinv:remove_item("main", pay)
					pinv:add_item("main", stack)
					if type==1 then 
						inv:remove_item("main", stack)
						inv:add_item("main", pay)
						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, stack)
						end
					end
				end
			end
		else
			local pos=smartshop.user[player:get_player_name()]
			smartshop.update_info(pos)
			if smartshop.user[player:get_player_name()] or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
				local meta=minetest.get_meta(smartshop.user[player:get_player_name()])
				if meta:get_string("owner")==player:get_player_name() then
					smartshop.update(smartshop.user[player:get_player_name()],"update")
				end
			end
			smartshop.user[player:get_player_name()]=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)
	local meta=minetest.get_meta(pos)
	local inv = meta:get_inventory()
	local owner=meta:get_string("owner")
	local gve=0
	if meta:get_int("sellall")==1 then gve=1 end
	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]=gve*stuff["count" ..i]
		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]=""
		else
			--if string.find(stuff["name" ..i],":")~=nil then
			--	stuff["name" ..i]=stuff["name" ..i].split(stuff["name" ..i],":")[2]
		   --end
		   if core.registered_items[stuff["name"..i]] then
		      stuff["name"..i] = core.registered_items[stuff["name"..i]].description
		   else
		      stuff["name"..i] = "Unknown Object"
		   end
		   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
		.. stuff.buy2 ..  stuff.name2
		.. stuff.buy3 ..  stuff.name3
		.. stuff.buy4 ..  stuff.name4
		)
end




smartshop.update=function(pos,stat)
--clear
	local spos=minetest.pos_to_string(pos)
	for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
		if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
			ob:remove()	
		end
	end
	if stat=="clear" then return end
--update
	local meta=minetest.get_meta(pos)
	local inv = meta:get_inventory()
	local node=minetest.get_node(pos)
	local dp = smartshop.dir[node.param2+1]
	if not dp then return end
	pos.x = pos.x + dp.x*0.01
	pos.y = pos.y + dp.y*6.5/16
	pos.z = pos.z + dp.z*0.01
	for i=1,4,1 do
		local item=inv:get_stack("give" .. i,1):get_name()
		local pos2=smartshop.dpos[node.param2+1][i]
		if item~="" then
			smartshop.tmp.item=item
			smartshop.tmp.pos=spos
			local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
			e:setyaw(math.pi*2 - node.param2 * math.pi/2)
		end
	end
end


minetest.register_entity("smartshop:item",{
	hp_max = 1,
	visual="wielditem",
	visual_size={x=.20,y=.20},
	collisionbox = {0,0,0,0,0,0},
	physical=false,
	textures={"air"},
	smartshop=true,
	type="",
	on_activate = function(self, staticdata)
		if smartshop.tmp.item ~= nil then
			self.item=smartshop.tmp.item
			self.pos=smartshop.tmp.pos
			smartshop.tmp={}
		else
			if staticdata ~= nil and staticdata ~= "" then
				local data = staticdata:split(';')
				if data and data[1] and data[2] then
					self.item = data[1]
					self.pos = data[2]
				end
			end
		end
		if self.item ~= nil then
			self.object:set_properties({textures={self.item}})
		else
			self.object:remove()
		end
	end,
	get_staticdata = function(self)
		if self.item ~= nil and self.pos ~= nil then
			return self.item .. ';' ..  self.pos
		end
		return ""
	end,
})


smartshop.showform=function(pos,player,re)
	local meta=minetest.get_meta(pos)
	local creative=meta:get_int("creative")
	local inv = meta:get_inventory()
	local gui=""
	local spos=pos.x .. "," .. pos.y .. "," .. pos.z
	local owner=meta:get_string("owner")==player:get_player_name()
	if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
	if re then owner=false end
	smartshop.user[player:get_player_name()]=pos
	if owner then
	        meta:set_int("alerted",0) -- Player has been there to refill
		gui=""
		.."size[8,10]"
		.."button_exit[6,0;1.5,1;customer;Customer]"
		.."button[7.2,0;1,1;sellall;All]"
		.."label[0,0.2;Item:]"
		.."label[0,1.2;Price:]"
		.."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
		.."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
		.."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
		.."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
		.."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
		.."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
		.."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
		.."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
		if creative==1 then
			gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
			.."button[6,1;2.2,1;tooglelime;Toggle limit]"
		end
		gui=gui
		.."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
		.."list[current_player;main;0,6.2;8,4;]"
		.."listring[nodemeta:" .. spos .. ";main]"
		.."listring[current_player;main]"
	else
		gui=""
		.."size[8,6]"
		.."list[current_player;main;0,2.2;8,4;]"
		.."label[0,0.2;Item:]"
		.."label[0,1.2;Price:]"
		.."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
		.."item_image_button[2,1;1,1;".. inv:get_stack("pay1",1):get_name() ..";buy1;\n\n\b\b\b\b\b" .. inv:get_stack("pay1",1):get_count() .."]"
		.."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"