English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 17 October 2023, 10:01   #1
TheoTheoderich
Registered User
 
Join Date: Jun 2017
Location: Ruhrgebiet / Germany
Posts: 52
How to clear all values in a NewType array very quickly?

Hello,

I have a question regarding NewType Arrays.
It is possible to clear such array with just one command without the need to loop through the whole array to set every field to zero?


I am using this newtype array for the pathfinding in my game Settle the World.
But i have to clear the whole array for every unit which uses the pathfinding.
(to clear means pathmap(x,y)\tile = 0, pathmap(x,y)\g = 0, etc.)



Especially on a 68000 CPU it took a lot of time to loop through the whole array to set the newtype fields to 0.


Code:
NEWTYPE .tpathmap
  tile.b                ; Obstacle or not
  g.w                   ; G cost / see A*-Algorithm
  h.w                   ; H cost / see A*-Algorithm
  f.w                   ; F cost / see A*-Algorithm
  dir.b                 ; Direction
End NEWTYPE
Dim pathmap.tpathmap(#mapWidth, #mapHeight)
I already tried to DIM the pathmap again, hoping it would be cleared then...but unfortunately that did not work.

Do you have any ideas how I can otherwise set all the fields in a NewType array to the value = 0 very quickly?

Thank you very much
TheoTheoderich is offline  
Old 17 October 2023, 11:00   #2
Honitos
Registered User
 
Honitos's Avatar
 
Join Date: Nov 2019
Location: Celle / Germany
Posts: 145
The command ClearType is not a solution?
Honitos is offline  
Old 17 October 2023, 11:22   #3
TheoTheoderich
Registered User
 
Join Date: Jun 2017
Location: Ruhrgebiet / Germany
Posts: 52
Maybe...but how to use it?

Code:
ClearType pathmap()
does not work.
Error message is saying:
"Array is not a list!"
TheoTheoderich is offline  
Old 17 October 2023, 11:55   #4
Honitos
Registered User
 
Honitos's Avatar
 
Join Date: Nov 2019
Location: Celle / Germany
Posts: 145
Instead of using an array, I'd suggest to use a dynamic list in combination with a newtype.

So you can use ClearType and ClearList to clear a single entry or the complete list.
Honitos is offline  
Old 17 October 2023, 11:59   #5
Honitos
Registered User
 
Honitos's Avatar
 
Join Date: Nov 2019
Location: Celle / Germany
Posts: 145
You *could* clear the complete array by clearing the corresponding memory area this way:

memaddress.l = &myArray()
memlength.l = <array size> * SizeOf.<newtypename>

FillMem memaddress,memlength ; command from RiAmosFuncLib

That should work too, but is a kind of hack in my eyes...
Honitos is offline  
Old 17 October 2023, 12:02   #6
TheoTheoderich
Registered User
 
Join Date: Jun 2017
Location: Ruhrgebiet / Germany
Posts: 52
Thank you for your answer.

As far as i know lists are limited to one dimension.
And my map is stored in an 2d array.

So i think lists are more complicated to handle :-(
Because the pathfinding is using X and Y coordinates for the units.

Unfortunately, I can only program very simply...so i have no clue how to use an 1d list array with data from a 2d map array.

EDIT:
Thanx, i will try your idea with clearing the memory area.
But i agree...sounds like a hack...
TheoTheoderich is offline  
Old 17 October 2023, 17:24   #7
TheoTheoderich
Registered User
 
Join Date: Jun 2017
Location: Ruhrgebiet / Germany
Posts: 52
Clearing the array mem was not working for me.

I figured out another solution, but I have to run a lot of tests, if this is working correct.

I made a copy ("pathmapcopy") of the the "pathmap" array which is always empty.

Instead of looping through all 160 x 128 tiles to clear the "pathmap" array, I make a copy of the empty "pathmapcopy" array to the "pathmap" array.

Code:
CopyMem_ &pathmapcopy(0,0), &pathmap(0,0), (#mapWidth * #mapHeight) * SizeOf.tpathmap
On a plain 68000 CPU this is slightly faster than looping through the pathmap array....but it takes much more memory because of the second array.
So it feels like wasting memory ....but it seems to work.


I need to have fast pathfinding in my game, because at the end of a turn the native units are moved.
There are (at the moment for testing) round about 220 native units in the game. But only some of them will need to calculate a path on turn end.
Most of the time they are moving only from tile to tile, so no pathfinding is needed.
But sometimes, for example if they move too far away from their home village or want to attack a city of a player, the pathfinding is needed.

Last edited by TheoTheoderich; 17 October 2023 at 17:29.
TheoTheoderich is offline  
Old 22 October 2023, 14:20   #8
E-Penguin
Banana
 
E-Penguin's Avatar
 
Join Date: Jul 2016
Location: Darmstadt
Posts: 1,214
If your array can live in chipmem then you can use the blitter to clear it very quickly. Asm code here but you can call it from blitz
http://amigadev.elowar.com/read/ADCD.../node02DE.html

If you want to put it in FastMem then maybe use AllocMem/FreeMem. There's a flag to get the memory cleared on initialisation
http://amigadev.elowar.com/read/ADCD.../node0332.html

The InitBank blitz command should do the same. You'll need to then create a pointer of your desired type to point to the memory address returned.

In all the cases I suspect there's a way to just avoid setting everything to zero first. I don't know the A* algorithm but I guess you're iterating through your array to generate the path and the G, H and F costs need to be cleared before doing the calculations? Why not just clear them at the start of the calculation loop for that cell?
E-Penguin is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
NewType for ANIM object earok Coders. Blitz Basic 3 10 January 2023 09:00
Multiplying, quickly. Ernst Blofeld Coders. Asm / Hardware 32 28 December 2020 16:06
Share NewType to Statement/Function earok Coders. Blitz Basic 21 30 May 2018 13:37
return newtype item no. in Blitz leathered Coders. Blitz Basic 4 20 March 2013 11:59
Looking for this one quickly :P Amiga1992 request.Old Rare Games 14 13 October 2001 14:16

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 22:23.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08396 seconds with 15 queries