summaryrefslogtreecommitdiff
path: root/src
Commit message (Expand)AuthorAge
...
* little fixesPerttu Ahola2010-12-14
* added some missing filesPerttu Ahola2010-12-14
* better graphics, zlib to work on vc++Perttu Ahola2010-12-14
* grass edges workin'Perttu Ahola2010-12-14
* in before messing with face drawing orientationPerttu Ahola2010-12-14
* commit before content-tile separationPerttu Ahola2010-12-13
* fixes for vc++Perttu Ahola2010-12-13
* random stuff and different node lightingPerttu Ahola2010-12-13
* working nicelyPerttu Ahola2010-12-13
* starting to separate "material" to "content" and "tile"Perttu Ahola2010-12-12
* removed accidental double flowWaterPerttu Ahola2010-12-11
* commit before some radicallish changes to water behaviorPerttu Ahola2010-12-11
* some work-in-progress water stuffPerttu Ahola2010-12-01
* better waterPerttu Ahola2010-11-30
* doc updatePerttu Ahola2010-11-29
* testPerttu Ahola2010-11-29
* license stuffPerttu Ahola2010-11-29
* license stuff (more to come)Perttu Ahola2010-11-29
* default setting change, random tinkeringPerttu Ahola2010-11-29
* Windows bug fixesPerttu Ahola2010-11-29
* plant amount fix and ravine amount settingPerttu Ahola2010-11-29
* fixed face updating slowness bugPerttu Ahola2010-11-29
* reversed meshbuffer cachePerttu Ahola2010-11-29
* meshbuffer cache test. No much speed improvement.Perttu Ahola2010-11-29
* - added map generating scriptPerttu Ahola2010-11-29
* constant size for array for vcPerttu Ahola2010-11-29
* vokselijuttu lisättyPerttu Ahola2010-11-29
* sitä sun tätä tekeillä, toimii kivastiPerttu Ahola2010-11-29
* block send priority queuePerttu Ahola2010-11-27
* Working version before block send priorization updatePerttu Ahola2010-11-27
* Initial filesPerttu Ahola2010-11-27
262' href='#n262'>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
/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>

This program 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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/*
(c) 2010 Perttu Ahola <celeron55@gmail.com>
*/

#ifndef INVENTORY_HEADER
#define INVENTORY_HEADER

#include <iostream>
#include <sstream>
#include <string>
#include "common_irrlicht.h"
#include "debug.h"
#include "mapblockobject.h"
// For g_materials
#include "main.h"

#define QUANTITY_ITEM_MAX_COUNT 99

class InventoryItem
{
public:
	InventoryItem(u16 count);
	virtual ~InventoryItem();
	
	static InventoryItem* deSerialize(std::istream &is);
	
	virtual const char* getName() const = 0;
	// Shall write the name and the parameters
	virtual void serialize(std::ostream &os) = 0;
	// Shall make an exact clone of the item
	virtual InventoryItem* clone() = 0;
#ifndef SERVER
	// Shall return an image to show in the GUI (or NULL)
	virtual video::ITexture * getImage() { return NULL; }
#endif
	// Shall return a text to show in the GUI
	virtual std::string getText() { return ""; }

	// Shall return true if the item can be add()ed to the other
	virtual bool addableTo(InventoryItem *other)
	{
		return false;
	}
	
	/*
		Quantity methods
	*/
	u16 getCount()
	{
		return m_count;
	}
	void setCount(u16 count)
	{
		m_count = count;
	}
	virtual u16 freeSpace()
	{
		return 0;
	}
	void add(u16 count)
	{
		assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
		m_count += count;
	}
	void remove(u16 count)
	{
		assert(m_count >= count);
		m_count -= count;
	}

protected:
	u16 m_count;
};

