Chit* LumosChitBag::NewGoldChit( const grinliz::Vector3F& pos, Wallet* src )
{
	GLASSERT(src);
	if (src->Gold() == 0) return 0;

	const ChitContext* context = Context();

	Vector2F v2 = { pos.x, pos.z };

	ItemNameFilter goldFilter( ISC::gold);
	this->QuerySpatialHash( &chitList, v2, 1.0f, 0, &goldFilter );
	Chit* chit = 0;

	// Evil bug this: adding gold to a wallet just before
	// deletion. I'm a little concerned where else
	// this could be a problem. Would be nice to make
	// deletion immediate.
	for (int i = 0; i < chitList.Size(); ++i) {
		Chit* c = chitList[i];
		if (c && !IsQueuedForDelete(c) && c->GetWallet() && !c->GetWallet()->Closed()) {
			chit = chitList[i];
			break;
		}
	}

	if ( !chit ) {
		chit = this->NewChit();
		AddItem( "gold", chit, context->engine, 0, 0 );
		chit->Add( new RenderComponent( chit->GetItem()->ResourceName() ));
		chit->SetPosition( pos );
		chit->Add(new GameMoveComponent());
	}
	chit->GetWallet()->Deposit(src, src->Gold());
	return chit;
}
Chit* LumosChitBag::NewMonsterChit(const Vector3F& pos, const char* name, int team)
{
	const ChitContext* context = Context();
	Chit* chit = NewChit();

	AddItem( name, chit, context->engine, team, 0 );

	chit->Add( new RenderComponent( chit->GetItem()->ResourceName() ));
	chit->Add( new PathMoveComponent());
	chit->Add( new AIComponent());

	chit->SetPosition( pos );

	chit->Add( new HealthComponent());

	IString mob = chit->GetItem()->keyValues.GetIString(ISC::mob);
	if (ReserveBank::Instance()) {
		ReserveBank::Instance()->WithdrawMonster(chit->GetWallet(), mob == ISC::greater);
	}
	if (mob == ISC::greater) {
		// Mark this item as important with a destroyMsg:
		chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::GREATER_MOB_CREATED, NewsEvent::GREATER_MOB_KILLED, 0);
	}
	chit->GetItem()->GetTraitsMutable()->Roll(chit->ID());
	chit->GetItem()->FullHeal();

	if (XenoAudio::Instance()) {
		XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos);
	}
	return chit;
}
Chit* LumosChitBag::NewBadGuy(const grinliz::Vector2I& pos, 
							  const IString& name, 
							  const grinliz::IString& type, 
							  int team, int level )
{
	const ChitContext* context = Context();
	Chit* chit = NewChit();
	const GameItem& root = ItemDefDB::Instance()->Get(type.safe_str());

	chit->Add( new RenderComponent(root.ResourceName()));
	chit->Add( new PathMoveComponent());
	AddItem(root.Name(), chit, context->engine, team, level, 0, "human");

	ReserveBank::Instance()->WithdrawMonster(chit->GetWallet(), true);
	chit->GetItem()->GetTraitsMutable()->Roll( random.Rand() );
	chit->GetItem()->GetPersonalityMutable()->Roll( random.Rand(), &chit->GetItem()->Traits() );
	chit->GetItem()->FullHeal();

	chit->GetItem()->SetProperName(name);

	AIComponent* ai = new AIComponent();
	chit->Add( ai );

	chit->Add( new HealthComponent());
	chit->SetPosition( (float)pos.x+0.5f, 0, (float)pos.y+0.5f );

	for (int i = 0; i < ForgeScript::NUM_ITEM_TYPES; ++i) {
		ForgeScript::ForgeData forgeData;
		forgeData.type = i;
		forgeData.subType = 0;
		forgeData.tech = 3;
		forgeData.level = level;
		forgeData.team = team;
		int seed = random.Rand();
		ForgeScript::BestSubItem(&forgeData, seed);

		TransactAmt cost;
		TransactAmt freeCreate;
		static const int CRYSTAL[NUM_CRYSTAL_TYPES] = { 3, 2, 1, 1 };
		freeCreate.Set(0, CRYSTAL);

		GameItem* loot = ForgeScript::ForgeRandomItem(forgeData, freeCreate, &cost, seed, ReserveBank::GetWallet());
		if (loot) {
			chit->GetItemComponent()->AddToInventory(loot);
			loot->SetSignificant(chit->Context()->chitBag->GetNewsHistory(), 
								 ToWorld2F(chit->Position()),
								 NewsEvent::FORGED, NewsEvent::UN_FORGED, chit->GetItem());
		}
	}

	chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::DENIZEN_CREATED, NewsEvent::DENIZEN_KILLED, 0);

	if (XenoAudio::Instance()) {
		Vector3F pos3 = ToWorld3F(pos);
		XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3);
	}
	return chit;
}
Chit* LumosChitBag::NewDenizen( const grinliz::Vector2I& pos, int team )
{
	const ChitContext* context = Context();
	IString itemName;

	switch (Team::Group(team)) {
		case TEAM_HOUSE:	itemName = (random.Bit()) ? ISC::humanFemale : ISC::humanMale;	break;
		case TEAM_GOB:		itemName = ISC::gobman;											break;
		case TEAM_KAMAKIRI:	itemName = ISC::kamakiri;										break;
		default: GLASSERT(0); break;
	}

	Chit* chit = NewChit();
	const GameItem& root = ItemDefDB::Instance()->Get(itemName.safe_str());

	chit->Add( new RenderComponent(root.ResourceName()));
	chit->Add( new PathMoveComponent());

	const char* altName = 0;
	if (Team::Group(team) == TEAM_HOUSE) {
		altName = "human";
	}
	AddItem(root.Name(), chit, context->engine, team, 0, 0, altName);

	ReserveBank::Instance()->WithdrawDenizen(chit->GetWallet());
	chit->GetItem()->GetTraitsMutable()->Roll( random.Rand() );
	chit->GetItem()->GetPersonalityMutable()->Roll( random.Rand(), &chit->GetItem()->Traits() );
	chit->GetItem()->FullHeal();

	IString nameGen = chit->GetItem()->keyValues.GetIString( "nameGen" );
	if ( !nameGen.empty() ) {
		LumosChitBag* chitBag = chit->Context()->chitBag;
		if ( chitBag ) {
			chit->GetItem()->SetProperName(chitBag->NameGen(nameGen.c_str(), chit->ID()));
		}
	}

	AIComponent* ai = new AIComponent();
	chit->Add( ai );

	chit->Add( new HealthComponent());
	chit->SetPosition( (float)pos.x+0.5f, 0, (float)pos.y+0.5f );

	chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::DENIZEN_CREATED, NewsEvent::DENIZEN_KILLED, 0);

	if (XenoAudio::Instance()) {
		Vector3F pos3 = ToWorld3F(pos);
		XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3);
	}

	return chit;
}
Chit* LumosChitBag::NewCrystalChit( const grinliz::Vector3F& _pos, Wallet* src, bool fuzz )
{
	Vector3F pos = _pos;
	if ( fuzz ) {
		pos.x = floorf(_pos.x) + random.Uniform();
		pos.z = floorf(_pos.z) + random.Uniform();
	}

	int crystal = -1;
	for (int i = 0; i < NUM_CRYSTAL_TYPES; ++i) {
		if (src->Crystal(i)) {
			crystal = i;
			break;
		}
	}

	if (crystal == -1) return 0;	// done

	const char* name = 0;
	switch ( crystal ) {
	case CRYSTAL_GREEN:		name="crystal_green";	break;
	case CRYSTAL_RED:		name="crystal_red";		break;
	case CRYSTAL_BLUE:		name="crystal_blue";	break;
	case CRYSTAL_VIOLET:	name="crystal_violet";	break;
	}

	const ChitContext* context = Context();

	Chit* chit = this->NewChit();
	AddItem( name, chit, context->engine, 0, 0 );
	chit->Add( new RenderComponent( chit->GetItem()->ResourceName() ));
	chit->Add(new GameMoveComponent());

	chit->SetPosition( pos );
	
	int c[NUM_CRYSTAL_TYPES] = { 0 };
	c[crystal] = 1;

	chit->GetWallet()->Deposit(src, 0, c);

	return chit;
}