Need volunteer to create simple item generator for Flare

Need volunteer to create simple item generator for Flare

Postby Danimal » 25 Jun 2021, 18:03

Hello everyone, im developing a mod for Flare:
https://forum.freegamedev.net/viewtopic ... 22&t=14397

The biggest headache of it all is item creation. Every single item must be created by hand, numbered, named and given its properties. Its pure hell, and its pretty obvious ingame with items repeating a lot, sadly the engine doesnt have nor will have in the short future a random item generator. But it can be given a fake sense of randomness with lots of pre-created items; i tried making one but failed miserably, at the very least i got a mock-up of what i wanted.

If someone is interested in helping, it should be relatively easy for someone who knows programming, only requeriment is that it must run on Windows, ill fill on the details anyone interested.
Attachments
Captura de pantalla 2021-06-25 19.01.59.png
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 25 Jun 2021, 20:34

Here you go. This is an extremely simple, GPL-licensed, Open Source templater to generate random items for flare. Precompiled binaries for Linux and Windows included in the zip.

This is a command line tool, so invoke it from "cmd.exe". Has three arguments: a json file, a starting id, and a last id. It will use the field definitions in the json to generate the items to stdout, which you can redirect to a file or append to items.txt. Basically you want to create as many json files as many item types you want. There's no restriction, you're absolutely free to create these as you like. Documentation included with an example json of course.

Cheers,
bzt
Attachments
flaregen.zip
(104.15 KiB) Downloaded 38 times
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 26 Jun 2021, 18:19

Hello BZT, even thought its a nice basic generator is too limited.

You see, the idea i had is being able to input the item level, plus the stat gain per level (like Lv3 sword with gains of Min 1-Max 3 gives swords of damage ranging 3-9 points at random), since there are basically 3 damage types on Flare i would check the weapon circle button and then Melee/magic/ranged damage in the now empty combox under weapon (or any kind of similar control object). Armor is similar in which instead of having damage ranges (like dmg=melee,3,9; dmg=ment,3,9; dmg=ranged,3,9) has Absortion (abs=2,4). "Others" just means its not a weapon or amor so it doesnt need those values (more on this later); You got pretty well the starting and finish id.

The middle column with requeriment need to check any of the four stats and press 70%,50%,30% button. This outputs the item 70%,50%,30% level * 5 (char stat gain per lv) of the needed stat to wear, as a line into the extra input data textbox. Ex:

{l Code}: {l Select All Code}
[item]
id=287
name=Hunter shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=2
price=600
dmg=ranged,6,10
requires_stat=offense,7
requires_stat=physical,3
...


5*2 = 10 -> 70% =7
5*2 = 10 -> 30% =3
So if i press Dextery 75% and Strenght 30%, this should appear into the extra input data textbox.
requires_stat=offense,7
requires_stat=physical,3

Why that mysterious extra input data textbox? That box would contain any object declaration or requeriments i add in the future and doesnt exist yet. And would get dumped into the body of every item created (say, i started creating chitin armor sets) i just put the declaration in there so every item gets it along with requeriments or any thing i add in there:

INCLUDE items/base/armor/chitin_light/chest.txt

Ideally, there would be buttoms for the most used item definitions in the empty space over the textbox, so i can just press it and it gets added (like rings or swords definition).

So after that, the quality button needs to be pressed with common being the default. What does it do? upon generation time it picks from a list of magical properties (cant repeat same property twice, Flare cant do that, common has no magical properties, its just the base item) as many times as quality allows (2,3,5), the property X variable gets replaced by the number generated depending on item level and gain ratio like the weapons. Also adds quality tag: quality=magic; quality=rare; quality=unique.

