« Urban Terror Equipments | メイン | Conception of mds/mdm/mdx importer »

April 22, 2005

■ W:ETソースコード朗読会 補足2

Reading the Source Code of W:ET Appendix #2

The world of an 'Entity'

みなさんこんにちは。挨拶しても恐らく誰もここを見ていないのが寂しいところです。しかしめげずに今回もソースコードを読んでいきます。

この回で取り上げるのは実際の処理部分ではなく、entityという構造体です。Quake3のコード提供を受けて開発されたゲームのほぼ全てはこの構造体も継承しています。それだけ重要な部分ということですから、ソースコードを読む立場の我々もこれを知っておく必要があるでしょう。

An Entity

entityという英単語はなかなか目にしません。辞書で意味を調べると、「(実在する)物;(他から独立した)存在(物)」と、実にあいまいな記述にとどまっています。字面をそのまま解釈すれば、要するに何か「もの」を指す便利な言葉で、thingに近いのではないでしょうか。

例えばあなたが手榴弾を投げますと、gentity_tという型を持ったg_entities配列の中の使われていない一つが選択され、手榴弾の情報が書き込まれます。書き込まれる内容は、移動速度、向き、投げた人、ヘルス、弾性、大きさ、見た目、炸裂した時の音、ダメージ量、炸裂範囲、爆発するまでの時間、爆発時間に達した時に呼び出される関数などなど、この手榴弾に関するあらゆる情報が格納されます。

武器に限らず、ゲーム中に登場するほぼ全てのものがg_entitiesによって管理制御されています。プレイヤーもその一つです(厳密にはクライアントはentityに加えてplayerstate_tなどを含むgclient_tによって制御されます)。

結局のところ、entityとはつまり何でもありで、とにかくゲーム中登場する全てのものを一元管理するアイデアの下に作られた構造体であるものでしょう。そこはカーマックさんに尋ねてみないと真実はわかりません。しかし、拡張性の高いシステムであることには間違いないので、さすがはカーマックさんといったところです。スーパープログラマーとはかくありきですね。

A Structure 'entity_t'

概念的なことばかり紹介してもイメージが掴みにくいかもしれませんので、実際どのように定義されているのかを見てゆきましょう。gentity_t構造体の定義はg_local.hにあります。213行以降です。g_entitiesの宣言はg_main.cの最初あたりにあります。まずはg_main.cからみましょう。

gentity_t		g_entities[MAX_GENTITIES];

冒頭近傍でこのように宣言されているかと思います。ちなみにMAX_GENTITIESは別のところで定義されております。

#define	GENTITYNUM_BITS		10
#define	MAX_GENTITIES		(1<<GENTITYNUM_BITS)

なぜ定数定義にビット演算を用いているのか甚だ疑問ではございますが、結局MAX_GENTITIESは2の10乗、つまり1024です。つまり1024個のentityを同時に扱うことが出来るわけです。

ではgentity_tの定義をみてみましょう。

struct gentity_s {
	entityState_t	s;				// communicated by server to clients
	entityShared_t	r;				// shared by both the server system and game
	// DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER
	// EXPECTS THE FIELDS IN THAT ORDER!
	//================================
	struct gclient_s	*client;			// NULL if not a client
	qboolean	inuse;
	
	(省略)

	// bleh - ugly
	int		backupWeaponTime;
	int		mg42weapHeat;
	vec3_t	oldOrigin;
	qboolean runthisframe;
	g_constructible_stats_t	constructibleStats;
};

かなり大きく複雑な構造体になっております。もしC++で組まれていたらもう少しスマートなクラスとして定義できたかもしれませんね。

まずは最初の部分をみてみましょう。

	entityState_t	s;				// communicated by server to clients
	entityShared_t	r;				// shared by both the server system and game

注釈を見るとわかるとおり、サーバー通信機能とゲーム処理部分によってsとrが区別されています。しかしながらどちらもentityの基本的な情報を含んでいることには変わりません。具体的には位置、速度、向き、種類、大きさ、イベントなどなど。サーバーとクライアントでやり取りされている部分でもあります。

	struct gclient_s	*client;			// NULL if not a client
	qboolean	inuse;
	vec3_t		instantVelocity;	// ydnar: per entity instantaneous velocity, set per frame
	char		*classname;			// set in QuakeEd
	int			spawnflags;			// set in QuakeEd

