ZX Format #06
29 июля 1997

Programmers - AI on B. Mednonogova. A detailed description of the "wave of the algorithm" trace (automatic calculation of optimal) path, with an example implementation at Basic.

<b>Programmers</b> - AI on B. Mednonogova. A detailed description of the
Construction kratchayshego marshruta.

music by DNK
(C) B. Mednonogov
_______________________________



 HOW, I remember, back in the game, "UFO-1,
slipped pozhelanie to those who want to
stat good programmistom, povyshat,
povyshat and still raz povyshat your obrazovatelny level. Ha 
that with a very stranits uvazhaemogo izdaniya made chitatel 
with the following thought (not literally remember): "I'm a 
dark person, but the encoder - that nado. Znachit, I have a 
good programmist. Dannaya statya have You tried it razveyat

deep zabluzhdenie. In the first place
ona adresovana those zanimaetsya sozdaniem games and those who 
do not daet rest thought: "A tam what's inside?" To ponimaniya 
dostatochno znaniya bases Beysika and his takih structures, 
two-HOW massivy. 

 Zadacha nahozhdeniya samogo short path
by some tochkami between A and B, nA playing field with 
arbitrary raspolozhennymi harakterna obstacles, especially for 
popular today takticheskih and strategicheskih games. HOW 
podzadacha, ona can voznikat prakticheski any igrah -

RPG, kvestah, logic (a typical example
- "Color Lines", kstati, blind the next version of takoy toys 
after stati - raz spit). Why nado iskat samy short marshrut? In 
some igrah, naprimer "UFO-2", "Laser Squad", the length of the

marshruta zavisit number potrachennyh
units of time - than will optimalney
nayden path, the soldier will get faster
to the target. A, naprimer in "Color Lines"
dlina marshruta not ogovorena pravilami,
znachenie has only sam fakt opportunities
or the inability to move shara.No in
this game will look nice if
sharik srazu napravitsya kuda nado, a not
will zagadochno defilirovat across the board.

 The solution to this zadachi came to Us from takoy dalekoy, 
kazalos would the games oblasti HOW elektronika. A is - 
razvodka pechatnyh plat (all znayut it takoe?).  There are many 
trassirovschikov (programm for razvodki platy) osnovannyh nA no 
fewer razlichnyh methods zanimayuschihsya compound kontaktov 
two single conductor. But we rassmotrim only one of them, samy

simple (a znachit, samy samy nadezhny and
popular) - Wave trassirovschik.

 Postavim before the wave trassirovschikom
zadachu in terminah razrabatyvaemoy nami
Games:
 There is a level playing field P (MxN), where M and N,
respectively razmer field of vertikali
and gorizontali. Simply, it is massiv razmernostyu MxN (DIM P 
(M, N)). Kazhdaya kletka playing field (element massiva) can 
obladat large number of properties on vashemu discretion, but 
only for nas vazhno one - its patency (or obstruction). Kakim 
obrazom ona determined - vashe that's the case. Dalshe: there 
nekotoraya startovaya tochka where nahoditsya hero vashey game 
and konechnaya tochka, kuda him must popast. Poka We agree that

He could walk only four napravleniyam (which requires nas 
klassichesky wave method) - vpravo, left, forward,

nazad. Want to move him from
CAREERS starta to finish za naimenshee number of moves, if at 
all possible takoe movement. 

 Algorithm nahozhdeniya kratchayshego marshruta
between the two tochkami for takoy zadachi dostatochno simple:

 1. Snachala need sozdat rabochy
massiv R (MxN), ravny on razmeru massivu
playing field P (MxN)
 2. Each time an element rabochego massiva
R (i, j) prisvaivaetsya some znachenie in
zavisimosti on the properties of elementa game
field P (i, j) the following pravilam:
 a) If the field P (i, j) impassable, then
R (i, j): = 255;
 b) If the field P (i, j) is passable, then
R (i, j): = 254;
 c) If the field P (i, j) is the target
(Final) position, then R (i, j): = 0;
 d) If the field P (i, j) is startovoy
position, then R (i, j): = 253.
 3. Etap "Rasprostraneniya waves." Introduce