Ideally it would also contain a box for item basic name, if any of the magicals is selected, its get and abjective by ramdon from a list ("Short sword" "of carnage". Also a price box with min and max values, ideally ,it would get multiplied if quality is superior to common x1.20,x1.30,x1.50.
I would provide all needed lists with names, extra properties...

Sadly if its not this complex, its not useful.
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 26 Jun 2021, 23:06

Danimal {l Wrote}:Hello BZT, even thought its a nice basic generator is too limited.
No, it's not you just didn't understood how it works. It can be used to generate any item configuration you want.

Danimal {l Wrote}:You see, the idea i had is being able to input the item level, plus the stat gain per level (like Lv3 sword with gains of Min 1-Max 3 gives swords of damage ranging 3-9 points at random), since there are basically 3 damage types on Flare i would check the weapon circle button and then Melee/magic/ranged damage in the now empty combox under weapon (or any kind of similar control object). Armor is similar in which instead of having damage ranges (like dmg=melee,3,9; dmg=ment,3,9; dmg=ranged,3,9) has Absortion (abs=2,4). "Others" just means its not a weapon or amor so it doesnt need those values (more on this later); You got pretty well the starting and finish id.
You should have a json for each of those. Values can be generated randomly, and they can be shifted by an offset using the "add" field if needed.

Danimal {l Wrote}:The middle column with requeriment need to check any of the four stats and press 70%,50%,30% button. This outputs the item 70%,50%,30% level * 5 (char stat gain per lv) of the needed stat to wear, as a line into the extra input data textbox. Ex:
This means you'll have to create a json for each of these combinations. Where you say you a press a 70%,50%,30% button, I say pick one of the jsons.

Danimal {l Wrote}:So if i press Dextery 75% and Strenght 30%, this should appear into the extra input data textbox.
requires_stat=offense,7
requires_stat=physical,3
You should have a json with
{l Code}: {l Select All Code}
"name": "requires_stat", "value": "offense,7"
"name": "requires_stat", "value": "physical,3"
Since you're using only 4 percentages, there are not so many combinations. But if I were you, I'd use an INCLUDE for each combination, that would simplify your items greatly.

Danimal {l Wrote}:Why that mysterious extra input data textbox? That box would contain any object declaration or requeriments i add in the future and doesnt exist yet.
Yeah, I've figured that. That's why I've told you that you haven't understood how my tool works. It has NOTHING hadwired, except for the id of course. If in the future you want to add new declarations, just add them to the json, and those fields will be dumped to the output as well.

Danimal {l Wrote}:Ideally, there would be buttoms for the most used item definitions in the empty space over the textbox, so i can just press it and it gets added (like rings or swords definition).
For those most used combinations you should have an INCLUDE. But you can just as well create one json for each, like ring.json, sword.json, etc.

For example, you could also create 20 different combinations, and then generate 1000 items out of those randomly with
{l Code}: {l Select All Code}
"name": "INCLUDE", "value": "items/types/combination%d.txt", "min": 1, "max": "20"
This works too.

As I've said, you have complete freedom how you create those jsons and what fields you define in them, flaregen makes no assumptions and does not tie your hands.



If you want more complex things like fields depending on each other and have calulcated values for example, then I'd recommend to pick a scripting language and use that script as a template. My pick would be php, because in CLI mode it runs out-of-the-box under Windows and Linux too, easy to install, simple to use (no need to install Apache for the CLI php) and has templater capabilities. For example:
{l Code}: {l Select All Code}
<?php
/* get arguments from command line */
$lvl = $_SERVER['argv'][1];    // first is the level
$off = $_SERVER['argv'][2];    // second is the offense percentage
$phy = $_SERVER[argv][3];      // third is the physical percentage
?>
level=<?=$lvl?>
dmg=ranged,<?=($lvl * 3)?>,<?=($lvl * 3 + 4)?>
requires_stat=offense,<?=$off?>
requires_stat=physical,<?=$phy?>
As you see, here "dmg" has calculated values which depend on the level for example. Random goes like this:
{l Code}: {l Select All Code}
price=<?=rand(100, 500)?>
You could also add conditional blocks.
{l Code}: {l Select All Code}
<? if($off >= 3) { ?>
requires_stat=offense,<?=$off?>
<? } ?>

But php would be just my choice, feel free to use any scripting language, I'm sure all of them can print strings and calculated values. For example python would work as well.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 27 Jun 2021, 18:35

Thanks, but the thing is i cant code for my life... :cry:
Ill try to chew this slowly
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 27 Jun 2021, 23:53

Danimal {l Wrote}:Thanks, but the thing is i cant code for my life... :cry:
Ill try to chew this slowly
That's why I've recommended php. You see, unlike other programming languages, php works as a templater by default. This means everything that's not inside a <? ?> block gets copied to the output verbatim, so it is particularly usable for generating item files. No programming skills needed, the first block just gets the arguments and places them in named variables, otherwise all you need is <?=...?> which calculates an expression and outputs its value. For these only mathematical expressions needed, like addition, subtraction, multiplication, division and grouping with parenthesis. Just the basic things. About the programming stuff, all you need to know is that variables are prefixed by '$', and that you can get a random number with "rand(min,max)", that's all.

But I've come up with an even better solution for you. Just copy'n'paste this one liner to the start of your template:
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
and end it with
{l Code}: {l Select All Code}
<?}?>
Don't try to understand this, don't let it frighten you. Think of it as a one line magic. What this does is allowing you to specify variables directly from the command line.

For example (everything between the first and the last line are freely modifiable by you):
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
[item]
id=<?=$id?>
name=Hunter shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand(100 * $level, 150 * $level)?>
dmg=ranged,6,10
requires_stat=offense,7
requires_stat=physical,3

<?}?>