大抵のものには注釈がついており、ほとんどの場合その通りの使われ方をしています。clientはentityがクライアントかどうかについてのもので、もしそうならクライアント情報を含むアドレス、そうでないなら0となります。inuseはこのentityが使用中かどうかのフラグです。instantVelociityは注釈どおりフレームにおける瞬間速度です。classnameとspawnflagはマップオブジェクトに対して定義され、マップエディタ中で設定した値がここに入ります。

これ以外もentityの性質、状態、動きに関するデータが並んでいます。この中で注目すべきはイベント発生に関する部分です。

	int		nextthink;
	void		(*free)(gentity_t *self);
	void		(*think)(gentity_t *self);
	void		(*reached)(gentity_t *self);	// movers call this when hitting endpoint
	void		(*blocked)(gentity_t *self, gentity_t *other);
	void		(*touch)(gentity_t *self, gentity_t *other, trace_t *trace);
	void		(*use)(gentity_t *self, gentity_t *other, gentity_t *activator);
	void		(*pain)(gentity_t *self, gentity_t *attacker, int damage, vec3_t point);
	void		(*die)(gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod);

nextthinkは設定されたイベントが発生するまでの時間で、ミリ秒が単位です。以下に並ぶのが各イベントが発生した場合に呼び出される関数のアドレスです。freeはこのentityが開放される、つまりinuseになる時にコールされます。thinkは設定した時刻が経過した場合に呼び出されます。手榴弾が5秒後に爆発するのはこのためです。その他各イベントにも対応した処理が自動的に行われます。なかなか興味深い部分です。

具体的な例として、g_weapon.cの2237行に空爆缶に関するコードがありますので見てみましょう。

void weapon_checkAirStrikeThink1( gentity_t *ent ) {
	if( !weapon_checkAirStrike( ent ) ) {
		ent->think = G_ExplodeMissile;
		ent->nextthink = level.time + 1000;
		return;
	}

	ent->think = weapon_callAirStrike;
	ent->nextthink = level.time + 1500;
}

関数の引数としてentというentityが渡されます。これは空爆缶を指しているentityです。if文は空爆を行うことが出来る条件のもとに空爆缶が投げられたかを見ています。そうでないならこの空爆缶は空爆をせずにG_ExplodeMissileという単に爆発するだけの処理をlevel.time + 1000ミリ秒後、つまり一秒後に行います。条件が満たされた場合、空爆缶は空爆処理を行うweapon_callAirStrikeを1.5秒後に呼び出します。

Entity is Everything

これまでみたように、entityとはつまりなんでもありということが理解してもらえたかと思います。またデータでなくイベント自動処理に関する項目も含まれており、いかにゲーム処理アルゴリズムの大きなところを担っているかもお分かりなったのではないでしょうか。

今回紹介した部分以外にもコードに登場する構造体はたくさんあります。クライアント、アイテム、武器、マップ、コンフィグなどなど色々なものに構造体が定義されています。新しくMODを作る場合にはこれらの理解が欠かせませんので、そのおつもりのある場合は是非一見なさることをお勧めします。

投稿者 ikanatto : April 22, 2005 06:11 PM

■ コメント