variable Ni - counter iteratsy (repetitions) and prisvaivaem 
her nachalnoe znachenie 0.  4. Introduce konstantu Nk, which 
ustanavlivaem ravnoy maksimalno possible number iteratsy.  5. 
Line prosmatrivaem rabochy massiv R (ie, large screen enclosed 
organizuem tsikla: Index massiva i from 1 to M, the massiva 
index j from 1 to N).  6. If R (i, j) raven Ni, then 
просмaтривaютсясоседниеэлементыR (i +1, j), R (i-1, j), R (i, j 
+1), R (i, j-1) following pravilu (as a possible primera 
rassmotrim R (i +1, j)):

 a) If the R (i +1, j) = 253, then go to
paragraph 10;
And b) If the R (i +1, j) = 254, holds prisvaivanie R (i +1, 
j): = Ni +1;  c) In all ostalnyh a case of R (i +1, j)

ostaetsya unchanged.
 Analogichnopostupaem with elementami
R (i-1, j), R (i, j +1), R (i, j-1).
 7. By zaversheniyu interline prosmotra
all massiva uvelichivaem Ni nA 1.
 8. If Ni> Nk, search marshruta priznaetsya neudachnym. Exiting 
programmy.  9. Go to step 5.

 10.Etap build marshruta movement. Prisvaivaem variables X and 
Y znacheniya koordinat startovoy position.  11.V vicinity of 
the position R (X, Y) are looking for element naimenshim 
znacheniem (ie This prosmatrivaem R (X +1, Y), R (X-1, Y),

R (X, Y +1), R (X, Y-1)). Koordinaty this
elementa zanosim the variables X1 and Y1.
 12.Sovershaem movement obekta (who
tam y vas to be - a robot akvanavt, Winnie the Pooh) on the 
playing field from the position [X, Y] in position [X1, Y1]. 
(By zhelaniyu, you can predvaritelno zanosit koordinaty X1, Y1

in some massiv, and only zakonchiv
construction of all marshruta, zanyatsya movement hero ekrane 
nA). , 13.Esli R (X1, Y1) = 0 then go to step 15.

 14.Vypolnyaem prisvaivanie X: = X1, Y: = Y1.
Go to step 11.
 15.Vse!

 For those who are srazu realized recommend
dalshe not chitat. For people normalnyh
I repeat still raz with PURPORT and explanations:

 1. Snachala need sozdat rabochy
massiv R (MxN), ravny on razmeru massivu
playing field P (MxN).

REM> Poka simple. Ha BASIC - just
komanda DIM R (M, N). assemblere Ha -
something tipa "_R DEFS _M * _N". If the playing field a lot, 
it makes sense to a window, kuda popadayut nachalnaya and

konechnaya point (naprimer in "UFO-2. Devils are deep" in the 
field razmere 64x64 rabota is only chastyu field 32x32) that  
would ogranichit number of calculations. 

 2. Each time an element rabochego massiva
R (i, j) prisvaivaetsya some znachenie in
zavisimosti on the properties of elementa game
field P (i, j) the following pravilam:
 a) If the field P (i, j) impassable, then
R (i, j): = 255;
 b) If the field P (i, j) is passable, then
R (i, j): = 254;
 c) If the field P (i, j) is the target
(Final) position, then R (i, j): = 0;
 d) If the field P (i, j) is startovoy
position, then R (i, j): = 253.

REM> also without complications. Come on
massivu playing field F, define prohodima / neprohodima 
tekuschaya kletka, in accordance with this zapisyvaete in cell 
massiva R number 254/255. According to zavershenii the position 
of start / stop zanosite 253 / 0. There are several ways 
zadaniya properties elementa playing field. Primera specific 
large screen: the "NLO1 / 2" organizovan baytovy massiv 
properties spraytov landshafta, Each time a bat has its own 
property, za patency otvechaet, naprimer, the seventh bit. In 
"Black Crow" sdelano easier - sprayty with nomerami from 0 to 
31 - Passable, starshe - no.


 3. Etap "Rasprostraneniya waves." Introduce
variable Ni - counter iteratsy (repetitions) and prisvaivaem 
her nachalnoe znachenie 0. 

REM> Etap nazvan tak because zapolnenie rabochego massiva 
really napominaet wave. Obratite vnimanie that rasprostranenie 
wave nachinaetsya with the endpoint. 

 4. Introduce konstantu Nk, which ustanavlivaem ravnoy 
maksimalno possible number iteratsy. 

REM> It's a very delicate moment. Suppose that between nachalom 
and the end is insurmountable obstacle, togda search

zaydet way to a standstill and programma zatsiklitsya. To avoid 
this, and introduces a takaya peremennaya. Her velichina 
podbiraetsya eksperimentalno.Naprimer, in the same