Now you can generate items with it like this:
{l Code}: {l Select All Code}
php.exe template.php startid=287 endid=299 level=2
As you see, the first two arguments needed to execute the interpreter and name the template file. Everything else are variable=value pairs. You always must specify "startid" and "endid" and you can always refer to the actual id with "$id", but the rest depends on what variables you were using in the template. In this little example the one and only variable referenced was "$level", so you must give a value to it (otherwise you'll get a php warning) on the command line, in this example "level=2". Note: I've added an empty line at the end so that in the output the items will be separated, not needed, just for visibility.

This might seem a bit complex at first, but it is not. I'm doing it this way because I don't want to tell you what variables and fields to use for your items. I want to give you the freedom to create your templates as you like, and to modify them later as you need. You can create as many templates as you want with this method, just make sure that all starts with that one-liner and ends in "<?}?>".

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 29 Jun 2021, 12:51

I think, I get php a bit better than JavaScript, maybe because of having more samples, can the same calculations be done in the one you sent me?
Like im guessing i should go like this to make random damage values and requires:

<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
[item]
id=<?=$id?>
name=Hunter shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand(100 * $level, 150 * $level)?>
dmg=ranged,<?=rand(0, 1 * $level)?>, <?=rand(1 * $level, 3 * $level)?> (for min-max damage random values )
requires_stat=offense,<?=$level *= 5 *= 70 /= 100?> (i have no idea if thats how you nest operations on php, and top of it, it needs to return an integer
requires_stat=physical,<?=$level *= 5 *= 30 /= 100?>

<?}?>

Could i do the same with your teamplater? it would be much less hassle for me just creating/modifying .jsons; but it doesnt accept level variables? And how would i go with name variations? like i said before base name + random abjective from a list: "Shortbow" "of poachers", same for random magical properties, i dont understand what you are telling me here:

For example, you could also create 20 different combinations, and then generate 1000 items out of those randomly with

{l Code}: {l Select All Code}
"name": "INCLUDE", "value": "items/types/combination%d.txt", "min": 1, "max": "20"


Would it be better to try php?
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 29 Jun 2021, 18:20

Danimal {l Wrote}:I think, I get php a bit better than JavaScript, maybe because of having more samples, can the same calculations be done in the one you sent me?
Like im guessing i should go like this to make random damage values and requires:
Yes, you got that right!

Danimal {l Wrote}:requires_stat=offense,<?=$level *= 5 *= 70 /= 100?> (i have no idea if thats how you nest operations on php, and top of it, it needs to return an integer
Not sure what you wanted, but all you need is simple algebra. The calculated number is automatically converted to a string and added to the output, you don't have to worry about that. For example <?=$level * 5 * 70 / 100?> returns the 70% of 5 times level. But <?=$level * 5 * 0.7?> would do too. About those, it reminded me that you might need to convert floating point numbers to integers, for that use "floor()" (rounds down) and "ceil()" (rounds up). So for example <?=floor(1.7)?> will print "1" to the output, while <?=ceil(1.7)?> will print "2".

Danimal {l Wrote}:Could i do the same with your teamplater? it would be much less hassle for me just creating/modifying .jsons; but it doesnt accept level variables?
Sadly no. All it can do is add, but no complex expressions and no variables (except an implicit one). That's why I've said if you need calculated things, you should use a scripting language.

Danimal {l Wrote}:And how would i go with name variations? like i said before base name + random abjective from a list: "Shortbow" "of poachers", same for random magical properties, i dont understand what you are telling me here:
Ah ok, with my templater you create several txt with "name=something" variations, name them "nameX.txt" and use a random INCLUDE. But I guess you want more complex expressions anyway, so here's how you can do it with php: <?=["a","b","c"][rand(0,2)]?> (might look ugly, I admit). So here in the first [] block, you list the strings. In the second, as the parameter to rand, you use 0 and the number of strings minus 1.
{l Code}: {l Select All Code}
name=<?=["Short","Long","Recursive","Asymmetric","Mongol"][rand(0,4)]?>bow


Danimal {l Wrote}:Would it be better to try php?
Yes. If you want calculated expressions and variables, then definitely a scripting language is the way to go. If you were on Linux, I'd say use shell script with HEREDOC or awk, but on Windows I'd recommend to download php, probably that's the simplest (for templating, simpler than python or batch files).

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 15 Jul 2021, 12:14

Bzt, i just downloaded a windows version pf php:
https://www.php.net/downloads.php

Running php.exe give me that screen. How do i go at creating the teamplater with the code you gave me? and calling it?.

Sorry for taking so long but where i live its too hot and im having constant brainmelts.
Attachments
php.png
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 16 Jul 2021, 13:35

Danimal {l Wrote}:Running php.exe give me that screen. How do i go at creating the teamplater with the code you gave me? and calling it?.
That's good, that's exactly how it should look like (it enters an interactive mode without any error message). Now to use it, create template php files, starting with that one liner and ending in that "<?}?>". At first, just try to copy'n'paste mine from above. Let's say you save it by the name "abc.php", then you can run it like this:
{l Code}: {l Select All Code}
php.exe abc.php startid=10 endid=11 level=2
First is the name of the interpreter (php.exe in this case), then comes the name of the script (abc.php in this case), and all the other arguments are parsed by the script, and therefore depend on the script (in this case the start and end ids and the values for variables defined by the template).

Tips and tricks: you can simply redirect the output to a text file:
{l Code}: {l Select All Code}
php.exe abc.php startid=10 endid=11 level=2 >items.txt
and it might be possible that you might need double newlines, I'm not sure how the Win php version handles these as it's using "\r\n" for line endings, not "\n" like Linux.
So if the redirected txt doesn't have line breaks, then just double the newlines in the php file, like this:
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>

[item]

id=<?=$id?>

name=Hunter shortbow

INCLUDE items/base/weapons/ranged/shortbow.txt

level=<?=$level?>

price=<?=rand(100 * $level, 150 * $level)?>

dmg=ranged,6,10

requires_stat=offense,7

requires_stat=physical,3


<?}?>
Once you got one php right, the door is open, you can alter that or copy and modify for a new template. You can modify the template as you like, use any fields, any expressions, any string lists etc. If you have any question, just shoot, I'll try to answer.

Danimal {l Wrote}:Sorry for taking so long but where i live its too hot and im having constant brainmelts.
Have no worries, it's the same here. We have no air conditioner.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby jdc » 23 Jul 2021, 11:40

The code is not that hard, but you need to practice:

Need knowledge in the interface how is done. (don't have)
The code it self is easy.

let say is in C and in the console try getting codeblocks.

first you need to know what is for. To generate new items or from existing ones? I assume existing:

Get the list of items, lets also assume to variances (level and rarity) . in what level we are if we can get it in current level and second how rare.

need a loop

exit = 0;
while (not equal to 1) { ( (1 in computers is true)

check for level
check for rarity

if want the user to input values

scanf(option1)
scanf(option2)
scanf(option3)

if option 1(item_name){

}

if option 1(item_name){

}

- also try checking "the box" project in standard variables

}


if is just to add items usely this stuff should be in some kind of bidings for Lua, Python or even PHP as some mention. It should be in some kind of text file.

item name
item level
item min damage
item max damage
item rarity
...

It should allow to add information

item name new item
item level 4
item min damage 2
item max damage 10
item rarity 5
jdc
 
Posts: 118
Joined: 29 Jun 2017, 16:48

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 23 Jul 2021, 16:52

@jdc: I literally have absolutely no clue what you are talking about.

First, about compiled languages: the OP doesn't want to learn programming. I wrote a little C tool to create object definitions from JSON files. It works, but the problem with that is that the OP wants variables and calculated values. For that, the only option would be a scripting language. I've suggested PHP because it runs under Windows and it works as a templater by default.

The difference to learning a programming language is this: with my last suggested solution, the OP should create object definition templates, with a special first and last line (that part contains the "magic", minimal addition to the object template). It supports variables (values provided from command line), and calculated values as well (expressions in the template). So all the "programming" is limited to the first and last line in the template, which is just copied verbatim as-is. With expressions only basic arithmetic needed, no complex programming skills required at all.

Second, codeblocks is an IDE, absolutely irrelevant and independent to the language used.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 27 Jul 2021, 14:16

Thanks for the answer Jdc, but I am almost done.

Bzt, i finally got to it, this is the fully working base ill expand and modify upon:
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
[item]
id=<?=$id?>
name=<?=["Goblin","Hunter","Poacher","Metal","Flexible"][rand(0,4)]?> shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand(75 * $level, 150 * $level)?>
dmg=ranged,<?=rand(0, 1 * $level)?>,<?=rand(1 * $level, 3 * $level)?>
requires_stat=offense,<?=ceil($level * 5 * 0.7)?>
requires_stat=physical,<?=ceil($level * 5 * 0.3)?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>,<?=rand(1, 1 * $level)?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>,<?=rand(1, 1 * $level)?>

<?}?>


I solved the line break problem by chance, neither \n or \r\n worked, but it just needed an empty space after the line of code. The results are pretty nice:
{l Code}: {l Select All Code}
c:\php>php.exe abc.php startid=1 endid=3 level=5
[item]
id=1
name=Flexible shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=5
price=607
dmg=ranged,0,14
requires_stat=offense,18
requires_stat=physical,8
bonus=mental,1
bonus=defense,4
quality=magic

[item]
id=2
name=Flexible shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=5
price=647
dmg=ranged,1,5
requires_stat=offense,18
requires_stat=physical,8
bonus=hp,2
bonus=mental,1
quality=magic

[item]
id=3
name=Goblin shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=5
price=411
dmg=ranged,2,12
requires_stat=offense,18
requires_stat=physical,8
bonus=mp,5
bonus=absorb_min,5
quality=magic


Thanks for your help, but don´t go too far, Im sure I´ll need you again. ;)
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 27 Jul 2021, 15:04

Danimal {l Wrote}:Bzt, i finally got to it, this is the fully working base
Well done, good to hear!

Danimal {l Wrote}:I solved the line break problem by chance, neither \n or \r\n worked, but it just needed an empty space after the line of code.
Great, haven't popped into my mind because my IDE automatically removes spaces at the end of lines. But if it works, great!

Danimal {l Wrote}:Thanks for your help, but don´t go too far, Im sure I´ll need you again. ;)
You're welcome, and if you have more questions, just shout! I hope this solution is flexible enough to satisfy all your needs.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 02 Sep 2021, 23:28