class MaterialItem : public InventoryItem
{
public:
	MaterialItem(u8 content, u16 count):
		InventoryItem(count)
	{
		m_content = content;
	}
	/*
		Implementation interface
	*/
	virtual const char* getName() const
	{
		return "MaterialItem";
	}
	virtual void serialize(std::ostream &os)
	{
		//os.imbue(std::locale("C"));
		os<<getName();
		os<<" ";
		os<<(unsigned int)m_content;
		os<<" ";
		os<<m_count;
	}
	virtual InventoryItem* clone()
	{
		return new MaterialItem(m_content, m_count);
	}
#ifndef SERVER
	video::ITexture * getImage()
	{
		/*if(m_content == CONTENT_TORCH)
			return g_texturecache.get("torch_on_floor");

		u16 tile = content_tile(m_content, v3s16(1,0,0));
		return g_tile_contents[tile].getTexture(0);*/
		
		if(m_content >= USEFUL_CONTENT_COUNT)
			return NULL;
			
		return g_irrlicht->getTexture(g_content_inventory_texture_paths[m_content]);
	}
#endif
	std::string getText()
	{
		std::ostringstream os;
		os<<m_count;
		return os.str();
	}

	virtual bool addableTo(InventoryItem *other)
	{
		if(std::string(other->getName()) != "MaterialItem")
			return false;
		MaterialItem *m = (MaterialItem*)other;
		if(m->getMaterial() != m_content)
			return false;
		return true;
	}
	u16 freeSpace()
	{
		if(m_count > QUANTITY_ITEM_MAX_COUNT)
			return 0;
		return QUANTITY_ITEM_MAX_COUNT - m_count;
	}
	/*
		Special methods
	*/
	u8 getMaterial()
	{
		return m_content;
	}
private:
	u8 m_content;
};

class MapBlockObjectItem : public InventoryItem
{
public:
	MapBlockObjectItem(std::string inventorystring):
		InventoryItem(1)
	{
		m_inventorystring = inventorystring;
	}
	
	/*
		Implementation interface
	*/
	virtual const char* getName() const
	{
		return "MBOItem";
	}
	virtual void serialize(std::ostream &os)
	{
		for(;;)
		{
			size_t t = m_inventorystring.find('|');
			if(t == std::string::npos)
				break;
			m_inventorystring[t] = '?';
		}
		os<<getName();
		os<<" ";
		os<<m_inventorystring;
		os<<"|";
	}
	virtual InventoryItem* clone()
	{
		return new MapBlockObjectItem(m_inventorystring);
	}

#ifndef SERVER
	video::ITexture * getImage();
#endif
	std::string getText();

	/*
		Special methods
	*/
	std::string getInventoryString()
	{
		return m_inventorystring;
	}

	MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);

private:
	std::string m_inventorystring;
};

/*
	An item that is used as a mid-product when crafting.
	Subnames:
	- Stick
*/
class CraftItem : public InventoryItem
{
public:
	CraftItem(std::string subname, u16 count):
		InventoryItem(count)
	{
		m_subname = subname;
	}
	/*
		Implementation interface
	*/
	virtual const char* getName() const
	{
		return "CraftItem";
	}
	virtual void serialize(std::ostream &os)
	{
		os<<getName();
		os<<" ";
		os<<m_subname;
		os<<" ";
		os<<m_count;
	}
	virtual InventoryItem* clone()
	{
		return new CraftItem(m_subname, m_count);
	}
#ifndef SERVER
	video::ITexture * getImage()
	{
		std::string basename;
		if(m_subname == "Stick")
			basename = porting::getDataPath("stick.png").c_str();
		// Default to cloud texture
		else
			basename = tile_texture_path_get(TILE_CLOUD);
		
		// Get such a texture
		return g_irrlicht->getTexture(basename);
		//return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
	}
#endif
	std::string getText()
	{
		std::ostringstream os;
		os<<m_count;
		return os.str();
	}
	virtual bool addableTo(InventoryItem *other)
	{
		if(std::string(other->getName()) != "CraftItem")
			return false;
		CraftItem *m = (CraftItem*)other;
		if(m->m_subname != m_subname)
			return false;
		return true;
	}
	u16 freeSpace()
	{
		if(m_count > QUANTITY_ITEM_MAX_COUNT)
			return 0;
		return QUANTITY_ITEM_MAX_COUNT - m_count;
	}
	/*
		Special methods
	*/
	std::string getSubName()
	{
		return m_subname;
	}
private:
	std::string m_subname;
};

class ToolItem : public InventoryItem
{
public:
	ToolItem(std::string toolname, u16 wear):
		InventoryItem(1)
	{
		m_toolname = toolname;
		m_wear = wear;
	}
	/*
		Implementation interface
	*/
	virtual const char* getName() const
	{
		return "ToolItem";
	}
	virtual void serialize(std::ostream &os)
	{