"UFO-2" dazhe akvanavt-general, having 108
units of time and a lot of energy can not
za course will move more than nA to 27 cells. Odnako I still 
sdelal Nk = 64. In either a case of when nashem method of 
solving zadachi Nk can not prevyshat 253 (dogadalis why). 

 5. Line prosmatrivaem rabochy massiv R (ie, large screen 
enclosed organizuem tsikla: Index massiva i from 1 to M, the 

  massiva index j from 1 to N)

REM> Dumayu, of course, is HOW sdelat nA
BASIC. Ha assemblere I would not stal delat tsikla large 
screen, a sdelal least one, remembering that line in memory is 
massiva hranyatsya za each other and obrazuyut continuous chain 
baytov. 

 6. If R (i, j) raven Ni, then просмaтривaютсясоседниеэлементыR 
(i +1, j), R (i-1, j), R (i, j +1), R (i, j-1) following 
pravilu (as a possible primera rassmotrim R (i +1, j)):

 a) If R (i +1, j) = 253, then go to
paragraph 10;
 b) if R (i +1, j) = 254, holds prisvaivanie R (i +1, j): = Ni 
+1;  c) In all ostalnyh a case of R (i +1, j)

ostaetsya unchanged.
 Analogichnopostupaem with elementami
R (i-1, j), R (i, j +1), R (i, j-1).

REM> A few points to programmiruyuschih nA assemblere. Because 
we are looking sovpadenie massiva elements with only one number 
(Ni), in order to achieve naibolshey speed poiska recommended 
ispolzovat komandu CPIR. Second zamechanie: when fiksirovannyh 
razmerah playing field adresa adjacent elements can not compute 
for complex formulam, a zadat numeric offset (naprimer, at a 
field 32x32 displacement of the four neighboring cells ravny

-32, -1, +1, +32). Third zamechanie, vazhnoe for all: a lot of 
time in the calculations can account otnimat kraevyh effects 
(Meaning the elements raspolozhennye nA granitsah massiva). 
Indeed, if naprimer, i = 1, then to obraschenie R (i-1, j) has 
no smysla and may lead to perish dannyh and zavisaniyu 
kompyutera. I recommend further in paragraph 1 sozdat rabochy

massiv razmerom not nA M N, a (M +2) x (N +2)
and all dat granichnym elementam znachenie
255 (impassable). The memory is a little tratitsya
more zato programmirovat easier, and yes
raschety will go faster. Tak and I delal
in "UFO-2.

 7. By zaversheniyu interline prosmotra
all massiva uvelichivaem Ni nA 1.

 8. If Ni> Nk, search marshruta priznaetsya neudachnym. Exiting 
programmy. 

REM> I vas a little obmanul. Matematicheski exact conditions 
neudachnogo poiska zvuchat tak: "If nA current shage no naydeno 
no elementa R (i, j), ravnogo Ni, then marshruta connecting the 
two points that are not There. "You can vospolzovatsya

pravilom this if you like absolyutnuyu
Accuracy (in this a case of parametr Nk does not need one), but 
I kazhetsya better sdelat one test at the end than a hundred nA 
etape poiska.  Yes, I almost zabyl, Algorithm rasprostraneniya 
wave can prekrasno ispolzovatsya for zalivki small pieces of 
arbitrary shape. Tak, if you want sozdat its own Art Studio, 
and the head did not climb, can ispolzovat this method (for 
this vybrasyvaem items 10-15 and slegka modify Algorithm.

HOW? Pridumayte sami).

 9. Go to step 5.

REM> That is prodolzhaem generatsiyu wave.

 10.Etap build marshruta movement.
Prisvaivaem variables X and Y znacheniya koordinat startovoy 
position. 

 11.V vicinity of the position R (X, Y) are looking for
element naimenshim znacheniem (ie
This prosmatrivaem R (X +1, Y), R (X-1, Y),
R (X, Y +1), R (X, Y-1)). Koordinaty this
elementa zanosim the variables X1 and Y1.

REM> way to view the surrounding elements
analogichen what HOW is delalos in paragraph 6. If vash the 
hero is able to walk on diagonali, you can include in your 
searches, and more four neighboring diagonalnyh elementa,

nado view that in the first place. Tak same, but slightly more 
complicated in sdelano "UFO-2 (at rassmotrenii diagonalnyh

uchastkov navigate pravilam taken to bolshinstva strategy 
should not be hindering traffic or sprava sleva).


  Vnimanie! Takoy way ucheta diagonalnyh movements daet 