Hello again Bzt, now that my holidays are over i´ll restart working on my mod; but i need something else for the item generator. Is there a way to make a condition that checks when a certain bonus is randomly selected if it´s one of a few (hp, mp or hp_regen, mp_regen) it´s multiplied by some value before output?;

Example:
Lv 5 item -> bonus=mp,5 ; which is basically useless at that level, so it would need to be multiplied with a rand(3,7).

Equally, I would like a more inteligent name generator, is there a way to check the first two randomly generated bonus and give an according name? like, defense and hp = "Turtle" "Shortbow", maybe even expand and check the 3th and 4th bonus for extra naming?
1st Hp ,2nd Defense, 3rd Strength, 4th speed -> Turtle maniac shortbow
I think this is done the reverse way in games and the name determinates the bonus?
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 03 Sep 2021, 07:07

Danimal {l Wrote}:Hello again Bzt, now that my holidays are over i´ll restart working on my mod; but i need something else for the item generator. Is there a way to make a condition that checks when a certain bonus is randomly selected if it´s one of a few (hp, mp or hp_regen, mp_regen) it´s multiplied by some value before output?;
Not entirely sure what you want to achieve, but yeah, you can do conditional values. Without diving into conditional control blocks like "if" and "switch", the simplest is to use the trinary operator. It looks like this:
{l Code}: {l Select All Code}
(expression ? true value : false value)

Danimal {l Wrote}:Lv 5 item -> bonus=mp,5 ; which is basically useless at that level, so it would need to be multiplied with a rand(3,7).
I guess that could be something like
{l Code}: {l Select All Code}
bonus=mp,<?=5 * ($level > 5 ? 1 : rand(3,7))?>
Of course you can use any variable and less than (<), greater than (>), less or equal (<=), greater or equal (>=), equal (==), not equal (!=) operators too. Whatever fits your required logic.

To avoid surprises, I suggest to always use parenthesis around a trinary operator for fixed precedence order. For example "5 * $level > 5 ? 1 : 2" would evaluate "5 *" as the part of the expression, therefore returning either 1 or 2, while the same with parenthesis "5 * ($level > 5 ? 1 : 2)" multiplies the trinary's result, therefore returns either 5 or 10.

