Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

randomly select from a list of things 1

Status
Not open for further replies.

Stainnd

Programmer
Jul 9, 2001
65
US
I'm writing an RPG. I want the computer to randomly generate which monster to create. Ideally, I'd like to have a bunch of records for each monster, containing that monsters hit points, damage, etc... but for now, i'll content myself with randomly generating those numbers, and then randomly assigning a name to this monster (i.e. dragon, giant, goblin, etc.). What would be the best way to do this? -Mike
 
declare all of the names in a one dimensional array and then randomly generate them.

DIM monster(3)
monster(1) = "dragon"
monster(2) = "goblin"
monster(3) = "giant"
draw = INT(RND * 5) + 1
newmonster = monster(draw)
 
I was actually hoping to catch you on the board sometime, because you seem to be somewhat of an authority on put/get, (and therefore, records i assumed). I didn't realize that the array thing was so easy, so maybe you could help me with the next step. In the RPG, i have a sub called mongen that generates a monster based on the character level. So if a guy walked on a certain tile, it says "CALL mongen(player.lev)" and figures out what monster to spawn, and what the info of that monster is. Could I make a database of monsters, with each monster have a name, a couple of attributes like hit points, etc. and then level. And have it randomly spawn a monster within, say, two levels of the character (obviously not spawning a -1 level monster for a level 1 character.) I guess the main problem I have is how to save the values to a record. Once I can do that, I would just let records 1-8 be level one monsters, 9-16 be level two, etc. Then randomly generate a number within two of the character's level, and then just GET the randomly selected record. Any help you or anyone else could provide on the subject would be much appreciated. Thanks -Mike
 
I'm kinda fuzzy on exactly what you want, but I thinkt aht I can help you. For the attributes just use the same thing.

DIM monster$(3)
monster$(1) = "goblin"
monster$(2) = "dragon"
monster$(3) = "giant"
DIM hitpoint(3)
hitpoint(1) = 4
hitpoint(2) = 3
hitpoint(3) = 5
et cetera

if you want to save them to a file using GET/PUT then make three files. use DIM again

DIM file$(3)
file$(1) = "c:\goblin.mon"
file$(2) = "c:\dragon.mon"
file$(3) = "c:\giant.mon"
FOR aa = 1 to 3
OPEN file$(aa) FOR RANDOM AS #1
PUT #1, 1, hitpoint(aa)
....
CLOSE #1
NEXT
 
So if I wanted to assign two attributes, say...hit points and damage...would i dim the damage array, and then PUT #1, 1, damage(aa), or would it be PUT #1, 2, damage(aa). -Mike
 
[tt]TYPE monsterType
monsterName AS STRING * 30
maxHP AS INTEGER
strength AS INTEGER
' add any other properties that are common to all monsters of a given type
END TYPE
TYPE
monsterInstanceType
whichMonster AS monsterType
curHP AS INTEGER
' add any other properties for which each monster of a given type has its own value
END TYPE

SUB
InitializeDatabase()
DIM m AS monsterType

ff% = FREEFILE
OPEN
"monsters.dat" FOR BINARY AS #ff%

m.monsterName = "Goblin"
m.maxHP = 4
m.strength = 5
PUT #ff%, , m

m.monsterName = "Giant"
m.maxHP = 10
m.strength = 8
PUT #ff%, , m

m.monsterName = "Dragon"
m.maxHP = 15
m.Strength = 10
PUT #ff%, m

'TODO: add more monsters ^_^
CLOSE #ff%
END SUB

SUB
CreateMonsterInstance(playerLevel%, instance AS monsterInstanceType)
lowestLevel% = playerLevel% - 2
highestLevel% = playerLevel% + 2

'assuming you have 8 per level
firstMonster% = 1 + (lowestLevel% - 1) * 8
lastMonster% = highestLevel% * 8

IF firstMonster% < 1 THEN firstMonster% = 1
IF lastMonster% > 500 THEN lastMonster% = 500
'change 500 to the number of monsters you have

range% = lastMonster% - firstMonster% + 1
chosenMonster% = INT(RND * range%) + firstMonster%

fileOffset& = 1 + (chosenMonster% - 1) * LEN(instance.whichMonster)

ff% = FREEFILE
OPEN
&quot;monsters.dat&quot; FOR BINARY AS #ff%

GET #ff%, fileOffset&, instance.whichMonster

CLOSE #ff%

instance.curHP = instance.whichMonster.maxHP
' add any other instance initialization that is needed
END SUB

FUNCTION
GetRandomDamage%(instance AS monsterInstanceType)
GetRandomDamage% = INT(RND * instance.whichMonster.strength) + 1
END FUNCTION
 
Thanks SO much! Just curious, what program do you use to edit your programs? My QBASIC has become very unstable lately, and I need to find something else to use. Thanks again. -Mike
 
You can edit Qbasic programs in any text editor. Use Word or Notepad.
 
Hey, don't forget to put RANDOMIZE TIMER at the beginning of your code or the same random values will come up every time you start your program. :)
 
In practice, I find that that never happens, even though the behaviour is documented.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top