approximately 95% probability nahozhdeniya really samogo short 
marshruta. Ha my opinion, this quite dostatochno. If unto you 
suddenly need a samy a short way from garantiey nA to 100%, 
already in paragraph 6, you must prinimat in vnimanie 
diagonalnye elements based nalozhennyh vashey ogranicheny game. 
Speed ​​rasprostraneniya wave thus strongly padaet.


 12.Sovershaem movement obekta the game field from the position 
[X, Y] in the position [X1, Y1]. (By zhelaniyu, you can 
predvaritelno zanosit koordinaty X1, Y1 in a massiv, and only 
building the whole zakonchiv marshruta, zanyatsya movement Hero 
ekrane nA). 

REM> Zanosit koordinaty marshruta in takoy intermediate list 
makes sense, if both vas peremeschaetsya several characters, a 
memory is only vydelena under one rabochy massiv R. Or, if

place in the R stands out in a certain general oblasti that 
others may podprogrammy ispolzovat fit your needs. Kstati can 
not zapominat sami koordinaty, nA that in nashem allow 2 bayta 
example, a code napravleny movement nA dostatochno and that one.


 13.Esli R (X1, Y1) = 0 then go to step 15.
REM> Well, we got to handle, ie to
onechnoy point.

 14.Vypolnyaem prisvaivanie X: = X1, Y: = Y1.
Go to step 11.

 15. Everything!

Pravda is not just? In izbezhanii ambiguities in this issue is 
zhurnala nA simple example of BASIC. Looking

it, you HOW least be able to repeat
"Color Lines".

Dostoinstva and nedostatki metoda.

 Dostoinstva - prostota, nadezhnost, 100%
samy shortest path (for klassicheskogo
metoda). Nedostatki - a large amount of memory is required and 
no samaya vysokaya rate nahozhdeniya way. In "UFO-2, when the 
conditions listed above, the path may nahozhdenie dostigat time 
to 1 / 10 seconds. This, of course, acceptable to poshagovyh

strategy and logic toys, but hardly suitable for dinamicheskih 
games. A pro attempt realizatsii nA BASIC I do

silence (razve as a possible primera).

 Variatsii metoda.

 Dvoynaya volna - rasprostranenie wave
HOW nachinaetsya from the end, and from tak nachalnoy point, a 
marshrut prevalent made from two uchastkov - from the point of 
meeting waves to starta until finisha. Theoretically, can

increase the speed poiska 3-4 raza.No
Here's HOW nA praktike?

 In a case of acute nehvatki memory is, naprimer if you do not 
zadumali game, a samy nastoyaschy trassirovschik plat nA 
Spectrum, can be used truncated kodirovanie

wave. Ie pervaya volna is number 1,
vtoraya - 2, the third - snova 2, chetvertaya
-1, And tak dalee. Ha encoding one elementa require large 
screen bita (chisla 0 / 3 will be traversed opisyvat / 
impassable field).  When searching for search marshruta 
adjacent cells The memory in the same order (... 1 1 2 2 1 1

2 2 1 1 2 2 ...). None of kakih diagonalnyh
movements can not be considered.

 In addition to the wave, there sravnitelno
large number of methods for poiska
marshrutov. Somewhere required naibolshaya
raschetov speed at the expense of kachestvu,
somewhere - naimenshee number of rotations,
somewhere - need to marshrut obyazatelno went through some of 
the key point (nevazhno in kakom order). New

Methods trassirovki allow iskat marshruty where the path may be 
held under any uglami (not just kratnymi 90-a and

45 and gradusam). Progress does not stand nA place.

 Therefore zakonchit statyu want slovami
VI Lenina, skazannymi them nA III Congress
Young Communist League: "Learn, learn and learn - that
vasha glavnaya zadacha!

P.S. I would like to poblagodarit prepodavateley SPbGTU (ETU) 
with kafedry SAPR, which I have all this nauchili, a I, HOW 
could rasskazal unto you. Well, even raz napominayu who want to 
stat nastoyaschim programmistom should go only to the 
institution directly nA kafedru this:) 

Vsegda vash, Vyacheslav Mednonogov.
_______________________________




Other articles:

Today in the room - the contents of the magazine.

Authors - The authors of the journal ZX-Format No.6

From the authors - the long awaited event finally happened ...

Toys - The last iron (short story on the game 48 irons).