Danimal {l Wrote}:Equally, I would like a more inteligent name generator, is there a way to check the first two randomly generated bonus and give an according name?
Yes, but you have to store those randomly generated values into variables first so that you can later reference them. Probably the best place for that is inside the first <??> block. For example:
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){     // note, ?> moved down
$bonus1 = rand(1,5);      // save the random value into a variable
?>
bonus=mp,<?=$bonus1?>
name=<?=($bonus1 < 3 ? "Simple" : "Gigamega")?> Shortbow
As you can see, in the template you can reference these variables exactly the same way as you reference the command line parameters, so make sure to avoid name collision.

If you want to have more than two options, I'd recommend to use the array trick I've mentioned before:
{l Code}: {l Select All Code}
...
$bonus1 = rand(1,5);
$names = [ "", "Simple", "Average", "Cool", "Super", "Gigamega" ];     // note, array index starts at 0, hence the first empty string
?>
bonus=mp,<?=$bonus1?>
name=<?=$names[$bonus1]?> Shortbow
Here the value of the bonus selects the string from the array. For optional extra naming, you can use empty strings where you don't want extra name, for example
{l Code}: {l Select All Code}
...
$names = [ "", "", "", "", "Super ", "Gigamega " ];     // note space added at the end here and not below
?>
name=<?=$names[$bonus1]?>Shortbow

Danimal {l Wrote}:I think this is done the reverse way in games and the name determinates the bonus?
Could be, but I think your approach is also good. Knowing the numerical value first helps to write simpler expressions, and you can also use a numerical value as an array index.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 03 Sep 2021, 18:49

Thanks for answering but please remember I am a bit slow; and I think i didn´t explain myself properly. Please have look at this:

{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
[item]
id=<?=$id?>
name=<?=["Goblin","Hunter","Poacher","Metal","Flexible"][rand(0,4)]?> shortbow
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand(75 * $level, 150 * $level)?>
dmg=ranged,<?=rand(0, 1 * $level)?>,<?=rand(1 * $level, 3 * $level)?>
requires_stat=offense,<?=ceil($level * 5 * 0.7)?>
requires_stat=physical,<?=ceil($level * 5 * 0.3)?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>,<?=rand(1, 1 * $level)?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>,<?=rand(1, 1 * $level)?>

<?}?>


That is my current code, what I want is that when:
{l Code}: {l Select All Code}
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>,<?=rand(1, 1 * $level)?>

Returns something like this:
{l Code}: {l Select All Code}
bonus=hp,2

a condition checks if the bonus type is "hp", "mp" (or any other I´ll add in the future); 2 extra hp or mp is useless on a lv5 item, so the condition would check first if any of those bonus types have been output (hp, mp...) leaving the other types untouched(mental, defense...) but if hp, mp... are detected the numerical value (bonus=hp,2) is multiplied with random value of 3 to 7, so "2 * rand(3,7)" it would then print something like "bonus=hp,8" (2*4).

From what little I remember about programming a switch would be best? Then no doubt more complex but nicer looking, the names could be generated by expanding the switch loop to house all possible bonus types. So an empty name variable would be created at start of code and be filled as the item bonus are created, example that I expanded upon (not working of course, just something for easier understanding):

{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>      
[item]
id=<?=$id?>     
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand(75 * $level, 150 * $level)?>
dmg=ranged,<?=rand(0, 1 * $level)?>,<?=rand(1 * $level, 3 * $level)?>    
requires_stat=offense,<?=ceil($level * 5 * 0.7)?>
requires_stat=physical,<?=ceil($level * 5 * 0.3)?>       [b]#Up to this point same as before[/b]


# Variables declaration, i guess this goes right after header and not here?

$traits #stores how many bonus traits the item will have, needs to take it from console input
$name   #empty name variable
$adj1   #empty name variable
$adj2   #empty name variable
$bonus  #empty variable for bonus type
$rand_value  # empty variable to store a random numerical value to be used along bonus
$i         #for the loop control

i= 1       #not starting from 0
while i =! $traits, do                                                                                           #controls how many times the loops runs based on numbers of desired item traits
{
   $bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]?>      #decides type of bonus randomly and stores it
   $rand_value= <?=rand(1, 1 * $level)?>                                                             #generates the random numerical value bonus will use and stores it

switch (bonus) {
        case offense:
            bonus=<?="offense",<?=rand_value     #prints the line for the game item, output ex. "bonus=offense,5"
            if i=1 and i<3                    #this one makes it so the object only has 2 adjectives + item name, it checks if it´s the first or second adjective and allows no more
               if rand_value >= 100                    #this one checks the numerical value and adds a word to the adjective
               {
                 adj1= "Titan power"
               } else if rand_value >=80
                 adj1= "Giant power"
               } else if rand_value >=60
                 adj1= "Ogre power"
               } else if rand_value >=40
                 adj1= "Bull power"
               } else if rand_value >=20
                 adj1= "Boar power"
               } else if rand_value >=10
                 adj1= "Wolf power"

        elseif  i=2 and i<3                     #if first adjective is taken fills the second adjective variable which is shorter/different to the first
      if rand_value >= 100                   
               {
                 adj2= "Titan"
               } else if rand_value >=80
                 adj2= "Giant"
               } else if rand_value >=60
                 adj2= "Ogre"
               } else if rand_value >=40
                 adj2= "Bull"
               } else if rand_value >=20
                 adj2= "Boar"
               } else if rand_value >=10
                 adj2= "Wolf"      
            break;
        case hp:
            bonus=<?="hp", rand_value * rand(3,7)          #prints the line for the game item, output ex. "bonus=hp,21"; this and the previous case are what I need to expand by myself the switch loop
            ................ same adjective process as before
            break;
        case defense:
      ................ same as before until all cases are covered
            break;
   }             #end of switch loop
} #end of i controled loop