Undisciplined spenders who fall behind in paying off the home equity loans could lose their houses. Both here 1.0 ctand there trillian diamonds emerald cut diamond engagement ring with baguettes [URL=http://websearch.cs.com/cs/search?query=natalia+ds+017] emerald cut diamond engagement ring with baguettes[/URL] http://websearch.cs.com/cs/search?query=natalia+ds+017 Commission Building Line or Setback Recission

投稿者 emerald cut diamond engagement ring with baguettes : October 16, 2006 03:31 PM

Recent trends support the idea that homeowners are realizing this and are refinancing to take advantage of today's lower rates while they are still available. "LowLender is the perfect option for people who want to cut out the legwork. Both here diamond earrings channel hoops white goldand there oval diamonds clarity si1 enhanced [URL=http://search.abacho.com/se/abacho.se/index.cfm?country=se&q=natalia+rs+025] clarity si1 enhanced[/URL] http://search.abacho.com/se/abacho.se/index.cfm?country=se&q=natalia+rs+025 Eviction Appreciation Multi Family Mortgage

投稿者 clarity si1 enhanced : October 17, 2006 03:08 AM

Another reason to refinance is to get out of an adjustable-rate mortgage, and lock in a lower rate long-term. Both here debt loansand there debt assistance reduce debt http://oldweb.uwp.edu/academic/business/news/articles/reduce-debt.html Common Areas Loan-to-Value Ratio Fully Extended Accrual Rate

投稿者 reduce debt : October 20, 2006 01:55 AM

Debt and debt-consolidation strategies go together in the American economy like peanut butter and jelly. Both here paying off debtand there debt creditor debt collections http://oldweb.uwp.edu/academic/business/news/articles/debt-collections.html Contract of Purchase Federal National Mortgage Association Certificate of Deposit

投稿者 debt collections : October 20, 2006 02:15 AM

Paying one debt at one rate usually trumps eight credit cards at varying rates in the debt game. REALTOR ASSOCIATE http://oldweb.uwp.edu/academic/business/news/articles/allowance-for-bad-debt.html Holographic Will Power of Attorney GPM

投稿者 REALTOR ASSOCIATE : October 21, 2006 04:46 AM

When you consolidate with us you are eligible for their Borrower Benefits Package, which can reduce your interest rate by an additional 1.25 percent. Devisor http://cc.ee.ntu.edu.tw/~tim/1/Student-Loans-With-Bad-Credit.html Power of Attorney First Mortgage Qualifying Ratios

投稿者 Devisor : October 22, 2006 09:28 PM

Recent trends support the idea that homeowners are realizing this and are refinancing to take advantage of today's lower rates while they are still available. "LowLender is the perfect option for people who want to cut out the legwork. Gross Monthly Income http://oldweb.uwp.edu/academic/business/news/articles/debt-facts.html Eminent Domain American Land Title Association FNMA

投稿者 Gross Monthly Income : October 24, 2006 12:30 PM

If you currently pay, say, 6 percent for the mortgage and can get a total refi at 6.5 percent, it might be a much better deal than keeping a small balance first mortgage at 6 percent and getting a second loan at a much higher interest rate. Common Areas http://cc.ee.ntu.edu.tw/~tim/1/index.html Real Estate Tax Installment Statute of Fraud

投稿者 Common Areas : October 25, 2006 11:38 PM

Do you end every month with barely enough cash to pay the bills, and sometimes have to tap into your savings to get by? how to buy a house with bad credit http://cc.ee.ntu.edu.tw/~tim/1/How-To-Buy-A-House-With-Bad-Credit.html Payment Cap Coverage Federal Tax Return

投稿者 how to buy a house with bad credit : October 25, 2006 11:56 PM

Zero percent is the ultimate in great deals and zero-percent cards are great for consolidating debt by transferring balances from other cards into one. credit card rate http://cc.ee.ntu.edu.tw/~tim/1/Mortgages-For-Bad-Credit.html lowest morgage rates bad credit history mortgage calculators

投稿者 credit card rate : October 30, 2006 06:30 AM

If you currently pay, say, 6 percent for the mortgage and can get a total refi at 6.5 percent, it might be a much better deal than keeping a small balance first mortgage at 6 percent and getting a second loan at a much higher interest rate. best mortgage deal http://xdesign.ucsd.edu/mrtg/calculator/Interest-Calculator.html fixed mortgage rates morgage interest rates emc mortgage corp

投稿者 best mortgage deal : November 9, 2006 08:45 PM

Do you end every month with barely enough cash to pay the bills, and sometimes have to tap into your savings to get by? north carolina mortgage http://theory.lcs.mit.edu/theory-seminars/2005fa/docs/Lenders.html lowest mortgage interest rates today's mortgage interest rates stock broker

投稿者 north carolina mortgage : November 9, 2006 10:43 PM

Paying one debt at one rate usually trumps eight credit cards at varying rates in the debt game. no-load fund http://www.cv.nrao.edu/~abridle/php/gallery/docs/Money-Loans.html mortgage info after-tax mortgage interest

投稿者 no-load fund : November 15, 2006 04:35 AM

Home equity loans are like a financial (addiction) because as your home value goes up, every year you're able to borrow a little more and a little more. after-tax http://www.hcs.harvard.edu/adams/help/doc/Bad-Credit-Mortgage-Refinance.html lowest mortgage rate mortgage commitment mortgage list

投稿者 after-tax : November 15, 2006 10:34 AM

If you're a homeowner, the recent drop in interest rates has opened up the possibility of big savings on your monthly payments. Rates have dropped in recent weeks and the expectation is that they will remain steady for a few more weeks, creating a money-saving opportunity for homeowners as well as those looking for new homes. abn amro mortgage [URL=http://ishi.arp.harvard.edu/atmobs/docs/Abn-Amro-Mortgage.html] abn amro mortgage[/URL] http://ishi.arp.harvard.edu/atmobs/docs/Abn-Amro-Mortgage.html

投稿者 abn amro mortgage : November 15, 2006 07:01 PM

When you consolidate with us you are eligible for their Borrower Benefits Package, which can reduce your interest rate by an additional 1.25 percent. mortgage banking [URL=http://ishi.arp.harvard.edu/atmobs/docs/Mortgage-Banking.html] mortgage banking[/URL] http://ishi.arp.harvard.edu/atmobs/docs/Mortgage-Banking.html

投稿者 mortgage banking : November 15, 2006 07:21 PM

When you consolidate with us you are eligible for their Borrower Benefits Package, which can reduce your interest rate by an additional 1.25 percent. de mortage rates http://lepton.phys.washington.edu/mrtg/forms/DE-Mortage-Rates.html

投稿者 de mortage rates : December 3, 2006 04:49 AM

When you consolidate with us you are eligible for their Borrower Benefits Package, which can reduce your interest rate by an additional 1.25 percent. union mortgage http://www.physics.ohio-state.edu/asc/docs/Debt-Calculator.html

投稿者 union mortgage : December 3, 2006 04:53 AM

Recent trends support the idea that homeowners are realizing this and are refinancing to take advantage of today's lower rates while they are still available. "LowLender is the perfect option for people who want to cut out the legwork. ut morgage rates http://interactive.cens.ucla.edu/nims/docs/UT-Morgage-Rates.html

投稿者 ut morgage rates : December 3, 2006 07:57 PM

投稿者 Naogddk : December 21, 2006 06:29 AM

投稿者 Victor : December 29, 2006 05:38 AM

投稿者 Bob : December 31, 2006 11:47 PM

投稿者 Unison : January 7, 2007 06:10 PM

投稿者 Anne : January 9, 2007 09:20 AM

[url]http://loiza.fuckfactor.com/amateur_facial[/url]
[url]http://porevo.orgfree.com/amateur_facial[/url]
amateur facial
amateur facial
[url]porevo.orgfree.com/amateur_voyeur[/url]
[url]porevo.orgfree.com/amateur_porn_site[/url]
amateur porn site
amateur voyeur

投稿者 Vanessa : January 10, 2007 08:46 AM

投稿者 Guruchel : January 10, 2007 05:59 PM

投稿者 Lohness : January 14, 2007 06:42 AM

投稿者 Mister : January 14, 2007 06:46 AM

投稿者 Guruchel : January 14, 2007 06:57 AM

投稿者 Brytney : January 14, 2007 05:18 PM

投稿者 Bobi : January 14, 2007 05:23 PM

投稿者 Vanessa : January 15, 2007 02:16 AM

投稿者 Mister : January 15, 2007 02:23 AM

投稿者 Brytney : January 15, 2007 02:27 AM

投稿者 Jak : January 16, 2007 03:54 AM

投稿者 Victor : January 16, 2007 04:07 AM

投稿者 Victor : January 16, 2007 04:13 AM

投稿者 John : January 16, 2007 02:50 PM

投稿者 Jak : January 17, 2007 01:53 AM

投稿者 Bobi : January 17, 2007 01:54 AM

投稿者 Ron : January 17, 2007 06:19 PM

[url]http://amater.9cy.com[/url]
amateur porn

投稿者 Peter : January 17, 2007 06:34 PM

投稿者 John : January 17, 2007 06:41 PM

[url]http://volodya.741.com[/url]
homemade blowjob

投稿者 Ron : January 17, 2007 06:46 PM

投稿者 bob : January 18, 2007 05:58 AM

投稿者 syndra : January 18, 2007 06:14 PM

投稿者 Mister : January 19, 2007 03:12 AM

投稿者 John : January 19, 2007 06:16 AM

投稿者 kate : January 22, 2007 06:02 AM

投稿者 Unison : January 22, 2007 02:40 PM

投稿者 Luk : January 22, 2007 10:37 PM

投稿者 Anne : January 23, 2007 01:42 AM

投稿者 Vanessa : January 23, 2007 04:47 AM

投稿者 pedro : January 23, 2007 08:29 AM

投稿者 bob : January 23, 2007 03:11 PM

投稿者 Rick : January 24, 2007 08:00 AM

投稿者 kate : January 24, 2007 08:22 AM

投稿者 jonn : January 24, 2007 08:55 AM

投稿者 Victor : January 27, 2007 10:10 PM

投稿者 Lohness : February 5, 2007 07:38 AM

投稿者 buy furniture : February 5, 2007 08:02 AM

投稿者 syndra : February 5, 2007 09:28 PM

Hello visit my homepage
http://automobil.ueuo.com
[url]http://automobil.ueuo.com[/url]
automobile

投稿者 pedro : February 6, 2007 02:55 AM

投稿者 Lohness : February 9, 2007 09:26 AM

投稿者 Ron : February 10, 2007 07:06 AM

投稿者 Brytney : February 10, 2007 09:28 PM

[url]http://pharmack.9999mb.com[/url]
download mobile ringtones
[url]http://naxuya.1111mb.com[/url]
free download mobile ringtones
[url]http://kapec.fr33host.net[/url]
free mobile ringtones
[url]http://tabletki.xhostar.com[/url]
free mp3 mobile ringtones

投稿者 Vanessa : February 13, 2007 08:57 AM

投稿者 Luk : February 14, 2007 05:46 AM

投稿者 Guruchel : February 14, 2007 05:52 AM

投稿者 John : February 15, 2007 05:12 AM

投稿者 Guruchel : February 16, 2007 01:12 AM

投稿者 syndra : February 18, 2007 05:42 AM

投稿者 Kary : February 18, 2007 06:02 AM

[url]http://furnit.1111mb.com[/url]
shop furniture

投稿者 dodo : February 18, 2007 06:09 AM

投稿者 Luk : February 18, 2007 08:06 PM

投稿者 dodo : February 18, 2007 10:03 PM

投稿者 Jesika : February 18, 2007 10:03 PM

投稿者 syndra : February 18, 2007 10:27 PM

[url]http://melodis.110mb.com[/url]
free mobile ringtones
[url]http://freeringtone.9999mb.com[/url]
mp3 mobile ringtones
[url]http://ring.8888mb.com[/url]
free poly ringtones
[url]http://ringtones.1111mb.com[/url]
free mp3 ringtones

投稿者 Free ringtone : February 19, 2007 06:15 AM

投稿者 Futniture : February 20, 2007 12:03 AM

投稿者 Guruchel : February 25, 2007 10:50 AM

投稿者 Bred : February 25, 2007 11:02 AM

投稿者 Anderson : February 25, 2007 11:21 AM

投稿者 Bill : February 25, 2007 01:30 PM

投稿者 pedro : February 25, 2007 07:03 PM

投稿者 Bobi : February 26, 2007 08:20 AM

投稿者 Michael : February 26, 2007 08:21 AM

投稿者 Bill : February 26, 2007 09:56 AM

投稿者 Anne : February 26, 2007 10:24 AM

投稿者 Ron : February 26, 2007 10:28 AM

Hello nice site http://musik.110mb.com
[url]http://musik.110mb.com[/url]
free mobile ringtones

投稿者 Bob : February 28, 2007 12:56 AM

投稿者 Brytney : February 28, 2007 03:09 AM

投稿者 aglyk : March 2, 2007 02:12 AM

Hell is underground

投稿者 Zasd Iusty : March 2, 2007 04:47 AM

投稿者 Bred : March 2, 2007 07:18 AM

投稿者 Vanessa : March 2, 2007 10:24 PM

投稿者 aglyk : March 2, 2007 10:32 PM

online xanax [url=http://xanaxcheapintheuk.blogg.de/]online xanax[/url] cheap cialis [url=http://xhref.com/1994]cheap cialis[/url] 2 mg xanax [url=http://www.surfindark.com/index.cgi/000000A/http/www.stormloader.com/users/bylovers/1/2mgxanax.html]2 mg xanax[/url] xanax generic [url=http://xanaxgeneric.spotbb.com/]xanax generic[/url] viagra uk [url=http://yourfreebb.de/4934/]viagra uk[/url] prescription xanax [url=http://www.stickypond.com/prescriptionxanax/]prescription xanax[/url] buy cialis generic [url=http://www.tbns.net/kolisterol/viagracialislevitra.html]buy cialis generic[/url]

投稿者 Rvpuuknf : March 3, 2007 12:56 AM

投稿者 pedro : March 3, 2007 01:15 AM

投稿者 bob : March 3, 2007 01:15 AM

投稿者 Ilhhnk : March 3, 2007 10:05 PM

cheap viagra [url=http://catalog.hotindex.ru/Jump.aspx?id=25340]cheap viagra[/url] xanax cheap [url=http://www.bestfreeforums.com/forums/xanaxgenericuk.html]xanax cheap[/url] advanced skin care [url=http://gnu.vu/?EabnDcum]advanced skin care[/url] order cialis online [url=http://shrinkalink.com/8917]order cialis online[/url] viagra prescription [url=http://miniurl.pl/orderviagrauk]viagra prescription[/url]

投稿者 Ykoidc : March 4, 2007 12:09 AM

herbal viagra [url=http://yourfreebb.de/4947/]herbal viagra[/url] phentermine online order [url=http://www.gwebspace.de/phentermi/phentermineonlineorder.html]phentermine online order[/url] all natural skin care [url=http://miniurl.pl/skincare]all natural skin care[/url] cialis soft tab [url=http://www.tbns.net/dredomans/buycialisgenericonline.html]cialis soft tab[/url] viagra cialis [url=http://www.christopherleestreet.com/links/redirect.asp?link=20725]viagra cialis[/url]

投稿者 Jylxp : March 4, 2007 01:14 AM

1mg xanax [url=http://www.realhp.de/members/neuron/prescriptionxanax.html]1mg xanax[/url] herbal viagra [url=http://www.gwebspace.de/phentermi/viagrapills.html]herbal viagra[/url] best skin care [url=http://gnu.vu/?XuEyLCGv]best skin care[/url] order xanax [url=http://c711.com/?QQpT]order xanax[/url] prescription xanax [url=http://shrinkalink.com/8928]prescription xanax[/url]

投稿者 Cbpzcjq : March 4, 2007 02:08 AM

viagra cialis [url=http://viracialislevmx.mybbland.com/]viagra cialis[/url] african american skin care [url=http://www.stickypond.com/acneskincare/]african american skin care[/url] advanced skin care [url=http://linkzip.net/F/1H4]advanced skin care[/url] buy cialis online [url=http://tdurl.com/yn]buy cialis online[/url] 1mg xanax [url=http://gnu.vu/?VqHpenbj]1mg xanax[/url]

投稿者 Pujliad : March 4, 2007 03:02 AM

order cialis online [url=http://www.bestfreeforums.com/forums/ordercialisonli.html]order cialis online[/url] generic cialis [url=http://catalog.hotindex.ru/Jump.aspx?id=25349]generic cialis[/url] cheap viagra [url=http://miniurl.pl/cheapviagraus]cheap viagra[/url] viagra uk [url=http://www.gossiping.net/phpBB2/index.php?mforum=bgecviagra]viagra uk[/url] buy viagra online [url=http://xhref.com/19bf]buy viagra online[/url]

投稿者 Hrxfu : March 4, 2007 03:53 AM

baby skin care [url=http://c711.com/?DPKY]baby skin care[/url] discount phentermine [url=http://www.bestfreeforums.com/forums/phentermineplsu.html]discount phentermine[/url] all natural skin care [url=http://club.giovani.it/.root/media/21312//africanamericanskincare.html]all natural skin care[/url] buy phentermine [url=http://shrinkalink.com/8919]buy phentermine[/url] no prescription phentermine [url=http://catalog.hotindex.ru/Jump.aspx?id=25339]no prescription phentermine[/url]

投稿者 Ouuajt : March 4, 2007 05:31 AM