Toys - Many Adventures of Winnie the Pooh. Part Two.

Toys - the game description The Crypt (Castle Master 2).

Toys - description editor Adeventyur - PAW (Part 1).

Toys - description editor Adeventyur - PAW (Part 2).

Toys - description editor Adeventyur - PAW (Part 3).

Toys - description editor Adeventyur - PAW (part 4).

Toys - description editor Adeventyur - PAW (Part 5).

Programmers - Beta Basic: continued talking about BASIC (Part 2).

Programmers - General Sound: Programming Guide.

Programmers - MMD - the driver. Description of the structure of the modem driver for the terminal program MMD.

Programmers - AI on B. Mednonogova. A detailed description of the "wave of the algorithm" trace (automatic calculation of optimal) path, with an example implementation at Basic.

Programmers - Artificial Intelligence. Continuation of a series of articles about "AI". General basis for finding the way to goal.

Programmers - Tr-Dos for programmers. Max Petrov concludes his story about nontraditional methods of work with the disk.

Programmers - sharing experiences: "3-colour". Description of the effect of colors on 8-point ", help to the viewer, and how many words on the conversion of images in format "3-colour".

Programmers - sharing experiences: "3-colour". A few words about converting images in the format of RGB.

Programmers - the exchange of experience: programming Multicolor effects.

IS-DOS - users: how to personalize your system IS-DOS on a specific model of ZX Spectrum-compatible PC and to perform your tasks.

IS-DOS - users: how to copy the system disk IS-DOS and stay with the dead.

IS-DOS - the programmer: a short course - programming in IS-DOS.

IS-DOS - news: new software IS-DOS.

Iron - A short story about the capabilities of the processor Z-180.

Iron - Multiviewer. Description dorabotochki allowing to measure the speed of programs to curb without climbing in the codes - an easy push of a button.

Iron - A new project the firm Peters - "Sprinter". New Spectrum-compatible PC with a new generation of Speccy.

Iron - Opinions about skorpionovskom controller IDE HDD - SMUC.

Iron - SuperSpectrum: one project Spectrum-compatible machines. Its feature is compatible with the PC.

Iron - X-Trade FAQ. Answers to frequently asked questions on the GS and XTR-modem.

Premiere - Flash tracker. Description 4-channel editor of digital music, working with SoundDrive, from the author SoundDrive - Flash Inc.

Premiere - Description of the latest version of the universal terminal program used in SpbZxNet.

Premiere - Mortal Kombat: what awaits you in the full version of the game and some comments to the demo version.

Premiere - XReversy: presentation of a new toy from the popular family of "Solve puzzle - see the picture."

Interview - An interview with one of the most famous spektrumistov - Andrew Larchenko.

It was you - The story "Absolute Power".

It was you - The story "The Road".

It was you - Lord of the teeth: a parody of a popular trilogy ...

Mail - Contact us: an e-mail Alex'a from Nizhny Tagil, exhibited in the last room at the Corner of lamer. "

Mail - Letters from readers: Andrei Yakovlev, Denis Tokarchuk, Alex Garkulim, Alexander Gordeev, Evgenii Shumilov Nitochkin Vadim, Michael Larkin.

Mail - free advertising and announcements.

Miscellaneous - Scarecrow.: Nemo talks about the place of the PC and Spectrum in the modern Russia.

Miscellaneous - Review of Nemo in the book on digital circuitry. For anyone who has ever ever been tempted to turn on the soldering iron and ...

Miscellaneous - Questionnaire: Results of our poll spektrumistov.

Miscellaneous - Competition. A brief account of our contests.

Miscellaneous - The problems of the software market: when zagnetsya Spectrum. All over whether to blame hackers?

Miscellaneous - Outlook software. A brief overview of the forthcoming software: Fast Tracker, Pro Sound Creator, Black Crow.

Miscellaneous - Outlook software. Adventyura From Beyond or outside. "

Miscellaneous - A Memoir of the Peter modem network for ZX Spectrum - SPbZXNet.

Amiga Club - Between Us, by users: a comparison of characteristics of the Amiga 1200 with the IBM PC.

Amiga Club - compare the performance of Amigo and PC. As far as Amiga relevant in today's games?


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
ANOTHER WORLD - CPU Pentium III.
waggons - Press Review: Always one, Micro 23, ZX-News 50, Polesie 15.
Stories - A computer virus.
Techno-nature - Electronic music: Dj.Ironman talks about technology (Part 1).

В этот день...   21 November