name = ?>adj1 ?>"shortbow of the" ?>?>adj2              #prints the name using the adjectives variables, ex. "Bull power shortbow of the ranger"

if $traits >= 5                                 #based on the number of traits print rarity
   quality=Unique
   elseif $traits <= 3
      quality=Rare
      elseif $traits <= 2
         quality=Magic

}



Im sorry if it made your eyes bleed, but this the best i can express what want it to be.
Last edited by Danimal on 14 Sep 2021, 14:42, edited 2 times in total.
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 03 Sep 2021, 20:23

Danimal {l Wrote}:a condition checks if the bonus type is "hp", "mp" (or any other I´ll add in the future);
As I've said, if you want to reference a value later, then just put it in a variable. For example,
{l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, 1 * $level)?>
Here I've moved "rand(0, 5)" up and gave a name to it. Then you can use this $bonustype variable in the array index as well as in expressions for the conditionals.

Danimal {l Wrote}:2 extra hp or mp is useless on a lv5 item, so the condition would check first if any of those bonus types have been output (hp, mp...) leaving the other types untouched(mental, defense...) but if hp, mp... are detected the numerical value (bonus=hp,2) is multiplied with random value of 3 to 7, so "2 * rand(3,7)" it would then print something like "bonus=hp,8" (2*4).
For example:
{l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, $level) * ($bonustype < 2 ? rand(3, 7) : 1)?>
Or you could reference more $bonustype with logical and (&&) and logical or (||) operators. For example
{l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, $level) * ($bonustype == 0 || $bonustype == 1 ? rand(3, 7) : 1)?>
Which means bonus type is hp or bonus type is mp, then mupltiply by a random value.

Danimal {l Wrote}:From what little I remember about programming a switch would be best?
That depends how many kind of output you want. If two different kind is enough, then trinary or "if" block is the best (here you can count an empty output as one option). Looks like this:
{l Code}: {l Select All Code}
<? if(expression) { ?>
things to output if expression is true
<? } else { ?>
things to output if expression is false
<? } ?>
If the second option is empty, then you can simply omit the "else" part, like this:
{l Code}: {l Select All Code}
<? if(expression) { ?>
things to output if expression is true
<? } ?>


For a switch, the scheme is similar, but as you've said it, multiple outputs can be defined based on the value of the given expression.
{l Code}: {l Select All Code}
<? switch(expression) {
case (v1): ?>things to print if value is v1
<? case (v2): ?>things to print if value is v2
<? case (v3): ?>things to print if value is v3
<? case (v4): ?>things to print if value is v4
<?
...
} ?>

Danimal {l Wrote}:Then no doubt more complex but nicer looking, the names could be generated by expanding the switch loop to house all possible bonus types.
I don't know if this is more readable, or nicer looking, but definitely a solution and useful if you want different output per the expression's value. For example:
{l Code}: {l Select All Code}
<? switch($bonustype) {
case 0: ?>bonus=hp,<?=rand(1,2)?>
<? case 1: ?>bonus=mp,<?=rand(3,4)?>
<? case 2: ?>bonus=mental,<?=rand(5,6)?>
<? case 3: ?>bonus=defense,<?=rand(7,8)?>
<? } ?>

Danimal {l Wrote}:Variables declaration, i guess this goes right after header and not here?
Yes. Go to the end of the first line, go back two characters and press an Enter. So
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
should became
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
?>
Now define your variables after the first line, moving the "?>" line down.
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
$bonustype = rand(3,7);       // here comes a variable definition.
?>
On the left side of "=" you name the variable (all variables must be prefixed by dollar sign), and on the right side use the same expression that you've used in the template. Finally close the line with a semicolon ";" and an Enter.
Danimal {l Wrote}:$traits #stores how many bonus traits the item will have, needs to take it from console input
For that, no need to define a variable, just add "traits=N" on the command line, and the $traits variable will be created for you.

The rest should look like this (note: I haven't checked this code, just wrote from memory to demonstrate syntax for you. Might contain errors)
{l Code}: {l Select All Code}
for($i = 1; $i <= $traits; $i++) {            // iterate traits time, $i will be 1 .. $traits
   $bonus=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)];      // no need for <??> here, you're not in output mode
   $rand_value= rand(1, 1 * $level);
switch ($bonus) {
        case "offense":                                          // $bonus is a string, so you need to use quotes here
            ?>bonus=offense,<?=rand_value?>
<?     // use ?>things to print<?, and newline might be important here as you need that in the output too
And
{l Code}: {l Select All Code}
  if i=1 and i<3
makes no point, because if "i" is 1, then it is also less than 3, but just for demonstation
{l Code}: {l Select All Code}
 if ($i == 1 && $i < 3) {

Danimal {l Wrote}:Im sorry if it made your eyes bleed, but this the best i can express what want it to be.
Don't worry, and I hope I could help. A few rules to keep in mind:
- variables are always prefixed by "$"
- strings must be in quotes in programming mode
- to output something, switch to output mode with "?>" and switch back to programming mode with "<?"
- one equal sign "=" assigns a variable, two "==" checks if it's equal to (if you're not declaring a variable at the top, then remember to always use double "==").

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 03 Sep 2021, 20:33

Oh, and before you get lost with the "if" and "switch" blocks... consider using more .php files! :-) If you feel the structure is getting way too complex, create two simple .php files instead.

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 03 Sep 2021, 22:34

