Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 788 789 [790] 791 792 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 818762 times)

WealthyRadish

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11835 on: October 08, 2020, 08:35:02 pm »

Are you checking out SIMD intrinsics as well? It's like writing shaders on the CPU, but with everything stored as SoA.

I'm writing code that less low-level programmers have to deal with and has to work even on pretty-bad computers (hell, the server I'm writing it for is running on an older and less powerful CPU than the one in my PC, which I learned the hard way when my AVX2 auto-vectorizing caused performance loss when pushed live), so I've been avoiding intrinsics and instead just writing loops to be as auto-vectorizable as possible.

One thing to mention (though it sounds like you're familiar with this) is that introducing AVX can slow down a program on most architectures if it's being mixed with SSE instructions, due to a very costly need to save the upper half of the registers when switching between AVX and SSE. I don't know how easy it is to force a program to purely use AVX (external code linked in might have SSE), but calling the _mm_256zeroupper intrinsic at the end of code using AVX instructions prevents the large penalty from state-switching to SSE. I'm not sure how compilers auto-vectorizing AVX handle this, but it's a common "gotcha".
Logged

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #11836 on: October 15, 2020, 05:26:28 pm »

I have avoided C++ for 7 years. I didn't want to touch it. I still hate its build systems, the build ecosystem surrounding it.

But I have literally never had as much fun programming as I do when I'm working in C++, doing optimizations. Nothing has ever come close. I feel like I haven't been programming until recently. What the heck is happening. I could infodump about C++17 and C++20 features for hours. I'm getting an actual desire to optimize Dwarf Fortress, like some sort of rando programmer who stumbles into the forums and gets rebuked. It's unbelievable, just how much I'm enjoying C++ work and optimization. Thinking about cache locality is fun, considering where multithreading might get a use and when the overhead of an atomic variable might not be worth it is stimulating. Good lord.
What the hell is wrong with you and also what steps do I take to actually feel this way about C++
Logged

Vicomt

  • Bay Watcher
  • Just call me Vic.
    • View Profile
    • Steam Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11837 on: October 16, 2020, 03:30:04 am »

I just wish I could use C++ in my day-to-day work.... fucking vb6 man. just don't ever get involved with it.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11838 on: October 23, 2020, 04:20:56 pm »

I have avoided C++ for 7 years. I didn't want to touch it. I still hate its build systems, the build ecosystem surrounding it.

But I have literally never had as much fun programming as I do when I'm working in C++, doing optimizations. Nothing has ever come close. I feel like I haven't been programming until recently. What the heck is happening. I could infodump about C++17 and C++20 features for hours. I'm getting an actual desire to optimize Dwarf Fortress, like some sort of rando programmer who stumbles into the forums and gets rebuked. It's unbelievable, just how much I'm enjoying C++ work and optimization. Thinking about cache locality is fun, considering where multithreading might get a use and when the overhead of an atomic variable might not be worth it is stimulating. Good lord.
What the hell is wrong with you and also what steps do I take to actually feel this way about C++

i've switched to Rust recently and am having the same feeling so I think I just like optimization

Folly

  • Bay Watcher
  • Steam Profile: 76561197996956175
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11839 on: October 31, 2020, 08:20:10 pm »

I've been struggling with this problem for the last 24 hours or so, and am ready to call it unsolvable. But I figured that first I'd asked some smarter minds than my own. That's you guys.

I'm playing a game called Dual Universe, in which I build industry machines, set recipes, feed them input materials, start them up, and they make products in cycles. Just as one example, let's say I tell a Metalworking industry to make Screws, so it eats a piece of steel, works on it for a 60 second cycle, then spits out a screw. If it runs out of steel then it stalls, until I feed it more steel, then it starts up again.

Now I have a programming board in the game that I can plug into the industry to parse information, and run it through a LUA script. What I would like to do is make a script that will monitor a set of machines and display their current progress or remaining time on a screen. Unfortunately I cannot parse current progress or remaining time directly. The information I can parse is:

Uptime: The total time in seconds since the machine was started
Efficiency: The total uptime efficiency since the machine was started, expressed as a value between 0 and 1. So if it's been running half of the time, this value would be 0.5
CompletedCycleCount: the number of cycles that have completed since the machine was started
CycleTime: the time that it takes to complete one cycle

My thinking thus far is:
RemainingTime = Uptime - CurrentCycleTime?
CurrentCycleTime = Uptime - CompletedCycleSum?
CompletedCycleSum = CompletedCycleCount * CompletedCycleTime?
CompletedCycleTime = CycleTime / CompletedCycleEfficiency?
CompletedCycleEfficiency = ?

See, I can parse the efficiency up to the current second, but I can't figure out a way to calculate the efficiency that the completed cycles ran at, and without that I can't see a way to get to RemainingTime.

So, can any of you smart people figure a way through this problem? Or at least confirm my suspicion that it's impossible with the information available?
« Last Edit: October 31, 2020, 09:24:42 pm by Folly »
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11840 on: November 01, 2020, 03:46:34 pm »

If I'm reading this correctly, then there isn't an efficiency the previous cycles ran at since every cycle always goes for the specified length of time and reduced efficiency is just due to not being able to start cycles. You probably want something like:


RemainingTime = CompletedCycleSum + CycleTime - Efficiency*Uptime
CurrentCycleTime = Efficiency*Uptime - CompletedCycleSum
CompletedCycleSum = CompletedCycleCount * CycleTime

EDIT: since we are only interested in the times the machine is operating, you need to multiply the Uptime by the Efficiency to get the total operating time of the machine (including the current cycle).
« Last Edit: November 02, 2020, 07:20:17 am by RulerOfNothing »
Logged

Folly

  • Bay Watcher
  • Steam Profile: 76561197996956175
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11841 on: November 02, 2020, 02:42:27 am »

Wow, that works!
Well, it works on 38 of the 39 industries I have it hooked up to. I'm assuming the outlier is returning inaccurate data due to server error. I'll restart the machine and monitor.

It seems so obvious after you explained it. Much appreciated.
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11842 on: November 02, 2020, 05:29:43 pm »

is there any javascript library to manage trees with drag/drop? I need to represent a hierarchical order of battle in a strategy game am doing and I don't want to make user click add/remove units from formations hundreds of times
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11843 on: November 02, 2020, 06:55:47 pm »

I'm not aware of any, but if you do find one it'll probably be difficult to convert it to work with the UI you have in mind.  I'd probably try to find a simpler drag and drop library and add the tree hierarchy myself.

Are you using any other library or framework that it should work with?  AngularJS?  React?  jQuery?  Pure JavaScript + HTML?
Logged
Through pain, I find wisdom.

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11844 on: November 03, 2020, 02:42:51 am »

I'm not aware of any, but if you do find one it'll probably be difficult to convert it to work with the UI you have in mind.  I'd probably try to find a simpler drag and drop library and add the tree hierarchy myself.

Are you using any other library or framework that it should work with?  AngularJS?  React?  jQuery?  Pure JavaScript + HTML?

vue, vuex, the rest is just glue (store.js, jquery)
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11845 on: November 03, 2020, 07:06:52 am »

Wow, that works!
Well, it works on 38 of the 39 industries I have it hooked up to. I'm assuming the outlier is returning inaccurate data due to server error. I'll restart the machine and monitor.

It seems so obvious after you explained it. Much appreciated.

I'm guessing that RemainingTime will be accurate unless the machine stalls out, since there's nothing there that changes state when a new batch starts, only when one ends.

Folly

  • Bay Watcher
  • Steam Profile: 76561197996956175
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11846 on: November 03, 2020, 12:29:13 pm »

I'm guessing that RemainingTime will be accurate unless the machine stalls out, since there's nothing there that changes state when a new batch starts, only when one ends.

That's the problem I was having, trying to figure out how to account for stalled timed. RulerOfNothing's explanation was that I don't need to account for the stalled time; I can just ignore it and focus on the active time and that's enough to figure out the progress of the current cycle, and therefore the remaining time.
« Last Edit: November 03, 2020, 12:30:45 pm by Folly »
Logged

methylatedspirit

  • Bay Watcher
  • it/its
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11847 on: December 15, 2020, 09:25:10 pm »

Alright, I have a copy of Python, and I don't have a damn clue how to use it. I'm not a programmer by any stretch of the imagination, so forgive my lack of understanding of what's going on. What I'm trying to do is generate this 256x256 color image. The x-axis is a linear gradient between black (#000000) and blue (#0026FF), and the y-axis is a linear gradient between black (#000000) and pink (#FF00DC). Each pixel is the sum of the colors at (x,y), x + y, then divided by 2.

I vaguely understand what that entails, but I don't have any clue how to implement it. I don't even know if Python has vectors or matrices.
1. Generate the gradients between the colors given using linear interpolation for 256 pixels long. Place each into its own vector such that every cell is itself a 3-dimensional vector (representing the RGB values). These are the x and y vectors, and they're 256-dimensional.
2. Generate a matrix by "combining" the x and y vectors, essentially turning it into a 2D table with values on the x and y axes defined by the x and y vectors respectively. Then, for each cell (color) in the matrix, Color = (x + y) / 2.
3. Somehow convert this matrix into a color image defined by the RGB values in the matrix.

Something like that, anyway. I have the high-level idea in my head, but I lack the ability to translate that into Python code. I realize this is basically making y'all do it for me, but I'm hoping that by dissecting the code, I have a better idea of how to do stuff like this. Probably a bit much for Babby's First Programming Project, but I really want to do this, and trying to do it manually made me realize there has to be a better way.
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11848 on: December 16, 2020, 12:19:09 am »

@methylatedspirit

I'd do this to create the matrix:
Code: [Select]
bitmap = [] #initialize a list named "bitmap"
for x in range(256):
    #initialize a list of y-coords for each x from 0 to 255
    bitmap.append([])
    for y in range(256):
        #create a list of integers representing RBG values for each y-coord
        bitmap[x].append([0,0,0])
This results in a 256x256 list of pixels. Each pixel is represented by a list of 3 integers, standing for the RBG values.

You can manipulate it like so:
Code: [Select]
bitmap[0][0][1] = 255 #the pixel at (0,0) is now green (#00FF00)
bitmap[255][0][0] = 0xFF #the pixel at (255,0) is now red (#FF0000)
bitmap[0][255] = [0xFF,0xFF,0xFF] #the pixel at (0,255) is now white (#FFFFFF)

I'm not familiar with printing graphics in Python. It could be that the relevant module uses its own bitmap class, in which case it would be easier to use that instead of the example I gave.

Also note that the python operator for integer division is //. Using / can result in a floating point instead of an integer, which will cause problems for you.


Looks like Pillow can be used for drawing bitmaps.
« Last Edit: December 16, 2020, 12:44:49 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

methylatedspirit

  • Bay Watcher
  • it/its
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11849 on: December 16, 2020, 01:41:35 am »

Alright, here's some hot garbage for code. I cannot imagine this is even remotely good. It works, surprisingly, but I think I may have to rethink how I add those colors together, since the blue and pink at the x and y edges isn't quite as strong as I wanted.

Code: [Select]
from math import floor
import numpy as np
from PIL import Image

# Defining RGB values of colors in the only way I understand
Rblack=0
Gblack=0
Bblack=0

Rblue=0
Gblue=26
Bblue=255

Rpink=255
Gpink=0
Bpink=220


bitmap = [] #initialize a list named "bitmap"
for x in range(256):
    #initialize a list of y-coords for each x from 0 to 255
    bitmap.append([])
    for y in range(256):
        #create a list of integers representing RBG values for each y-coord
        bitmap[x].append([0,0,0])

# welcome to some crap.
# cycling through all colors

# cycling through x-axis (black to blue)
for Ryblue in range (256):
    for Rxblue in range (256):
        bitmap[Rxblue][Ryblue][0] = floor((Rxblue/256)*Rblue)
           
for Gyblue in range(256):
    for Gxblue in range(256):
        bitmap[Gxblue][Gyblue][1] = floor((Gxblue/256)*Gblue)
       
for Byblue in range(256):
    for Bxblue in range(256):
        bitmap[Bxblue][Byblue][2] = floor((Bxblue/256)*Bblue)
   
# Cycling through y-axis (black to pink)
for Rypink in range (256):
    for Rxpink in range (256):
        bitmap[Rxpink][Rypink][0] = floor(((floor((Rypink/256)*Rpink) + bitmap[Rxpink][Rypink][0]))/2)
           
for Gypink in range(256):
    for Gxpink in range(256):
        bitmap[Gxpink][Gypink][1] = floor((floor((Gypink/256)*Gpink) + bitmap[Gxpink][Gypink][1])/2)
       
for Bypink in range(256):
    for Bxpink in range(256):
        bitmap[Bxpink][Bypink][2] = floor((floor((Bypink/256)*Bpink) + bitmap[Bxpink][Bypink][2])/2)

# convert bitmap to array
array = np.array(bitmap, dtype=np.uint8)

# Use PIL to create image from array
new_image = Image.fromarray(array)
new_image.save('new.png')

I apologize for that travesty. Here's the output of that mess:


It's flipped and I may have swapped the axes around, yes, but at least I have the image. Still thinking on how to average those colors together while making sure the bottom-left and top-left corners are fully blue and pink while making sure it doesn't make the lavender on the bottom-right corner too bright. If you have any ideas, please suggest them.
Logged
Pages: 1 ... 788 789 [790] 791 792 ... 795