Thanks, but how could i go about nesting this monster?, what its the proper sintax ?
{l Code}: {l Select All Code}
   if i=1 and i<3                   
               if rand_value >= 100                   #this one checks the numerical value and adds a word to the adjective
               {
                 adj1= "Titan power"
               } else if rand_value >=80
                 adj1= "Giant power"
               } else if rand_value >=60
                 adj1= "Ogre power"
               } else if rand_value >=40
                 adj1= "Bull power"
               } else if rand_value >=20
                 adj1= "Boar power"
               } else if rand_value >=10
                 adj1= "Wolf power"


something like this?

{l Code}: {l Select All Code}
<? if ($i == 1 && rand_value >= 100) { ?>
 adj1= "Titan power"
<? } ?>
<? if ($i == 1 && rand_value >= 80) { ?>
 adj1= "Giant power"
:
But, like that there no way to break out in the case the first condition is true, "else if" must exist, right? or is there a better method?
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 04 Sep 2021, 00:09

Make sure you open (with "{") and close (with "}") blocks properly, and don't go to output mode if you don't want to print (don't use "?>" when you're just assigning values to variables).
{l Code}: {l Select All Code}
<?...
if ($i == 1) {
    if ($rand_value >= 100) {
        $adj1 = "Titan power";
    } else if ($rand_value >= 80) {
        $adj1 = "Giant power";
    } else if ($rand_value >= 60) {
...
    }
}
?>
After that, $adj1 should contain the proper value. (Move this whole thing into the first programming block, before the first "?>", and then use $adj1 in the template part as you would use any other variable).

Note that there's an opening "{" after each "if", which opens the code block that's closed by the corresponding "}" before the "else". You must close the last block to, as well as the outter "if" at the end. You can add "else" to the outter "if" too depending on your desired logic.

Another, probably simpler approach would be to use a string array, indexed by $rand_value / 20 rounded. Using "intval" makes sure of it to return integers appropriate for indices, 'cos for example 3.1415 isn't a valid index, but 3 is.
{l Code}: {l Select All Code}
$rand_index = intval($rand_value / 20);

Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 11 Sep 2021, 11:48

Hello again, I got into a wall while trying to implement this.
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
$name  = "Shortbow";
$adj1 = "";
$adj2 = "";
$bonus = "";
$rand_value = "";
$i = 1;
?>

[item]
id=<?=$id?>
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand($level * 100, $level * 100 * ($traits * 0.1 + 1))?>

for ($i = 1; $i <= $traits; $i++) {
i=<?=$i?>
}

name= <?=$name?> <?=$adj1?>
<?}?>

I´m keeping it simple until I get all pieces working, this had to print several times "i" until reaching trait value, but it doesn´t
{l Code}: {l Select All Code}
c:\php>php.exe 123.php startid=1 endid=1 level=2 traits=6

[item]
id=1
INCLUDE items/base/weapons/ranged/shortbow.txt
level=2
price=269

for ($i = 1; $i <= $traits; $i++) {
i=1}

name= Shortbow

Googling around I found the error maybe that FOR needs to be inside of <?php ........ ?>; it´s that so?, if yes, do I need to create a variable for each value FOR creates so later it outputs a clean and tidy item definition?


Edit: got it to work inside of <?php....?>; but its the only way? this forces me to either create more variables to hold all information output of FOR or creating an array to do so, which is clearly a better choice since it can dinamically adjust its size opposed to having a fixed amount of variables which may come short. The problem is I have no fugging idea of how to do it; neither creation nor accesing later to it´s data to be printed.
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » 11 Sep 2021, 18:29

Danimal {l Wrote}:Edit: got it to work inside of <?php....?>; but its the only way?
Yes, you have to put it in that <??> because you want it to be executed and not printed.
Danimal {l Wrote}:this forces me to either create more variables to hold all information output of FOR or creating an array to do so, which is clearly a better choice since it can dinamically adjust its size opposed to having a fixed amount of variables which may come short. The problem is I have no fugging idea of how to do it; neither creation nor accesing later to it´s data to be printed.
Creating arrays is simple, you've done it before, just wrap the "," comma separated data in "[" and "]". For example
{l Code}: {l Select All Code}
$intarray = [ 1, 1, 2, 3, 5, 8, 13 ];
$strarray = [ "zero", "one", "two", "three" ];

Now to access, you use an index, starting from 0, you have already done that too.
{l Code}: {l Select All Code}
<?=$intarray[0]?>
<?=$strarray[2]?>


Concerning iteration, you can have a loop variable that indexes the array; or you could use this trick instead:
{l Code}: {l Select All Code}
foreach( $strarray as $element ) {
...
}
This works exactly like the "for", it iterates as many times as the number of elements in the array. On each iteration, $element (or whatever variable name you prefer) will get the next element's value in the array (like if you had a "$element = $strarray[$i];" in the loop).

If you don't want to create additional variables, then you can simply use the array declaration in the foreach, for example:
{l Code}: {l Select All Code}
foreach( [ "zero", "one", "two", "three" ] as $numstr ) {
...
}
This will repeat the block between { and } four times, each time giving a different value to $numstr.

Hope this helps.
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Re: Need volunteer to create simple item generator for Flare

Postby Danimal » 14 Sep 2021, 22:24

Took me a good while:
{l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
$name  = "Shortbow";
$adj1 = "";
$adj2 = "";
$bonus = "";
$rand_value;
$i;
$mult_value;
$bonus_array= [""];


for ($i = 1; $i <= $traits; $i++)
{
   $bonus=["hp","offense"][rand(0,1)];
   $rand_value= rand(1, 1 * $level);
   $bonus_array[$i] = "bonus=$bonus,$rand_value";

switch ($bonus)
   {
   case "hp":
      $mult_value = $rand_value * rand(3,7);
      $bonus_array[$i] = "bonus=$bonus,$mult_value";

      if ($i == 1)
      {
             if ($rand_value >= 100) {$adj1 = "Holy aegis";
             } else if ($rand_value >= 80) {$adj1 = "Dragon scale";
         } else if ($rand_value <= 60) {$adj1 = "Stone skin";}

      }

      if ($i == 2)
      {
             if ($rand_value >= 100) {$adj2 = "god";
             } else if ($rand_value >= 80) {$adj2 = "dragon";
         } else if ($rand_value <= 60) {$adj2 = "golem";}

      }
      
      break;

        case "offense":
           if ($i == 1)
      {
             if ($rand_value >= 100) {$adj1 = "Titan power";
             } else if ($rand_value >= 80) {$adj1 = "Giant power";
         } else if ($rand_value <= 60) {$adj1 = "Bull power";}

      }

      if ($i == 2)
      {
             if ($rand_value >= 100) {$adj1 = "Titan";
             } else if ($rand_value >= 80) {$adj2 = "Giant";
         } else if ($rand_value <= 60) {$adj2 = "Bull";}

      }
      break;
   
   }
}


?>

[item]
id=<?=$id?>
<?if ($traits >= 5) {?>name=<?=["Legendary","Epic","Godly","Mythic","Ancient","Supreme"][rand(0,5)]?> <?=$adj1?> <?=$name?> of the <?=$adj2?> <?;
   } else if ($traits >= 3) {?>name=<?=$adj1?> <?=$name?> of the <?=$adj2?><?;
   } else if ($traits >= 1) {?>name=<?=$adj1?> <?=$name?> <?;
   } else if ($traits == 0) {?>name=<?=$name?> <?;}
?>
INCLUDE items/base/weapons/ranged/shortbow.txt
level=<?=$level?>
price=<?=rand($level * 100, $level * 100 * ($traits * 0.1 + 1))?>
dmg=ranged,<?=rand(1, 1 * $level)?>,<?=rand(1 * $level, 3 * $level)?>
requires_stat=offense,<?=ceil($level * 5 * 0.7)?>
requires_stat=physical,<?=ceil($level * 5 * 0.3)?>
<? foreach( $bonus_array as $x ) {echo "$x" . PHP_EOL . "";}?>
<?if ($traits >= 5) {?>quality=unique <?;
   } else if ($traits >= 3) {?>quality=rare <?;
   } else if ($traits >= 1) {?>quality=magic <?;
   } else if ($traits == 0) {?>quality=common <?;}?>
<?}?>

I still need to create all bonus/cases inside switch loop and many more adjectives (just the two basics included atm), so that section will grow a lot. What do you thing, can it be improved before starting that?

Before, I was using "if´s" to print only five possible cases from a fixed lenght array, but I switched to an empty variable to serve as a dinamic array (but not declaring the first value creates error at foreach loop) which gets printed with a "foreach", the problem is I dont use the first position of the array, $bonus_array[0] because it creates an extra bonus even when inserting 0 traits. Is there a way to ignore it without creating a classic "for" loop?. By the way I found the way to break line after lots of suffering: echo "$x" . PHP_EOL . ""
Right now the output is totally viable, but I would like to erase that empty line corresponding to $bonus_array[0].

{l Code}: {l Select All Code}
c:\php>php.exe 123.php startid=1 endid=3 level=10 traits=4

[item]
id=1
name=Bull power Shortbow of the golem
INCLUDE items/base/weapons/ranged/shortbow.txt
level=10
price=1398
dmg=ranged,10,23
requires_stat=offense,35
requires_stat=physical,15

bonus=offense,8
bonus=hp,16
bonus=offense,5
bonus=hp,24
quality=rare
User avatar
Danimal
OD Moderator
 
Posts: 1396
Joined: 23 Nov 2010, 13:50

Re: Need volunteer to create simple item generator for Flare

Postby bzt » Yesterday, 11:50

Danimal {l Wrote}:I still need to create all bonus/cases inside switch loop and many more adjectives (just the two basics included atm), so that section will grow a lot. What do you thing, can it be improved before starting that?
Nope, I think this is good. You could make it better of course, but I see no point in optimisation. You'll only run this once, so performance isn't an issue and I believe reflecting your logic behind the values in a way that you understand is much more important. Also put a lot of comments, so that if you put your game down and you pick it up a year later, you'll know what's going on.
{l Code}: {l Select All Code}
// $i is used to select X here


Danimal {l Wrote}:but not declaring the first value creates error at foreach loop
You can surpress the error message, but it doesn't really matter as it's printed to the stderr, not to stdout, therefore not included in the output. If this bothers you, add "error_reporting(0);" (but then you won't get any error message at all, not even the ones you should care about).

Danimal {l Wrote}:Is there a way to ignore it without creating a classic "for" loop?.
You can use a classic for loop with "count($bonus_array)", which returns the number of elements. Something like
{l Code}: {l Select All Code}
for($i = 1; $i < count($bonus_array); $i++)


Danimal {l Wrote}:Right now the output is totally viable, but I would like to erase that empty line corresponding to $bonus_array[0].
Glad to hear! Well done! About the first element, you should only print if it's not empty (this way newline won't be printed either):
{l Code}: {l Select All Code}
if( !empty($x) ) echo "$x" .  PHP_EOL;

or you can also remove the first element from the array if it's not needed any more:
{l Code}: {l Select All Code}
array_shift($bonus_array);
foreach( $bobus_array as $x ) { ...


Cheers,
bzt
User avatar
bzt
 
Posts: 99
Joined: 23 May 2021, 21:46

Who is online

Users browsing this forum: No registered users and 1 guest